diff --git a/Control/lib/TickerV2/keywords.txt b/Control/lib/TickerV2/keywords.txt new file mode 100644 index 0000000..52026d7 --- /dev/null +++ b/Control/lib/TickerV2/keywords.txt @@ -0,0 +1,14 @@ +####################################### +# Datatypes (KEYWORD1) +####################################### + +Ticker KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +attach KEYWORD2 +attach_ms KEYWORD2 +once KEYWORD2 +detach KEYWORD2 \ No newline at end of file diff --git a/Control/lib/TickerV2/library.properties b/Control/lib/TickerV2/library.properties new file mode 100644 index 0000000..96528a2 --- /dev/null +++ b/Control/lib/TickerV2/library.properties @@ -0,0 +1,9 @@ +name=TickerV2 +version=2.0.0 +author=Bert Melis +maintainer=Hristo Gochkov +sentence=Allows to call functions with a given interval. +paragraph= +category=Timing +url= +architectures=esp32 \ No newline at end of file diff --git a/Control/lib/TickerV2/src/TickerV2.cpp b/Control/lib/TickerV2/src/TickerV2.cpp new file mode 100644 index 0000000..c2267f8 --- /dev/null +++ b/Control/lib/TickerV2/src/TickerV2.cpp @@ -0,0 +1,58 @@ +/* + 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 "TickerV2.h" + +Ticker::Ticker() : + _timer(nullptr) {} + +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::detach() { + if (_timer) { + esp_timer_stop(_timer); + esp_timer_delete(_timer); + _timer = nullptr; + } +} \ No newline at end of file diff --git a/Control/lib/TickerV2/src/TickerV2.h b/Control/lib/TickerV2/src/TickerV2.h new file mode 100644 index 0000000..af1fb0f --- /dev/null +++ b/Control/lib/TickerV2/src/TickerV2.h @@ -0,0 +1,107 @@ +/* + 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 TICKERV2_H +#define TICKERV2_H + +extern "C" { + #include "esp_timer.h" +} + +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); + + +protected: + esp_timer_handle_t _timer; +}; + + +#endif // TICKERV2_H \ No newline at end of file diff --git a/Control/platformio.ini b/Control/platformio.ini index 9880f9b..0f97db2 100644 --- a/Control/platformio.ini +++ b/Control/platformio.ini @@ -23,7 +23,6 @@ build_flags = build_type = debug lib_deps = plerup/EspSoftwareSerial@^6.12.6 - sstaub/Ticker@^4.2.0 me-no-dev/ESP Async WebServer@^1.2.3 me-no-dev/AsyncTCP@^1.1.1 links2004/WebSockets@^2.3.6