3.3.7
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
#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() {}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
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.h>
|
||||
|
||||
#ifndef LED_BUILTIN
|
||||
#define LED_BUILTIN 13
|
||||
#endif
|
||||
|
||||
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() {}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
Passing parameters 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.h>
|
||||
|
||||
#ifndef LED_BUILTIN
|
||||
#define LED_BUILTIN 13
|
||||
#endif
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void setPinChar(char state) {
|
||||
digitalWrite(LED_BUILTIN, state);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
|
||||
// every 25 ms, call setPinLow()
|
||||
tickerSetLow.attach_ms(25, setPinLow);
|
||||
|
||||
// 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() {}
|
||||
@@ -0,0 +1,21 @@
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
Ticker KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
attach KEYWORD2
|
||||
attach_ms KEYWORD2
|
||||
attach_us KEYWORD2
|
||||
once KEYWORD2
|
||||
once_ms KEYWORD2
|
||||
once_us KEYWORD2
|
||||
restart KEYWORD2
|
||||
restart_ms KEYWORD2
|
||||
restart_us KEYWORD2
|
||||
detach KEYWORD2
|
||||
active KEYWORD2
|
||||
@@ -0,0 +1,9 @@
|
||||
name=Ticker
|
||||
version=3.3.7
|
||||
author=Bert Melis
|
||||
maintainer=Hristo Gochkov <hristo@espressif.com>
|
||||
sentence=Allows to call functions with a given interval.
|
||||
paragraph=
|
||||
category=Timing
|
||||
url=
|
||||
architectures=esp32
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
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_us(uint64_t micros, bool repeat, callback_with_arg_t callback, void *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, 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 {
|
||||
if (!_timer) {
|
||||
return false;
|
||||
}
|
||||
return esp_timer_is_active(_timer);
|
||||
}
|
||||
|
||||
void Ticker::_static_callback(void *arg) {
|
||||
Ticker *_this = reinterpret_cast<Ticker *>(arg);
|
||||
if (_this && _this->_callback_function) {
|
||||
_this->_callback_function();
|
||||
}
|
||||
}
|
||||
|
||||
void Ticker::restart(float seconds) {
|
||||
if (active()) {
|
||||
esp_timer_restart(_timer, 1000000ULL * seconds);
|
||||
}
|
||||
}
|
||||
|
||||
void Ticker::restart_ms(uint64_t milliseconds) {
|
||||
if (active()) {
|
||||
esp_timer_restart(_timer, 1000ULL * milliseconds);
|
||||
}
|
||||
}
|
||||
|
||||
void Ticker::restart_us(uint64_t micros) {
|
||||
if (active()) {
|
||||
esp_timer_restart(_timer, micros);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
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 <functional>
|
||||
|
||||
class Ticker {
|
||||
public:
|
||||
Ticker();
|
||||
~Ticker();
|
||||
|
||||
typedef void (*callback_with_arg_t)(void *);
|
||||
typedef std::function<void(void)> 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<typename TArg> 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_with_arg_t>(callback), reinterpret_cast<void *>(arg));
|
||||
}
|
||||
|
||||
template<typename TArg> 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_with_arg_t>(callback), reinterpret_cast<void *>(arg));
|
||||
}
|
||||
|
||||
template<typename TArg> 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_with_arg_t>(callback), reinterpret_cast<void *>(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<typename TArg> 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_with_arg_t>(callback), reinterpret_cast<void *>(arg));
|
||||
}
|
||||
|
||||
template<typename TArg> 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_with_arg_t>(callback), reinterpret_cast<void *>(arg));
|
||||
}
|
||||
|
||||
template<typename TArg> 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_with_arg_t>(callback), reinterpret_cast<void *>(arg));
|
||||
}
|
||||
|
||||
void restart(float seconds);
|
||||
void restart_ms(uint64_t milliseconds);
|
||||
void restart_us(uint64_t micros);
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
#endif // TICKER_H
|
||||
Reference in New Issue
Block a user