From c33b3513c38d297ab9588f4d762d3ed1f95db882 Mon Sep 17 00:00:00 2001 From: Bert Melis Date: Wed, 31 Jan 2018 21:47:15 +0100 Subject: [PATCH 1/2] Add Ticker library --- .../Ticker/examples/Arguments/Arguments.ino | 24 ++++ libraries/Ticker/examples/Blinker/Blinker.ino | 39 +++++++ libraries/Ticker/keywords.txt | 14 +++ libraries/Ticker/library.properties | 9 ++ libraries/Ticker/src/Ticker.cpp | 58 ++++++++++ libraries/Ticker/src/Ticker.h | 107 ++++++++++++++++++ 6 files changed, 251 insertions(+) create mode 100644 libraries/Ticker/examples/Arguments/Arguments.ino create mode 100644 libraries/Ticker/examples/Blinker/Blinker.ino create mode 100644 libraries/Ticker/keywords.txt create mode 100644 libraries/Ticker/library.properties create mode 100644 libraries/Ticker/src/Ticker.cpp create mode 100644 libraries/Ticker/src/Ticker.h diff --git a/libraries/Ticker/examples/Arguments/Arguments.ino b/libraries/Ticker/examples/Arguments/Arguments.ino new file mode 100644 index 00000000000..7e6190f5504 --- /dev/null +++ b/libraries/Ticker/examples/Arguments/Arguments.ino @@ -0,0 +1,24 @@ +#include +#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() { + +} \ No newline at end of file diff --git a/libraries/Ticker/examples/Blinker/Blinker.ino b/libraries/Ticker/examples/Blinker/Blinker.ino new file mode 100644 index 00000000000..7d8d0aaef65 --- /dev/null +++ b/libraries/Ticker/examples/Blinker/Blinker.ino @@ -0,0 +1,39 @@ +#include +#include + +Ticker blinker; +Ticker toggler; +Ticker changer; +float blinkerPace = 0.1; //seconds +const float togglePeriod = 5; //seconds + +void change() { + blinkerPace = 0.5; +} + +void blink() { + digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); +} + +void toggle() { + static bool isBlinking = false; + if (isBlinking) { + blinker.detach(); + isBlinking = false; + } + else { + blinker.attach(blinkerPace, blink); + isBlinking = true; + } + digitalWrite(LED_BUILTIN, LOW); //make sure LED on on after toggling (pin LOW = led ON) +} + +void setup() { + pinMode(LED_BUILTIN, OUTPUT); + toggler.attach(togglePeriod, toggle); + changer.once(30, change); +} + +void loop() { + +} diff --git a/libraries/Ticker/keywords.txt b/libraries/Ticker/keywords.txt new file mode 100644 index 00000000000..c387d1ef2fd --- /dev/null +++ b/libraries/Ticker/keywords.txt @@ -0,0 +1,14 @@ +####################################### +# Datatypes (KEYWORD1) +####################################### + +Ticker KEYWORD1 + +####################################### +# Methods and Functions (KEYWORD2) +####################################### + +attach KEYWORD2 +attach_ms KEYWORD2 +once KEYWORD2 +detach KEYWORD2 diff --git a/libraries/Ticker/library.properties b/libraries/Ticker/library.properties new file mode 100644 index 00000000000..0f399447d4d --- /dev/null +++ b/libraries/Ticker/library.properties @@ -0,0 +1,9 @@ +name=Ticker +version=1.1 +author=Bert Melis +maintainer=Hristo Gochkov +sentence=Allows to call functions with a given interval. +paragraph= +category=Timing +url= +architectures=esp32 diff --git a/libraries/Ticker/src/Ticker.cpp b/libraries/Ticker/src/Ticker.cpp new file mode 100644 index 00000000000..ce5cf69332c --- /dev/null +++ b/libraries/Ticker/src/Ticker.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 "Ticker.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 * 1000); + } else { + esp_timer_start_once(_timer, milliseconds * 1000); + } +} + +void Ticker::detach() { + if (_timer) { + esp_timer_stop(_timer); + esp_timer_delete(_timer); + _timer = nullptr; + } +} diff --git a/libraries/Ticker/src/Ticker.h b/libraries/Ticker/src/Ticker.h new file mode 100644 index 00000000000..82804e0f37d --- /dev/null +++ b/libraries/Ticker/src/Ticker.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 TICKER_H +#define TICKER_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 // TICKER_H From d2d24f995fc5af616d47abae0b89e0ba0e1544f6 Mon Sep 17 00:00:00 2001 From: Bert Melis Date: Fri, 16 Feb 2018 12:38:11 +0100 Subject: [PATCH 2/2] Fix CI (#1) remove LED_BUILTIN in examples --- libraries/Ticker/examples/Arguments/Arguments.ino | 9 ++++++--- libraries/Ticker/examples/Blinker/Blinker.ino | 11 +++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/libraries/Ticker/examples/Arguments/Arguments.ino b/libraries/Ticker/examples/Arguments/Arguments.ino index 7e6190f5504..cde8acbfa09 100644 --- a/libraries/Ticker/examples/Arguments/Arguments.ino +++ b/libraries/Ticker/examples/Arguments/Arguments.ino @@ -1,15 +1,18 @@ #include #include +// attach a LED to GPIO 21 +#define LED_PIN 21 + Ticker tickerSetHigh; Ticker tickerSetLow; void setPin(int state) { - digitalWrite(LED_BUILTIN, state); + digitalWrite(LED_PIN, state); } void setup() { - pinMode(LED_BUILTIN, OUTPUT); + pinMode(LED_PIN, OUTPUT); digitalWrite(1, LOW); // every 25 ms, call setPin(0) @@ -21,4 +24,4 @@ void setup() { void loop() { -} \ No newline at end of file +} diff --git a/libraries/Ticker/examples/Blinker/Blinker.ino b/libraries/Ticker/examples/Blinker/Blinker.ino index 7d8d0aaef65..aca1f450cca 100644 --- a/libraries/Ticker/examples/Blinker/Blinker.ino +++ b/libraries/Ticker/examples/Blinker/Blinker.ino @@ -1,6 +1,9 @@ #include #include +// attach a LED to pPIO 21 +#define LED_PIN 21 + Ticker blinker; Ticker toggler; Ticker changer; @@ -12,7 +15,7 @@ void change() { } void blink() { - digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); + digitalWrite(LED_PIN, !digitalRead(LED_PIN)); } void toggle() { @@ -25,12 +28,12 @@ void toggle() { blinker.attach(blinkerPace, blink); isBlinking = true; } - digitalWrite(LED_BUILTIN, LOW); //make sure LED on on after toggling (pin LOW = led ON) + digitalWrite(LED_PIN, LOW); //make sure LED on on after toggling (pin LOW = led ON) } void setup() { - pinMode(LED_BUILTIN, OUTPUT); - toggler.attach(togglePeriod, toggle); + pinMode(LED_PIN, OUTPUT); + toggler.attach(togglePeriod, toggle); changer.once(30, change); }