From b57ec144509da04b81a5be5ef6ab624c3a79a1ae Mon Sep 17 00:00:00 2001 From: hreintke Date: Mon, 5 Dec 2016 17:47:31 +0100 Subject: [PATCH 1/3] Add functional callbacks to ticker --- libraries/Ticker/Ticker.cpp | 14 ++++++++++++++ libraries/Ticker/Ticker.h | 28 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) 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..3ecfce29e2 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(milliseconds, internalCallback, (void*)this); + } + void attach_ms(uint32_t milliseconds, callback_t callback) { _attach_ms(milliseconds, true, reinterpret_cast(callback), 0); @@ -66,11 +80,22 @@ 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) + { + 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 +121,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; + }; From 4b012f632d6b38c3a9f36b9186a9f0b37a02b286 Mon Sep 17 00:00:00 2001 From: napierala Date: Fri, 14 Apr 2017 20:49:46 +0200 Subject: [PATCH 2/3] Update Ticker.h Small bug fix in which the TickerFunction that was passed to: void once_ms(uint32_t milliseconds, TickerFunction tf) was ignored. --- libraries/Ticker/Ticker.h | 1 + 1 file changed, 1 insertion(+) diff --git a/libraries/Ticker/Ticker.h b/libraries/Ticker/Ticker.h index 3ecfce29e2..48761ebff7 100644 --- a/libraries/Ticker/Ticker.h +++ b/libraries/Ticker/Ticker.h @@ -93,6 +93,7 @@ class Ticker void once_ms(uint32_t milliseconds, TickerFunction tf) { + internalTicker = tf; once_ms(milliseconds, internalCallback, (void*)this); } From 04d44c4e6fa4fc30b9a1e18d58a6f476ad7f4def Mon Sep 17 00:00:00 2001 From: hreintke Date: Wed, 19 Apr 2017 17:50:12 +0200 Subject: [PATCH 3/3] Correct call to attach_ms --- libraries/Ticker/Ticker.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Ticker/Ticker.h b/libraries/Ticker/Ticker.h index 48761ebff7..94ba689570 100644 --- a/libraries/Ticker/Ticker.h +++ b/libraries/Ticker/Ticker.h @@ -53,7 +53,7 @@ class Ticker void attach_ms(uint32_t milliseconds, TickerFunction tf) { internalTicker = tf; - attach(milliseconds, internalCallback, (void*)this); + attach_ms(milliseconds, internalCallback, (void*)this); } void attach_ms(uint32_t milliseconds, callback_t callback)