|
28 | 28 | extern "C" {
|
29 | 29 | #include "esp_timer.h"
|
30 | 30 | }
|
| 31 | +#include <functional> |
31 | 32 |
|
32 | 33 | class Ticker
|
33 | 34 | {
|
34 | 35 | public:
|
35 | 36 | Ticker();
|
36 | 37 | ~Ticker();
|
37 |
| - typedef void (*callback_t)(void); |
| 38 | + |
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_us(1000000ULL * seconds, true, _static_callback, this); |
| 46 | + } |
39 | 47 |
|
40 |
| - void attach(float seconds, callback_t callback) |
| 48 | + void attach_ms(uint64_t milliseconds, callback_function_t callback) |
41 | 49 | {
|
42 |
| - _attach_ms(seconds * 1000, true, reinterpret_cast<callback_with_arg_t>(callback), 0); |
| 50 | + _callback_function = std::move(callback); |
| 51 | + _attach_us(1000ULL * milliseconds, true, _static_callback, this); |
43 | 52 | }
|
44 | 53 |
|
45 |
| - void attach_ms(uint32_t milliseconds, callback_t callback) |
| 54 | + void attach_us(uint64_t micros, callback_function_t callback) |
46 | 55 | {
|
47 |
| - _attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), 0); |
| 56 | + _callback_function = std::move(callback); |
| 57 | + _attach_us(micros, true, _static_callback, this); |
48 | 58 | }
|
49 | 59 |
|
50 | 60 | template<typename TArg>
|
51 | 61 | void attach(float seconds, void (*callback)(TArg), TArg arg)
|
52 | 62 | {
|
53 |
| - static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach() callback argument size must be <= 4 bytes"); |
| 63 | + static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); |
54 | 64 | // C-cast serves two purposes:
|
55 | 65 | // static_cast for smaller integer types,
|
56 | 66 | // reinterpret_cast + const_cast for pointer types
|
57 |
| - uint32_t arg32 = (uint32_t)arg; |
58 |
| - _attach_ms(seconds * 1000, true, reinterpret_cast<callback_with_arg_t>(callback), arg32); |
| 67 | + _attach_us(1000000ULL * seconds, true, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<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_us(1000ULL * milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void*>(arg)); |
59 | 75 | }
|
60 | 76 |
|
61 | 77 | template<typename TArg>
|
62 |
| - void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg) |
| 78 | + void attach_us(uint64_t micros, void (*callback)(TArg), TArg arg) |
63 | 79 | {
|
64 |
| - static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach_ms() callback argument size must be <= 4 bytes"); |
65 |
| - uint32_t arg32 = (uint32_t)arg; |
66 |
| - _attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), arg32); |
| 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), reinterpret_cast<void*>(arg)); |
67 | 82 | }
|
68 | 83 |
|
69 |
| - void once(float seconds, callback_t callback) |
| 84 | + void once(float seconds, callback_function_t callback) |
70 | 85 | {
|
71 |
| - _attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), 0); |
| 86 | + _callback_function = std::move(callback); |
| 87 | + _attach_us(1000000ULL * seconds, false, _static_callback, this); |
72 | 88 | }
|
73 | 89 |
|
74 |
| - void once_ms(uint32_t milliseconds, callback_t callback) |
| 90 | + void once_ms(uint64_t milliseconds, callback_function_t callback) |
75 | 91 | {
|
76 |
| - _attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), 0); |
| 92 | + _callback_function = std::move(callback); |
| 93 | + _attach_us(1000ULL * 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); |
77 | 100 | }
|
78 | 101 |
|
79 | 102 | template<typename TArg>
|
80 | 103 | void once(float seconds, void (*callback)(TArg), TArg arg)
|
81 | 104 | {
|
82 |
| - static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach() callback argument size must be <= 4 bytes"); |
83 |
| - uint32_t arg32 = (uint32_t)(arg); |
84 |
| - _attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), arg32); |
| 105 | + static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)"); |
| 106 | + _attach_us(1000000ULL * seconds, false, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<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_us(1000ULL * milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void*>(arg)); |
85 | 114 | }
|
86 | 115 |
|
87 | 116 | template<typename TArg>
|
88 |
| - void once_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg) |
| 117 | + void once_us(uint64_t micros, void (*callback)(TArg), TArg arg) |
89 | 118 | {
|
90 |
| - static_assert(sizeof(TArg) <= sizeof(uint32_t), "attach_ms() callback argument size must be <= 4 bytes"); |
91 |
| - uint32_t arg32 = (uint32_t)(arg); |
92 |
| - _attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), arg32); |
| 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), reinterpret_cast<void*>(arg)); |
93 | 121 | }
|
94 | 122 |
|
95 | 123 | void detach();
|
96 |
| - bool active(); |
| 124 | + bool active() const; |
97 | 125 |
|
98 | 126 | protected:
|
99 |
| - void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg); |
| 127 | + static void _static_callback(void* arg); |
100 | 128 |
|
| 129 | + callback_function_t _callback_function = nullptr; |
101 | 130 |
|
102 |
| -protected: |
103 | 131 | esp_timer_handle_t _timer;
|
| 132 | + |
| 133 | +private: |
| 134 | + void _attach_us(uint64_t micros, bool repeat, callback_with_arg_t callback, void* arg); |
104 | 135 | };
|
105 | 136 |
|
106 | 137 |
|
|
0 commit comments