From 0e807c1c34d4d395ed643f673f2cdd557280654f Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Sun, 2 Jun 2019 11:51:59 +0200 Subject: [PATCH 01/22] Update Ticker API to compatibility with ESP8266, prepares for co-op loop Scheduler --- CMakeLists.txt | 3 +- cores/esp32/Ticker.cpp | 75 +++++++++++++++++ cores/esp32/Ticker.h | 111 ++++++++++++++++++++++++ libraries/Ticker/src/Ticker.cpp | 73 +++++++++------- libraries/Ticker/src/Ticker.h | 144 ++++++++++++++++---------------- 5 files changed, 304 insertions(+), 102 deletions(-) create mode 100644 cores/esp32/Ticker.cpp create mode 100644 cores/esp32/Ticker.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 851d1488eca..d42aab40f70 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,6 +60,7 @@ set(CORE_SRCS cores/esp32/StreamString.cpp cores/esp32/Tone.cpp cores/esp32/HWCDC.cpp + cores/esp32/Ticker.cpp cores/esp32/USB.cpp cores/esp32/USBCDC.cpp cores/esp32/USBMSC.cpp @@ -106,7 +107,6 @@ set(LIBRARY_SRCS libraries/SimpleBLE/src/SimpleBLE.cpp libraries/SPIFFS/src/SPIFFS.cpp libraries/SPI/src/SPI.cpp - libraries/Ticker/src/Ticker.cpp libraries/Update/src/Updater.cpp libraries/Update/src/HttpsOTAUpdate.cpp libraries/USB/src/USBHID.cpp @@ -195,7 +195,6 @@ set(includedirs libraries/SimpleBLE/src libraries/SPIFFS/src libraries/SPI/src - libraries/Ticker/src libraries/Update/src libraries/USB/src libraries/WebServer/src diff --git a/cores/esp32/Ticker.cpp b/cores/esp32/Ticker.cpp new file mode 100644 index 00000000000..a92c474ec42 --- /dev/null +++ b/cores/esp32/Ticker.cpp @@ -0,0 +1,75 @@ +/* + Ticker.cpp - esp32 library that calls functions periodically + + Copyright (c) 2017 Bert Melis. All rights reserved. + + Based on the original work of: + Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. + The original version is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "Ticker.h" + +Ticker::Ticker() + : _timer(nullptr) +{ +} + +Ticker::~Ticker() +{ + detach(); +} + +void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg) +{ + esp_timer_create_args_t _timerConfig; + _timerConfig.arg = reinterpret_cast(arg); + _timerConfig.callback = callback; + _timerConfig.dispatch_method = ESP_TIMER_TASK; + _timerConfig.name = "Ticker"; + if (_timer) { + esp_timer_stop(_timer); + esp_timer_delete(_timer); + } + esp_timer_create(&_timerConfig, &_timer); + if (repeat) { + esp_timer_start_periodic(_timer, milliseconds * 1000); + } + else { + esp_timer_start_once(_timer, milliseconds * 1000); + } +} + +void Ticker::detach() { + if (_timer) { + esp_timer_stop(_timer); + esp_timer_delete(_timer); + _timer = nullptr; + } +} + +bool Ticker::active() const +{ + return _timer; +} + +void Ticker::_static_callback(void* arg) +{ + Ticker* _this = reinterpret_cast(arg); + if (!_this) return; + if (_this->_callback_function) _this->_callback_function(); +} diff --git a/cores/esp32/Ticker.h b/cores/esp32/Ticker.h new file mode 100644 index 00000000000..dfb5159ef61 --- /dev/null +++ b/cores/esp32/Ticker.h @@ -0,0 +1,111 @@ +/* + Ticker.h - esp32 library that calls functions periodically + + Copyright (c) 2017 Bert Melis. All rights reserved. + + Based on the original work of: + Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. + The original version is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef TICKER_H +#define TICKER_H + +extern "C" { +#include "esp_timer.h" +} +#include + +class Ticker +{ +public: + Ticker(); + ~Ticker(); + + typedef void (*callback_with_arg_t)(void*); + typedef std::function callback_function_t; + + void attach(float seconds, callback_function_t callback) + { + _callback_function = std::move(callback); + _attach_ms(seconds * 1000, true, _static_callback, this); + } + + 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_ms(seconds * 1000, true, callback, 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, callback, arg); + } + + void once(float seconds, callback_function_t callback) + { + _callback_function = std::move(callback); + _attach_ms(seconds * 1000, false, _static_callback, this); + } + + 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_ms(seconds * 1000, false, callback, 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, callback, 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); + + callback_function_t _callback_function = nullptr; + +protected: + esp_timer_handle_t _timer; +}; + + +#endif//TICKER_H diff --git a/libraries/Ticker/src/Ticker.cpp b/libraries/Ticker/src/Ticker.cpp index 629361b2dd0..c5f084eb851 100644 --- a/libraries/Ticker/src/Ticker.cpp +++ b/libraries/Ticker/src/Ticker.cpp @@ -1,8 +1,8 @@ -/* +/* Ticker.cpp - esp32 library that calls functions periodically Copyright (c) 2017 Bert Melis. All rights reserved. - + Based on the original work of: Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. The original version is part of the esp8266 core for Arduino environment. @@ -24,40 +24,53 @@ #include "Ticker.h" -Ticker::Ticker() : - _timer(nullptr) {} +Ticker::Ticker() + : _timer(nullptr) +{ +} -Ticker::~Ticker() { - detach(); +Ticker::~Ticker() +{ + detach(); } -void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg) { - esp_timer_create_args_t _timerConfig; - _timerConfig.arg = reinterpret_cast(arg); - _timerConfig.callback = callback; - _timerConfig.dispatch_method = ESP_TIMER_TASK; - _timerConfig.name = "Ticker"; - if (_timer) { - esp_timer_stop(_timer); - esp_timer_delete(_timer); - } - esp_timer_create(&_timerConfig, &_timer); - if (repeat) { - esp_timer_start_periodic(_timer, milliseconds * 1000ULL); - } else { - esp_timer_start_once(_timer, milliseconds * 1000ULL); - } +void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg) +{ + esp_timer_create_args_t _timerConfig; + _timerConfig.arg = reinterpret_cast(arg); + _timerConfig.callback = callback; + _timerConfig.dispatch_method = ESP_TIMER_TASK; + _timerConfig.name = "Ticker"; + if (_timer) { + esp_timer_stop(_timer); + esp_timer_delete(_timer); + } + esp_timer_create(&_timerConfig, &_timer); + if (repeat) { + esp_timer_start_periodic(_timer, milliseconds * 1000ULL); + } + else { + esp_timer_start_once(_timer, milliseconds * 1000ULL); + } } void Ticker::detach() { - if (_timer) { - esp_timer_stop(_timer); - esp_timer_delete(_timer); - _timer = nullptr; - } + if (_timer) { + esp_timer_stop(_timer); + esp_timer_delete(_timer); + _timer = nullptr; + } } -bool Ticker::active() { - if (!_timer) return false; - return esp_timer_is_active(_timer); +bool Ticker::active() const { + if (!_timer) return false; + return esp_timer_is_active(_timer); } + +void Ticker::_static_callback(void* arg) +{ + Ticker* _this = reinterpret_cast(arg); + if (!_this) return; + if (_this->_callback_function) _this->_callback_function(); +} + diff --git a/libraries/Ticker/src/Ticker.h b/libraries/Ticker/src/Ticker.h index 82804e0f37d..dfb5159ef61 100644 --- a/libraries/Ticker/src/Ticker.h +++ b/libraries/Ticker/src/Ticker.h @@ -1,8 +1,8 @@ -/* +/* Ticker.h - esp32 library that calls functions periodically Copyright (c) 2017 Bert Melis. All rights reserved. - + Based on the original work of: Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. The original version is part of the esp8266 core for Arduino environment. @@ -26,82 +26,86 @@ #define TICKER_H extern "C" { - #include "esp_timer.h" +#include "esp_timer.h" } +#include class Ticker { public: - Ticker(); - ~Ticker(); - typedef void (*callback_t)(void); - typedef void (*callback_with_arg_t)(void*); - - void attach(float seconds, callback_t callback) - { - _attach_ms(seconds * 1000, true, reinterpret_cast(callback), 0); - } - - void attach_ms(uint32_t milliseconds, callback_t callback) - { - _attach_ms(milliseconds, true, reinterpret_cast(callback), 0); - } - - template - void attach(float seconds, void (*callback)(TArg), TArg arg) - { - static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach() callback argument size must be <= 4 bytes"); - // C-cast serves two purposes: - // static_cast for smaller integer types, - // reinterpret_cast + const_cast for pointer types - uint32_t arg32 = (uint32_t)arg; - _attach_ms(seconds * 1000, true, reinterpret_cast(callback), arg32); - } - - template - void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg) - { - static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach_ms() callback argument size must be <= 4 bytes"); - uint32_t arg32 = (uint32_t)arg; - _attach_ms(milliseconds, true, reinterpret_cast(callback), arg32); - } - - void once(float seconds, callback_t callback) - { - _attach_ms(seconds * 1000, false, reinterpret_cast(callback), 0); - } - - void once_ms(uint32_t milliseconds, callback_t callback) - { - _attach_ms(milliseconds, false, reinterpret_cast(callback), 0); - } - - template - void once(float seconds, void (*callback)(TArg), TArg arg) - { - static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach() callback argument size must be <= 4 bytes"); - uint32_t arg32 = (uint32_t)(arg); - _attach_ms(seconds * 1000, false, reinterpret_cast(callback), arg32); - } - - template - void once_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg) - { - static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach_ms() callback argument size must be <= 4 bytes"); - uint32_t arg32 = (uint32_t)(arg); - _attach_ms(milliseconds, false, reinterpret_cast(callback), arg32); - } - - void detach(); - bool active(); - -protected: - void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg); + Ticker(); + ~Ticker(); + + typedef void (*callback_with_arg_t)(void*); + typedef std::function callback_function_t; + + void attach(float seconds, callback_function_t callback) + { + _callback_function = std::move(callback); + _attach_ms(seconds * 1000, true, _static_callback, this); + } + + 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_ms(seconds * 1000, true, callback, 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, callback, arg); + } + + void once(float seconds, callback_function_t callback) + { + _callback_function = std::move(callback); + _attach_ms(seconds * 1000, false, _static_callback, this); + } + + 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_ms(seconds * 1000, false, callback, 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, callback, 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); + callback_function_t _callback_function = nullptr; protected: - esp_timer_handle_t _timer; + esp_timer_handle_t _timer; }; -#endif // TICKER_H +#endif//TICKER_H From 5e1c3cd83d672d6dd77f4a85b8dd14be2e1107c3 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Sun, 2 Jun 2019 12:17:26 +0200 Subject: [PATCH 02/22] Fixing Build server complaints --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index d42aab40f70..36786da9751 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -107,6 +107,7 @@ set(LIBRARY_SRCS libraries/SimpleBLE/src/SimpleBLE.cpp libraries/SPIFFS/src/SPIFFS.cpp libraries/SPI/src/SPI.cpp + libraries/Ticker/src/Ticker.cpp libraries/Update/src/Updater.cpp libraries/Update/src/HttpsOTAUpdate.cpp libraries/USB/src/USBHID.cpp @@ -195,6 +196,7 @@ set(includedirs libraries/SimpleBLE/src libraries/SPIFFS/src libraries/SPI/src + libraries/Ticker/src libraries/Update/src libraries/USB/src libraries/WebServer/src From 6dda01003f4bce70029b5067151e3303e2ece330 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Sun, 2 Jun 2019 12:37:04 +0200 Subject: [PATCH 03/22] Fix omitted casts in template member function --- cores/esp32/Ticker.h | 8 ++++---- libraries/Ticker/src/Ticker.h | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cores/esp32/Ticker.h b/cores/esp32/Ticker.h index dfb5159ef61..cc816f96815 100644 --- a/cores/esp32/Ticker.h +++ b/cores/esp32/Ticker.h @@ -58,14 +58,14 @@ class Ticker // C-cast serves two purposes: // static_cast for smaller integer types, // reinterpret_cast + const_cast for pointer types - _attach_ms(seconds * 1000, true, callback, arg); + _attach_ms(seconds * 1000, true, reinterpret_cast(callback), (void*)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, callback, arg); + _attach_ms(milliseconds, true, reinterpret_cast(callback), (void*)arg); } void once(float seconds, callback_function_t callback) @@ -84,14 +84,14 @@ 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*)"); - _attach_ms(seconds * 1000, false, callback, arg); + _attach_ms(seconds * 1000, false, reinterpret_cast(callback), (void*)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, callback, arg); + _attach_ms(milliseconds, false, reinterpret_cast(callback), (void*)arg); } void detach(); diff --git a/libraries/Ticker/src/Ticker.h b/libraries/Ticker/src/Ticker.h index dfb5159ef61..cc816f96815 100644 --- a/libraries/Ticker/src/Ticker.h +++ b/libraries/Ticker/src/Ticker.h @@ -58,14 +58,14 @@ class Ticker // C-cast serves two purposes: // static_cast for smaller integer types, // reinterpret_cast + const_cast for pointer types - _attach_ms(seconds * 1000, true, callback, arg); + _attach_ms(seconds * 1000, true, reinterpret_cast(callback), (void*)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, callback, arg); + _attach_ms(milliseconds, true, reinterpret_cast(callback), (void*)arg); } void once(float seconds, callback_function_t callback) @@ -84,14 +84,14 @@ 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*)"); - _attach_ms(seconds * 1000, false, callback, arg); + _attach_ms(seconds * 1000, false, reinterpret_cast(callback), (void*)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, callback, arg); + _attach_ms(milliseconds, false, reinterpret_cast(callback), (void*)arg); } void detach(); From afde0a357c242a5a668b899f7058f28da29bda37 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Tue, 4 Jun 2019 23:37:09 +0200 Subject: [PATCH 04/22] Changes after review --- cores/esp32/Ticker.cpp | 19 +++++++++++++++---- cores/esp32/Ticker.h | 13 ++++++++----- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/cores/esp32/Ticker.cpp b/cores/esp32/Ticker.cpp index a92c474ec42..6a9193e6ee8 100644 --- a/cores/esp32/Ticker.cpp +++ b/cores/esp32/Ticker.cpp @@ -34,7 +34,17 @@ Ticker::~Ticker() detach(); } +void Ticker::_attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg) +{ + _attach_us(1000000 * seconds, repeat, callback, arg); +} + void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg) +{ + _attach_us(1000 * milliseconds, repeat, callback, arg); +} + +void Ticker::_attach_us(uint32_t micros, bool repeat, callback_with_arg_t callback, void* arg) { esp_timer_create_args_t _timerConfig; _timerConfig.arg = reinterpret_cast(arg); @@ -47,10 +57,10 @@ void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t } esp_timer_create(&_timerConfig, &_timer); if (repeat) { - esp_timer_start_periodic(_timer, milliseconds * 1000); + esp_timer_start_periodic(_timer, micros); } else { - esp_timer_start_once(_timer, milliseconds * 1000); + esp_timer_start_once(_timer, micros); } } @@ -59,6 +69,7 @@ void Ticker::detach() { esp_timer_stop(_timer); esp_timer_delete(_timer); _timer = nullptr; + _callback_function = nullptr; } } @@ -70,6 +81,6 @@ bool Ticker::active() const void Ticker::_static_callback(void* arg) { Ticker* _this = reinterpret_cast(arg); - if (!_this) return; - if (_this->_callback_function) _this->_callback_function(); + if (_this && _this->_callback_function) + _this->_callback_function(); } diff --git a/cores/esp32/Ticker.h b/cores/esp32/Ticker.h index cc816f96815..44379841ee9 100644 --- a/cores/esp32/Ticker.h +++ b/cores/esp32/Ticker.h @@ -42,7 +42,7 @@ class Ticker void attach(float seconds, callback_function_t callback) { _callback_function = std::move(callback); - _attach_ms(seconds * 1000, true, _static_callback, this); + _attach_s(seconds, true, _static_callback, this); } void attach_ms(uint32_t milliseconds, callback_function_t callback) @@ -58,7 +58,7 @@ class Ticker // C-cast serves two purposes: // static_cast for smaller integer types, // reinterpret_cast + const_cast for pointer types - _attach_ms(seconds * 1000, true, reinterpret_cast(callback), (void*)arg); + _attach_s(seconds, true, reinterpret_cast(callback), (void*)arg); } template @@ -71,7 +71,7 @@ class Ticker void once(float seconds, callback_function_t callback) { _callback_function = std::move(callback); - _attach_ms(seconds * 1000, false, _static_callback, this); + _attach_s(seconds, false, _static_callback, this); } void once_ms(uint32_t milliseconds, callback_function_t callback) @@ -84,7 +84,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*)"); - _attach_ms(seconds * 1000, false, reinterpret_cast(callback), (void*)arg); + _attach_s(seconds, false, reinterpret_cast(callback), (void*)arg); } template @@ -103,8 +103,11 @@ class Ticker callback_function_t _callback_function = nullptr; -protected: esp_timer_handle_t _timer; + +private: + void _attach_us(uint32_t micros, bool repeat, callback_with_arg_t callback, void* arg); + void _attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg); }; From ec95f590a89fb36609d4c169ca0b6a8634cdc519 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Tue, 4 Jun 2019 23:44:41 +0200 Subject: [PATCH 05/22] =?UTF-8?q?Expose=20=C2=B5s=20resolution=20of=20OS?= =?UTF-8?q?=20API=20in=20Ticker=20class?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cores/esp32/Ticker.h | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/cores/esp32/Ticker.h b/cores/esp32/Ticker.h index 44379841ee9..f7db7b75e3e 100644 --- a/cores/esp32/Ticker.h +++ b/cores/esp32/Ticker.h @@ -29,6 +29,7 @@ extern "C" { #include "esp_timer.h" } #include +#include class Ticker { @@ -51,6 +52,17 @@ class Ticker _attach_ms(milliseconds, true, _static_callback, this); } + void attach_us_scheduled(uint32_t micros, callback_function_t callback) + { + attach_us(micros, [callback]() { schedule_function(callback); }); + } + + void attach_us(uint32_t micros, callback_function_t callback) + { + _callback_function = std::move(callback); + _attach_us(micros, true, _static_callback, this); + } + template void attach(float seconds, void (*callback)(TArg), TArg arg) { @@ -68,6 +80,13 @@ class Ticker _attach_ms(milliseconds, true, reinterpret_cast(callback), (void*)arg); } + template + void attach_us(uint32_t micros, void (*callback)(TArg), TArg arg) + { + static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); + _attach_us(micros, true, reinterpret_cast(callback), (void*)arg); + } + void once(float seconds, callback_function_t callback) { _callback_function = std::move(callback); @@ -80,6 +99,17 @@ class Ticker _attach_ms(milliseconds, false, _static_callback, this); } + void once_us_scheduled(uint32_t micros, callback_function_t callback) + { + once_us(micros, [callback]() { schedule_function(callback); }); + } + + void once_us(uint32_t micros, callback_function_t callback) + { + _callback_function = std::move(callback); + _attach_us(micros, false, _static_callback, this); + } + template void once(float seconds, void (*callback)(TArg), TArg arg) { @@ -94,6 +124,13 @@ class Ticker _attach_ms(milliseconds, false, reinterpret_cast(callback), (void*)arg); } + template + void once_us(uint32_t micros, void (*callback)(TArg), TArg arg) + { + static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); + _attach_us(micros, false, reinterpret_cast(callback), (void*)arg); + } + void detach(); bool active() const; From 861ecd2b277dc858db1739e104e8a65bdaa443e9 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Wed, 5 Jun 2019 09:50:38 +0200 Subject: [PATCH 06/22] Return Ticker to libraries only for modularity. --- CMakeLists.txt | 1 - cores/esp32/Ticker.cpp | 86 ------------------ cores/esp32/Ticker.h | 151 -------------------------------- libraries/Ticker/keywords.txt | 7 ++ libraries/Ticker/src/Ticker.cpp | 19 +++- libraries/Ticker/src/Ticker.h | 39 +++++++-- 6 files changed, 56 insertions(+), 247 deletions(-) delete mode 100644 cores/esp32/Ticker.cpp delete mode 100644 cores/esp32/Ticker.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 36786da9751..851d1488eca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,7 +60,6 @@ set(CORE_SRCS cores/esp32/StreamString.cpp cores/esp32/Tone.cpp cores/esp32/HWCDC.cpp - cores/esp32/Ticker.cpp cores/esp32/USB.cpp cores/esp32/USBCDC.cpp cores/esp32/USBMSC.cpp diff --git a/cores/esp32/Ticker.cpp b/cores/esp32/Ticker.cpp deleted file mode 100644 index 6a9193e6ee8..00000000000 --- a/cores/esp32/Ticker.cpp +++ /dev/null @@ -1,86 +0,0 @@ -/* - Ticker.cpp - esp32 library that calls functions periodically - - Copyright (c) 2017 Bert Melis. All rights reserved. - - Based on the original work of: - Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. - The original version is part of the esp8266 core for Arduino environment. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#include "Ticker.h" - -Ticker::Ticker() - : _timer(nullptr) -{ -} - -Ticker::~Ticker() -{ - detach(); -} - -void Ticker::_attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg) -{ - _attach_us(1000000 * seconds, repeat, callback, arg); -} - -void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg) -{ - _attach_us(1000 * milliseconds, repeat, callback, arg); -} - -void Ticker::_attach_us(uint32_t micros, bool repeat, callback_with_arg_t callback, void* arg) -{ - esp_timer_create_args_t _timerConfig; - _timerConfig.arg = reinterpret_cast(arg); - _timerConfig.callback = callback; - _timerConfig.dispatch_method = ESP_TIMER_TASK; - _timerConfig.name = "Ticker"; - if (_timer) { - esp_timer_stop(_timer); - esp_timer_delete(_timer); - } - esp_timer_create(&_timerConfig, &_timer); - if (repeat) { - esp_timer_start_periodic(_timer, micros); - } - else { - esp_timer_start_once(_timer, micros); - } -} - -void Ticker::detach() { - if (_timer) { - esp_timer_stop(_timer); - esp_timer_delete(_timer); - _timer = nullptr; - _callback_function = nullptr; - } -} - -bool Ticker::active() const -{ - return _timer; -} - -void Ticker::_static_callback(void* arg) -{ - Ticker* _this = reinterpret_cast(arg); - if (_this && _this->_callback_function) - _this->_callback_function(); -} diff --git a/cores/esp32/Ticker.h b/cores/esp32/Ticker.h deleted file mode 100644 index f7db7b75e3e..00000000000 --- a/cores/esp32/Ticker.h +++ /dev/null @@ -1,151 +0,0 @@ -/* - Ticker.h - esp32 library that calls functions periodically - - Copyright (c) 2017 Bert Melis. All rights reserved. - - Based on the original work of: - Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. - The original version is part of the esp8266 core for Arduino environment. - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#ifndef TICKER_H -#define TICKER_H - -extern "C" { -#include "esp_timer.h" -} -#include -#include - -class Ticker -{ -public: - Ticker(); - ~Ticker(); - - typedef void (*callback_with_arg_t)(void*); - typedef std::function callback_function_t; - - void attach(float seconds, callback_function_t callback) - { - _callback_function = std::move(callback); - _attach_s(seconds, true, _static_callback, this); - } - - void attach_ms(uint32_t milliseconds, callback_function_t callback) - { - _callback_function = std::move(callback); - _attach_ms(milliseconds, true, _static_callback, this); - } - - void attach_us_scheduled(uint32_t micros, callback_function_t callback) - { - attach_us(micros, [callback]() { schedule_function(callback); }); - } - - void attach_us(uint32_t micros, callback_function_t callback) - { - _callback_function = std::move(callback); - _attach_us(micros, 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), (void*)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); - } - - template - void attach_us(uint32_t micros, void (*callback)(TArg), TArg arg) - { - static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - _attach_us(micros, true, reinterpret_cast(callback), (void*)arg); - } - - void once(float seconds, callback_function_t callback) - { - _callback_function = std::move(callback); - _attach_s(seconds, false, _static_callback, this); - } - - void once_ms(uint32_t milliseconds, callback_function_t callback) - { - _callback_function = std::move(callback); - _attach_ms(milliseconds, false, _static_callback, this); - } - - void once_us_scheduled(uint32_t micros, callback_function_t callback) - { - once_us(micros, [callback]() { schedule_function(callback); }); - } - - void once_us(uint32_t micros, callback_function_t callback) - { - _callback_function = std::move(callback); - _attach_us(micros, 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), (void*)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); - } - - template - void once_us(uint32_t micros, void (*callback)(TArg), TArg arg) - { - static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - _attach_us(micros, 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); - - callback_function_t _callback_function = nullptr; - - esp_timer_handle_t _timer; - -private: - void _attach_us(uint32_t micros, bool repeat, callback_with_arg_t callback, void* arg); - void _attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg); -}; - - -#endif//TICKER_H diff --git a/libraries/Ticker/keywords.txt b/libraries/Ticker/keywords.txt index 81cce2c8ea5..b1020c4e59e 100644 --- a/libraries/Ticker/keywords.txt +++ b/libraries/Ticker/keywords.txt @@ -8,7 +8,14 @@ Ticker KEYWORD1 # Methods and Functions (KEYWORD2) ####################################### +attach_scheduled KEYWORD2 attach KEYWORD2 +attach_ms_scheduled KEYWORD2 attach_ms KEYWORD2 +once_scheduled KEYWORD2 once KEYWORD2 +once_ms_scheduled KEYWORD2 +once_ms KEYWORD2 detach KEYWORD2 +active KEYWORD2 + diff --git a/libraries/Ticker/src/Ticker.cpp b/libraries/Ticker/src/Ticker.cpp index c5f084eb851..19e6b60742d 100644 --- a/libraries/Ticker/src/Ticker.cpp +++ b/libraries/Ticker/src/Ticker.cpp @@ -34,7 +34,17 @@ Ticker::~Ticker() detach(); } +void Ticker::_attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg) +{ + _attach_us(1000000ULL * seconds, repeat, callback, arg); +} + void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg) +{ + _attach_us(1000ULL * milliseconds, repeat, callback, arg); +} + +void Ticker::_attach_us(uint32_t micros, bool repeat, callback_with_arg_t callback, void* arg) { esp_timer_create_args_t _timerConfig; _timerConfig.arg = reinterpret_cast(arg); @@ -47,10 +57,10 @@ void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t } esp_timer_create(&_timerConfig, &_timer); if (repeat) { - esp_timer_start_periodic(_timer, milliseconds * 1000ULL); + esp_timer_start_periodic(_timer, micros); } else { - esp_timer_start_once(_timer, milliseconds * 1000ULL); + esp_timer_start_once(_timer, micros); } } @@ -59,6 +69,7 @@ void Ticker::detach() { esp_timer_stop(_timer); esp_timer_delete(_timer); _timer = nullptr; + _callback_function = nullptr; } } @@ -70,7 +81,7 @@ bool Ticker::active() const { void Ticker::_static_callback(void* arg) { Ticker* _this = reinterpret_cast(arg); - if (!_this) return; - if (_this->_callback_function) _this->_callback_function(); + if (_this && _this->_callback_function) + _this->_callback_function(); } diff --git a/libraries/Ticker/src/Ticker.h b/libraries/Ticker/src/Ticker.h index cc816f96815..88a79962c3a 100644 --- a/libraries/Ticker/src/Ticker.h +++ b/libraries/Ticker/src/Ticker.h @@ -42,7 +42,7 @@ class Ticker void attach(float seconds, callback_function_t callback) { _callback_function = std::move(callback); - _attach_ms(seconds * 1000, true, _static_callback, this); + _attach_s(seconds, true, _static_callback, this); } void attach_ms(uint32_t milliseconds, callback_function_t callback) @@ -51,6 +51,12 @@ class Ticker _attach_ms(milliseconds, true, _static_callback, this); } + void attach_us(uint32_t micros, callback_function_t callback) + { + _callback_function = std::move(callback); + _attach_us(micros, true, _static_callback, this); + } + template void attach(float seconds, void (*callback)(TArg), TArg arg) { @@ -58,7 +64,7 @@ class Ticker // C-cast serves two purposes: // static_cast for smaller integer types, // reinterpret_cast + const_cast for pointer types - _attach_ms(seconds * 1000, true, reinterpret_cast(callback), (void*)arg); + _attach_s(seconds, true, reinterpret_cast(callback), (void*)arg); } template @@ -68,10 +74,17 @@ class Ticker _attach_ms(milliseconds, true, reinterpret_cast(callback), (void*)arg); } + template + void attach_us(uint32_t micros, void (*callback)(TArg), TArg arg) + { + static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); + _attach_us(micros, true, reinterpret_cast(callback), (void*)arg); + } + void once(float seconds, callback_function_t callback) { _callback_function = std::move(callback); - _attach_ms(seconds * 1000, false, _static_callback, this); + _attach_s(seconds, false, _static_callback, this); } void once_ms(uint32_t milliseconds, callback_function_t callback) @@ -80,11 +93,17 @@ class Ticker _attach_ms(milliseconds, false, _static_callback, this); } + void once_us(uint32_t micros, callback_function_t callback) + { + _callback_function = std::move(callback); + _attach_us(micros, 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_ms(seconds * 1000, false, reinterpret_cast(callback), (void*)arg); + _attach_s(seconds, false, reinterpret_cast(callback), (void*)arg); } template @@ -94,6 +113,13 @@ class Ticker _attach_ms(milliseconds, false, reinterpret_cast(callback), (void*)arg); } + template + void once_us(uint32_t micros, void (*callback)(TArg), TArg arg) + { + static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); + _attach_us(micros, false, reinterpret_cast(callback), (void*)arg); + } + void detach(); bool active() const; @@ -103,8 +129,11 @@ class Ticker callback_function_t _callback_function = nullptr; -protected: esp_timer_handle_t _timer; + +private: + void _attach_us(uint32_t micros, bool repeat, callback_with_arg_t callback, void* arg); + void _attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg); }; From 9ac40a0de1c9515cf34240019ca60ac0e84495c6 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Thu, 6 Jun 2019 16:06:36 +0200 Subject: [PATCH 07/22] Unify Ticker examples. --- .../Ticker/examples/Arguments/Arguments.ino | 51 --------------- .../examples/TickerBasic/TickerBasic.ino | 45 +++++++++++++ .../TickerFunctional/TickerFunctional.ino | 64 +++++++++++++++++++ .../TickerParameter/TickerParameter.ino | 35 ++++++++++ 4 files changed, 144 insertions(+), 51 deletions(-) delete mode 100644 libraries/Ticker/examples/Arguments/Arguments.ino create mode 100644 libraries/Ticker/examples/TickerBasic/TickerBasic.ino create mode 100644 libraries/Ticker/examples/TickerFunctional/TickerFunctional.ino create mode 100644 libraries/Ticker/examples/TickerParameter/TickerParameter.ino diff --git a/libraries/Ticker/examples/Arguments/Arguments.ino b/libraries/Ticker/examples/Arguments/Arguments.ino deleted file mode 100644 index 7f5bc5cde21..00000000000 --- a/libraries/Ticker/examples/Arguments/Arguments.ino +++ /dev/null @@ -1,51 +0,0 @@ -/* - * This example demonstrates used of Ticker with arguments. - * You can call the same callback function with different argument on different times. - * Based on the argument the callback can perform different tasks. - */ - -#include -#include - -// Arguments for the function must remain valid (not run out of scope) otherwise the function would read garbage data. -int LED_PIN_1 = 4; -#ifdef LED_BUILTIN - int LED_PIN_2 = LED_BUILTIN; -#else - int LED_PIN_2 = 8; -#endif - -Ticker tickerSetHigh; -Ticker tickerSetLow; - -// Argument to callback must always be passed a reference -void swapState(int *pin) { - static int led_1_state = 1; - static int led_2_state = 1; - if(*pin == LED_PIN_1){ - Serial.printf("[%lu ms] set pin %d to state: %d\n", millis(), *pin, led_1_state); - digitalWrite(*pin, led_1_state); - led_1_state = led_1_state ? 0 : 1; // reverse for next pass - }else if(*pin == LED_PIN_2){ - Serial.printf("[%lu ms] set pin %d to state: %d\n", millis(), *pin, led_2_state); - digitalWrite(*pin, led_2_state); - led_2_state = led_2_state ? 0 : 1; // reverse for next pass - } -} - -void setup() { - Serial.begin(115200); - pinMode(LED_PIN_1, OUTPUT); - pinMode(LED_PIN_2, OUTPUT); - //digitalWrite(1, LOW); - - // Blink LED every 500 ms on LED_PIN_1 - tickerSetLow.attach_ms(500, swapState, &LED_PIN_1); - - // Blink LED every 1000 ms on LED_PIN_2 - tickerSetHigh.attach_ms(1000, swapState, &LED_PIN_2); -} - -void loop() { - -} diff --git a/libraries/Ticker/examples/TickerBasic/TickerBasic.ino b/libraries/Ticker/examples/TickerBasic/TickerBasic.ino new file mode 100644 index 00000000000..2fabd983159 --- /dev/null +++ b/libraries/Ticker/examples/TickerBasic/TickerBasic.ino @@ -0,0 +1,45 @@ +/* + Basic Ticker usage + + Ticker is an object that will call a given function with a certain period. + Each Ticker calls one function. You can have as many Tickers as you like, + memory being the only limitation. + + A function may be attached to a ticker and detached from the ticker. + There are two variants of the attach function: attach and attach_ms. + The first one takes period in seconds, the second one in milliseconds. + + The built-in LED will be blinking. +*/ + +#include + +Ticker flipper; + +int count = 0; + +void flip() { + int state = digitalRead(LED_BUILTIN); // get the current state of GPIO1 pin + digitalWrite(LED_BUILTIN, !state); // set pin to the opposite state + + ++count; + // when the counter reaches a certain value, start blinking like crazy + if (count == 20) { + flipper.attach(0.1, flip); + } + // when the counter reaches yet another value, stop blinking + else if (count == 120) { + flipper.detach(); + } +} + +void setup() { + pinMode(LED_BUILTIN, OUTPUT); + digitalWrite(LED_BUILTIN, LOW); + + // flip the pin every 0.3s + flipper.attach(0.3, flip); +} + +void loop() { +} diff --git a/libraries/Ticker/examples/TickerFunctional/TickerFunctional.ino b/libraries/Ticker/examples/TickerFunctional/TickerFunctional.ino new file mode 100644 index 00000000000..387e6d6bcb0 --- /dev/null +++ b/libraries/Ticker/examples/TickerFunctional/TickerFunctional.ino @@ -0,0 +1,64 @@ +#include +#include + +#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() { +} diff --git a/libraries/Ticker/examples/TickerParameter/TickerParameter.ino b/libraries/Ticker/examples/TickerParameter/TickerParameter.ino new file mode 100644 index 00000000000..67c6256b5b7 --- /dev/null +++ b/libraries/Ticker/examples/TickerParameter/TickerParameter.ino @@ -0,0 +1,35 @@ +/* + Passing paramters to Ticker callbacks + + Apart from void(void) functions, the Ticker library supports + functions taking one argument. This argument's size has to be less or + equal to 4 bytes (so char, short, int, float, void*, char* types will do). + + This sample runs two tickers that both call one callback function, + but with different arguments. + + The built-in LED will be pulsing. +*/ + +#include + +Ticker tickerSetHigh; +Ticker tickerSetLow; + +void setPin(int state) { + digitalWrite(LED_BUILTIN, state); +} + +void setup() { + pinMode(LED_BUILTIN, OUTPUT); + digitalWrite(1, LOW); + + // every 25 ms, call setPin(0) + tickerSetLow.attach_ms(25, setPin, 0); + + // every 26 ms, call setPin(1) + tickerSetHigh.attach_ms(26, setPin, 1); +} + +void loop() { +} From 3b9ad3750aa03e37caf9c9c8f4de4eeee79aeec3 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Thu, 6 Jun 2019 16:26:11 +0200 Subject: [PATCH 08/22] Default for LED_BUILTIN --- libraries/Ticker/examples/TickerBasic/TickerBasic.ino | 4 ++++ libraries/Ticker/examples/TickerParameter/TickerParameter.ino | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/libraries/Ticker/examples/TickerBasic/TickerBasic.ino b/libraries/Ticker/examples/TickerBasic/TickerBasic.ino index 2fabd983159..a387b554b71 100644 --- a/libraries/Ticker/examples/TickerBasic/TickerBasic.ino +++ b/libraries/Ticker/examples/TickerBasic/TickerBasic.ino @@ -14,6 +14,10 @@ #include +#ifndef LED_BUILTIN +#define LED_BUILTIN 13 +#endif + Ticker flipper; int count = 0; diff --git a/libraries/Ticker/examples/TickerParameter/TickerParameter.ino b/libraries/Ticker/examples/TickerParameter/TickerParameter.ino index 67c6256b5b7..99c69c62567 100644 --- a/libraries/Ticker/examples/TickerParameter/TickerParameter.ino +++ b/libraries/Ticker/examples/TickerParameter/TickerParameter.ino @@ -13,6 +13,10 @@ #include +#ifndef LED_BUILTIN +#define LED_BUILTIN 13 +#endif + Ticker tickerSetHigh; Ticker tickerSetLow; From fef35f3d3e5fa25cb2893701b5cd4e5bb00297e0 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Thu, 6 Jun 2019 16:36:25 +0200 Subject: [PATCH 09/22] In Ticker, the *scheduled functions become available in another development branch. --- .../TickerFunctional/TickerFunctional.ino | 64 ------------------- 1 file changed, 64 deletions(-) delete mode 100644 libraries/Ticker/examples/TickerFunctional/TickerFunctional.ino diff --git a/libraries/Ticker/examples/TickerFunctional/TickerFunctional.ino b/libraries/Ticker/examples/TickerFunctional/TickerFunctional.ino deleted file mode 100644 index 387e6d6bcb0..00000000000 --- a/libraries/Ticker/examples/TickerFunctional/TickerFunctional.ino +++ /dev/null @@ -1,64 +0,0 @@ -#include -#include - -#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() { -} From ba08a711581a7a8a33e894789ec7f6012424a028 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Thu, 6 Jun 2019 22:58:51 +0200 Subject: [PATCH 10/22] Astyle from ESP8266 --- libraries/Ticker/examples/Blinker/Blinker.ino | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libraries/Ticker/examples/Blinker/Blinker.ino b/libraries/Ticker/examples/Blinker/Blinker.ino index aca1f450cca..ce5a18a3168 100644 --- a/libraries/Ticker/examples/Blinker/Blinker.ino +++ b/libraries/Ticker/examples/Blinker/Blinker.ino @@ -23,8 +23,7 @@ void toggle() { if (isBlinking) { blinker.detach(); isBlinking = false; - } - else { + } else { blinker.attach(blinkerPace, blink); isBlinking = true; } @@ -38,5 +37,5 @@ void setup() { } void loop() { - + } From 76c5f8bc3ee49310f8895448a6260733e74e19eb Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Fri, 28 Jun 2019 23:37:50 +0200 Subject: [PATCH 11/22] Fixed Arduino keywords.txt --- libraries/Ticker/keywords.txt | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/libraries/Ticker/keywords.txt b/libraries/Ticker/keywords.txt index b1020c4e59e..f5f1266516d 100644 --- a/libraries/Ticker/keywords.txt +++ b/libraries/Ticker/keywords.txt @@ -8,14 +8,11 @@ Ticker KEYWORD1 # Methods and Functions (KEYWORD2) ####################################### -attach_scheduled KEYWORD2 attach KEYWORD2 -attach_ms_scheduled KEYWORD2 attach_ms KEYWORD2 -once_scheduled KEYWORD2 +attach_us KEYWORD2 once KEYWORD2 -once_ms_scheduled KEYWORD2 once_ms KEYWORD2 +once_us KEYWORD2 detach KEYWORD2 active KEYWORD2 - From d9e9e1b26e0b1f5665328ccbeb8a8997955accd7 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Sun, 8 Sep 2019 19:07:13 +0200 Subject: [PATCH 12/22] 64bit integers instead of 32bits, timer functions on ESP32 accept 64bit integers. --- libraries/Ticker/src/Ticker.cpp | 4 ++-- libraries/Ticker/src/Ticker.h | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/libraries/Ticker/src/Ticker.cpp b/libraries/Ticker/src/Ticker.cpp index 19e6b60742d..dad371ef3d5 100644 --- a/libraries/Ticker/src/Ticker.cpp +++ b/libraries/Ticker/src/Ticker.cpp @@ -39,12 +39,12 @@ void Ticker::_attach_s(float seconds, bool repeat, callback_with_arg_t callback, _attach_us(1000000ULL * seconds, repeat, callback, arg); } -void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg) +void Ticker::_attach_ms(uint64_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg) { _attach_us(1000ULL * milliseconds, repeat, callback, arg); } -void Ticker::_attach_us(uint32_t micros, bool repeat, callback_with_arg_t callback, void* arg) +void Ticker::_attach_us(uint64_t micros, bool repeat, callback_with_arg_t callback, void* arg) { esp_timer_create_args_t _timerConfig; _timerConfig.arg = reinterpret_cast(arg); diff --git a/libraries/Ticker/src/Ticker.h b/libraries/Ticker/src/Ticker.h index 88a79962c3a..f9fa16ceb70 100644 --- a/libraries/Ticker/src/Ticker.h +++ b/libraries/Ticker/src/Ticker.h @@ -45,13 +45,13 @@ class Ticker _attach_s(seconds, true, _static_callback, this); } - void attach_ms(uint32_t milliseconds, callback_function_t callback) + void attach_ms(uint64_t milliseconds, callback_function_t callback) { _callback_function = std::move(callback); _attach_ms(milliseconds, true, _static_callback, this); } - void attach_us(uint32_t micros, callback_function_t callback) + void attach_us(uint64_t micros, callback_function_t callback) { _callback_function = std::move(callback); _attach_us(micros, true, _static_callback, this); @@ -68,14 +68,14 @@ class Ticker } template - void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg) + void attach_ms(uint64_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); } template - void attach_us(uint32_t micros, void (*callback)(TArg), TArg arg) + void attach_us(uint64_t micros, void (*callback)(TArg), TArg arg) { static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); _attach_us(micros, true, reinterpret_cast(callback), (void*)arg); @@ -87,13 +87,13 @@ class Ticker _attach_s(seconds, false, _static_callback, this); } - void once_ms(uint32_t milliseconds, callback_function_t callback) + void once_ms(uint64_t milliseconds, callback_function_t callback) { _callback_function = std::move(callback); _attach_ms(milliseconds, false, _static_callback, this); } - void once_us(uint32_t micros, callback_function_t callback) + void once_us(uint64_t micros, callback_function_t callback) { _callback_function = std::move(callback); _attach_us(micros, false, _static_callback, this); @@ -107,14 +107,14 @@ class Ticker } template - void once_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg) + void once_ms(uint64_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); } template - void once_us(uint32_t micros, void (*callback)(TArg), TArg arg) + void once_us(uint64_t micros, void (*callback)(TArg), TArg arg) { static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); _attach_us(micros, false, reinterpret_cast(callback), (void*)arg); @@ -124,7 +124,7 @@ class Ticker bool active() const; protected: - void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg); + void _attach_ms(uint64_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg); static void _static_callback(void* arg); callback_function_t _callback_function = nullptr; @@ -132,7 +132,7 @@ class Ticker esp_timer_handle_t _timer; private: - void _attach_us(uint32_t micros, bool repeat, callback_with_arg_t callback, void* arg); + void _attach_us(uint64_t micros, bool repeat, callback_with_arg_t callback, void* arg); void _attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg); }; From 3e8fd81098851a7fb2b62d93c29480e593eb1516 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Sun, 8 Sep 2019 21:03:16 +0200 Subject: [PATCH 13/22] Move code from header into compiliation unit. Reformat. --- libraries/Ticker/src/Ticker.cpp | 88 +++++++++++------- libraries/Ticker/src/Ticker.h | 160 +++++++++++++------------------- 2 files changed, 121 insertions(+), 127 deletions(-) diff --git a/libraries/Ticker/src/Ticker.cpp b/libraries/Ticker/src/Ticker.cpp index dad371ef3d5..84ad9c8a1ef 100644 --- a/libraries/Ticker/src/Ticker.cpp +++ b/libraries/Ticker/src/Ticker.cpp @@ -25,52 +25,76 @@ #include "Ticker.h" Ticker::Ticker() - : _timer(nullptr) + : _timer(nullptr) {} + +Ticker::~Ticker() { + detach(); } -Ticker::~Ticker() +void Ticker::attach(float seconds, callback_function_t callback) +{ + _callback_function = std::move(callback); + _attach_us(1000000ULL * seconds, true, _static_callback, this); +} + +void Ticker::attach_ms(uint64_t milliseconds, callback_function_t callback) +{ + _callback_function = std::move(callback); + _attach_us(1000ULL * milliseconds, true, _static_callback, this); +} + +void Ticker::attach_us(uint64_t micros, callback_function_t callback) +{ + _callback_function = std::move(callback); + _attach_us(micros, true, _static_callback, this); +} + +void Ticker::once(float seconds, callback_function_t callback) { - detach(); + _callback_function = std::move(callback); + _attach_us(1000000ULL * seconds, false, _static_callback, this); } -void Ticker::_attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg) +void Ticker::once_ms(uint64_t milliseconds, callback_function_t callback) { - _attach_us(1000000ULL * seconds, repeat, callback, arg); + _callback_function = std::move(callback); + _attach_us(1000ULL * milliseconds, false, _static_callback, this); } -void Ticker::_attach_ms(uint64_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg) +void Ticker::once_us(uint64_t micros, callback_function_t callback) { - _attach_us(1000ULL * milliseconds, repeat, callback, arg); + _callback_function = std::move(callback); + _attach_us(micros, false, _static_callback, this); } void Ticker::_attach_us(uint64_t micros, bool repeat, callback_with_arg_t callback, void* arg) { - esp_timer_create_args_t _timerConfig; - _timerConfig.arg = reinterpret_cast(arg); - _timerConfig.callback = callback; - _timerConfig.dispatch_method = ESP_TIMER_TASK; - _timerConfig.name = "Ticker"; - if (_timer) { - esp_timer_stop(_timer); - esp_timer_delete(_timer); - } - esp_timer_create(&_timerConfig, &_timer); - if (repeat) { - esp_timer_start_periodic(_timer, micros); - } - else { - esp_timer_start_once(_timer, micros); - } + esp_timer_create_args_t _timerConfig; + _timerConfig.arg = reinterpret_cast(arg); + _timerConfig.callback = callback; + _timerConfig.dispatch_method = ESP_TIMER_TASK; + _timerConfig.name = "Ticker"; + if (_timer) { + esp_timer_stop(_timer); + esp_timer_delete(_timer); + } + esp_timer_create(&_timerConfig, &_timer); + if (repeat) { + esp_timer_start_periodic(_timer, micros); + } + else { + esp_timer_start_once(_timer, micros); + } } void Ticker::detach() { - if (_timer) { - esp_timer_stop(_timer); - esp_timer_delete(_timer); - _timer = nullptr; - _callback_function = nullptr; - } + if (_timer) { + esp_timer_stop(_timer); + esp_timer_delete(_timer); + _timer = nullptr; + _callback_function = nullptr; + } } bool Ticker::active() const { @@ -80,8 +104,8 @@ bool Ticker::active() const { 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/src/Ticker.h b/libraries/Ticker/src/Ticker.h index f9fa16ceb70..47c3fcefacb 100644 --- a/libraries/Ticker/src/Ticker.h +++ b/libraries/Ticker/src/Ticker.h @@ -33,107 +33,77 @@ extern "C" { class Ticker { public: - Ticker(); - ~Ticker(); - - typedef void (*callback_with_arg_t)(void*); - typedef std::function callback_function_t; - - void attach(float seconds, callback_function_t callback) - { - _callback_function = std::move(callback); - _attach_s(seconds, true, _static_callback, this); - } - - void attach_ms(uint64_t milliseconds, callback_function_t callback) - { - _callback_function = std::move(callback); - _attach_ms(milliseconds, true, _static_callback, this); - } - - void attach_us(uint64_t micros, callback_function_t callback) - { - _callback_function = std::move(callback); - _attach_us(micros, 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), (void*)arg); - } - - template - void attach_ms(uint64_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); - } - - template - void attach_us(uint64_t micros, void (*callback)(TArg), TArg arg) - { - static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - _attach_us(micros, true, reinterpret_cast(callback), (void*)arg); - } - - void once(float seconds, callback_function_t callback) - { - _callback_function = std::move(callback); - _attach_s(seconds, false, _static_callback, this); - } - - void once_ms(uint64_t milliseconds, callback_function_t callback) - { - _callback_function = std::move(callback); - _attach_ms(milliseconds, false, _static_callback, this); - } - - void once_us(uint64_t micros, callback_function_t callback) - { - _callback_function = std::move(callback); - _attach_us(micros, 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), (void*)arg); - } - - template - void once_ms(uint64_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); - } - - template - void once_us(uint64_t micros, void (*callback)(TArg), TArg arg) - { - static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - _attach_us(micros, false, reinterpret_cast(callback), (void*)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(uint64_t milliseconds, callback_function_t callback); + void attach_us(uint64_t micros, 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_us(1000000ULL * seconds, true, reinterpret_cast(callback), (void*)arg); + } + + template + void attach_ms(uint64_t milliseconds, void (*callback)(TArg), TArg arg) + { + static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); + _attach_us(1000ULL * milliseconds, true, reinterpret_cast(callback), (void*)arg); + } + + template + void attach_us(uint64_t micros, void (*callback)(TArg), TArg arg) + { + static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); + _attach_us(micros, true, reinterpret_cast(callback), (void*)arg); + } + + void once(float seconds, callback_function_t callback); + void once_ms(uint64_t milliseconds, callback_function_t callback); + void once_us(uint64_t micros, 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_us(1000000ULL * seconds, false, reinterpret_cast(callback), (void*)arg); + } + + template + void once_ms(uint64_t milliseconds, void (*callback)(TArg), TArg arg) + { + static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); + _attach_us(1000ULL * milliseconds, false, reinterpret_cast(callback), (void*)arg); + } + + template + void once_us(uint64_t micros, void (*callback)(TArg), TArg arg) + { + static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); + _attach_us(micros, false, reinterpret_cast(callback), (void*)arg); + } + + void detach(); + bool active() const; protected: - void _attach_ms(uint64_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg); - static void _static_callback(void* arg); + static void _static_callback(void* arg); - callback_function_t _callback_function = nullptr; + callback_function_t _callback_function = nullptr; - esp_timer_handle_t _timer; + esp_timer_handle_t _timer; private: - void _attach_us(uint64_t micros, bool repeat, callback_with_arg_t callback, void* arg); - void _attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg); + void _attach_us(uint64_t micros, bool repeat, callback_with_arg_t callback, void* arg); }; From 25f2eeac42de3664c70d684ed2ca1bb515c8b443 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Mon, 9 Sep 2019 14:32:40 +0200 Subject: [PATCH 14/22] Test case same as ESP8266 --- .../examples/TickerParameter/TickerParameter.ino | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/libraries/Ticker/examples/TickerParameter/TickerParameter.ino b/libraries/Ticker/examples/TickerParameter/TickerParameter.ino index 99c69c62567..a6c7d9d2348 100644 --- a/libraries/Ticker/examples/TickerParameter/TickerParameter.ino +++ b/libraries/Ticker/examples/TickerParameter/TickerParameter.ino @@ -24,15 +24,19 @@ void setPin(int state) { digitalWrite(LED_BUILTIN, state); } +void setPinChar(char state) { + digitalWrite(LED_BUILTIN, state); +} + void setup() { pinMode(LED_BUILTIN, OUTPUT); - digitalWrite(1, LOW); // every 25 ms, call setPin(0) tickerSetLow.attach_ms(25, setPin, 0); - // every 26 ms, call setPin(1) - tickerSetHigh.attach_ms(26, setPin, 1); + // every 26 ms, call setPinChar(1) + tickerSetHigh.attach_ms(26, setPinChar, (char)1); + } void loop() { From c8c09e200a75aa674a905fb70db83e7ddf111d48 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Fri, 11 Oct 2019 12:43:31 +0200 Subject: [PATCH 15/22] Implementing inline in header saves 204+ bytes program size. --- libraries/Ticker/src/Ticker.cpp | 36 ----------------------------- libraries/Ticker/src/Ticker.h | 40 ++++++++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 42 deletions(-) diff --git a/libraries/Ticker/src/Ticker.cpp b/libraries/Ticker/src/Ticker.cpp index 84ad9c8a1ef..f1d03e69b08 100644 --- a/libraries/Ticker/src/Ticker.cpp +++ b/libraries/Ticker/src/Ticker.cpp @@ -32,42 +32,6 @@ Ticker::~Ticker() detach(); } -void Ticker::attach(float seconds, callback_function_t callback) -{ - _callback_function = std::move(callback); - _attach_us(1000000ULL * seconds, true, _static_callback, this); -} - -void Ticker::attach_ms(uint64_t milliseconds, callback_function_t callback) -{ - _callback_function = std::move(callback); - _attach_us(1000ULL * milliseconds, true, _static_callback, this); -} - -void Ticker::attach_us(uint64_t micros, callback_function_t callback) -{ - _callback_function = std::move(callback); - _attach_us(micros, true, _static_callback, this); -} - -void Ticker::once(float seconds, callback_function_t callback) -{ - _callback_function = std::move(callback); - _attach_us(1000000ULL * seconds, false, _static_callback, this); -} - -void Ticker::once_ms(uint64_t milliseconds, callback_function_t callback) -{ - _callback_function = std::move(callback); - _attach_us(1000ULL * milliseconds, false, _static_callback, this); -} - -void Ticker::once_us(uint64_t micros, callback_function_t callback) -{ - _callback_function = std::move(callback); - _attach_us(micros, false, _static_callback, this); -} - void Ticker::_attach_us(uint64_t micros, bool repeat, callback_with_arg_t callback, void* arg) { esp_timer_create_args_t _timerConfig; diff --git a/libraries/Ticker/src/Ticker.h b/libraries/Ticker/src/Ticker.h index 47c3fcefacb..612f0a1b4df 100644 --- a/libraries/Ticker/src/Ticker.h +++ b/libraries/Ticker/src/Ticker.h @@ -39,9 +39,23 @@ 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(uint64_t milliseconds, callback_function_t callback); - void attach_us(uint64_t micros, callback_function_t callback); + void attach(float seconds, callback_function_t callback) + { + _callback_function = std::move(callback); + _attach_us(1000000ULL * seconds, true, _static_callback, this); + } + + void attach_ms(uint64_t milliseconds, callback_function_t callback) + { + _callback_function = std::move(callback); + _attach_us(1000ULL * milliseconds, true, _static_callback, this); + } + + void attach_us(uint64_t micros, callback_function_t callback) + { + _callback_function = std::move(callback); + _attach_us(micros, true, _static_callback, this); + } template void attach(float seconds, void (*callback)(TArg), TArg arg) @@ -67,9 +81,23 @@ class Ticker _attach_us(micros, true, reinterpret_cast(callback), (void*)arg); } - void once(float seconds, callback_function_t callback); - void once_ms(uint64_t milliseconds, callback_function_t callback); - void once_us(uint64_t micros, callback_function_t callback); + void once(float seconds, callback_function_t callback) + { + _callback_function = std::move(callback); + _attach_us(1000000ULL * seconds, false, _static_callback, this); + } + + void once_ms(uint64_t milliseconds, callback_function_t callback) + { + _callback_function = std::move(callback); + _attach_us(1000ULL * milliseconds, false, _static_callback, this); + } + + void once_us(uint64_t micros, callback_function_t callback) + { + _callback_function = std::move(callback); + _attach_us(micros, false, _static_callback, this); + } template void once(float seconds, void (*callback)(TArg), TArg arg) From 253e84c5930265f84274898bb3ac19fb22b44006 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Wed, 13 Nov 2019 09:53:04 +0100 Subject: [PATCH 16/22] Examples --- .../examples/TickerBasic/TickerBasic.ino | 32 +++++++++---------- .../TickerParameter/TickerParameter.ino | 27 +++++++++++----- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/libraries/Ticker/examples/TickerBasic/TickerBasic.ino b/libraries/Ticker/examples/TickerBasic/TickerBasic.ino index a387b554b71..8de5a1282ad 100644 --- a/libraries/Ticker/examples/TickerBasic/TickerBasic.ino +++ b/libraries/Ticker/examples/TickerBasic/TickerBasic.ino @@ -23,26 +23,26 @@ Ticker flipper; int count = 0; void flip() { - int state = digitalRead(LED_BUILTIN); // get the current state of GPIO1 pin - digitalWrite(LED_BUILTIN, !state); // set pin to the opposite state - - ++count; - // when the counter reaches a certain value, start blinking like crazy - if (count == 20) { - flipper.attach(0.1, flip); - } - // when the counter reaches yet another value, stop blinking - else if (count == 120) { - flipper.detach(); - } + int state = digitalRead(LED_BUILTIN); // get the current state of GPIO1 pin + digitalWrite(LED_BUILTIN, !state); // set pin to the opposite state + + ++count; + // when the counter reaches a certain value, start blinking like crazy + if (count == 20) { + flipper.attach(0.1, flip); + } + // when the counter reaches yet another value, stop blinking + else if (count == 120) { + flipper.detach(); + } } void setup() { - pinMode(LED_BUILTIN, OUTPUT); - digitalWrite(LED_BUILTIN, LOW); + pinMode(LED_BUILTIN, OUTPUT); + digitalWrite(LED_BUILTIN, LOW); - // flip the pin every 0.3s - flipper.attach(0.3, flip); + // flip the pin every 0.3s + flipper.attach(0.3, flip); } void loop() { diff --git a/libraries/Ticker/examples/TickerParameter/TickerParameter.ino b/libraries/Ticker/examples/TickerParameter/TickerParameter.ino index a6c7d9d2348..ceb79e32f54 100644 --- a/libraries/Ticker/examples/TickerParameter/TickerParameter.ino +++ b/libraries/Ticker/examples/TickerParameter/TickerParameter.ino @@ -17,26 +17,37 @@ #define LED_BUILTIN 13 #endif -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); + digitalWrite(LED_BUILTIN, state); } void setPinChar(char state) { - digitalWrite(LED_BUILTIN, state); + digitalWrite(LED_BUILTIN, state); } void setup() { - pinMode(LED_BUILTIN, OUTPUT); + 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() { From ab3f7ff1015fd4297bf2564354c5f875f8f3d5c0 Mon Sep 17 00:00:00 2001 From: "Dirk O. Kaar" Date: Tue, 7 Feb 2023 08:21:50 +0100 Subject: [PATCH 17/22] Fix a compiler warning due to c-style casting. --- libraries/Ticker/src/Ticker.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libraries/Ticker/src/Ticker.h b/libraries/Ticker/src/Ticker.h index 612f0a1b4df..1ee5064cbb2 100644 --- a/libraries/Ticker/src/Ticker.h +++ b/libraries/Ticker/src/Ticker.h @@ -64,21 +64,21 @@ class Ticker // C-cast serves two purposes: // static_cast for smaller integer types, // reinterpret_cast + const_cast for pointer types - _attach_us(1000000ULL * seconds, true, reinterpret_cast(callback), (void*)arg); + _attach_us(1000000ULL * seconds, true, reinterpret_cast(callback), reinterpret_cast(arg)); } template void attach_ms(uint64_t milliseconds, void (*callback)(TArg), TArg arg) { static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - _attach_us(1000ULL * milliseconds, true, reinterpret_cast(callback), (void*)arg); + _attach_us(1000ULL * milliseconds, true, reinterpret_cast(callback), reinterpret_cast(arg)); } template void attach_us(uint64_t micros, void (*callback)(TArg), TArg arg) { static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - _attach_us(micros, true, reinterpret_cast(callback), (void*)arg); + _attach_us(micros, true, reinterpret_cast(callback), reinterpret_cast(arg)); } void once(float seconds, callback_function_t callback) @@ -103,21 +103,21 @@ 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*)"); - _attach_us(1000000ULL * seconds, false, reinterpret_cast(callback), (void*)arg); + _attach_us(1000000ULL * seconds, false, reinterpret_cast(callback), reinterpret_cast(arg)); } template void once_ms(uint64_t milliseconds, void (*callback)(TArg), TArg arg) { static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - _attach_us(1000ULL * milliseconds, false, reinterpret_cast(callback), (void*)arg); + _attach_us(1000ULL * milliseconds, false, reinterpret_cast(callback), reinterpret_cast(arg)); } template void once_us(uint64_t micros, void (*callback)(TArg), TArg arg) { static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - _attach_us(micros, false, reinterpret_cast(callback), (void*)arg); + _attach_us(micros, false, reinterpret_cast(callback), reinterpret_cast(arg)); } void detach(); From bcf0e7b387dff164fc396b48e4f765adafffef68 Mon Sep 17 00:00:00 2001 From: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> Date: Fri, 26 Jan 2024 10:55:25 -0300 Subject: [PATCH 18/22] Revert formatting changes --- libraries/Ticker/src/Ticker.cpp | 53 +++++---- libraries/Ticker/src/Ticker.h | 198 ++++++++++++++++---------------- 2 files changed, 125 insertions(+), 126 deletions(-) diff --git a/libraries/Ticker/src/Ticker.cpp b/libraries/Ticker/src/Ticker.cpp index f1d03e69b08..4f66585ba39 100644 --- a/libraries/Ticker/src/Ticker.cpp +++ b/libraries/Ticker/src/Ticker.cpp @@ -34,42 +34,41 @@ Ticker::~Ticker() void Ticker::_attach_us(uint64_t micros, bool repeat, callback_with_arg_t callback, void* arg) { - esp_timer_create_args_t _timerConfig; - _timerConfig.arg = reinterpret_cast(arg); - _timerConfig.callback = callback; - _timerConfig.dispatch_method = ESP_TIMER_TASK; - _timerConfig.name = "Ticker"; - if (_timer) { - esp_timer_stop(_timer); - esp_timer_delete(_timer); - } - esp_timer_create(&_timerConfig, &_timer); - if (repeat) { - esp_timer_start_periodic(_timer, micros); - } - else { - esp_timer_start_once(_timer, micros); - } + esp_timer_create_args_t _timerConfig; + _timerConfig.arg = reinterpret_cast(arg); + _timerConfig.callback = callback; + _timerConfig.dispatch_method = ESP_TIMER_TASK; + _timerConfig.name = "Ticker"; + if (_timer) { + esp_timer_stop(_timer); + esp_timer_delete(_timer); + } + esp_timer_create(&_timerConfig, &_timer); + if (repeat) { + esp_timer_start_periodic(_timer, micros); + } else { + esp_timer_start_once(_timer, micros); + } } void Ticker::detach() { - if (_timer) { - esp_timer_stop(_timer); - esp_timer_delete(_timer); - _timer = nullptr; - _callback_function = nullptr; - } + if (_timer) { + esp_timer_stop(_timer); + esp_timer_delete(_timer); + _timer = nullptr; + _callback_function = nullptr; + } } bool Ticker::active() const { - if (!_timer) return false; - return esp_timer_is_active(_timer); + if (!_timer) return false; + return esp_timer_is_active(_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/src/Ticker.h b/libraries/Ticker/src/Ticker.h index 1ee5064cbb2..0eeca0e9260 100644 --- a/libraries/Ticker/src/Ticker.h +++ b/libraries/Ticker/src/Ticker.h @@ -33,106 +33,106 @@ extern "C" { class Ticker { public: - Ticker(); - ~Ticker(); - - typedef void (*callback_with_arg_t)(void*); - typedef std::function callback_function_t; - - void attach(float seconds, callback_function_t callback) - { - _callback_function = std::move(callback); - _attach_us(1000000ULL * seconds, true, _static_callback, this); - } - - void attach_ms(uint64_t milliseconds, callback_function_t callback) - { - _callback_function = std::move(callback); - _attach_us(1000ULL * milliseconds, true, _static_callback, this); - } - - void attach_us(uint64_t micros, callback_function_t callback) - { - _callback_function = std::move(callback); - _attach_us(micros, 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_us(1000000ULL * seconds, true, reinterpret_cast(callback), reinterpret_cast(arg)); - } - - template - void attach_ms(uint64_t milliseconds, void (*callback)(TArg), TArg arg) - { - static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - _attach_us(1000ULL * milliseconds, true, reinterpret_cast(callback), reinterpret_cast(arg)); - } - - template - void attach_us(uint64_t micros, void (*callback)(TArg), TArg arg) - { - static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - _attach_us(micros, true, reinterpret_cast(callback), reinterpret_cast(arg)); - } - - void once(float seconds, callback_function_t callback) - { - _callback_function = std::move(callback); - _attach_us(1000000ULL * seconds, false, _static_callback, this); - } - - void once_ms(uint64_t milliseconds, callback_function_t callback) - { - _callback_function = std::move(callback); - _attach_us(1000ULL * milliseconds, false, _static_callback, this); - } - - void once_us(uint64_t micros, callback_function_t callback) - { - _callback_function = std::move(callback); - _attach_us(micros, 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_us(1000000ULL * seconds, false, reinterpret_cast(callback), reinterpret_cast(arg)); - } - - template - void once_ms(uint64_t milliseconds, void (*callback)(TArg), TArg arg) - { - static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - _attach_us(1000ULL * milliseconds, false, reinterpret_cast(callback), reinterpret_cast(arg)); - } - - template - void once_us(uint64_t micros, void (*callback)(TArg), TArg arg) - { - static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); - _attach_us(micros, false, reinterpret_cast(callback), reinterpret_cast(arg)); - } - - void detach(); - bool active() const; - -protected: - static void _static_callback(void* arg); - - callback_function_t _callback_function = nullptr; - - esp_timer_handle_t _timer; + Ticker(); + ~Ticker(); + + typedef void (*callback_with_arg_t)(void*); + typedef std::function callback_function_t; + + void attach(float seconds, callback_function_t callback) + { + _callback_function = std::move(callback); + _attach_us(1000000ULL * seconds, true, _static_callback, this); + } + + void attach_ms(uint64_t milliseconds, callback_function_t callback) + { + _callback_function = std::move(callback); + _attach_us(1000ULL * milliseconds, true, _static_callback, this); + } + + void attach_us(uint64_t micros, callback_function_t callback) + { + _callback_function = std::move(callback); + _attach_us(micros, 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_us(1000000ULL * seconds, true, reinterpret_cast(callback), reinterpret_cast(arg)); + } + + template + void attach_ms(uint64_t milliseconds, void (*callback)(TArg), TArg arg) + { + static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); + _attach_us(1000ULL * milliseconds, true, reinterpret_cast(callback), reinterpret_cast(arg)); + } + + template + void attach_us(uint64_t micros, void (*callback)(TArg), TArg arg) + { + static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); + _attach_us(micros, true, reinterpret_cast(callback), reinterpret_cast(arg)); + } + + void once(float seconds, callback_function_t callback) + { + _callback_function = std::move(callback); + _attach_us(1000000ULL * seconds, false, _static_callback, this); + } + + void once_ms(uint64_t milliseconds, callback_function_t callback) + { + _callback_function = std::move(callback); + _attach_us(1000ULL * milliseconds, false, _static_callback, this); + } + + void once_us(uint64_t micros, callback_function_t callback) + { + _callback_function = std::move(callback); + _attach_us(micros, 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_us(1000000ULL * seconds, false, reinterpret_cast(callback), reinterpret_cast(arg)); + } + + template + void once_ms(uint64_t milliseconds, void (*callback)(TArg), TArg arg) + { + static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); + _attach_us(1000ULL * milliseconds, false, reinterpret_cast(callback), reinterpret_cast(arg)); + } + + template + void once_us(uint64_t micros, void (*callback)(TArg), TArg arg) + { + static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); + _attach_us(micros, false, reinterpret_cast(callback), reinterpret_cast(arg)); + } + + void detach(); + bool active() const; + +protected: + static void _static_callback(void* arg); + + callback_function_t _callback_function = nullptr; + + esp_timer_handle_t _timer; private: - void _attach_us(uint64_t micros, bool repeat, callback_with_arg_t callback, void* arg); -}; + void _attach_us(uint64_t micros, bool repeat, callback_with_arg_t callback, void* arg); + }; -#endif//TICKER_H +#endif // TICKER_H From 64aec3aaf8cbacea22db37a9904930722589c519 Mon Sep 17 00:00:00 2001 From: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> Date: Fri, 26 Jan 2024 10:59:52 -0300 Subject: [PATCH 19/22] More format reversions --- libraries/Ticker/examples/Blinker/Blinker.ino | 5 +++-- libraries/Ticker/src/Ticker.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/libraries/Ticker/examples/Blinker/Blinker.ino b/libraries/Ticker/examples/Blinker/Blinker.ino index ce5a18a3168..aca1f450cca 100644 --- a/libraries/Ticker/examples/Blinker/Blinker.ino +++ b/libraries/Ticker/examples/Blinker/Blinker.ino @@ -23,7 +23,8 @@ void toggle() { if (isBlinking) { blinker.detach(); isBlinking = false; - } else { + } + else { blinker.attach(blinkerPace, blink); isBlinking = true; } @@ -37,5 +38,5 @@ void setup() { } void loop() { - + } diff --git a/libraries/Ticker/src/Ticker.h b/libraries/Ticker/src/Ticker.h index 0eeca0e9260..e49715d0680 100644 --- a/libraries/Ticker/src/Ticker.h +++ b/libraries/Ticker/src/Ticker.h @@ -123,7 +123,7 @@ class Ticker void detach(); bool active() const; -protected: +protected: static void _static_callback(void* arg); callback_function_t _callback_function = nullptr; From 5ff9e3243dc5f44d65dd18e0dad4d7441242c2cf Mon Sep 17 00:00:00 2001 From: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> Date: Fri, 26 Jan 2024 11:01:50 -0300 Subject: [PATCH 20/22] Revert --- libraries/Ticker/src/Ticker.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/libraries/Ticker/src/Ticker.cpp b/libraries/Ticker/src/Ticker.cpp index 4f66585ba39..3e638419f22 100644 --- a/libraries/Ticker/src/Ticker.cpp +++ b/libraries/Ticker/src/Ticker.cpp @@ -1,8 +1,8 @@ -/* +/* Ticker.cpp - esp32 library that calls functions periodically Copyright (c) 2017 Bert Melis. All rights reserved. - + Based on the original work of: Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. The original version is part of the esp8266 core for Arduino environment. @@ -24,16 +24,14 @@ #include "Ticker.h" -Ticker::Ticker() - : _timer(nullptr) {} +Ticker::Ticker() : + _timer(nullptr) {} -Ticker::~Ticker() -{ +Ticker::~Ticker() { detach(); } -void Ticker::_attach_us(uint64_t micros, bool repeat, callback_with_arg_t callback, void* arg) -{ +void Ticker::_attach_us(uint64_t micros, bool repeat, callback_with_arg_t callback, void* arg) { esp_timer_create_args_t _timerConfig; _timerConfig.arg = reinterpret_cast(arg); _timerConfig.callback = callback; From c2dfadfd4b46b068bcc675aed946696f9835cf65 Mon Sep 17 00:00:00 2001 From: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> Date: Fri, 26 Jan 2024 11:40:05 -0300 Subject: [PATCH 21/22] Revert --- 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 e49715d0680..c65a0e44595 100644 --- a/libraries/Ticker/src/Ticker.h +++ b/libraries/Ticker/src/Ticker.h @@ -26,7 +26,7 @@ #define TICKER_H extern "C" { -#include "esp_timer.h" + #include "esp_timer.h" } #include @@ -132,7 +132,7 @@ class Ticker private: void _attach_us(uint64_t micros, bool repeat, callback_with_arg_t callback, void* arg); - }; +}; #endif // TICKER_H From 9a1c4e5477b0066231b9ee3c2309b0ad4bfc2ffb Mon Sep 17 00:00:00 2001 From: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> Date: Fri, 26 Jan 2024 11:41:42 -0300 Subject: [PATCH 22/22] Revert --- libraries/Ticker/src/Ticker.cpp | 2 +- libraries/Ticker/src/Ticker.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libraries/Ticker/src/Ticker.cpp b/libraries/Ticker/src/Ticker.cpp index 3e638419f22..6c996ed56b3 100644 --- a/libraries/Ticker/src/Ticker.cpp +++ b/libraries/Ticker/src/Ticker.cpp @@ -28,7 +28,7 @@ Ticker::Ticker() : _timer(nullptr) {} Ticker::~Ticker() { - detach(); + detach(); } void Ticker::_attach_us(uint64_t micros, bool repeat, callback_with_arg_t callback, void* arg) { diff --git a/libraries/Ticker/src/Ticker.h b/libraries/Ticker/src/Ticker.h index c65a0e44595..50ab424c275 100644 --- a/libraries/Ticker/src/Ticker.h +++ b/libraries/Ticker/src/Ticker.h @@ -1,8 +1,8 @@ -/* +/* Ticker.h - esp32 library that calls functions periodically Copyright (c) 2017 Bert Melis. All rights reserved. - + Based on the original work of: Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. The original version is part of the esp8266 core for Arduino environment. @@ -123,7 +123,7 @@ class Ticker void detach(); bool active() const; -protected: +protected: static void _static_callback(void* arg); callback_function_t _callback_function = nullptr;