Skip to content

Commit 7bc9250

Browse files
committed
Update Ticker API to compatibility with ESP8266, prepares for co-op loop Scheduler
1 parent c2b3f2d commit 7bc9250

File tree

5 files changed

+305
-99
lines changed

5 files changed

+305
-99
lines changed

CMakeLists.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ set(CORE_SRCS
3131
cores/esp32/stdlib_noniso.c
3232
cores/esp32/Stream.cpp
3333
cores/esp32/StreamString.cpp
34+
cores/esp32/Ticker.cpp
3435
cores/esp32/wiring_pulse.c
3536
cores/esp32/wiring_shift.c
3637
cores/esp32/WMath.cpp
@@ -58,7 +59,6 @@ set(LIBRARY_SRCS
5859
libraries/SimpleBLE/src/SimpleBLE.cpp
5960
libraries/SPIFFS/src/SPIFFS.cpp
6061
libraries/SPI/src/SPI.cpp
61-
libraries/Ticker/src/Ticker.cpp
6262
libraries/Update/src/Updater.cpp
6363
libraries/WebServer/src/WebServer.cpp
6464
libraries/WebServer/src/Parsing.cpp
@@ -195,7 +195,6 @@ set(COMPONENT_ADD_INCLUDEDIRS
195195
libraries/SimpleBLE/src
196196
libraries/SPIFFS/src
197197
libraries/SPI/src
198-
libraries/Ticker/src
199198
libraries/Update/src
200199
libraries/WebServer/src
201200
libraries/WiFiClientSecure/src

cores/esp32/Ticker.cpp

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
}
31+
32+
Ticker::~Ticker()
33+
{
34+
detach();
35+
}
36+
37+
void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg)
38+
{
39+
esp_timer_create_args_t _timerConfig;
40+
_timerConfig.arg = reinterpret_cast<void*>(arg);
41+
_timerConfig.callback = callback;
42+
_timerConfig.dispatch_method = ESP_TIMER_TASK;
43+
_timerConfig.name = "Ticker";
44+
if (_timer) {
45+
esp_timer_stop(_timer);
46+
esp_timer_delete(_timer);
47+
}
48+
esp_timer_create(&_timerConfig, &_timer);
49+
if (repeat) {
50+
esp_timer_start_periodic(_timer, milliseconds * 1000);
51+
}
52+
else {
53+
esp_timer_start_once(_timer, milliseconds * 1000);
54+
}
55+
}
56+
57+
void Ticker::detach() {
58+
if (_timer) {
59+
esp_timer_stop(_timer);
60+
esp_timer_delete(_timer);
61+
_timer = nullptr;
62+
}
63+
}
64+
65+
bool Ticker::active() const
66+
{
67+
return _timer;
68+
}
69+
70+
void Ticker::_static_callback(void* arg)
71+
{
72+
Ticker* _this = reinterpret_cast<Ticker*>(arg);
73+
if (!_this) return;
74+
if (_this->_callback_function) _this->_callback_function();
75+
}

cores/esp32/Ticker.h

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
#include <functional>
32+
33+
class Ticker
34+
{
35+
public:
36+
Ticker();
37+
~Ticker();
38+
39+
typedef void (*callback_with_arg_t)(void*);
40+
typedef std::function<void(void)> callback_function_t;
41+
42+
void attach(float seconds, callback_function_t callback)
43+
{
44+
_callback_function = std::move(callback);
45+
_attach_ms(seconds * 1000, true, _static_callback, this);
46+
}
47+
48+
void attach_ms(uint32_t milliseconds, callback_function_t callback)
49+
{
50+
_callback_function = std::move(callback);
51+
_attach_ms(milliseconds, true, _static_callback, this);
52+
}
53+
54+
template<typename TArg>
55+
void attach(float seconds, void (*callback)(TArg), TArg arg)
56+
{
57+
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
58+
// C-cast serves two purposes:
59+
// static_cast for smaller integer types,
60+
// reinterpret_cast + const_cast for pointer types
61+
_attach_ms(seconds * 1000, true, callback, arg);
62+
}
63+
64+
template<typename TArg>
65+
void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
66+
{
67+
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
68+
_attach_ms(milliseconds, true, callback, arg);
69+
}
70+
71+
void once(float seconds, callback_function_t callback)
72+
{
73+
_callback_function = std::move(callback);
74+
_attach_ms(seconds * 1000, false, _static_callback, this);
75+
}
76+
77+
void once_ms(uint32_t milliseconds, callback_function_t callback)
78+
{
79+
_callback_function = std::move(callback);
80+
_attach_ms(milliseconds, false, _static_callback, this);
81+
}
82+
83+
template<typename TArg>
84+
void once(float seconds, void (*callback)(TArg), TArg arg)
85+
{
86+
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
87+
_attach_ms(seconds * 1000, false, callback, arg);
88+
}
89+
90+
template<typename TArg>
91+
void once_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
92+
{
93+
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
94+
_attach_ms(milliseconds, false, callback, arg);
95+
}
96+
97+
void detach();
98+
bool active() const;
99+
100+
protected:
101+
void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg);
102+
static void _static_callback(void* arg);
103+
104+
callback_function_t _callback_function = nullptr;
105+
106+
protected:
107+
esp_timer_handle_t _timer;
108+
};
109+
110+
111+
#endif//TICKER_H

libraries/Ticker/src/Ticker.cpp

+44-27
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,52 @@
2424

2525
#include "Ticker.h"
2626

27-
Ticker::Ticker() :
28-
_timer(nullptr) {}
27+
Ticker::Ticker()
28+
: _timer(nullptr)
29+
{
30+
}
2931

30-
Ticker::~Ticker() {
31-
detach();
32+
Ticker::~Ticker()
33+
{
34+
detach();
3235
}
3336

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 * 1000ULL);
47-
} else {
48-
esp_timer_start_once(_timer, milliseconds * 1000ULL);
49-
}
37+
void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg)
38+
{
39+
esp_timer_create_args_t _timerConfig;
40+
_timerConfig.arg = reinterpret_cast<void*>(arg);
41+
_timerConfig.callback = callback;
42+
_timerConfig.dispatch_method = ESP_TIMER_TASK;
43+
_timerConfig.name = "Ticker";
44+
if (_timer) {
45+
esp_timer_stop(_timer);
46+
esp_timer_delete(_timer);
47+
}
48+
esp_timer_create(&_timerConfig, &_timer);
49+
if (repeat) {
50+
esp_timer_start_periodic(_timer, milliseconds * 1000ULL);
51+
}
52+
else {
53+
esp_timer_start_once(_timer, milliseconds * 1000ULL);
54+
}
5055
}
5156

5257
void Ticker::detach() {
53-
if (_timer) {
54-
esp_timer_stop(_timer);
55-
esp_timer_delete(_timer);
56-
_timer = nullptr;
57-
}
58+
if (_timer) {
59+
esp_timer_stop(_timer);
60+
esp_timer_delete(_timer);
61+
_timer = nullptr;
62+
}
63+
}
64+
65+
bool Ticker::active() const
66+
{
67+
return _timer;
68+
}
69+
70+
void Ticker::_static_callback(void* arg)
71+
{
72+
Ticker* _this = reinterpret_cast<Ticker*>(arg);
73+
if (!_this) return;
74+
if (_this->_callback_function) _this->_callback_function();
5875
}

0 commit comments

Comments
 (0)