arkhipenko / TaskScheduler

Cooperative multitasking for Arduino, ESPx, STM32, nRF and other microcontrollers
http://playground.arduino.cc/Code/TaskScheduler
BSD 3-Clause "New" or "Revised" License
1.22k stars 224 forks source link

Unable to pass class member funtion into Task #101

Closed nxg3n closed 4 years ago

nxg3n commented 4 years ago

I am writing a class that will manage the tasking side of my program. However, when I tray to instantiate a new Task object by passing in &SomeClass::startDayTask I get the following error.

Task startDayTask(1000, 10, &SomeClass::startDay);

Same error for:

Task startDayTask(1000, 10, &startDay);

error: no matching function for call to 'Task::Task(int, int, void (SomeClass::*)())' Task startDayTask(1000, 10, &SomeClass::startDay);

nxg3n commented 4 years ago

Also to clarify this code resides inside a SomeClass method.

arkhipenko commented 4 years ago

You can not use dynamically created objects as a reference with a static task. How would a compiler know which instance of &SomeClass::startDay to pass to?

If you want to use dynamic binding you have to compile with #define _TASK_OO_CALLBACKS compile option. There is not much documentation for it yet - did not have enough time to create one.

It is completely different approach where instead of declaring tasks as static objects you create your specific tasks as objects which you inherit from the class "Task".

This example should give you an idea: https://github.com/arkhipenko/TaskScheduler/tree/master/examples/Scheduler_example21_OO_Callbacks

I personally find this approach more trouble than benefit, but there is at least one guy who likes it a lot.

As an ugly workaround, you can pass the object pointer via LTS to a task, but that is, as I said, an ugly workaround. https://github.com/arkhipenko/TaskScheduler/wiki/API-Task#local-task-storage-methods-1 https://github.com/arkhipenko/TaskScheduler/tree/master/examples/Scheduler_example08_LTS

nxg3n commented 4 years ago

Hi @arkhipenko ,

Thanks so much for the response. I will take a look at the examples now.

I am doing it this way as I have a control system in which i have various different schedule possibilities. Each of these and its params are stored in an object. This allows me to init these objects at compile-time and cycle through the options on the keypad.

I Will let you know how I go.

GitMoDu commented 4 years ago

but there is at least one guy who likes it a lot.

Hi!

The big point is to decouple the Project from the Task itself, meaning I can add any functionality that requires tasking (think a library that also needs some loop time), without needing any further setup beyond the contructor.

There is not much documentation for it yet - did not have enough time to create one.

I'll see what I can do to help there.