Arduino icon indicating copy to clipboard operation
Arduino copied to clipboard

Documentation request: Ticker, attach_ms and attach_ms_scheduled

Open d-a-v opened this issue 6 years ago • 3 comments

Hint: Ticker/TickerFunctional.ino example edit ~This conversation~ (<= no more accessible) Here is the very first backuped chat message since change from gitter to elements/matrix

d-a-v avatar Aug 26 '19 15:08 d-a-v

The linked conversation does not help. It points to a matrix room with discussions of various things. I'd love to see the discussion here, so that anybody can follow it

tom-ch1 avatar Oct 16 '23 14:10 tom-ch1

Here's the documentation of Ticker: https://arduino-esp8266.readthedocs.io/en/latest/libraries.html#ticker

It is currently not recommended to do blocking IO operations (network, serial, file) from Ticker callback functions. Instead, set a flag inside the ticker callback and check for that flag inside the loop function.

Here's the source code of attach_scheduled:

// callback will be called at following loop() after ticker fires

As there is no documentation other than the source code and the examples, one would deduce that using attach_scheduled resolves the warning given in the recommendation

Is that correct?

According to the Example TickerFunctional.ino, there is no need for any funcion call inside loop(), and nevertheless, the callback is called inside loop()

Is that correct?

tom-ch1 avatar Oct 16 '23 14:10 tom-ch1

As there is no documentation other than the source code and the examples, one would deduce that using attach_scheduled resolves the warning given in the recommendation

Is that correct?

Right, _scheduled are the exception. Note the code flow - scheduled funcs are called only after loop() normally returns , or when yield() suspends loop() and our user task to run SDK tasks https://github.com/esp8266/Arduino/blob/1662248b394740d82824f504a021d4dad3d314b6/cores/esp8266/core_esp8266_main.cpp#L247-L251 https://github.com/esp8266/Arduino/blob/1662248b394740d82824f504a021d4dad3d314b6/cores/esp8266/core_esp8266_main.cpp#L262-L263 edit: disregard the yield() bit, normal scheduled funcs can only run after loop() normally returns. there are recurrent ones that do both, but this is not the type of scheduled function that ticker uses

As there is no documentation other than the source code and the examples, one would deduce that using attach_scheduled resolves the warning given in the recommendation

Is that correct?

attach_ms callback would execute whenever SDK decides it is time for the timer to expire and in SDK context. That happens outside of user task, after yield() gives SDK task a chance to run. For attach_ms_scheduled we effectively do schedule_function([user_callback]() { user_callback(); })

mcspr avatar Oct 16 '23 18:10 mcspr