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.33k stars 243 forks source link

Method to get the name of the Callback registered in the Task #169

Closed georgevbsantiago closed 1 month ago

georgevbsantiago commented 1 year ago

Hello @arkhipenko

First of all, THANK YOU SO MUCH for this fantastic library!!!

A doubt. Is there a method to get the current callback name of the task? In this case, when we use Task as a finite state machine, it would be interesting to know which Callback is registered in Task.

Ex:

void function_01 () { ... }

void function_02 () { ... }

void function_03 () { ... }

task.setCallback(&function_01);
...
task.setCallback(&function_02);
....
task.setCallback(&function_03);
....
    if( equal(task.getCallback(), &function_02) ) {

    ....
    }
arkhipenko commented 1 year ago

I never needed this functionality and it was not asked for until now, hence it does not exist in the current version.

georgevbsantiago commented 1 year ago

I'm using Task as a finite state machine to manage the messages on the 20x04 Display. The need to know which Callback is registered in the Task arose from the need to know which message was being viewed on the Display. If a 'getCallback' method is made available in a future version, it would be possible to identify the current Callback registered in the Task.

Thanks.

georgevbsantiago commented 1 year ago

I got this idea (to use the Task to visualize the messages on the 20x04 Display) after reading this example from the documentation. 2. "Native" support for finite state machine https://github.com/arkhipenko/TaskScheduler/wiki/Implementation-scenarios-and-ideas/#2-native-support-for-finite-state-machine

arkhipenko commented 1 year ago

If a 'getCallback' method is made available in a future version, it would be possible to identify the current Callback registered in the Task.

The callback is a piece of code, not an object, so it can not really hold a "name". I can have Tasks return a pointer to the current callbacks, and you would need to compare to known pointers to figure out which one is assigned. Would that satisfy your requirement?

georgevbsantiago commented 1 year ago

Hi, I believe so.

What would be the proper approach to purchasing the 'getCallback' method with the compared function pointer? Something like? : task.getCallback() == &function_02

arkhipenko commented 1 year ago

Yes, something like that I guess.

georgevbsantiago commented 1 year ago

I'm a beginner in C++, but maybe this post can help: https://stackoverflow.com/questions/12898227/comparing-function-pointers

arkhipenko commented 1 month ago

Meanwhile you can use void setLtsPointer(void *aPtr); and void *getLtsPointer(); to store and compare the address of the callback method:

void function_01 () { ... }
task.setCallback(&function_01);
task.setLtsPointer((void*) &function_01);

would that work as a workaround?

(to enable LTS - use #define _TASK_LTS_POINTER compile option