Skip to content

Commit 8e29347

Browse files
bertmelisme-no-dev
authored andcommitted
Add Ticker library (#1057)
* Add Ticker library * Fix CI (#1) remove LED_BUILTIN in examples
1 parent f5c2aff commit 8e29347

File tree

6 files changed

+257
-0
lines changed

6 files changed

+257
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <Arduino.h>
2+
#include <Ticker.h>
3+
4+
// attach a LED to GPIO 21
5+
#define LED_PIN 21
6+
7+
Ticker tickerSetHigh;
8+
Ticker tickerSetLow;
9+
10+
void setPin(int state) {
11+
digitalWrite(LED_PIN, state);
12+
}
13+
14+
void setup() {
15+
pinMode(LED_PIN, OUTPUT);
16+
digitalWrite(1, LOW);
17+
18+
// every 25 ms, call setPin(0)
19+
tickerSetLow.attach_ms(25, setPin, 0);
20+
21+
// every 26 ms, call setPin(1)
22+
tickerSetHigh.attach_ms(26, setPin, 1);
23+
}
24+
25+
void loop() {
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <Arduino.h>
2+
#include <Ticker.h>
3+
4+
// attach a LED to pPIO 21
5+
#define LED_PIN 21
6+
7+
Ticker blinker;
8+
Ticker toggler;
9+
Ticker changer;
10+
float blinkerPace = 0.1; //seconds
11+
const float togglePeriod = 5; //seconds
12+
13+
void change() {
14+
blinkerPace = 0.5;
15+
}
16+
17+
void blink() {
18+
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
19+
}
20+
21+
void toggle() {
22+
static bool isBlinking = false;
23+
if (isBlinking) {
24+
blinker.detach();
25+
isBlinking = false;
26+
}
27+
else {
28+
blinker.attach(blinkerPace, blink);
29+
isBlinking = true;
30+
}
31+
digitalWrite(LED_PIN, LOW); //make sure LED on on after toggling (pin LOW = led ON)
32+
}
33+
34+
void setup() {
35+
pinMode(LED_PIN, OUTPUT);
36+
toggler.attach(togglePeriod, toggle);
37+
changer.once(30, change);
38+
}
39+
40+
void loop() {
41+
42+
}

libraries/Ticker/keywords.txt

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#######################################
2+
# Datatypes (KEYWORD1)
3+
#######################################
4+
5+
Ticker KEYWORD1
6+
7+
#######################################
8+
# Methods and Functions (KEYWORD2)
9+
#######################################
10+
11+
attach KEYWORD2
12+
attach_ms KEYWORD2
13+
once KEYWORD2
14+
detach KEYWORD2

libraries/Ticker/library.properties

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=Ticker
2+
version=1.1
3+
author=Bert Melis
4+
maintainer=Hristo Gochkov <[email protected]>
5+
sentence=Allows to call functions with a given interval.
6+
paragraph=
7+
category=Timing
8+
url=
9+
architectures=esp32

libraries/Ticker/src/Ticker.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Ticker.cpp - esp32 library that calls functions periodically
3+
4+
Copyright (c) 2017 Bert Melis. All rights reserved.
5+
6+
Based on the original work of:
7+
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
8+
The original version is part of the esp8266 core for Arduino environment.
9+
10+
This library is free software; you can redistribute it and/or
11+
modify it under the terms of the GNU Lesser General Public
12+
License as published by the Free Software Foundation; either
13+
version 2.1 of the License, or (at your option) any later version.
14+
15+
This library is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18+
Lesser General Public License for more details.
19+
20+
You should have received a copy of the GNU Lesser General Public
21+
License along with this library; if not, write to the Free Software
22+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23+
*/
24+
25+
#include "Ticker.h"
26+
27+
Ticker::Ticker() :
28+
_timer(nullptr) {}
29+
30+
Ticker::~Ticker() {
31+
detach();
32+
}
33+
34+
void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg) {
35+
esp_timer_create_args_t _timerConfig;
36+
_timerConfig.arg = reinterpret_cast<void*>(arg);
37+
_timerConfig.callback = callback;
38+
_timerConfig.dispatch_method = ESP_TIMER_TASK;
39+
_timerConfig.name = "Ticker";
40+
if (_timer) {
41+
esp_timer_stop(_timer);
42+
esp_timer_delete(_timer);
43+
}
44+
esp_timer_create(&_timerConfig, &_timer);
45+
if (repeat) {
46+
esp_timer_start_periodic(_timer, milliseconds * 1000);
47+
} else {
48+
esp_timer_start_once(_timer, milliseconds * 1000);
49+
}
50+
}
51+
52+
void Ticker::detach() {
53+
if (_timer) {
54+
esp_timer_stop(_timer);
55+
esp_timer_delete(_timer);
56+
_timer = nullptr;
57+
}
58+
}

libraries/Ticker/src/Ticker.h

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
Ticker.h - esp32 library that calls functions periodically
3+
4+
Copyright (c) 2017 Bert Melis. All rights reserved.
5+
6+
Based on the original work of:
7+
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
8+
The original version is part of the esp8266 core for Arduino environment.
9+
10+
This library is free software; you can redistribute it and/or
11+
modify it under the terms of the GNU Lesser General Public
12+
License as published by the Free Software Foundation; either
13+
version 2.1 of the License, or (at your option) any later version.
14+
15+
This library is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18+
Lesser General Public License for more details.
19+
20+
You should have received a copy of the GNU Lesser General Public
21+
License along with this library; if not, write to the Free Software
22+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23+
*/
24+
25+
#ifndef TICKER_H
26+
#define TICKER_H
27+
28+
extern "C" {
29+
#include "esp_timer.h"
30+
}
31+
32+
class Ticker
33+
{
34+
public:
35+
Ticker();
36+
~Ticker();
37+
typedef void (*callback_t)(void);
38+
typedef void (*callback_with_arg_t)(void*);
39+
40+
void attach(float seconds, callback_t callback)
41+
{
42+
_attach_ms(seconds * 1000, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
43+
}
44+
45+
void attach_ms(uint32_t milliseconds, callback_t callback)
46+
{
47+
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
48+
}
49+
50+
template<typename TArg>
51+
void attach(float seconds, void (*callback)(TArg), TArg arg)
52+
{
53+
static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach() callback argument size must be <= 4 bytes");
54+
// C-cast serves two purposes:
55+
// static_cast for smaller integer types,
56+
// reinterpret_cast + const_cast for pointer types
57+
uint32_t arg32 = (uint32_t)arg;
58+
_attach_ms(seconds * 1000, true, reinterpret_cast<callback_with_arg_t>(callback), arg32);
59+
}
60+
61+
template<typename TArg>
62+
void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
63+
{
64+
static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach_ms() callback argument size must be <= 4 bytes");
65+
uint32_t arg32 = (uint32_t)arg;
66+
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), arg32);
67+
}
68+
69+
void once(float seconds, callback_t callback)
70+
{
71+
_attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
72+
}
73+
74+
void once_ms(uint32_t milliseconds, callback_t callback)
75+
{
76+
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
77+
}
78+
79+
template<typename TArg>
80+
void once(float seconds, void (*callback)(TArg), TArg arg)
81+
{
82+
static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach() callback argument size must be <= 4 bytes");
83+
uint32_t arg32 = (uint32_t)(arg);
84+
_attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), arg32);
85+
}
86+
87+
template<typename TArg>
88+
void once_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
89+
{
90+
static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach_ms() callback argument size must be <= 4 bytes");
91+
uint32_t arg32 = (uint32_t)(arg);
92+
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), arg32);
93+
}
94+
95+
void detach();
96+
bool active();
97+
98+
protected:
99+
void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg);
100+
101+
102+
protected:
103+
esp_timer_handle_t _timer;
104+
};
105+
106+
107+
#endif // TICKER_H

0 commit comments

Comments
 (0)