Skip to content

Commit 5972c52

Browse files
committed
Ticker and Schedule updated from ESP8266 Arduino
squash! Ticker and Schedule updated from ESP8266 Arduino
1 parent adcc380 commit 5972c52

File tree

3 files changed

+208
-1
lines changed

3 files changed

+208
-1
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ set(CORE_SRCS
2929
cores/esp32/MD5Builder.cpp
3030
cores/esp32/Print.cpp
3131
cores/esp32/stdlib_noniso.c
32-
cores/esp32/Schedule.cpp
3332
cores/esp32/Stream.cpp
3433
cores/esp32/StreamString.cpp
34+
cores/esp32/Ticker.cpp
3535
cores/esp32/wiring_pulse.c
3636
cores/esp32/wiring_shift.c
3737
cores/esp32/WMath.cpp

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

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

0 commit comments

Comments
 (0)