Skip to content

Commit

Permalink
fixed std:function to be only used on SAMD devices
Browse files Browse the repository at this point in the history
  • Loading branch information
s-light committed Jun 27, 2019
1 parent 5929f0d commit 385b91c
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 62 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ arduino library for extended / event-based button handling
to get started try the [minimal sketch](./examples/slight_ButtonInput__minimal/slight_ButtonInput__minimal.ino).

if you need more functionality have a look at the other examples:
- [basic](./examples/slight_ButtonInput__basic/slight_ButtonInput__basic.ino)
- [basic](./examples/slight_ButtonInput__basic/slight_ButtonInput__basic.ino)
some more options..
- [advanced](./examples/slight_ButtonInput__advanced/slight_ButtonInput__advanced.ino)
- [advanced](./examples/slight_ButtonInput__advanced/slight_ButtonInput__advanced.ino)
shows how to use lots of buttons in an array fashion
- [advanced2](./examples/slight_ButtonInput__advanced2/slight_ButtonInput__advanced2.ino)
example how to use in a object-oriented multi-class setup.
- [advanced2](./examples/slight_ButtonInput__advanced2/slight_ButtonInput__advanced2.ino)
example how to use in a object-oriented multi-class setup.
this only works on SAMD boards.
37 changes: 23 additions & 14 deletions examples/callback_test/callback_test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,33 @@
// experiments with std:function
// https://stackoverflow.com/questions/14189440/c-callback-using-class-member#14189561

// fix "error: macro "min" passed 3 arguments, but takes just 2"
#undef min
#undef max

// this tries to fix
// undefined reference to `std::__throw_bad_function_call()'
// found at
// https://forum.arduino.cc/index.php?topic=382211.msg2790687#msg2790687
namespace std {
void __throw_bad_function_call() {
Serial.println(F("STL ERROR - __throw_bad_function_call"));

#if defined(ARDUINO_ARCH_SAMD)
// fix "error: macro "min" passed 3 arguments, but takes just 2"
#undef min
#undef max
// fix
// undefined reference to `std::__throw_bad_function_call()'
// found at
// https://forum.arduino.cc/index.php?topic=382211.msg2790687#msg2790687
namespace std {
void __throw_bad_function_call() {
Serial.println(F("STL ERROR - __throw_bad_function_call"));
}
}
}
#include <functional>
#else
#error “This library currently only supports boards with an AVR or SAMD processor.”
#endif

#include <functional>


using tCallbackFunction = std::function<void(uint8_t)>;
#if defined(ARDUINO_ARCH_AVR)
using tCallbackFunction = void (*)(uint8_t);
// using tCallbackFunction = std::function<void(uint8_t)>;
#elif defined(ARDUINO_ARCH_SAMD)
using tCallbackFunction = std::function<void(uint8_t)>;
#endif

tCallbackFunction callmeifyoucan;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ https://opensource.org/licenses/mit-license.php
******************************************/


#include <slight_ButtonInputHelper.h>
#include <slight_ButtonInput_CallbackHelper.h>
#include <slight_ButtonInput.h>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ SOFTWARE.
https://opensource.org/licenses/mit-license.php
******************************************/

#include <slight_ButtonInputHelper.h>
#include <slight_ButtonInput_CallbackHelper.h>
#include <slight_ButtonInput.h>

#include "myinput.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ https://opensource.org/licenses/mit-license.php
******************************************/


#include <slight_ButtonInputHelper.h>
#include <slight_ButtonInput_CallbackHelper.h>
#include <slight_ButtonInput.h>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ SOFTWARE.
https://opensource.org/licenses/mit-license.php
******************************************/

#include <slight_ButtonInputHelper.h>
#include <slight_ButtonInput_CallbackHelper.h>
#include <slight_ButtonInput.h>


Expand Down
66 changes: 39 additions & 27 deletions src/slight_ButtonInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,39 +63,51 @@ SOFTWARE.

// experiments with std:function
// https://stackoverflow.com/questions/14189440/c-callback-using-class-member#14189561

// fix "error: macro "min" passed 3 arguments, but takes just 2"
#undef min
#undef max
// fix
// undefined reference to `std::__throw_bad_function_call()'
// found at
// https://forum.arduino.cc/index.php?topic=382211.msg2790687#msg2790687
// namespace std {
// void __throw_bad_function_call() {
// Serial.println(F("STL ERROR - __throw_bad_function_call"));
// }
// }
// but results in
// warning: 'noreturn' function does return [enabled by default
// and
// multiple definition of `std::__throw_bad_function_call()'
// if we move this to the main .ino file it works...

#include <functional>

// more on this topic at
// https://github.com/arduino/ArduinoCore-avr/pull/58
#if defined(ARDUINO_ARCH_SAMD)
// fix "error: macro "min" passed 3 arguments, but takes just 2"
#undef min
#undef max
// fix
// undefined reference to `std::__throw_bad_function_call()'
// found at
// https://forum.arduino.cc/index.php?topic=382211.msg2790687#msg2790687
// namespace std {
// void __throw_bad_function_call() {
// Serial.println(F("STL ERROR - __throw_bad_function_call"));
// }
// }
// but results in
// warning: 'noreturn' function does return [enabled by default
// and
// multiple definition of `std::__throw_bad_function_call()'
// if we move this to the main .ino file it works...
// → please include slight_ButtonInput_CallbackHelper.h from the main sketch.
#include <functional>
#endif

class slight_ButtonInput {
public:
// typedefs:

// call back functions
// typedef boolean (* tCallbackFunctionGetInput) (slight_ButtonInput *instance);
using tCallbackFunctionGetInput =
std::function<boolean(slight_ButtonInput *instance)>;
// typedef void (* tCallbackFunction) (slight_ButtonInput *instance);
using tCallbackFunction =
std::function<void(slight_ButtonInput *instance)>;
#if defined(ARDUINO_ARCH_AVR)
// using tCallbackFunction = void (*)(uint8_t);
using tCallbackFunctionGetInput =
boolean (*)(slight_ButtonInput *instance);
using tCallbackFunction =
void (*)(slight_ButtonInput *instance);
#elif defined(ARDUINO_ARCH_SAMD)
// using tCallbackFunction = std::function<void(uint8_t)>;
using tCallbackFunctionGetInput =
std::function<boolean(slight_ButtonInput *instance)>;
using tCallbackFunction =
std::function<void(slight_ButtonInput *instance)>;
#else
#error “Not implemented yet. please create a pull-request :-)”
#endif


// definitions:

Expand Down
28 changes: 15 additions & 13 deletions src/slight_ButtonInput_CallbackHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,22 @@ SOFTWARE.
#ifndef slight_FIX__THROW_BAD_FUNCTION_CALL_h
#define slight_FIX__THROW_BAD_FUNCTION_CALL_h

// fix
// undefined reference to `std::__throw_bad_function_call()'
// found at
// https://forum.arduino.cc/index.php?topic=382211.msg2790687#msg2790687
namespace std {
void __throw_bad_function_call() {
Serial.println(F("STL ERROR - __throw_bad_function_call"));
#if defined(ARDUINO_ARCH_SAMD)
// fix
// undefined reference to `std::__throw_bad_function_call()'
// found at
// https://forum.arduino.cc/index.php?topic=382211.msg2790687#msg2790687
namespace std {
void __throw_bad_function_call() {
Serial.println(F("STL ERROR - __throw_bad_function_call"));
}
}
}
// but results in
// warning: 'noreturn' function does return [enabled by default
// and
// multiple definition of `std::__throw_bad_function_call()'
// if we move this to the main .ino file it works...
// but results in
// warning: 'noreturn' function does return [enabled by default
// and
// multiple definition of `std::__throw_bad_function_call()'
// if we include it with this Helper it works..
#endif

#endif // ifndef slight_FIX__THROW_BAD_FUNCTION_CALL_h

Expand Down

0 comments on commit 385b91c

Please sign in to comment.