Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f764af0

Browse files
dok-netlucasssvaz
andauthored
Ticker updated to match extensions in ESP8266 API (#2849)
* Update Ticker API to compatibility with ESP8266, prepares for co-op loop Scheduler * Fixing Build server complaints * Fix omitted casts in template member function * Changes after review * Expose μs resolution of OS API in Ticker class * Return Ticker to libraries only for modularity. * Unify Ticker examples. * Default for LED_BUILTIN * In Ticker, the *scheduled functions become available in another development branch. * Astyle from ESP8266 * Fixed Arduino keywords.txt * 64bit integers instead of 32bits, timer functions on ESP32 accept 64bit integers. * Move code from header into compiliation unit. Reformat. * Test case same as ESP8266 * Implementing inline in header saves 204+ bytes program size. * Examples * Fix a compiler warning due to c-style casting. * Revert formatting changes * More format reversions * Revert * Revert * Revert --------- Co-authored-by: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com>
1 parent ead76fd commit f764af0

File tree

6 files changed

+177
-81
lines changed

6 files changed

+177
-81
lines changed

‎libraries/Ticker/examples/Arguments/Arguments.ino‎

Lines changed: 0 additions & 51 deletions
This file was deleted.
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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 tickerSetLow;
21+
Ticker tickerSetHigh;
22+
Ticker tickerSetChar;
23+
24+
void setPinLow() {
25+
digitalWrite(LED_BUILTIN, 0);
26+
}
27+
28+
void setPinHigh() {
29+
digitalWrite(LED_BUILTIN, 1);
30+
}
31+
32+
void setPin(int state) {
33+
digitalWrite(LED_BUILTIN, state);
34+
}
35+
36+
void setPinChar(char state) {
37+
digitalWrite(LED_BUILTIN, state);
38+
}
39+
40+
void setup() {
41+
pinMode(LED_BUILTIN, OUTPUT);
42+
43+
// every 25 ms, call setPinLow()
44+
tickerSetLow.attach_ms(25, setPinLow);
45+
46+
// every 26 ms, call setPinHigh()
47+
tickerSetHigh.attach_ms(26, setPinHigh);
48+
49+
// every 54 ms, call setPinChar(1)
50+
tickerSetChar.attach_ms(26, setPinChar, (char)1);
51+
}
52+
53+
void loop() {
54+
}

‎libraries/Ticker/keywords.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@ Ticker KEYWORD1
1010

1111
attach KEYWORD2
1212
attach_ms KEYWORD2
13+
attach_us KEYWORD2
1314
once KEYWORD2
15+
once_ms KEYWORD2
16+
once_us KEYWORD2
1417
detach KEYWORD2
18+
active KEYWORD2

‎libraries/Ticker/src/Ticker.cpp‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Ticker::~Ticker() {
3131
detach();
3232
}
3333

34-
void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg) {
34+
void Ticker::_attach_us(uint64_t micros, bool repeat, callback_with_arg_t callback, void* arg) {
3535
esp_timer_create_args_t _timerConfig;
3636
_timerConfig.arg = reinterpret_cast<void*>(arg);
3737
_timerConfig.callback = callback;
@@ -43,9 +43,9 @@ void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t
4343
}
4444
esp_timer_create(&_timerConfig, &_timer);
4545
if (repeat) {
46-
esp_timer_start_periodic(_timer, milliseconds * 1000ULL);
46+
esp_timer_start_periodic(_timer, micros);
4747
} else {
48-
esp_timer_start_once(_timer, milliseconds * 1000ULL);
48+
esp_timer_start_once(_timer, micros);
4949
}
5050
}
5151

@@ -54,10 +54,19 @@ void Ticker::detach() {
5454
esp_timer_stop(_timer);
5555
esp_timer_delete(_timer);
5656
_timer = nullptr;
57+
_callback_function = nullptr;
5758
}
5859
}
5960

60-
bool Ticker::active() {
61+
bool Ticker::active() const{
6162
if (!_timer) return false;
6263
return esp_timer_is_active(_timer);
6364
}
65+
66+
void Ticker::_static_callback(void* arg)
67+
{
68+
Ticker* _this = reinterpret_cast<Ticker*>(arg);
69+
if (_this && _this->_callback_function)
70+
_this->_callback_function();
71+
}
72+

‎libraries/Ticker/src/Ticker.h‎

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,79 +28,110 @@
2828
extern "C" {
2929
#include "esp_timer.h"
3030
}
31+
#include <functional>
3132

3233
class Ticker
3334
{
3435
public:
3536
Ticker();
3637
~Ticker();
37-
typedefvoid (*callback_t)(void);
38+
3839
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_us(1000000ULL * seconds, true, _static_callback, this);
46+
}
3947

40-
void attach(float seconds, callback_t callback)
48+
void attach_ms(uint64_t milliseconds, callback_function_t callback)
4149
{
42-
_attach_ms(seconds * 1000, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
50+
_callback_function = std::move(callback);
51+
_attach_us(1000ULL * milliseconds, true, _static_callback, this);
4352
}
4453

45-
void attach_ms(uint32_t milliseconds, callback_t callback)
54+
void attach_us(uint64_t micros, callback_function_t callback)
4655
{
47-
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
56+
_callback_function = std::move(callback);
57+
_attach_us(micros, true, _static_callback, this);
4858
}
4959

5060
template<typename TArg>
5161
void attach(float seconds, void (*callback)(TArg), TArg arg)
5262
{
53-
static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach() callback argument size must be <= 4 bytes");
63+
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
5464
// C-cast serves two purposes:
5565
// static_cast for smaller integer types,
5666
// 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);
67+
_attach_us(1000000ULL * seconds, true, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void*>(arg));
68+
}
69+
70+
template<typename TArg>
71+
void attach_ms(uint64_t milliseconds, void (*callback)(TArg), TArg arg)
72+
{
73+
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
74+
_attach_us(1000ULL * milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void*>(arg));
5975
}
6076

6177
template<typename TArg>
62-
void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
78+
void attach_us(uint64_t micros, void (*callback)(TArg), TArg arg)
6379
{
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);
80+
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
81+
_attach_us(micros, true, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void*>(arg));
6782
}
6883

69-
void once(float seconds, callback_t callback)
84+
void once(float seconds, callback_function_t callback)
7085
{
71-
_attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
86+
_callback_function = std::move(callback);
87+
_attach_us(1000000ULL * seconds, false, _static_callback, this);
7288
}
7389

74-
void once_ms(uint32_t milliseconds, callback_t callback)
90+
void once_ms(uint64_t milliseconds, callback_function_t callback)
7591
{
76-
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
92+
_callback_function = std::move(callback);
93+
_attach_us(1000ULL * milliseconds, false, _static_callback, this);
94+
}
95+
96+
void once_us(uint64_t micros, callback_function_t callback)
97+
{
98+
_callback_function = std::move(callback);
99+
_attach_us(micros, false, _static_callback, this);
77100
}
78101

79102
template<typename TArg>
80103
void once(float seconds, void (*callback)(TArg), TArg arg)
81104
{
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);
105+
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
106+
_attach_us(1000000ULL * seconds, false, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void*>(arg));
107+
}
108+
109+
template<typename TArg>
110+
void once_ms(uint64_t milliseconds, void (*callback)(TArg), TArg arg)
111+
{
112+
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
113+
_attach_us(1000ULL * milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void*>(arg));
85114
}
86115

87116
template<typename TArg>
88-
void once_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
117+
void once_us(uint64_t micros, void (*callback)(TArg), TArg arg)
89118
{
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);
119+
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
120+
_attach_us(micros, false, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void*>(arg));
93121
}
94122

95123
void detach();
96-
bool active();
124+
bool active()const;
97125

98126
protected:
99-
void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg);
127+
staticvoid _static_callback(void* arg);
100128

129+
callback_function_t _callback_function = nullptr;
101130

102-
protected:
103131
esp_timer_handle_t _timer;
132+
133+
private:
134+
void _attach_us(uint64_t micros, bool repeat, callback_with_arg_t callback, void* arg);
104135
};
105136

106137

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /