From c4a2e38e79a89af8baea97dd49310bd43616b2d3 Mon Sep 17 00:00:00 2001 From: hreintke Date: Mon, 1 Jan 2018 14:41:26 +0100 Subject: [PATCH] Add Functional callback to Ticker Add Scheduled callback to Ticker --- libraries/Ticker/Ticker.cpp | 21 +++++++++++++++++++++ libraries/Ticker/Ticker.h | 33 ++++++++++++++++++++++++--------- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/libraries/Ticker/Ticker.cpp b/libraries/Ticker/Ticker.cpp index dee3a6363d..d6b0fe611d 100644 --- a/libraries/Ticker/Ticker.cpp +++ b/libraries/Ticker/Ticker.cpp @@ -21,6 +21,7 @@ #include #include +#include "Arduino.h" #include "c_types.h" #include "eagle_soc.h" @@ -71,3 +72,23 @@ bool Ticker::active() { return (bool)_timer; } + +void Ticker::internalCallback(void* arg) +{ + Ticker* pTicker = (Ticker*)arg; + if (pTicker == nullptr) + { + return; + } + if (pTicker->internalTicker) + { + if (pTicker->scheduleTicker) + { + schedule_function(pTicker->internalTicker); + } + else + { + pTicker->internalTicker(); + } + } +} diff --git a/libraries/Ticker/Ticker.h b/libraries/Ticker/Ticker.h index 74725fb76d..b8103522f7 100644 --- a/libraries/Ticker/Ticker.h +++ b/libraries/Ticker/Ticker.h @@ -25,6 +25,8 @@ #include #include #include +#include +#include extern "C" { typedef struct _ETSTIMER_ ETSTimer; @@ -35,17 +37,22 @@ class Ticker public: Ticker(); ~Ticker(); - typedef void (*callback_t)(void); + typedef void (*callback_with_arg_t)(void*); + typedef std::function TickerFunction; - void attach(float seconds, callback_t callback) + void attach(float seconds, TickerFunction tf, bool reqSchedule = false) { - _attach_ms(seconds * 1000, true, reinterpret_cast(callback), 0); + internalTicker = tf; + scheduleTicker = reqSchedule; + attach(seconds, internalCallback, (void*)this); } - void attach_ms(uint32_t milliseconds, callback_t callback) + void attach_ms(uint32_t milliseconds, TickerFunction tf, bool reqSchedule = false) { - _attach_ms(milliseconds, true, reinterpret_cast(callback), 0); + internalTicker = tf; + scheduleTicker = reqSchedule; + attach_ms(milliseconds, internalCallback, (void*)this); } template @@ -67,14 +74,18 @@ class Ticker _attach_ms(milliseconds, true, reinterpret_cast(callback), arg32); } - void once(float seconds, callback_t callback) + void once(float seconds, TickerFunction tf, bool reqSchedule = false) { - _attach_ms(seconds * 1000, false, reinterpret_cast(callback), 0); + internalTicker = tf; + scheduleTicker = reqSchedule; + once(seconds, internalCallback, (void*)this); } - void once_ms(uint32_t milliseconds, callback_t callback) + void once_ms(uint32_t milliseconds, TickerFunction tf, bool reqSchedule = false) { - _attach_ms(milliseconds, false, reinterpret_cast(callback), 0); + internalTicker = tf; + scheduleTicker = reqSchedule; + once_ms(milliseconds, internalCallback, (void*)this); } template @@ -98,10 +109,14 @@ 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; + bool scheduleTicker = false; + };