Replies: 4 comments 6 replies
-
arduino does support std::function ? i guess not, but esp32 can for example. |
Beta Was this translation helpful? Give feedback.
-
See also #59. There is a workaround described in #11. Std::function is part of C++11 (2011)... but Arduino has been around since ~2005. It can be difficult to confirm that std::function is actually supported on all compilers for all Arduino platforms -- especially as people are still adding new boards. Hopefully new boards support the latest compilers. |
Beta Was this translation helpful? Give feedback.
-
well, that makes sense, I've never used atmega chips with arduino platform, only esp8266/esp32 which both support this. @philj404 while the workaround does the job, the direct path would have been much much shorter with zero hassle for the supported platforms. Task in(std::function<bool(void)> callback, unsinged long delay) or another way would be just # if !defined(ESP32) && !defined(ESP8266)
typedef bool (*handler_t)(T opaque); /* task handler func signature */
#else
typedef handler_t std::function<bool(T opaque)> callback
#endif |
Beta Was this translation helpful? Give feedback.
-
A work around could be to use the parametrized handler argument with a dispatcher function: (untested) #include <functional>
#include <arduino-timer.h>
template <typename T>
bool dispatcher(T action) {
return action();
}
Timer<16, millis, std::function<bool(void)> > timer;
void setup() {
int closure = 1;
timer.in(10, dispatcher, [&]() -> bool {
if (closure) return true;
else return false;
});
}
void loop() {
timer.tick();
} |
Beta Was this translation helpful? Give feedback.
-
Since using std::function has many benefits such as being able to pass multiple arguments (using capture clause) and simpler syntax, I'm curious to know is there a reason you chose function pointers over this?
here is a reference for comparison
std::function or a function pointer in C++?
are you open to adding another function overload using
std::function
without breaking backward compatibility?Beta Was this translation helpful? Give feedback.
All reactions