Skip to content

Commit 12c27a4

Browse files
committed
Ticker ported to Delegate
1 parent 8e75ef9 commit 12c27a4

File tree

2 files changed

+38
-51
lines changed

2 files changed

+38
-51
lines changed

libraries/Ticker/src/Ticker.cpp

+2-10
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Ticker::~Ticker()
3333
detach();
3434
}
3535

36-
void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg)
36+
void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t* callback, void* arg)
3737
{
3838
if (_timer)
3939
{
@@ -43,7 +43,6 @@ void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t
4343
{
4444
_timer = &_etsTimer;
4545
}
46-
4746
os_timer_setfn(_timer, callback, arg);
4847
os_timer_arm(_timer, milliseconds, repeat);
4948
}
@@ -55,17 +54,10 @@ void Ticker::detach()
5554

5655
os_timer_disarm(_timer);
5756
_timer = nullptr;
58-
_callback_function = nullptr;
57+
_callback = nullptr;
5958
}
6059

6160
bool Ticker::active() const
6261
{
6362
return _timer;
6463
}
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-
}

libraries/Ticker/src/Ticker.h

+36-41
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#ifndef TICKER_H
2323
#define TICKER_H
2424

25-
#include <functional>
25+
#include <Delegate.h>
2626
#include <Schedule.h>
2727
#include <ets_sys.h>
2828

@@ -32,42 +32,42 @@ class Ticker
3232
Ticker();
3333
~Ticker();
3434

35-
typedef void (*callback_with_arg_t)(void*);
36-
typedef std::function<void(void)> callback_function_t;
35+
using callback_t = Delegate<void(), void*>;
36+
using callback_with_arg_t = void(void*);
3737

3838
// callback will be called at following loop() after ticker fires
39-
void attach_scheduled(float seconds, callback_function_t callback)
39+
void attach_scheduled(float seconds, callback_t callback)
4040
{
41-
_callback_function = [callback]() { schedule_function(callback); };
42-
_attach_ms(1000UL * seconds, true);
41+
_callback = [callback]() { schedule_function(callback); };
42+
_attach_ms(1000UL * seconds, true, _callback, _callback.arg());
4343
}
4444

4545
// callback will be called in SYS ctx when ticker fires
46-
void attach(float seconds, callback_function_t callback)
46+
void attach(float seconds, callback_t callback)
4747
{
48-
_callback_function = std::move(callback);
49-
_attach_ms(1000UL * seconds, true);
48+
_callback = std::move(callback);
49+
_attach_ms(1000UL * seconds, true, _callback, _callback.arg());
5050
}
5151

5252
// callback will be called at following loop() after ticker fires
53-
void attach_ms_scheduled(uint32_t milliseconds, callback_function_t callback)
53+
void attach_ms_scheduled(uint32_t milliseconds, callback_t callback)
5454
{
55-
_callback_function = [callback]() { schedule_function(callback); };
56-
_attach_ms(milliseconds, true);
55+
_callback = [callback]() { schedule_function(callback); };
56+
_attach_ms(milliseconds, true, _callback, _callback.arg());
5757
}
5858

5959
// callback will be called at following yield() after ticker fires
60-
void attach_ms_scheduled_accurate(uint32_t milliseconds, callback_function_t callback)
60+
void attach_ms_scheduled_accurate(uint32_t milliseconds, callback_t callback)
6161
{
62-
_callback_function = [callback]() { schedule_recurrent_function_us([callback]() { callback(); return false; }, 0); };
63-
_attach_ms(milliseconds, true);
62+
_callback = [callback]() { schedule_recurrent_function_us([callback]() { callback(); return false; }, 0); };
63+
_attach_ms(milliseconds, true, _callback, _callback.arg());
6464
}
6565

6666
// callback will be called in SYS ctx when ticker fires
67-
void attach_ms(uint32_t milliseconds, callback_function_t callback)
67+
void attach_ms(uint32_t milliseconds, callback_t callback)
6868
{
69-
_callback_function = std::move(callback);
70-
_attach_ms(milliseconds, true);
69+
_callback = std::move(callback);
70+
_attach_ms(milliseconds, true, _callback, _callback.arg());
7171
}
7272

7373
// callback will be called in SYS ctx when ticker fires
@@ -77,7 +77,7 @@ class Ticker
7777
#pragma GCC diagnostic push
7878
#pragma GCC diagnostic ignored "-Wcast-function-type"
7979
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
80-
_attach_ms(1000UL * seconds, true, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void*>(arg));
80+
_attach_ms(1000UL * seconds, true, reinterpret_cast<callback_with_arg_t*>(callback), reinterpret_cast<void*>(arg));
8181
#pragma GCC diagnostic pop
8282
}
8383

@@ -88,67 +88,62 @@ class Ticker
8888
#pragma GCC diagnostic push
8989
#pragma GCC diagnostic ignored "-Wcast-function-type"
9090
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
91-
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void*>(arg));
91+
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t*>(callback), reinterpret_cast<void*>(arg));
9292
#pragma GCC diagnostic pop
9393
}
9494

9595
// callback will be called at following loop() after ticker fires
96-
void once_scheduled(float seconds, callback_function_t callback)
96+
void once_scheduled(float seconds, callback_t callback)
9797
{
98-
_callback_function = [callback]() { schedule_function(callback); };
99-
_attach_ms(1000UL * seconds, false);
98+
_callback = [callback]() { schedule_function(callback); };
99+
_attach_ms(1000UL * seconds, false, _callback, _callback.arg());
100100
}
101101

102102
// callback will be called in SYS ctx when ticker fires
103-
void once(float seconds, callback_function_t callback)
103+
void once(float seconds, callback_t callback)
104104
{
105-
_callback_function = std::move(callback);
106-
_attach_ms(1000UL * seconds, false);
105+
_callback = std::move(callback);
106+
_attach_ms(1000UL * seconds, false, _callback, _callback.arg());
107107
}
108108

109109
// callback will be called at following loop() after ticker fires
110-
void once_ms_scheduled(uint32_t milliseconds, callback_function_t callback)
110+
void once_ms_scheduled(uint32_t milliseconds, callback_t callback)
111111
{
112-
_callback_function = [callback]() { schedule_function(callback); };
113-
_attach_ms(milliseconds, false);
112+
_callback = [callback]() { schedule_function(callback); };
113+
_attach_ms(milliseconds, false, _callback, _callback.arg());
114114
}
115115

116116
// callback will be called in SYS ctx when ticker fires
117-
void once_ms(uint32_t milliseconds, callback_function_t callback)
117+
void once_ms(uint32_t milliseconds, callback_t callback)
118118
{
119-
_callback_function = std::move(callback);
120-
_attach_ms(milliseconds, false);
119+
_callback = std::move(callback);
120+
_attach_ms(milliseconds, false, _callback, _callback.arg());
121121
}
122122

123123
// callback will be called in SYS ctx when ticker fires
124124
template<typename TArg>
125125
void once(float seconds, void (*callback)(TArg), TArg arg)
126126
{
127127
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
128-
_attach_ms(1000UL * seconds, false, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void*>(arg));
128+
_attach_ms(1000UL * seconds, false, reinterpret_cast<callback_with_arg_t*>(callback), reinterpret_cast<void*>(arg));
129129
}
130130

131131
// callback will be called in SYS ctx when ticker fires
132132
template<typename TArg>
133133
void once_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
134134
{
135135
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
136-
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void*>(arg));
136+
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t*>(callback), reinterpret_cast<void*>(arg));
137137
}
138138

139139
void detach();
140140
bool active() const;
141141

142142
protected:
143-
static void _static_callback(void* arg);
144-
void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg);
145-
void _attach_ms(uint32_t milliseconds, bool repeat)
146-
{
147-
_attach_ms(milliseconds, repeat, _static_callback, this);
148-
}
143+
void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t* callback, void* arg);
149144

150145
ETSTimer* _timer;
151-
callback_function_t _callback_function = nullptr;
146+
callback_t _callback = nullptr;
152147

153148
private:
154149
ETSTimer _etsTimer;

0 commit comments

Comments
 (0)