UKHeliBob:
Changes made and it still works !Unless anyone is really interested I won't post the revised code but if anyone would like to see it I will gladly post it.
One more thing... since you are using a function pointer: If your function is only called from one place, you don't need to create a whole separate function, you can use a lambda with the same signature:
class TestCallback
{
private:
int _startCount;
int _currentCount;
int _previousCount;
unsigned long _currentTime;
unsigned long _startTime;
unsigned long _period;
int _number = 123;
public:
TestCallback();
using callbackFunc = void (*) (int);
callbackFunc cb1;
void begin();
void update(callbackFunc func = nullptr);
};
TestCallback::TestCallback()
{
}
void TestCallback::begin()
{
_startCount = 5;
_currentCount = 5;
_period = 500;
}
void TestCallback::update(callbackFunc func)
{
cb1 = func;
_currentTime = millis();
if (_currentTime - _startTime >= _period)
{
_previousCount = _currentCount;
_currentCount--;
if (_currentCount < 0)
{
_currentCount = _startCount;
}
_startTime = _currentTime;
if (cb1)
{
cb1(_currentCount); //execute the callback function
}
}
}
TestCallback testing;
void setup() {
Serial.begin(9600);
testing.begin();
}
void loop() {
testing.update([](int count){
Serial.println(count);
});
}
//or even like this:
//void loop() {
// auto printCount = [](int count) {
// Serial.println(count);
// };
//
// testing.update(printCount);
//}
//or even more pedantic, like this:
//void loop() {
// TestCallback::callbackFunc printCount = [](int count) {
// Serial.println(count);
// };
//
// testing.update(printCount);
//}