From 0061d06ba4b4ecb9ed651c315769edb047323009 Mon Sep 17 00:00:00 2001 From: hreintke Date: Mon, 1 Jan 2018 14:41:26 +0100 Subject: [PATCH 1/3] Add Functional callback to Ticker Add Scheduled callback to Ticker --- libraries/Ticker/Ticker.cpp | 22 ++++++++++++++++++++++ libraries/Ticker/Ticker.h | 33 ++++++++++++++++++++++++--------- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/libraries/Ticker/Ticker.cpp b/libraries/Ticker/Ticker.cpp index b348798c66..b68dead71f 100644 --- a/libraries/Ticker/Ticker.cpp +++ b/libraries/Ticker/Ticker.cpp @@ -21,6 +21,7 @@ #include #include +#include "Arduino.h" extern "C" { #include "c_types.h" @@ -67,4 +68,25 @@ 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) + { + if (pTicker->scheduleTicker) + { + schedule_function(pTicker->internalTicker); + } + else + { + pTicker->internalTicker(); + } + } } diff --git a/libraries/Ticker/Ticker.h b/libraries/Ticker/Ticker.h index ea3f59f1cc..ea91e85a94 100644 --- a/libraries/Ticker/Ticker.h +++ b/libraries/Ticker/Ticker.h @@ -24,6 +24,8 @@ #include #include +#include +#include extern "C" { typedef struct _ETSTIMER_ ETSTimer; @@ -34,17 +36,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 @@ -66,14 +73,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 @@ -96,10 +107,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; + }; From 17b00a7890e0624c683219e88319d78db4d8df06 Mon Sep 17 00:00:00 2001 From: hreintke Date: Tue, 2 Jan 2018 13:19:48 +0100 Subject: [PATCH 2/3] Updated based on remarks --- libraries/Ticker/Ticker.cpp | 19 ++++-------- libraries/Ticker/Ticker.h | 58 +++++++++++++++++++++++-------------- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/libraries/Ticker/Ticker.cpp b/libraries/Ticker/Ticker.cpp index b68dead71f..d913a872bb 100644 --- a/libraries/Ticker/Ticker.cpp +++ b/libraries/Ticker/Ticker.cpp @@ -68,25 +68,18 @@ void Ticker::detach() os_timer_disarm(_timer); delete _timer; _timer = 0; - internalTicker = nullptr; + _callback_function = nullptr; } -void Ticker::internalCallback(void* arg) +void Ticker::_static_callback(void* arg) { - Ticker* pTicker = (Ticker*)arg; - if (pTicker == nullptr) + Ticker* _this = (Ticker*)arg; + if (_this == nullptr) { return; } - if (pTicker->internalTicker) + if (_this->_callback_function) { - if (pTicker->scheduleTicker) - { - schedule_function(pTicker->internalTicker); - } - else - { - pTicker->internalTicker(); - } + _this->_callback_function(); } } diff --git a/libraries/Ticker/Ticker.h b/libraries/Ticker/Ticker.h index ea91e85a94..d1c6002cef 100644 --- a/libraries/Ticker/Ticker.h +++ b/libraries/Ticker/Ticker.h @@ -37,21 +37,30 @@ class Ticker Ticker(); ~Ticker(); + typedef void (*callback_t)(void); typedef void (*callback_with_arg_t)(void*); - typedef std::function TickerFunction; + typedef std::function callback_function_t; - void attach(float seconds, TickerFunction tf, bool reqSchedule = false) + void attach_scheduled(float seconds, callback_function_t callback) { - internalTicker = tf; - scheduleTicker = reqSchedule; - attach(seconds, internalCallback, (void*)this); + attach(seconds,std::bind(schedule_function, callback)); } - void attach_ms(uint32_t milliseconds, TickerFunction tf, bool reqSchedule = false) + void attach(float seconds, callback_function_t callback) { - internalTicker = tf; - scheduleTicker = reqSchedule; - attach_ms(milliseconds, internalCallback, (void*)this); + _callback_function = callback; + attach(seconds, _static_callback, (void*)this); + } + + void attach_ms_scheduled(uint32_t milliseconds, callback_function_t callback) + { + attach_ms(milliseconds, std::bind(schedule_function, callback)); + } + + void attach_ms(uint32_t milliseconds, callback_function_t callback) + { + _callback_function = callback; + attach_ms(milliseconds, _static_callback, (void*)this); } template @@ -73,18 +82,26 @@ class Ticker _attach_ms(milliseconds, true, reinterpret_cast(callback), arg32); } - void once(float seconds, TickerFunction tf, bool reqSchedule = false) + void once_scheduled(float seconds, callback_function_t callback) + { + once(seconds, std::bind(schedule_function, callback)); + } + + void once(float seconds, callback_function_t callback) { - internalTicker = tf; - scheduleTicker = reqSchedule; - once(seconds, internalCallback, (void*)this); + _callback_function = callback; + once(seconds, _static_callback, (void*)this); } - void once_ms(uint32_t milliseconds, TickerFunction tf, bool reqSchedule = false) + void once_ms_scheduled(uint32_t milliseconds, callback_function_t callback) { - internalTicker = tf; - scheduleTicker = reqSchedule; - once_ms(milliseconds, internalCallback, (void*)this); + once_ms(milliseconds, std::bind(schedule_function, callback)); + } + + void once_ms(uint32_t milliseconds, callback_function_t callback) + { + _callback_function = callback; + once_ms(milliseconds, _static_callback, (void*)this); } template @@ -107,15 +124,12 @@ class Ticker protected: void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg); - static void internalCallback (void* arg); - + static void _static_callback (void* arg); protected: ETSTimer* _timer; - TickerFunction internalTicker = nullptr; - bool scheduleTicker = false; + callback_function_t _callback_function = nullptr; }; - #endif//TICKER_H From 988d96e62137f09bba2fe8182779475531ba0eee Mon Sep 17 00:00:00 2001 From: hreintke Date: Tue, 16 Jan 2018 11:40:38 +0100 Subject: [PATCH 3/3] Add example --- .../TickerFunctional/TickerFunctional.ino | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 libraries/Ticker/examples/TickerFunctional/TickerFunctional.ino diff --git a/libraries/Ticker/examples/TickerFunctional/TickerFunctional.ino b/libraries/Ticker/examples/TickerFunctional/TickerFunctional.ino new file mode 100644 index 0000000000..08c9dbf032 --- /dev/null +++ b/libraries/Ticker/examples/TickerFunctional/TickerFunctional.ino @@ -0,0 +1,70 @@ +#include "Arduino.h" +#include "Ticker.h" + +#define LED1 2 +#define LED2 4 +#define LED3 12 +#define LED4 14 +#define LED5 15 + + +class ExampleClass +{ +public: + ExampleClass(int pin, int duration) : _pin(pin), _duration(duration) + { + pinMode(_pin, OUTPUT); + _myTicker.attach_ms(_duration, std::bind(&ExampleClass::classBlink,this)); + } + ~ExampleClass(){}; + + int _pin, _duration; + Ticker _myTicker; + + void classBlink() + { + digitalWrite(_pin, !digitalRead(_pin)); + } +}; + +void staticBlink() +{ + digitalWrite(LED2, !digitalRead(LED2)); +} + +void scheduledBlink() +{ + digitalWrite(LED3, !digitalRead(LED2)); +} + +void parameterBlink(int p) +{ + digitalWrite(p, !digitalRead(p)); +} + +Ticker staticTicker; +Ticker scheduledTicker; +Ticker parameterTicker; +Ticker lambdaTicker; + +ExampleClass example(LED1, 100); + + +void setup() +{ + pinMode(LED2, OUTPUT); + staticTicker.attach_ms(100, staticBlink); + + pinMode(LED3, OUTPUT); + scheduledTicker.attach_ms_scheduled(100, scheduledBlink); + + pinMode(LED4, OUTPUT); + parameterTicker.attach_ms(100, std::bind(parameterBlink,LED4)); + + pinMode(LED5, OUTPUT); + lambdaTicker.attach_ms(100, [](){digitalWrite(LED5, !digitalRead(LED5));} ); +} + +void loop() +{ +}