Skip to content

Commit a5df54f

Browse files
committed
Backport from ESP32
1 parent af85bd2 commit a5df54f

File tree

3 files changed

+121
-132
lines changed

3 files changed

+121
-132
lines changed

libraries/Ticker/Ticker.cpp

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,60 +25,96 @@
2525

2626
#include "Ticker.h"
2727

28-
namespace
28+
Ticker::Ticker()
29+
: _timer(nullptr) {}
30+
31+
Ticker::~Ticker()
2932
{
30-
constexpr int ONCE = 0;
31-
constexpr int REPEAT = 1;
33+
detach();
3234
}
3335

34-
Ticker::Ticker()
35-
: _timer(nullptr)
36+
void Ticker::attach(float seconds, callback_function_t callback)
3637
{
38+
_callback_function = std::move(callback);
39+
_attach_ms(1000UL * seconds, true, _static_callback, this);
3740
}
3841

39-
Ticker::~Ticker()
42+
void Ticker::attach_ms(uint32_t milliseconds, callback_function_t callback)
43+
{
44+
_callback_function = std::move(callback);
45+
_attach_ms(milliseconds, true, _static_callback, this);
46+
}
47+
48+
void Ticker::attach_scheduled(float seconds, callback_function_t callback)
49+
{
50+
attach(seconds, [callback]() { schedule_function(callback); });
51+
}
52+
53+
void Ticker::attach_ms_scheduled(uint32_t milliseconds, callback_function_t callback)
54+
{
55+
attach_ms(milliseconds, [callback]() { schedule_function(callback); });
56+
}
57+
58+
void Ticker::once(float seconds, callback_function_t callback)
59+
{
60+
_callback_function = std::move(callback);
61+
_attach_ms(1000UL * seconds, false, _static_callback, this);
62+
}
63+
64+
void Ticker::once_ms(uint32_t milliseconds, callback_function_t callback)
65+
{
66+
_callback_function = std::move(callback);
67+
_attach_ms(milliseconds, false, _static_callback, this);
68+
}
69+
70+
void Ticker::once_scheduled(float seconds, callback_function_t callback)
71+
{
72+
once(seconds, [callback]() { schedule_function(callback); });
73+
}
74+
75+
void Ticker::once_ms_scheduled(uint32_t milliseconds, callback_function_t callback)
4076
{
41-
detach();
77+
once_ms(milliseconds, [callback]() { schedule_function(callback); });
4278
}
4379

4480
void Ticker::_attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg)
4581
{
46-
_attach_ms(1000 * seconds, repeat, callback, arg);
82+
_attach_ms(1000UL * seconds, repeat, callback, arg);
4783
}
4884

4985
void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg)
5086
{
51-
if (_timer)
52-
{
53-
os_timer_disarm(_timer);
54-
}
55-
else
56-
{
57-
_timer = &_etsTimer;
58-
}
59-
60-
os_timer_setfn(_timer, callback, arg);
61-
os_timer_arm(_timer, milliseconds, (repeat) ? REPEAT : ONCE);
87+
if (_timer)
88+
{
89+
os_timer_disarm(_timer);
90+
}
91+
else
92+
{
93+
_timer = &_etsTimer;
94+
}
95+
96+
os_timer_setfn(_timer, callback, arg);
97+
os_timer_arm(_timer, milliseconds, repeat);
6298
}
6399

64100
void Ticker::detach()
65101
{
66-
if (!_timer)
67-
return;
102+
if (!_timer)
103+
return;
68104

69-
os_timer_disarm(_timer);
70-
_timer = nullptr;
71-
_callback_function = nullptr;
105+
os_timer_disarm(_timer);
106+
_timer = nullptr;
107+
_callback_function = nullptr;
72108
}
73109

74110
bool Ticker::active() const
75111
{
76-
return _timer;
112+
return _timer;
77113
}
78114

79115
void Ticker::_static_callback(void* arg)
80116
{
81-
Ticker* _this = reinterpret_cast<Ticker*>(arg);
82-
if (_this && _this->_callback_function)
83-
_this->_callback_function();
117+
Ticker* _this = reinterpret_cast<Ticker*>(arg);
118+
if (_this && _this->_callback_function)
119+
_this->_callback_function();
84120
}

libraries/Ticker/Ticker.h

Lines changed: 55 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -29,100 +29,66 @@
2929
class Ticker
3030
{
3131
public:
32-
Ticker();
33-
~Ticker();
34-
35-
typedef void (*callback_with_arg_t)(void *);
36-
typedef std::function<void(void)> callback_function_t;
37-
38-
void attach_scheduled(float seconds, callback_function_t callback)
39-
{
40-
attach(seconds, [callback]() { schedule_function(callback); });
41-
}
42-
43-
void attach(float seconds, callback_function_t callback)
44-
{
45-
_callback_function = std::move(callback);
46-
_attach_s(seconds, true, _static_callback, this);
47-
}
48-
49-
void attach_ms_scheduled(uint32_t milliseconds, callback_function_t callback)
50-
{
51-
attach_ms(milliseconds, [callback]() { schedule_function(callback); });
52-
}
53-
54-
void attach_ms(uint32_t milliseconds, callback_function_t callback)
55-
{
56-
_callback_function = std::move(callback);
57-
_attach_ms(milliseconds, true, _static_callback, this);
58-
}
59-
60-
template<typename TArg>
61-
void attach(float seconds, void (*callback)(TArg), TArg arg)
62-
{
63-
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
64-
// C-cast serves two purposes:
65-
// static_cast for smaller integer types,
66-
// reinterpret_cast + const_cast for pointer types
67-
_attach_s(seconds, true, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void *>(arg));
68-
}
69-
70-
template<typename TArg>
71-
void attach_ms(uint32_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_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void *>(arg));
75-
}
76-
77-
void once_scheduled(float seconds, callback_function_t callback)
78-
{
79-
once(seconds, [callback]() { schedule_function(callback); });
80-
}
81-
82-
void once(float seconds, callback_function_t callback)
83-
{
84-
_callback_function = std::move(callback);
85-
_attach_s(seconds, false, _static_callback, this);
86-
}
87-
88-
void once_ms_scheduled(uint32_t milliseconds, callback_function_t callback)
89-
{
90-
once_ms(milliseconds, [callback]() { schedule_function(callback); });
91-
}
92-
93-
void once_ms(uint32_t milliseconds, callback_function_t callback)
94-
{
95-
_callback_function = std::move(callback);
96-
_attach_ms(milliseconds, false, _static_callback, this);
97-
}
98-
99-
template<typename TArg>
100-
void once(float seconds, void (*callback)(TArg), TArg arg)
101-
{
102-
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
103-
_attach_s(seconds, false, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void *>(arg));
104-
}
105-
106-
template<typename TArg>
107-
void once_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
108-
{
109-
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
110-
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void *>(arg));
111-
}
112-
113-
void detach();
114-
bool active() const;
32+
Ticker();
33+
~Ticker();
34+
35+
typedef void (*callback_with_arg_t)(void*);
36+
typedef std::function<void(void)> callback_function_t;
37+
38+
void attach(float seconds, callback_function_t callback);
39+
void attach_ms(uint32_t milliseconds, callback_function_t callback);
40+
void attach_scheduled(float seconds, callback_function_t callback);
41+
void attach_ms_scheduled(uint32_t milliseconds, callback_function_t callback);
42+
43+
template<typename TArg>
44+
void attach(float seconds, void (*callback)(TArg), TArg arg)
45+
{
46+
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
47+
// C-cast serves two purposes:
48+
// static_cast for smaller integer types,
49+
// reinterpret_cast + const_cast for pointer types
50+
_attach_s(seconds, true, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void*>(arg));
51+
}
52+
53+
template<typename TArg>
54+
void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
55+
{
56+
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
57+
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
58+
}
59+
60+
void once(float seconds, callback_function_t callback);
61+
void once_ms(uint32_t milliseconds, callback_function_t callback);
62+
void once_scheduled(float seconds, callback_function_t callback);
63+
void once_ms_scheduled(uint32_t milliseconds, callback_function_t callback);
64+
65+
template<typename TArg>
66+
void once(float seconds, void (*callback)(TArg), TArg arg)
67+
{
68+
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
69+
_attach_s(seconds, false, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void*>(arg));
70+
}
71+
72+
template<typename TArg>
73+
void once_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
74+
{
75+
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
76+
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
77+
}
78+
79+
void detach();
80+
bool active() const;
11581

11682
protected:
117-
void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg);
118-
static void _static_callback(void* arg);
83+
static void _static_callback(void* arg);
84+
void _attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg);
85+
void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg);
11986

120-
ETSTimer* _timer;
121-
callback_function_t _callback_function = nullptr;
87+
ETSTimer* _timer;
88+
callback_function_t _callback_function = nullptr;
12289

12390
private:
124-
void _attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg);
125-
ETSTimer _etsTimer;
91+
ETSTimer _etsTimer;
12692
};
12793

12894

libraries/Ticker/keywords.txt

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
#######################################
2-
# Syntax Coloring Map For Wire
3-
#######################################
4-
51
#######################################
62
# Datatypes (KEYWORD1)
73
#######################################
84

5+
Ticker KEYWORD1
6+
97
#######################################
108
# Methods and Functions (KEYWORD2)
119
#######################################
@@ -16,14 +14,3 @@ once KEYWORD2
1614
once_ms KEYWORD2
1715
detach KEYWORD2
1816
active KEYWORD2
19-
20-
#######################################
21-
# Instances (KEYWORD2)
22-
#######################################
23-
24-
Ticker KEYWORD2
25-
26-
#######################################
27-
# Constants (LITERAL1)
28-
#######################################
29-

0 commit comments

Comments
 (0)