From adde93bcaa6eb32fee05bc11cde63ab335036bd3 Mon Sep 17 00:00:00 2001 From: hreintke Date: Tue, 14 Aug 2018 21:00:20 +0200 Subject: [PATCH] Implementation of Functional and Scheduled option in Ticker lib (#5030) * Implementation of Functional and Scheduled option in Ticker lib * Update example formatting * More example updates * More updates to example * More updates to example --- libraries/Ticker/Ticker.cpp | 14 ++++ libraries/Ticker/Ticker.h | 46 ++++++++++--- .../TickerFunctional/TickerFunctional.ino | 64 +++++++++++++++++++ 3 files changed, 115 insertions(+), 9 deletions(-) create mode 100644 libraries/Ticker/examples/TickerFunctional/TickerFunctional.ino diff --git a/libraries/Ticker/Ticker.cpp b/libraries/Ticker/Ticker.cpp index dee3a6363..5fa2dc960 100644 --- a/libraries/Ticker/Ticker.cpp +++ b/libraries/Ticker/Ticker.cpp @@ -65,9 +65,23 @@ void Ticker::detach() os_timer_disarm(_timer); delete _timer; _timer = nullptr; + _callback_function = nullptr; } bool Ticker::active() { return (bool)_timer; } + +void Ticker::_static_callback(void* arg) +{ + Ticker* _this = (Ticker*)arg; + if (_this == nullptr) + { + return; + } + if (_this->_callback_function) + { + _this->_callback_function(); + } +} diff --git a/libraries/Ticker/Ticker.h b/libraries/Ticker/Ticker.h index 74725fb76..d006c8ffc 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; @@ -37,15 +39,28 @@ public: ~Ticker(); typedef void (*callback_t)(void); typedef void (*callback_with_arg_t)(void*); + typedef std::function callback_function_t; - void attach(float seconds, callback_t callback) + void attach_scheduled(float seconds, callback_function_t callback) { - _attach_ms(seconds * 1000, true, reinterpret_cast(callback), 0); + attach(seconds,std::bind(schedule_function, callback)); } - void attach_ms(uint32_t milliseconds, callback_t callback) + void attach(float seconds, callback_function_t callback) { - _attach_ms(milliseconds, true, reinterpret_cast(callback), 0); + _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 @@ -67,14 +82,26 @@ public: _attach_ms(milliseconds, true, reinterpret_cast(callback), arg32); } - void once(float seconds, callback_t callback) + void once_scheduled(float seconds, callback_function_t callback) { - _attach_ms(seconds * 1000, false, reinterpret_cast(callback), 0); + once(seconds, std::bind(schedule_function, callback)); } - void once_ms(uint32_t milliseconds, callback_t callback) + void once(float seconds, callback_function_t callback) { - _attach_ms(milliseconds, false, reinterpret_cast(callback), 0); + _callback_function = callback; + once(seconds, _static_callback, (void*)this); + } + + void once_ms_scheduled(uint32_t milliseconds, callback_function_t callback) + { + 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 @@ -98,10 +125,11 @@ public: protected: void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg); - + static void _static_callback (void* arg); protected: ETSTimer* _timer; + callback_function_t _callback_function = nullptr; }; diff --git a/libraries/Ticker/examples/TickerFunctional/TickerFunctional.ino b/libraries/Ticker/examples/TickerFunctional/TickerFunctional.ino new file mode 100644 index 000000000..33c943598 --- /dev/null +++ b/libraries/Ticker/examples/TickerFunctional/TickerFunctional.ino @@ -0,0 +1,64 @@ +#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() { +}