@@ -35,19 +35,25 @@ class Ticker
35
35
typedef void (*callback_with_arg_t )(void *);
36
36
typedef std::function<void (void )> callback_function_t ;
37
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);
38
+ void attach_ms (uint32_t milliseconds, callback_function_t callback)
39
+ {
40
+ _callback_function = std::move (callback);
41
+ _attach_ms (milliseconds, true , _static_callback, this );
42
+ }
42
43
43
- template <typename TArg>
44
- void attach (float seconds, void (*callback)(TArg), TArg arg)
44
+ void attach (float seconds, callback_function_t callback)
45
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));
46
+ attach_ms (1000UL * seconds, callback);
47
+ }
48
+
49
+ void attach_scheduled (float seconds, callback_function_t callback)
50
+ {
51
+ attach (seconds, [callback]() { schedule_function (callback); });
52
+ }
53
+
54
+ void attach_ms_scheduled (uint32_t milliseconds, callback_function_t callback)
55
+ {
56
+ attach_ms (milliseconds, [callback]() { schedule_function (callback); });
51
57
}
52
58
53
59
template <typename TArg>
@@ -57,16 +63,32 @@ class Ticker
57
63
_attach_ms (milliseconds, true , reinterpret_cast <callback_with_arg_t >(callback), reinterpret_cast <void *>(arg));
58
64
}
59
65
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
66
template <typename TArg>
66
- void once (float seconds, void (*callback)(TArg), TArg arg)
67
+ void attach (float seconds, void (*callback)(TArg), TArg arg)
67
68
{
68
69
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
+ attach_ms (1000UL * seconds, callback, arg);
71
+ }
72
+
73
+ void once_ms (uint32_t milliseconds, callback_function_t callback)
74
+ {
75
+ _callback_function = std::move (callback);
76
+ _attach_ms (milliseconds, false , _static_callback, this );
77
+ }
78
+
79
+ void once (float seconds, callback_function_t callback)
80
+ {
81
+ once_ms (1000UL * seconds, callback);
82
+ }
83
+
84
+ void once_scheduled (float seconds, callback_function_t callback)
85
+ {
86
+ once (seconds, [callback]() { schedule_function (callback); });
87
+ }
88
+
89
+ void once_ms_scheduled (uint32_t milliseconds, callback_function_t callback)
90
+ {
91
+ once_ms (milliseconds, [callback]() { schedule_function (callback); });
70
92
}
71
93
72
94
template <typename TArg>
@@ -76,12 +98,18 @@ class Ticker
76
98
_attach_ms (milliseconds, false , reinterpret_cast <callback_with_arg_t >(callback), reinterpret_cast <void *>(arg));
77
99
}
78
100
101
+ template <typename TArg>
102
+ void once (float seconds, void (*callback)(TArg), TArg arg)
103
+ {
104
+ static_assert (sizeof (TArg) <= sizeof (void *), " attach() callback argument size must be <= sizeof(void*)" );
105
+ once_ms (1000UL * seconds, callback, arg);
106
+ }
107
+
79
108
void detach ();
80
109
bool active () const ;
81
110
82
111
protected:
83
112
static void _static_callback (void * arg);
84
- void _attach_s (float seconds, bool repeat, callback_with_arg_t callback, void * arg);
85
113
void _attach_ms (uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void * arg);
86
114
87
115
ETSTimer* _timer;
0 commit comments