Because I like programming in objects/classes, I got myself in a situation to use timers and callbacks. This is generally an issue in c++ (member function pointers)
I found this Delegate system that covers a whole lot (too much for standard Arduino programming)
http://www.codeproject.com/KB/cpp/FastDelegate.aspx
I works very nice, changed the SimpleTimer lib (Author: mromani@ottotecnica.com) to be used with this Delegate system.
What I was thinking, since Arduino programming is getting more and more professional on one-side, having a standard low-footprint Delegate system could be a nice addition.
I already got it working and the extra memory needed is very little, the guy who made it states that in most cases it would only compile to two lines of assembly. What still needs to be done is cutting it up (i guess 80% of the code could be discarded since Arduino uses a single compiler) and making it integegrate easy (without API breaks)
this is my compile output for a small program with this Delegate system:
With FastDelegate:
AVR Memory Usage
----------------
Device: atmega2560
Program: 17178 bytes (6.6% Full)
(.text + .data + .bootloader)
Data: 1292 bytes (15.8% Full)
(.data + .bss + .noinit)
Finished building: sizedummy
Without FastDelegate:
AVR Memory Usage
----------------
Device: atmega2560
Program: 17030 bytes (6.5% Full)
(.text + .data + .bootloader)
Data: 1292 bytes (15.8% Full)
(.data + .bss + .noinit)
Finished building: sizedummy
Code as in test (working)
typedef
typedef FastDelegate0<> FuncDelegate;
Code in robot class:
void Robot::test(){
FuncDelegate f_delegate;
f_delegate = MakeDelegate(this, &Robot::halt);
timer.setTimerDelg(1, f_delegate, 1);
}
void Robot::halt() {
Serial.println("TEST");
}
Code in SimpleTimer class:
int SimpleTimer::setTimerDelg(long d, FuncDelegate f, int n){
f();
}
Arduino prints TEST in the console
It all worked nice and fast, if it is not something to be used, mods may delete this post.