Add Ticker library (#1057)
* Add Ticker library * Fix CI (#1) remove LED_BUILTIN in examples
This commit is contained in:
parent
f5c2afff31
commit
8e29347d4f
27
libraries/Ticker/examples/Arguments/Arguments.ino
Normal file
27
libraries/Ticker/examples/Arguments/Arguments.ino
Normal file
@ -0,0 +1,27 @@
|
||||
#include <Arduino.h>
|
||||
#include <Ticker.h>
|
||||
|
||||
// attach a LED to GPIO 21
|
||||
#define LED_PIN 21
|
||||
|
||||
Ticker tickerSetHigh;
|
||||
Ticker tickerSetLow;
|
||||
|
||||
void setPin(int state) {
|
||||
digitalWrite(LED_PIN, state);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_PIN, 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() {
|
||||
|
||||
}
|
42
libraries/Ticker/examples/Blinker/Blinker.ino
Normal file
42
libraries/Ticker/examples/Blinker/Blinker.ino
Normal file
@ -0,0 +1,42 @@
|
||||
#include <Arduino.h>
|
||||
#include <Ticker.h>
|
||||
|
||||
// attach a LED to pPIO 21
|
||||
#define LED_PIN 21
|
||||
|
||||
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_PIN, !digitalRead(LED_PIN));
|
||||
}
|
||||
|
||||
void toggle() {
|
||||
static bool isBlinking = false;
|
||||
if (isBlinking) {
|
||||
blinker.detach();
|
||||
isBlinking = false;
|
||||
}
|
||||
else {
|
||||
blinker.attach(blinkerPace, blink);
|
||||
isBlinking = true;
|
||||
}
|
||||
digitalWrite(LED_PIN, LOW); //make sure LED on on after toggling (pin LOW = led ON)
|
||||
}
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
toggler.attach(togglePeriod, toggle);
|
||||
changer.once(30, change);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
}
|
14
libraries/Ticker/keywords.txt
Normal file
14
libraries/Ticker/keywords.txt
Normal file
@ -0,0 +1,14 @@
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
Ticker KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
attach KEYWORD2
|
||||
attach_ms KEYWORD2
|
||||
once KEYWORD2
|
||||
detach KEYWORD2
|
9
libraries/Ticker/library.properties
Normal file
9
libraries/Ticker/library.properties
Normal file
@ -0,0 +1,9 @@
|
||||
name=Ticker
|
||||
version=1.1
|
||||
author=Bert Melis
|
||||
maintainer=Hristo Gochkov <hristo@espressif.com>
|
||||
sentence=Allows to call functions with a given interval.
|
||||
paragraph=
|
||||
category=Timing
|
||||
url=
|
||||
architectures=esp32
|
58
libraries/Ticker/src/Ticker.cpp
Normal file
58
libraries/Ticker/src/Ticker.cpp
Normal file
@ -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<void*>(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;
|
||||
}
|
||||
}
|
107
libraries/Ticker/src/Ticker.h
Normal file
107
libraries/Ticker/src/Ticker.h
Normal file
@ -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_with_arg_t>(callback), 0);
|
||||
}
|
||||
|
||||
void attach_ms(uint32_t milliseconds, callback_t callback)
|
||||
{
|
||||
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
|
||||
}
|
||||
|
||||
template<typename TArg>
|
||||
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_with_arg_t>(callback), arg32);
|
||||
}
|
||||
|
||||
template<typename TArg>
|
||||
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_with_arg_t>(callback), arg32);
|
||||
}
|
||||
|
||||
void once(float seconds, callback_t callback)
|
||||
{
|
||||
_attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
|
||||
}
|
||||
|
||||
void once_ms(uint32_t milliseconds, callback_t callback)
|
||||
{
|
||||
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
|
||||
}
|
||||
|
||||
template<typename TArg>
|
||||
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_with_arg_t>(callback), arg32);
|
||||
}
|
||||
|
||||
template<typename TArg>
|
||||
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_with_arg_t>(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
|
Loading…
Reference in New Issue
Block a user