Skip to content

Commit 780efa9

Browse files
committed
Return Ticker to libraries only for modularity.
1 parent f32096a commit 780efa9

File tree

5 files changed

+49
-247
lines changed

5 files changed

+49
-247
lines changed

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ set(CORE_SRCS
3131
cores/esp32/stdlib_noniso.c
3232
cores/esp32/Stream.cpp
3333
cores/esp32/StreamString.cpp
34-
cores/esp32/Ticker.cpp
3534
cores/esp32/wiring_pulse.c
3635
cores/esp32/wiring_shift.c
3736
cores/esp32/WMath.cpp

cores/esp32/Ticker.cpp

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

cores/esp32/Ticker.h

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

libraries/Ticker/src/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
}

libraries/Ticker/src/Ticker.h

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Ticker
4242
void attach(float seconds, callback_function_t callback)
4343
{
4444
_callback_function = std::move(callback);
45-
_attach_ms(seconds * 1000, true, _static_callback, this);
45+
_attach_s(seconds, true, _static_callback, this);
4646
}
4747

4848
void attach_ms(uint32_t milliseconds, callback_function_t callback)
@@ -51,14 +51,20 @@ class Ticker
5151
_attach_ms(milliseconds, true, _static_callback, this);
5252
}
5353

54+
void attach_us(uint32_t micros, callback_function_t callback)
55+
{
56+
_callback_function = std::move(callback);
57+
_attach_us(micros, true, _static_callback, this);
58+
}
59+
5460
template<typename TArg>
5561
void attach(float seconds, void (*callback)(TArg), TArg arg)
5662
{
5763
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
5864
// C-cast serves two purposes:
5965
// static_cast for smaller integer types,
6066
// reinterpret_cast + const_cast for pointer types
61-
_attach_ms(seconds * 1000, true, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
67+
_attach_s(seconds, true, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
6268
}
6369

6470
template<typename TArg>
@@ -68,10 +74,17 @@ class Ticker
6874
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
6975
}
7076

77+
template<typename TArg>
78+
void attach_us(uint32_t micros, void (*callback)(TArg), TArg arg)
79+
{
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), (void*)arg);
82+
}
83+
7184
void once(float seconds, callback_function_t callback)
7285
{
7386
_callback_function = std::move(callback);
74-
_attach_ms(seconds * 1000, false, _static_callback, this);
87+
_attach_s(seconds, false, _static_callback, this);
7588
}
7689

7790
void once_ms(uint32_t milliseconds, callback_function_t callback)
@@ -80,11 +93,17 @@ class Ticker
8093
_attach_ms(milliseconds, false, _static_callback, this);
8194
}
8295

96+
void once_us(uint32_t micros, callback_function_t callback)
97+
{
98+
_callback_function = std::move(callback);
99+
_attach_us(micros, false, _static_callback, this);
100+
}
101+
83102
template<typename TArg>
84103
void once(float seconds, void (*callback)(TArg), TArg arg)
85104
{
86105
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
87-
_attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
106+
_attach_s(seconds, false, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
88107
}
89108

90109
template<typename TArg>
@@ -94,6 +113,13 @@ class Ticker
94113
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
95114
}
96115

116+
template<typename TArg>
117+
void once_us(uint32_t micros, void (*callback)(TArg), TArg arg)
118+
{
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), (void*)arg);
121+
}
122+
97123
void detach();
98124
bool active() const;
99125

@@ -103,8 +129,11 @@ class Ticker
103129

104130
callback_function_t _callback_function = nullptr;
105131

106-
protected:
107132
esp_timer_handle_t _timer;
133+
134+
private:
135+
void _attach_us(uint32_t micros, bool repeat, callback_with_arg_t callback, void* arg);
136+
void _attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg);
108137
};
109138

110139

0 commit comments

Comments
 (0)