diff --git a/libraries/Ticker/Ticker.cpp b/libraries/Ticker/Ticker.cpp index b348798c66..40f375be3a 100644 --- a/libraries/Ticker/Ticker.cpp +++ b/libraries/Ticker/Ticker.cpp @@ -67,4 +67,18 @@ void Ticker::detach() os_timer_disarm(_timer); delete _timer; _timer = 0; + internalTicker = nullptr; +} + +void Ticker::internalCallback(void* arg) +{ + Ticker* pTicker = (Ticker*)arg; + if (pTicker == nullptr) + { + return; + } + if (pTicker->internalTicker) + { + pTicker->internalTicker(); + } } diff --git a/libraries/Ticker/Ticker.h b/libraries/Ticker/Ticker.h index ea3f59f1cc..94ba689570 100644 --- a/libraries/Ticker/Ticker.h +++ b/libraries/Ticker/Ticker.h @@ -24,6 +24,7 @@ #include #include +#include extern "C" { typedef struct _ETSTIMER_ ETSTimer; @@ -36,12 +37,25 @@ class Ticker ~Ticker(); typedef void (*callback_t)(void); typedef void (*callback_with_arg_t)(void*); + typedef std::function TickerFunction; + + void attach(float seconds, TickerFunction tf) + { + internalTicker = tf; + attach(seconds, internalCallback, (void*)this); + } void attach(float seconds, callback_t callback) { _attach_ms(seconds * 1000, true, reinterpret_cast(callback), 0); } + void attach_ms(uint32_t milliseconds, TickerFunction tf) + { + internalTicker = tf; + attach_ms(milliseconds, internalCallback, (void*)this); + } + void attach_ms(uint32_t milliseconds, callback_t callback) { _attach_ms(milliseconds, true, reinterpret_cast(callback), 0); @@ -66,11 +80,23 @@ class Ticker _attach_ms(milliseconds, true, reinterpret_cast(callback), arg32); } + void once(float seconds, TickerFunction tf) + { + internalTicker = tf; + once(seconds, internalCallback, (void*)this); + } + void once(float seconds, callback_t callback) { _attach_ms(seconds * 1000, false, reinterpret_cast(callback), 0); } + void once_ms(uint32_t milliseconds, TickerFunction tf) + { + internalTicker = tf; + once_ms(milliseconds, internalCallback, (void*)this); + } + void once_ms(uint32_t milliseconds, callback_t callback) { _attach_ms(milliseconds, false, reinterpret_cast(callback), 0); @@ -96,10 +122,13 @@ class Ticker protected: void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg); + static void internalCallback (void* arg); protected: ETSTimer* _timer; + TickerFunction internalTicker = nullptr; + };