There is some news on this:
I meanwhile found some very interesting code in a (german) c++ forum
https://www.c-plusplus.net/forum/to...chere-eigene-variante-ersetzen-signal-slot/17, which shows how to implement a tiny drop in replacement of std::function which doesn't use dynamic memory allocation. Like in my CallbackHelper, it stores the passed in objects (function pointes, lambdas, functors...) in a preallocated buffer. I defaulted the buffer size to 16 byte which is plenty, but it can be adjusted from user code if necessary.
I had to tweak the code a bit to compile with std::c++11, but now it is pretty generic and works with a lot of boards (XIAO, Nucleo, ESP32, SAMD...). However, it doesn't compile with the old GCC5.4 (misses some utilities). So, to try it on a Teensy one currently needs to use Teensyduino beta version. Here the link to the library:
https://github.com/luni64/staticFunctional (actually it is a one header only library. So, just copy the file "staticFunctional.h" to your sketch folder and you are good to go)
The repo contains some examples, and, just for the fun of it
a reworked version of the IntervalTimer. Adopting IntervalTimer only required to change the declarations of the begin(...) functions from the explicit begin(void(*func)(), ....) to begin(callback_t func,....). callback_t is defined by "using callback_t = function<void(void)>;" That was basically all I needed to change. It shouldn't break any existing code since it still accepts void(*)() callbacks.
Here a simple usage example for the library.
Code:
#include "staticFunctional.h"
using namespace staticFunctional; // save typing
class MyClass
{
public:
void nonStaticMemberFunction() {
Serial.printf("nonStaticMemberFunction i=%d\n", i);
}
void operator()() const {
Serial.printf("functor 2i=%d\n", 2*i);
}
int i = 42;
};
MyClass myClass;
void freeFunc(){
Serial.println("free function");
}
void setup()
{
while (!Serial) {}
function<void(void)> f; // function taking no arguments and returning nothing
f = freeFunc;
f();
f = [] { Serial.println("lambda expression"); };
f();
f = [] { myClass.nonStaticMemberFunction(); }; // non static member function
f();
f = myClass; // functor, f uses operator ()()
f();
}
void loop(){
}
// prints:
// free function
// lambda expression
// nonStaticMemberFunction i=42
// functor 2i=84