Static Callback function with parameters?

C++ gurus,

Am using the TaskScheduler library to setup a timer for my object to "turn off" once the timer duration has expired after the object has been "turned on"

The task Scheduler library requires a Task object to be constructed as:

Task(unsigned long aInterval, long aIterations, void (aCallback)(), Scheduler aScheduler, bool aEnable, bool (*aOnEnable)(), void (*aOnDisable)());

I'm being told that the callback needs to be a static method.. and the following compiles and works fine in a class

Task * timer = new Task(duration, iterations, &turnOff, scheduler, false);

with void turnOff(){//blah}
defined in the main sketch (as a static method)

However since the turnOff method is static, I need to pass a parameter to the callback.
I can't get it to compile using

Task * timer = new Task(duration, iterations, &turnOff(pin), scheduler, false); //pin is an int value

with
with void turnOff(int pin){//blah} in the main sketch

My question is how to pass in an int parameter to the callback without modifying the library, or how to make the callback non static.. or ...

Thanks!

I could rewrite with millis, but am worried about the rollover.

The maximum timerDuration allowed is 23 hours 59 minutes so I'd only run into this issue if the switch was turned on within that duration of the rollover, but it could happen..

Am also reading that there are ways to workaround that issue but not sure how...

The two functions technique will not work - I need to have a way to reference back to the calling object (in which the turn off scheduler event fired), and for that I need to pass the GPIO pin # as the parameter...

BIOT:
I could rewrite with millis, but am worried about the rollover.

Nonsense. Written properly, a millis()-based timer can time for a duration of over 49 days regardless of when it starts relative to the roll over point.

Sorry, very much a C++ newbie as apparent.

My understanding of the two Functions method:
Step 1: Use static callback method with no parameters in Task definition (done)

_ Task * timer = new Task(dur, it, &turnTimedOff, device->scheduler, false);_

Step 2: In static turnTimedOff callback method, call the actual target function in the correct switch object

void turnTimedOff(){
//iterate vector containing up to 8 dynamically created "switch" objects and find the one with the GPIO Pin corresponding to the switch object that just fired the turnTimedOff scheduler event

//call the real turn off function on that object
object->turnOff();

}

Where Im stuck is I need the GPIO pin to get the object from the switches vector.....

Am I going about it wrong?

Will read up on millis and rollover to get a better understanding.

Thanks.

Okay, got it working

In the turnOn method
isTimerOn = true;
timerStartTime = millis();

Heres the loop function now

if(millis() - timerStartTime > timerDuration * 1000){ //timerduration is in seconds

  • turnTimedOff();*
    }

Hope I got it right.. :confused:

Make sure timerStartTime and timerDuration are of type 'unsigned long' (or uint32_t)