Skip to content

Commit f0d4886

Browse files
committed
Merge branch 'ticker_api'
2 parents f544815 + 28f71f3 commit f0d4886

File tree

7 files changed

+256
-128
lines changed

7 files changed

+256
-128
lines changed

libraries/Ticker/examples/Arguments/Arguments.ino

Lines changed: 0 additions & 27 deletions
This file was deleted.

libraries/Ticker/examples/Blinker/Blinker.ino

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ void toggle() {
2323
if (isBlinking) {
2424
blinker.detach();
2525
isBlinking = false;
26-
}
27-
else {
26+
} else {
2827
blinker.attach(blinkerPace, blink);
2928
isBlinking = true;
3029
}
@@ -38,5 +37,5 @@ void setup() {
3837
}
3938

4039
void loop() {
41-
40+
4241
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Basic Ticker usage
3+
4+
Ticker is an object that will call a given function with a certain period.
5+
Each Ticker calls one function. You can have as many Tickers as you like,
6+
memory being the only limitation.
7+
8+
A function may be attached to a ticker and detached from the ticker.
9+
There are two variants of the attach function: attach and attach_ms.
10+
The first one takes period in seconds, the second one in milliseconds.
11+
12+
The built-in LED will be blinking.
13+
*/
14+
15+
#include <Ticker.h>
16+
17+
#ifndef LED_BUILTIN
18+
#define LED_BUILTIN 13
19+
#endif
20+
21+
Ticker flipper;
22+
23+
int count = 0;
24+
25+
void flip() {
26+
int state = digitalRead(LED_BUILTIN); // get the current state of GPIO1 pin
27+
digitalWrite(LED_BUILTIN, !state); // set pin to the opposite state
28+
29+
++count;
30+
// when the counter reaches a certain value, start blinking like crazy
31+
if (count == 20) {
32+
flipper.attach(0.1, flip);
33+
}
34+
// when the counter reaches yet another value, stop blinking
35+
else if (count == 120) {
36+
flipper.detach();
37+
}
38+
}
39+
40+
void setup() {
41+
pinMode(LED_BUILTIN, OUTPUT);
42+
digitalWrite(LED_BUILTIN, LOW);
43+
44+
// flip the pin every 0.3s
45+
flipper.attach(0.3, flip);
46+
}
47+
48+
void loop() {
49+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Passing paramters to Ticker callbacks
3+
4+
Apart from void(void) functions, the Ticker library supports
5+
functions taking one argument. This argument's size has to be less or
6+
equal to 4 bytes (so char, short, int, float, void*, char* types will do).
7+
8+
This sample runs two tickers that both call one callback function,
9+
but with different arguments.
10+
11+
The built-in LED will be pulsing.
12+
*/
13+
14+
#include <Ticker.h>
15+
16+
#ifndef LED_BUILTIN
17+
#define LED_BUILTIN 13
18+
#endif
19+
20+
Ticker tickerSetHigh;
21+
Ticker tickerSetLow;
22+
23+
void setPin(int state) {
24+
digitalWrite(LED_BUILTIN, state);
25+
}
26+
27+
void setup() {
28+
pinMode(LED_BUILTIN, OUTPUT);
29+
digitalWrite(1, LOW);
30+
31+
// every 25 ms, call setPin(0)
32+
tickerSetLow.attach_ms(25, setPin, 0);
33+
34+
// every 26 ms, call setPin(1)
35+
tickerSetHigh.attach_ms(26, setPin, 1);
36+
}
37+
38+
void loop() {
39+
}

libraries/Ticker/keywords.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ Ticker KEYWORD1
88
# Methods and Functions (KEYWORD2)
99
#######################################
1010

11+
attach_scheduled KEYWORD2
1112
attach KEYWORD2
13+
attach_ms_scheduled KEYWORD2
1214
attach_ms KEYWORD2
15+
once_scheduled KEYWORD2
1316
once KEYWORD2
17+
once_ms_scheduled KEYWORD2
18+
once_ms KEYWORD2
1419
detach KEYWORD2
20+
active KEYWORD2
21+

libraries/Ticker/src/Ticker.cpp

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/*
1+
/*
22
Ticker.cpp - esp32 library that calls functions periodically
33
44
Copyright (c) 2017 Bert Melis. All rights reserved.
5-
5+
66
Based on the original work of:
77
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
88
The original version is part of the esp8266 core for Arduino environment.
@@ -24,35 +24,63 @@
2424

2525
#include "Ticker.h"
2626

27-
Ticker::Ticker() :
28-
_timer(nullptr) {}
27+
Ticker::Ticker()
28+
: _timer(nullptr)
29+
{
30+
}
31+
32+
Ticker::~Ticker()
33+
{
34+
detach();
35+
}
36+
37+
void Ticker::_attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg)
38+
{
39+
_attach_us(1000000 * seconds, repeat, callback, arg);
40+
}
2941

30-
Ticker::~Ticker() {
31-
detach();
42+
void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg)
43+
{
44+
_attach_us(1000 * milliseconds, repeat, callback, arg);
3245
}
3346

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-
}
47+
void Ticker::_attach_us(uint32_t micros, bool repeat, callback_with_arg_t callback, void* arg)
48+
{
49+
esp_timer_create_args_t _timerConfig;
50+
_timerConfig.arg = reinterpret_cast<void*>(arg);
51+
_timerConfig.callback = callback;
52+
_timerConfig.dispatch_method = ESP_TIMER_TASK;
53+
_timerConfig.name = "Ticker";
54+
if (_timer) {
55+
esp_timer_stop(_timer);
56+
esp_timer_delete(_timer);
57+
}
58+
esp_timer_create(&_timerConfig, &_timer);
59+
if (repeat) {
60+
esp_timer_start_periodic(_timer, micros);
61+
}
62+
else {
63+
esp_timer_start_once(_timer, micros);
64+
}
5065
}
5166

5267
void Ticker::detach() {
53-
if (_timer) {
54-
esp_timer_stop(_timer);
55-
esp_timer_delete(_timer);
56-
_timer = nullptr;
57-
}
68+
if (_timer) {
69+
esp_timer_stop(_timer);
70+
esp_timer_delete(_timer);
71+
_timer = nullptr;
72+
_callback_function = nullptr;
73+
}
74+
}
75+
76+
bool Ticker::active() const
77+
{
78+
return _timer;
79+
}
80+
81+
void Ticker::_static_callback(void* arg)
82+
{
83+
Ticker* _this = reinterpret_cast<Ticker*>(arg);
84+
if (_this && _this->_callback_function)
85+
_this->_callback_function();
5886
}

0 commit comments

Comments
 (0)