|
29 | 29 | class Ticker |
30 | 30 | { |
31 | 31 | 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; |
115 | 81 |
|
116 | 82 | 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); |
119 | 86 |
|
120 | | - ETSTimer* _timer; |
121 | | - callback_function_t _callback_function = nullptr; |
| 87 | + ETSTimer* _timer; |
| 88 | + callback_function_t _callback_function = nullptr; |
122 | 89 |
|
123 | 90 | private: |
124 | | - void _attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg); |
125 | | - ETSTimer _etsTimer; |
| 91 | + ETSTimer _etsTimer; |
126 | 92 | }; |
127 | 93 |
|
128 | 94 |
|
|
0 commit comments