Skip to content

Commit 1aa536a

Browse files
committed
Move code from header into compiliation unit.
Reformat.
1 parent e74cb37 commit 1aa536a

File tree

2 files changed

+122
-128
lines changed

2 files changed

+122
-128
lines changed

Diff for: libraries/Ticker/src/Ticker.cpp

+57-33
Original file line numberDiff line numberDiff line change
@@ -25,62 +25,86 @@
2525
#include "Ticker.h"
2626

2727
Ticker::Ticker()
28-
: _timer(nullptr)
28+
: _timer(nullptr) {}
29+
30+
Ticker::~Ticker()
2931
{
32+
detach();
3033
}
3134

32-
Ticker::~Ticker()
35+
void Ticker::attach(float seconds, callback_function_t callback)
36+
{
37+
_callback_function = std::move(callback);
38+
_attach_us(1000000ULL * seconds, true, _static_callback, this);
39+
}
40+
41+
void Ticker::attach_ms(uint64_t milliseconds, callback_function_t callback)
42+
{
43+
_callback_function = std::move(callback);
44+
_attach_us(1000ULL * milliseconds, true, _static_callback, this);
45+
}
46+
47+
void Ticker::attach_us(uint64_t micros, callback_function_t callback)
48+
{
49+
_callback_function = std::move(callback);
50+
_attach_us(micros, true, _static_callback, this);
51+
}
52+
53+
void Ticker::once(float seconds, callback_function_t callback)
3354
{
34-
detach();
55+
_callback_function = std::move(callback);
56+
_attach_us(1000000ULL * seconds, false, _static_callback, this);
3557
}
3658

37-
void Ticker::_attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg)
59+
void Ticker::once_ms(uint64_t milliseconds, callback_function_t callback)
3860
{
39-
_attach_us(1000000ULL * seconds, repeat, callback, arg);
61+
_callback_function = std::move(callback);
62+
_attach_us(1000ULL * milliseconds, false, _static_callback, this);
4063
}
4164

42-
void Ticker::_attach_ms(uint64_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg)
65+
void Ticker::once_us(uint64_t micros, callback_function_t callback)
4366
{
44-
_attach_us(1000ULL * milliseconds, repeat, callback, arg);
67+
_callback_function = std::move(callback);
68+
_attach_us(micros, false, _static_callback, this);
4569
}
4670

4771
void Ticker::_attach_us(uint64_t micros, bool repeat, callback_with_arg_t callback, void* arg)
4872
{
49-
esp_timer_create_args_t _timerConfig;
50-
_timerConfig.arg = reinterpret_cast<void*>(arg);
51-
_timerConfig.callback = callback;
52-
_timerConfig.dispatch_method = ESP_TIMER_TASK;
53-
_timerConfig.name = "Ticker";
54-
if (_timer) {
55-
esp_timer_stop(_timer);
56-
esp_timer_delete(_timer);
57-
}
58-
esp_timer_create(&_timerConfig, &_timer);
59-
if (repeat) {
60-
esp_timer_start_periodic(_timer, micros);
61-
}
62-
else {
63-
esp_timer_start_once(_timer, micros);
64-
}
73+
esp_timer_create_args_t _timerConfig;
74+
_timerConfig.arg = reinterpret_cast<void*>(arg);
75+
_timerConfig.callback = callback;
76+
_timerConfig.dispatch_method = ESP_TIMER_TASK;
77+
_timerConfig.name = "Ticker";
78+
if (_timer) {
79+
esp_timer_stop(_timer);
80+
esp_timer_delete(_timer);
81+
}
82+
esp_timer_create(&_timerConfig, &_timer);
83+
if (repeat) {
84+
esp_timer_start_periodic(_timer, micros);
85+
}
86+
else {
87+
esp_timer_start_once(_timer, micros);
88+
}
6589
}
6690

6791
void Ticker::detach() {
68-
if (_timer) {
69-
esp_timer_stop(_timer);
70-
esp_timer_delete(_timer);
71-
_timer = nullptr;
72-
_callback_function = nullptr;
73-
}
92+
if (_timer) {
93+
esp_timer_stop(_timer);
94+
esp_timer_delete(_timer);
95+
_timer = nullptr;
96+
_callback_function = nullptr;
97+
}
7498
}
7599

76100
bool Ticker::active() const
77101
{
78-
return _timer;
102+
return _timer;
79103
}
80104

81105
void Ticker::_static_callback(void* arg)
82106
{
83-
Ticker* _this = reinterpret_cast<Ticker*>(arg);
84-
if (_this && _this->_callback_function)
85-
_this->_callback_function();
107+
Ticker* _this = reinterpret_cast<Ticker*>(arg);
108+
if (_this && _this->_callback_function)
109+
_this->_callback_function();
86110
}

Diff for: libraries/Ticker/src/Ticker.h

+65-95
Original file line numberDiff line numberDiff line change
@@ -33,107 +33,77 @@ extern "C" {
3333
class Ticker
3434
{
3535
public:
36-
Ticker();
37-
~Ticker();
38-
39-
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_s(seconds, true, _static_callback, this);
46-
}
47-
48-
void attach_ms(uint64_t milliseconds, callback_function_t callback)
49-
{
50-
_callback_function = std::move(callback);
51-
_attach_ms(milliseconds, true, _static_callback, this);
52-
}
53-
54-
void attach_us(uint64_t micros, callback_function_t callback)
55-
{
56-
_callback_function = std::move(callback);
57-
_attach_us(micros, 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), (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_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
75-
}
76-
77-
template<typename TArg>
78-
void attach_us(uint64_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-
84-
void once(float seconds, callback_function_t callback)
85-
{
86-
_callback_function = std::move(callback);
87-
_attach_s(seconds, false, _static_callback, this);
88-
}
89-
90-
void once_ms(uint64_t milliseconds, callback_function_t callback)
91-
{
92-
_callback_function = std::move(callback);
93-
_attach_ms(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);
100-
}
101-
102-
template<typename TArg>
103-
void once(float seconds, void (*callback)(TArg), TArg arg)
104-
{
105-
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
106-
_attach_s(seconds, false, reinterpret_cast<callback_with_arg_t>(callback), (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_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
114-
}
115-
116-
template<typename TArg>
117-
void once_us(uint64_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-
123-
void detach();
124-
bool active() const;
36+
Ticker();
37+
~Ticker();
38+
39+
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+
void attach_ms(uint64_t milliseconds, callback_function_t callback);
44+
void attach_us(uint64_t micros, callback_function_t callback);
45+
46+
template<typename TArg>
47+
void attach(float seconds, void (*callback)(TArg), TArg arg)
48+
{
49+
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
50+
// C-cast serves two purposes:
51+
// static_cast for smaller integer types,
52+
// reinterpret_cast + const_cast for pointer types
53+
_attach_us(1000000ULL * seconds, true, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
54+
}
55+
56+
template<typename TArg>
57+
void attach_ms(uint64_t milliseconds, void (*callback)(TArg), TArg arg)
58+
{
59+
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
60+
_attach_us(1000ULL * milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
61+
}
62+
63+
template<typename TArg>
64+
void attach_us(uint64_t micros, void (*callback)(TArg), TArg arg)
65+
{
66+
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
67+
_attach_us(micros, true, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
68+
}
69+
70+
void once(float seconds, callback_function_t callback);
71+
void once_ms(uint64_t milliseconds, callback_function_t callback);
72+
void once_us(uint64_t micros, callback_function_t callback);
73+
74+
template<typename TArg>
75+
void once(float seconds, void (*callback)(TArg), TArg arg)
76+
{
77+
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
78+
_attach_us(1000000ULL * seconds, false, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
79+
}
80+
81+
template<typename TArg>
82+
void once_ms(uint64_t milliseconds, void (*callback)(TArg), TArg arg)
83+
{
84+
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
85+
_attach_us(1000ULL * milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
86+
}
87+
88+
template<typename TArg>
89+
void once_us(uint64_t micros, void (*callback)(TArg), TArg arg)
90+
{
91+
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
92+
_attach_us(micros, false, reinterpret_cast<callback_with_arg_t>(callback), (void*)arg);
93+
}
94+
95+
void detach();
96+
bool active() const;
12597

12698
protected:
127-
void _attach_ms(uint64_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg);
128-
static void _static_callback(void* arg);
99+
static void _static_callback(void* arg);
129100

130-
callback_function_t _callback_function = nullptr;
101+
callback_function_t _callback_function = nullptr;
131102

132-
esp_timer_handle_t _timer;
103+
esp_timer_handle_t _timer;
133104

134105
private:
135-
void _attach_us(uint64_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);
106+
void _attach_us(uint64_t micros, bool repeat, callback_with_arg_t callback, void* arg);
137107
};
138108

139109

0 commit comments

Comments
 (0)