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.24k stars 226 forks source link

enableDelayed() calls Callback immediately #185

Closed moritz89 closed 7 months ago

moritz89 commented 7 months ago

This might be a communication issue regarding the documentation and function naming, but when creating a task and the calling enableDelayed(wait_time_ms) in the constructor, the Callback is called immediately after without respecting the delay. There seems to be no difference between enableDelayed() and enable() when used in the class interface.

A work-around is to manually handle the first iteration by saving the initial delay to a class member and then in the callback handling the first iteration manually:

MyTask::MyTask() {
  if(some_bool) {
    initial_delay_ = WAIT_CONTSTANT_MS;
  }
  enable();
}

bool MyTask::Callback() {
  if (isFirstIteration() && initial_delay_) {
    Task::delay(initial_delay_);
    return true;
  }
  ...
}

Might this not be a problem if using this approach: https://github.com/arkhipenko/TaskScheduler/issues/78#issuecomment-538408706

moritz89 commented 7 months ago

After further research it is probably due to user code :sweat_smile: but it does not seem trivial...

moritz89 commented 7 months ago

It was trivial, was calling enable() after the task was created/constructor called... sorry for the commotion. Lesson: sleep on it and debug the next day