Skip to content

Commit 139acbe

Browse files
committed
Merge branch 'ticker_api' into add-schedule
2 parents 6707de3 + f32096a commit 139acbe

File tree

2 files changed

+60
-10
lines changed

2 files changed

+60
-10
lines changed

cores/esp32/Ticker.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,17 @@ Ticker::~Ticker()
3434
detach();
3535
}
3636

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+
}
41+
3742
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);
45+
}
46+
47+
void Ticker::_attach_us(uint32_t micros, bool repeat, callback_with_arg_t callback, void* arg)
3848
{
3949
esp_timer_create_args_t _timerConfig;
4050
_timerConfig.arg = reinterpret_cast<void*>(arg);
@@ -47,10 +57,10 @@ void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t
4757
}
4858
esp_timer_create(&_timerConfig, &_timer);
4959
if (repeat) {
50-
esp_timer_start_periodic(_timer, milliseconds * 1000);
60+
esp_timer_start_periodic(_timer, micros);
5161
}
5262
else {
53-
esp_timer_start_once(_timer, milliseconds * 1000);
63+
esp_timer_start_once(_timer, micros);
5464
}
5565
}
5666

@@ -59,6 +69,7 @@ void Ticker::detach() {
5969
esp_timer_stop(_timer);
6070
esp_timer_delete(_timer);
6171
_timer = nullptr;
72+
_callback_function = nullptr;
6273
}
6374
}
6475

@@ -70,6 +81,6 @@ bool Ticker::active() const
7081
void Ticker::_static_callback(void* arg)
7182
{
7283
Ticker* _this = reinterpret_cast<Ticker*>(arg);
73-
if (!_this) return;
74-
if (_this->_callback_function) _this->_callback_function();
84+
if (_this && _this->_callback_function)
85+
_this->_callback_function();
7586
}

cores/esp32/Ticker.h

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Ticker
4848
void attach(float seconds, callback_function_t callback)
4949
{
5050
_callback_function = std::move(callback);
51-
_attach_ms(seconds * 1000, true, _static_callback, this);
51+
_attach_s(seconds, true, _static_callback, this);
5252
}
5353

5454
void attach_ms_scheduled(uint32_t milliseconds, callback_function_t callback)
@@ -62,14 +62,25 @@ class Ticker
6262
_attach_ms(milliseconds, true, _static_callback, this);
6363
}
6464

65+
void attach_us_scheduled(uint32_t micros, callback_function_t callback)
66+
{
67+
attach_us(micros, [callback]() { schedule_function(callback); });
68+
}
69+
70+
void attach_us(uint32_t micros, callback_function_t callback)
71+
{
72+
_callback_function = std::move(callback);
73+
_attach_us(micros, true, _static_callback, this);
74+
}
75+
6576
template<typename TArg>
6677
void attach(float seconds, void (*callback)(TArg), TArg arg)
6778
{
6879
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
6980
// C-cast serves two purposes:
7081
// static_cast for smaller integer types,
7182
// reinterpret_cast + const_cast for pointer types
72-
_attach_ms(seconds * 1000, true, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
83+
_attach_s(seconds, true, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
7384
}
7485

7586
template<typename TArg>
@@ -79,15 +90,22 @@ class Ticker
7990
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
8091
}
8192

93+
template<typename TArg>
94+
void attach_us(uint32_t micros, void (*callback)(TArg), TArg arg)
95+
{
96+
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
97+
_attach_us(micros, true, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
98+
}
99+
82100
void once_scheduled(float seconds, callback_function_t callback)
83101
{
84102
once(seconds, [callback]() { schedule_function(callback); });
85103
}
86-
104+
87105
void once(float seconds, callback_function_t callback)
88106
{
89107
_callback_function = std::move(callback);
90-
_attach_ms(seconds * 1000, false, _static_callback, this);
108+
_attach_s(seconds, false, _static_callback, this);
91109
}
92110

93111
void once_ms_scheduled(uint32_t milliseconds, callback_function_t callback)
@@ -101,11 +119,22 @@ class Ticker
101119
_attach_ms(milliseconds, false, _static_callback, this);
102120
}
103121

122+
void once_us_scheduled(uint32_t micros, callback_function_t callback)
123+
{
124+
once_us(micros, [callback]() { schedule_function(callback); });
125+
}
126+
127+
void once_us(uint32_t micros, callback_function_t callback)
128+
{
129+
_callback_function = std::move(callback);
130+
_attach_us(micros, false, _static_callback, this);
131+
}
132+
104133
template<typename TArg>
105134
void once(float seconds, void (*callback)(TArg), TArg arg)
106135
{
107136
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
108-
_attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
137+
_attach_s(seconds, false, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
109138
}
110139

111140
template<typename TArg>
@@ -115,6 +144,13 @@ class Ticker
115144
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
116145
}
117146

147+
template<typename TArg>
148+
void once_us(uint32_t micros, void (*callback)(TArg), TArg arg)
149+
{
150+
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
151+
_attach_us(micros, false, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
152+
}
153+
118154
void detach();
119155
bool active() const;
120156

@@ -124,8 +160,11 @@ class Ticker
124160

125161
callback_function_t _callback_function = nullptr;
126162

127-
protected:
128163
esp_timer_handle_t _timer;
164+
165+
private:
166+
void _attach_us(uint32_t micros, bool repeat, callback_with_arg_t callback, void* arg);
167+
void _attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg);
129168
};
130169

131170

0 commit comments

Comments
 (0)