contrem / arduino-timer

Non-blocking library for delaying function calls
BSD 3-Clause "New" or "Revised" License
334 stars 50 forks source link

is there a more efficient way to cancel a timer in the future? #50

Closed johanneswn closed 3 years ago

johanneswn commented 3 years ago

In my code, alarm() is called once, which activates the previously defined Task blinkAlarm, calling the function alarmSub() every blinkDelay. I want this alarmSub() function to run for a limited time, duration, and then end. currently, I set up another function, alarmEnd(), which is called using timer.in(duration, alarmEnd), but this seems unnecessarily complicated to me. My setup might be more of an edge-case though..

Here's my full code

Timer<>::Task blinkAlarm;

bool alarmSub(void *)
{
  alarmState *= -1;
  if(alarmState == 1) {
    on();
  } else {
    off();
  }
  return true;
}
bool alarmEnd(void *) {
  timer.cancel(blinkAlarm);
  return true;
}
void alarm()
{
  blinkAlarm = timer.every(blinkDelay, alarmSub);
  timer.in(duration, alarmEnd);
}