Arduino icon indicating copy to clipboard operation
Arduino copied to clipboard

Longer delays for Ticker and some internal updates

Open mcspr opened this issue 3 years ago • 1 comments

resolves #8066

Adds max duration check. In case it is over SDK limit, enable 'repeat'ing timer with a duration proportional to the original one and count until it executes N times, only then run the callback. Code with durations less than that executes as usual. Original proposal was to not create anything or create some kind of error state... which seems counter-productive to not help out with this pretty solvable use-case.

Three additional updates, while refactoring the class

  • Stronger types for internal time management using std::chrono::duration. Works the same, std::chrono::duration handles seconds <-> milliseconds conversion.
  • ::detach() 'once' timer when it finishes. Fixes (unintentional?) side-effect that we remain ::active(). Plus, this destroys any lambda-bound variables that will persist with the Ticker object. And, since we can't re-arm with the same function (Ticker::attach_ms(uint32_t just_the_time) and etc.)
  • std::variant aka union for internal callback storage (kind-of similar to #6918). Instead of having two separate code paths, always attach our static function and dispatch using type info. Also helps with the issue described above, since it will call std::function dtor when ptr + arg is attached instead of doing nothing.

mcspr avatar Jul 02 '22 21:07 mcspr

Speaking of void(*)(char) to void(*)(void*) conversion... Does it actually work correctly for arguments less than 4 bytes? Don't we have some undefined behaviour here?

mcspr avatar Jul 02 '22 21:07 mcspr