From a5df54fa193a1d16e33566c09007c3e350e5e391 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Mon, 9 Sep 2019 14:14:34 +0200 Subject: [PATCH 01/10] Backport from ESP32 --- libraries/Ticker/Ticker.cpp | 92 +++++++++++++++------- libraries/Ticker/Ticker.h | 144 +++++++++++++--------------------- libraries/Ticker/keywords.txt | 17 +--- 3 files changed, 121 insertions(+), 132 deletions(-) diff --git a/libraries/Ticker/Ticker.cpp b/libraries/Ticker/Ticker.cpp index 35ec21cdcd..f94d23d488 100644 --- a/libraries/Ticker/Ticker.cpp +++ b/libraries/Ticker/Ticker.cpp @@ -25,60 +25,96 @@ #include "Ticker.h" -namespace +Ticker::Ticker() + : _timer(nullptr) {} + +Ticker::~Ticker() { - constexpr int ONCE = 0; - constexpr int REPEAT = 1; + detach(); } -Ticker::Ticker() - : _timer(nullptr) +void Ticker::attach(float seconds, callback_function_t callback) { + _callback_function = std::move(callback); + _attach_ms(1000UL * seconds, true, _static_callback, this); } -Ticker::~Ticker() +void Ticker::attach_ms(uint32_t milliseconds, callback_function_t callback) +{ + _callback_function = std::move(callback); + _attach_ms(milliseconds, true, _static_callback, this); +} + +void Ticker::attach_scheduled(float seconds, callback_function_t callback) +{ + attach(seconds, [callback]() { schedule_function(callback); }); +} + +void Ticker::attach_ms_scheduled(uint32_t milliseconds, callback_function_t callback) +{ + attach_ms(milliseconds, [callback]() { schedule_function(callback); }); +} + +void Ticker::once(float seconds, callback_function_t callback) +{ + _callback_function = std::move(callback); + _attach_ms(1000UL * seconds, false, _static_callback, this); +} + +void Ticker::once_ms(uint32_t milliseconds, callback_function_t callback) +{ + _callback_function = std::move(callback); + _attach_ms(milliseconds, false, _static_callback, this); +} + +void Ticker::once_scheduled(float seconds, callback_function_t callback) +{ + once(seconds, [callback]() { schedule_function(callback); }); +} + +void Ticker::once_ms_scheduled(uint32_t milliseconds, callback_function_t callback) { - detach(); + once_ms(milliseconds, [callback]() { schedule_function(callback); }); } void Ticker::_attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg) { - _attach_ms(1000 * seconds, repeat, callback, arg); + _attach_ms(1000UL * seconds, repeat, callback, arg); } void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg) { - if (_timer) - { - os_timer_disarm(_timer); - } - else - { - _timer = &_etsTimer; - } - - os_timer_setfn(_timer, callback, arg); - os_timer_arm(_timer, milliseconds, (repeat) ? REPEAT : ONCE); + if (_timer) + { + os_timer_disarm(_timer); + } + else + { + _timer = &_etsTimer; + } + + os_timer_setfn(_timer, callback, arg); + os_timer_arm(_timer, milliseconds, repeat); } void Ticker::detach() { - if (!_timer) - return; + if (!_timer) + return; - os_timer_disarm(_timer); - _timer = nullptr; - _callback_function = nullptr; + os_timer_disarm(_timer); + _timer = nullptr; + _callback_function = nullptr; } bool Ticker::active() const { - return _timer; + return _timer; } void Ticker::_static_callback(void* arg) { - Ticker* _this = reinterpret_cast(arg); - if (_this && _this->_callback_function) - _this->_callback_function(); + Ticker* _this = reinterpret_cast(arg); + if (_this && _this->_callback_function) + _this->_callback_function(); } diff --git a/libraries/Ticker/Ticker.h b/libraries/Ticker/Ticker.h index 074e5ba32c..c1989337cb 100644 --- a/libraries/Ticker/Ticker.h +++ b/libraries/Ticker/Ticker.h @@ -29,100 +29,66 @@ class Ticker { public: - Ticker(); - ~Ticker(); - - typedef void (*callback_with_arg_t)(void *); - typedef std::function callback_function_t; - - void attach_scheduled(float seconds, callback_function_t callback) - { - attach(seconds, [callback]() { schedule_function(callback); }); - } - - void attach(float seconds, callback_function_t callback) - { - _callback_function = std::move(callback); - _attach_s(seconds, true, _static_callback, this); - } - - void attach_ms_scheduled(uint32_t milliseconds, callback_function_t callback) - { - attach_ms(milliseconds, [callback]() { schedule_function(callback); }); - } - - void attach_ms(uint32_t milliseconds, callback_function_t callback) - { - _callback_function = std::move(callback); - _attach_ms(milliseconds, true, _static_callback, this); - } - - template - void attach(float seconds, void (*callback)(TArg), TArg arg) - { - static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - // C-cast serves two purposes: - // static_cast for smaller integer types, - // reinterpret_cast + const_cast for pointer types - _attach_s(seconds, true, reinterpret_cast(callback), reinterpret_cast(arg)); - } - - template - void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg) - { - static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - _attach_ms(milliseconds, true, reinterpret_cast(callback), reinterpret_cast(arg)); - } - - void once_scheduled(float seconds, callback_function_t callback) - { - once(seconds, [callback]() { schedule_function(callback); }); - } - - void once(float seconds, callback_function_t callback) - { - _callback_function = std::move(callback); - _attach_s(seconds, false, _static_callback, this); - } - - void once_ms_scheduled(uint32_t milliseconds, callback_function_t callback) - { - once_ms(milliseconds, [callback]() { schedule_function(callback); }); - } - - void once_ms(uint32_t milliseconds, callback_function_t callback) - { - _callback_function = std::move(callback); - _attach_ms(milliseconds, false, _static_callback, this); - } - - template - void once(float seconds, void (*callback)(TArg), TArg arg) - { - static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - _attach_s(seconds, false, reinterpret_cast(callback), reinterpret_cast(arg)); - } - - template - void once_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg) - { - static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - _attach_ms(milliseconds, false, reinterpret_cast(callback), reinterpret_cast(arg)); - } - - void detach(); - bool active() const; + Ticker(); + ~Ticker(); + + typedef void (*callback_with_arg_t)(void*); + typedef std::function callback_function_t; + + void attach(float seconds, callback_function_t callback); + void attach_ms(uint32_t milliseconds, callback_function_t callback); + void attach_scheduled(float seconds, callback_function_t callback); + void attach_ms_scheduled(uint32_t milliseconds, callback_function_t callback); + + template + void attach(float seconds, void (*callback)(TArg), TArg arg) + { + static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); + // C-cast serves two purposes: + // static_cast for smaller integer types, + // reinterpret_cast + const_cast for pointer types + _attach_s(seconds, true, reinterpret_cast(callback), reinterpret_cast(arg)); + } + + template + void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg) + { + static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); + _attach_ms(milliseconds, true, reinterpret_cast(callback), (void*)arg); + } + + void once(float seconds, callback_function_t callback); + void once_ms(uint32_t milliseconds, callback_function_t callback); + void once_scheduled(float seconds, callback_function_t callback); + void once_ms_scheduled(uint32_t milliseconds, callback_function_t callback); + + template + void once(float seconds, void (*callback)(TArg), TArg arg) + { + static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); + _attach_s(seconds, false, reinterpret_cast(callback), reinterpret_cast(arg)); + } + + template + void once_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg) + { + static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); + _attach_ms(milliseconds, false, reinterpret_cast(callback), (void*)arg); + } + + void detach(); + bool active() const; protected: - void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg); - static void _static_callback(void* arg); + static void _static_callback(void* arg); + void _attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg); + void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg); - ETSTimer* _timer; - callback_function_t _callback_function = nullptr; + ETSTimer* _timer; + callback_function_t _callback_function = nullptr; private: - void _attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg); - ETSTimer _etsTimer; + ETSTimer _etsTimer; }; diff --git a/libraries/Ticker/keywords.txt b/libraries/Ticker/keywords.txt index 1ecd8d0eda..ab0b07e039 100644 --- a/libraries/Ticker/keywords.txt +++ b/libraries/Ticker/keywords.txt @@ -1,11 +1,9 @@ -####################################### -# Syntax Coloring Map For Wire -####################################### - ####################################### # Datatypes (KEYWORD1) ####################################### +Ticker KEYWORD1 + ####################################### # Methods and Functions (KEYWORD2) ####################################### @@ -16,14 +14,3 @@ once KEYWORD2 once_ms KEYWORD2 detach KEYWORD2 active KEYWORD2 - -####################################### -# Instances (KEYWORD2) -####################################### - -Ticker KEYWORD2 - -####################################### -# Constants (LITERAL1) -####################################### - From a43f74b0c5bebd70fb0f135ea5fe340c5106d47e Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Mon, 9 Sep 2019 14:15:12 +0200 Subject: [PATCH 02/10] Use new library layout (.../src) --- libraries/Ticker/{ => src}/Ticker.cpp | 0 libraries/Ticker/{ => src}/Ticker.h | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename libraries/Ticker/{ => src}/Ticker.cpp (100%) rename libraries/Ticker/{ => src}/Ticker.h (100%) diff --git a/libraries/Ticker/Ticker.cpp b/libraries/Ticker/src/Ticker.cpp similarity index 100% rename from libraries/Ticker/Ticker.cpp rename to libraries/Ticker/src/Ticker.cpp diff --git a/libraries/Ticker/Ticker.h b/libraries/Ticker/src/Ticker.h similarity index 100% rename from libraries/Ticker/Ticker.h rename to libraries/Ticker/src/Ticker.h From 69d25589f6864b4182d74eae1bbe6084589ce5c0 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Mon, 9 Sep 2019 14:26:59 +0200 Subject: [PATCH 03/10] Cleanup test case. --- libraries/Ticker/examples/TickerParameter/TickerParameter.ino | 2 -- 1 file changed, 2 deletions(-) diff --git a/libraries/Ticker/examples/TickerParameter/TickerParameter.ino b/libraries/Ticker/examples/TickerParameter/TickerParameter.ino index 80734a1a6f..bb6995df2e 100644 --- a/libraries/Ticker/examples/TickerParameter/TickerParameter.ino +++ b/libraries/Ticker/examples/TickerParameter/TickerParameter.ino @@ -14,7 +14,6 @@ #include Ticker tickerSetHigh; -Ticker tickerSetAnalog; Ticker tickerSetLow; void setPin(int state) { @@ -27,7 +26,6 @@ void setPinChar(char state) { void setup() { pinMode(LED_BUILTIN, OUTPUT); - digitalWrite(1, LOW); // every 25 ms, call setPin(0) tickerSetLow.attach_ms(25, setPin, 0); From a833f270af61904700dd0c0492b53b1d53026ef1 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Wed, 11 Sep 2019 11:40:41 +0200 Subject: [PATCH 04/10] C++ style cast required. --- libraries/Ticker/src/Ticker.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Ticker/src/Ticker.h b/libraries/Ticker/src/Ticker.h index c1989337cb..2b9f2a952b 100644 --- a/libraries/Ticker/src/Ticker.h +++ b/libraries/Ticker/src/Ticker.h @@ -54,7 +54,7 @@ class Ticker void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg) { static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - _attach_ms(milliseconds, true, reinterpret_cast(callback), (void*)arg); + _attach_ms(milliseconds, true, reinterpret_cast(callback), reinterpret_cast(arg)); } void once(float seconds, callback_function_t callback); @@ -73,7 +73,7 @@ class Ticker void once_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg) { static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - _attach_ms(milliseconds, false, reinterpret_cast(callback), (void*)arg); + _attach_ms(milliseconds, false, reinterpret_cast(callback), reinterpret_cast(arg)); } void detach(); From 00c5313ea8c9795f5e8cc2cbee48fa1e959c039c Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Mon, 16 Sep 2019 09:52:43 +0200 Subject: [PATCH 05/10] Whitespace --- libraries/Ticker/src/Ticker.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Ticker/src/Ticker.h b/libraries/Ticker/src/Ticker.h index 2b9f2a952b..cbcc84de42 100644 --- a/libraries/Ticker/src/Ticker.h +++ b/libraries/Ticker/src/Ticker.h @@ -92,4 +92,4 @@ class Ticker }; -#endif//TICKER_H +#endif //TICKER_H From cab7657ee61a187c76c22de638c26501d9b55df1 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Fri, 11 Oct 2019 10:51:54 +0200 Subject: [PATCH 06/10] Inlining via header has better baseline ROM footprint. --- libraries/Ticker/src/Ticker.cpp | 49 ------------------------ libraries/Ticker/src/Ticker.h | 66 +++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 68 deletions(-) diff --git a/libraries/Ticker/src/Ticker.cpp b/libraries/Ticker/src/Ticker.cpp index f94d23d488..dca4435dc2 100644 --- a/libraries/Ticker/src/Ticker.cpp +++ b/libraries/Ticker/src/Ticker.cpp @@ -33,55 +33,6 @@ Ticker::~Ticker() detach(); } -void Ticker::attach(float seconds, callback_function_t callback) -{ - _callback_function = std::move(callback); - _attach_ms(1000UL * seconds, true, _static_callback, this); -} - -void Ticker::attach_ms(uint32_t milliseconds, callback_function_t callback) -{ - _callback_function = std::move(callback); - _attach_ms(milliseconds, true, _static_callback, this); -} - -void Ticker::attach_scheduled(float seconds, callback_function_t callback) -{ - attach(seconds, [callback]() { schedule_function(callback); }); -} - -void Ticker::attach_ms_scheduled(uint32_t milliseconds, callback_function_t callback) -{ - attach_ms(milliseconds, [callback]() { schedule_function(callback); }); -} - -void Ticker::once(float seconds, callback_function_t callback) -{ - _callback_function = std::move(callback); - _attach_ms(1000UL * seconds, false, _static_callback, this); -} - -void Ticker::once_ms(uint32_t milliseconds, callback_function_t callback) -{ - _callback_function = std::move(callback); - _attach_ms(milliseconds, false, _static_callback, this); -} - -void Ticker::once_scheduled(float seconds, callback_function_t callback) -{ - once(seconds, [callback]() { schedule_function(callback); }); -} - -void Ticker::once_ms_scheduled(uint32_t milliseconds, callback_function_t callback) -{ - once_ms(milliseconds, [callback]() { schedule_function(callback); }); -} - -void Ticker::_attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg) -{ - _attach_ms(1000UL * seconds, repeat, callback, arg); -} - void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg) { if (_timer) diff --git a/libraries/Ticker/src/Ticker.h b/libraries/Ticker/src/Ticker.h index cbcc84de42..4c55617edd 100644 --- a/libraries/Ticker/src/Ticker.h +++ b/libraries/Ticker/src/Ticker.h @@ -35,19 +35,25 @@ class Ticker typedef void (*callback_with_arg_t)(void*); typedef std::function callback_function_t; - void attach(float seconds, callback_function_t callback); - void attach_ms(uint32_t milliseconds, callback_function_t callback); - void attach_scheduled(float seconds, callback_function_t callback); - void attach_ms_scheduled(uint32_t milliseconds, callback_function_t callback); + void attach_ms(uint32_t milliseconds, callback_function_t callback) + { + _callback_function = std::move(callback); + _attach_ms(milliseconds, true, _static_callback, this); + } - template - void attach(float seconds, void (*callback)(TArg), TArg arg) + void attach(float seconds, callback_function_t callback) { - static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - // C-cast serves two purposes: - // static_cast for smaller integer types, - // reinterpret_cast + const_cast for pointer types - _attach_s(seconds, true, reinterpret_cast(callback), reinterpret_cast(arg)); + attach_ms(1000UL * seconds, callback); + } + + void attach_scheduled(float seconds, callback_function_t callback) + { + attach(seconds, [callback]() { schedule_function(callback); }); + } + + void attach_ms_scheduled(uint32_t milliseconds, callback_function_t callback) + { + attach_ms(milliseconds, [callback]() { schedule_function(callback); }); } template @@ -57,16 +63,32 @@ class Ticker _attach_ms(milliseconds, true, reinterpret_cast(callback), reinterpret_cast(arg)); } - void once(float seconds, callback_function_t callback); - void once_ms(uint32_t milliseconds, callback_function_t callback); - void once_scheduled(float seconds, callback_function_t callback); - void once_ms_scheduled(uint32_t milliseconds, callback_function_t callback); - template - void once(float seconds, void (*callback)(TArg), TArg arg) + void attach(float seconds, void (*callback)(TArg), TArg arg) { static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - _attach_s(seconds, false, reinterpret_cast(callback), reinterpret_cast(arg)); + attach_ms(1000UL * seconds, callback, arg); + } + + void once_ms(uint32_t milliseconds, callback_function_t callback) + { + _callback_function = std::move(callback); + _attach_ms(milliseconds, false, _static_callback, this); + } + + void once(float seconds, callback_function_t callback) + { + once_ms(1000UL * seconds, callback); + } + + void once_scheduled(float seconds, callback_function_t callback) + { + once(seconds, [callback]() { schedule_function(callback); }); + } + + void once_ms_scheduled(uint32_t milliseconds, callback_function_t callback) + { + once_ms(milliseconds, [callback]() { schedule_function(callback); }); } template @@ -76,12 +98,18 @@ class Ticker _attach_ms(milliseconds, false, reinterpret_cast(callback), reinterpret_cast(arg)); } + template + void once(float seconds, void (*callback)(TArg), TArg arg) + { + static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); + once_ms(1000UL * seconds, callback, arg); + } + void detach(); bool active() const; protected: static void _static_callback(void* arg); - void _attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg); void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg); ETSTimer* _timer; From 5548ba6a50421f024c06ab925414e3f298f21545 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Fri, 11 Oct 2019 11:21:49 +0200 Subject: [PATCH 07/10] Reordered functions for better code-compare to master --- libraries/Ticker/src/Ticker.h | 44 +++++++++++++++++------------------ 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/libraries/Ticker/src/Ticker.h b/libraries/Ticker/src/Ticker.h index 4c55617edd..8f164fceba 100644 --- a/libraries/Ticker/src/Ticker.h +++ b/libraries/Ticker/src/Ticker.h @@ -35,10 +35,9 @@ class Ticker typedef void (*callback_with_arg_t)(void*); typedef std::function callback_function_t; - void attach_ms(uint32_t milliseconds, callback_function_t callback) + void attach_scheduled(float seconds, callback_function_t callback) { - _callback_function = std::move(callback); - _attach_ms(milliseconds, true, _static_callback, this); + attach(seconds, [callback]() { schedule_function(callback); }); } void attach(float seconds, callback_function_t callback) @@ -46,34 +45,34 @@ class Ticker attach_ms(1000UL * seconds, callback); } - void attach_scheduled(float seconds, callback_function_t callback) + void attach_ms_scheduled(uint32_t milliseconds, callback_function_t callback) { - attach(seconds, [callback]() { schedule_function(callback); }); + attach_ms(milliseconds, [callback]() { schedule_function(callback); }); } - void attach_ms_scheduled(uint32_t milliseconds, callback_function_t callback) + void attach_ms(uint32_t milliseconds, callback_function_t callback) { - attach_ms(milliseconds, [callback]() { schedule_function(callback); }); + _callback_function = std::move(callback); + _attach_ms(milliseconds, true, _static_callback, this); } template - void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg) + void attach(float seconds, void (*callback)(TArg), TArg arg) { static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - _attach_ms(milliseconds, true, reinterpret_cast(callback), reinterpret_cast(arg)); + attach_ms(1000UL * seconds, callback, arg); } template - void attach(float seconds, void (*callback)(TArg), TArg arg) + void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg) { static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - attach_ms(1000UL * seconds, callback, arg); + _attach_ms(milliseconds, true, reinterpret_cast(callback), reinterpret_cast(arg)); } - void once_ms(uint32_t milliseconds, callback_function_t callback) + void once_scheduled(float seconds, callback_function_t callback) { - _callback_function = std::move(callback); - _attach_ms(milliseconds, false, _static_callback, this); + once(seconds, [callback]() { schedule_function(callback); }); } void once(float seconds, callback_function_t callback) @@ -81,28 +80,29 @@ class Ticker once_ms(1000UL * seconds, callback); } - void once_scheduled(float seconds, callback_function_t callback) + void once_ms_scheduled(uint32_t milliseconds, callback_function_t callback) { - once(seconds, [callback]() { schedule_function(callback); }); + once_ms(milliseconds, [callback]() { schedule_function(callback); }); } - void once_ms_scheduled(uint32_t milliseconds, callback_function_t callback) + void once_ms(uint32_t milliseconds, callback_function_t callback) { - once_ms(milliseconds, [callback]() { schedule_function(callback); }); + _callback_function = std::move(callback); + _attach_ms(milliseconds, false, _static_callback, this); } template - void once_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg) + void once(float seconds, void (*callback)(TArg), TArg arg) { static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - _attach_ms(milliseconds, false, reinterpret_cast(callback), reinterpret_cast(arg)); + once_ms(1000UL * seconds, callback, arg); } template - void once(float seconds, void (*callback)(TArg), TArg arg) + void once_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg) { static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - once_ms(1000UL * seconds, callback, arg); + _attach_ms(milliseconds, false, reinterpret_cast(callback), reinterpret_cast(arg)); } void detach(); From f78c774ac8df8069cb5351ff200a2bf3067b4df9 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Fri, 11 Oct 2019 12:15:19 +0200 Subject: [PATCH 08/10] Reduces ROM footprint some more. --- libraries/Ticker/src/Ticker.h | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/libraries/Ticker/src/Ticker.h b/libraries/Ticker/src/Ticker.h index 8f164fceba..1079c05cb3 100644 --- a/libraries/Ticker/src/Ticker.h +++ b/libraries/Ticker/src/Ticker.h @@ -37,17 +37,20 @@ class Ticker void attach_scheduled(float seconds, callback_function_t callback) { - attach(seconds, [callback]() { schedule_function(callback); }); + _callback_function = [callback]() { schedule_function(callback); }; + _attach_ms(1000UL * seconds, true, _static_callback, this); } void attach(float seconds, callback_function_t callback) { - attach_ms(1000UL * seconds, callback); + _callback_function = std::move(callback); + _attach_ms(1000UL * seconds, true, _static_callback, this); } void attach_ms_scheduled(uint32_t milliseconds, callback_function_t callback) { - attach_ms(milliseconds, [callback]() { schedule_function(callback); }); + _callback_function = [callback]() { schedule_function(callback); }; + _attach_ms(milliseconds, true, _static_callback, this); } void attach_ms(uint32_t milliseconds, callback_function_t callback) @@ -60,7 +63,7 @@ class Ticker void attach(float seconds, void (*callback)(TArg), TArg arg) { static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - attach_ms(1000UL * seconds, callback, arg); + _attach_ms(1000UL * seconds, true, reinterpret_cast(callback), reinterpret_cast(arg)); } template @@ -72,17 +75,20 @@ class Ticker void once_scheduled(float seconds, callback_function_t callback) { - once(seconds, [callback]() { schedule_function(callback); }); + _callback_function = [callback]() { schedule_function(callback); }; + _attach_ms(1000UL * seconds, false, _static_callback, this); } void once(float seconds, callback_function_t callback) { - once_ms(1000UL * seconds, callback); + _callback_function = std::move(callback); + _attach_ms(1000UL * seconds, false, _static_callback, this); } void once_ms_scheduled(uint32_t milliseconds, callback_function_t callback) { - once_ms(milliseconds, [callback]() { schedule_function(callback); }); + _callback_function = [callback]() { schedule_function(callback); }; + _attach_ms(milliseconds, false, _static_callback, this); } void once_ms(uint32_t milliseconds, callback_function_t callback) @@ -95,7 +101,7 @@ class Ticker void once(float seconds, void (*callback)(TArg), TArg arg) { static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - once_ms(1000UL * seconds, callback, arg); + _attach_ms(1000UL * seconds, false, reinterpret_cast(callback), reinterpret_cast(arg)); } template From 3417f08a4f9ec2061d254d1e492a94c763f0cb94 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Sat, 9 Nov 2019 10:47:26 +0100 Subject: [PATCH 09/10] Avoid unnecessary parameter passing - refactoring, same generated footprint. --- .../TickerParameter/TickerParameter.ino | 21 ++++++++++++++----- libraries/Ticker/src/Ticker.h | 20 +++++++++++------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/libraries/Ticker/examples/TickerParameter/TickerParameter.ino b/libraries/Ticker/examples/TickerParameter/TickerParameter.ino index bb6995df2e..99195458e5 100644 --- a/libraries/Ticker/examples/TickerParameter/TickerParameter.ino +++ b/libraries/Ticker/examples/TickerParameter/TickerParameter.ino @@ -13,8 +13,17 @@ #include -Ticker tickerSetHigh; Ticker tickerSetLow; +Ticker tickerSetHigh; +Ticker tickerSetChar; + +void setPinLow() { + digitalWrite(LED_BUILTIN, 0); +} + +void setPinHigh() { + digitalWrite(LED_BUILTIN, 1); +} void setPin(int state) { digitalWrite(LED_BUILTIN, state); @@ -27,12 +36,14 @@ void setPinChar(char state) { void setup() { pinMode(LED_BUILTIN, OUTPUT); - // every 25 ms, call setPin(0) - tickerSetLow.attach_ms(25, setPin, 0); + // every 25 ms, call setPinLow() + tickerSetLow.attach_ms(25, setPinLow); - // every 26 ms, call setPinChar(1) - tickerSetHigh.attach_ms(26, setPinChar, (char)1); + // every 26 ms, call setPinHigh() + tickerSetHigh.attach_ms(26, setPinHigh); + // every 54 ms, call setPinChar(1) + tickerSetChar.attach_ms(26, setPinChar, (char)1); } void loop() { diff --git a/libraries/Ticker/src/Ticker.h b/libraries/Ticker/src/Ticker.h index 1079c05cb3..436a68ce8e 100644 --- a/libraries/Ticker/src/Ticker.h +++ b/libraries/Ticker/src/Ticker.h @@ -38,25 +38,25 @@ class Ticker void attach_scheduled(float seconds, callback_function_t callback) { _callback_function = [callback]() { schedule_function(callback); }; - _attach_ms(1000UL * seconds, true, _static_callback, this); + _attach_ms(1000UL * seconds, true); } void attach(float seconds, callback_function_t callback) { _callback_function = std::move(callback); - _attach_ms(1000UL * seconds, true, _static_callback, this); + _attach_ms(1000UL * seconds, true); } void attach_ms_scheduled(uint32_t milliseconds, callback_function_t callback) { _callback_function = [callback]() { schedule_function(callback); }; - _attach_ms(milliseconds, true, _static_callback, this); + _attach_ms(milliseconds, true); } void attach_ms(uint32_t milliseconds, callback_function_t callback) { _callback_function = std::move(callback); - _attach_ms(milliseconds, true, _static_callback, this); + _attach_ms(milliseconds, true); } template @@ -76,25 +76,25 @@ class Ticker void once_scheduled(float seconds, callback_function_t callback) { _callback_function = [callback]() { schedule_function(callback); }; - _attach_ms(1000UL * seconds, false, _static_callback, this); + _attach_ms(1000UL * seconds, false); } void once(float seconds, callback_function_t callback) { _callback_function = std::move(callback); - _attach_ms(1000UL * seconds, false, _static_callback, this); + _attach_ms(1000UL * seconds, false); } void once_ms_scheduled(uint32_t milliseconds, callback_function_t callback) { _callback_function = [callback]() { schedule_function(callback); }; - _attach_ms(milliseconds, false, _static_callback, this); + _attach_ms(milliseconds, false); } void once_ms(uint32_t milliseconds, callback_function_t callback) { _callback_function = std::move(callback); - _attach_ms(milliseconds, false, _static_callback, this); + _attach_ms(milliseconds, false); } template @@ -117,6 +117,10 @@ class Ticker protected: static void _static_callback(void* arg); void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg); + void _attach_ms(uint32_t milliseconds, bool repeat) + { + _attach_ms(milliseconds, repeat, _static_callback, this); + } ETSTimer* _timer; callback_function_t _callback_function = nullptr; From 9382d77bda3d7d155cd1441b1a24a92b27468a34 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Tue, 12 Nov 2019 21:33:50 +0100 Subject: [PATCH 10/10] Reformat example sources --- libraries/Ticker/examples/TickerParameter/TickerParameter.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Ticker/examples/TickerParameter/TickerParameter.ino b/libraries/Ticker/examples/TickerParameter/TickerParameter.ino index 99195458e5..066905d098 100644 --- a/libraries/Ticker/examples/TickerParameter/TickerParameter.ino +++ b/libraries/Ticker/examples/TickerParameter/TickerParameter.ino @@ -18,11 +18,11 @@ Ticker tickerSetHigh; Ticker tickerSetChar; void setPinLow() { - digitalWrite(LED_BUILTIN, 0); + digitalWrite(LED_BUILTIN, 0); } void setPinHigh() { - digitalWrite(LED_BUILTIN, 1); + digitalWrite(LED_BUILTIN, 1); } void setPin(int state) {