mikaelpatel / Arduino-Scheduler

Portable Cooperative Multi-tasking Scheduler for Arduino
https://mikaelpatel.github.io/Arduino-Scheduler/
164 stars 41 forks source link

thread safe sleep() #39

Closed Sylensky closed 1 year ago

Sylensky commented 1 year ago

Hi,

Is it possible to add a popular sleep(ms) function back to the scheduler? I am looking for a similiar approach to a vTaskDelay(ms) from FreeRTOS for example.

So far there is just a yield() which context-switches to the next task immediately.

mikaelpatel commented 1 year ago

@Sylensky Thanks for the question. Actually the Arduino core function delay(ms) uses yield() and thus gives the coroutine/thread version of sleep().

https://github.com/arduino/ArduinoCore-avr/blob/42fa4a1ea1b1b11d1cc0a60298e529d37f9d14bd/cores/arduino/wiring.c#L106

https://github.com/mikaelpatel/Arduino-Scheduler/blob/8f339ae52fc2751f9e4d113e29849d4413226e05/examples/SchedulerDebounce/SchedulerDebounce.ino#L67

Cheers, Mikael

Sylensky commented 1 year ago

Oh thanks, this just pointed me into the direction of a bug in my code.