@@ -35,19 +35,25 @@ class Ticker
3535 typedef void (*callback_with_arg_t )(void *);
3636 typedef std::function<void (void )> callback_function_t ;
3737
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+ }
4243
43- template <typename TArg>
44- void attach (float seconds, void (*callback)(TArg), TArg arg)
44+ void attach (float seconds, callback_function_t callback)
4545 {
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); });
5157 }
5258
5359 template <typename TArg>
@@ -57,16 +63,32 @@ class Ticker
5763 _attach_ms (milliseconds, true , reinterpret_cast <callback_with_arg_t >(callback), reinterpret_cast <void *>(arg));
5864 }
5965
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-
6566 template <typename TArg>
66- void once (float seconds, void (*callback)(TArg), TArg arg)
67+ void attach (float seconds, void (*callback)(TArg), TArg arg)
6768 {
6869 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); });
7092 }
7193
7294 template <typename TArg>
@@ -76,12 +98,18 @@ class Ticker
7698 _attach_ms (milliseconds, false , reinterpret_cast <callback_with_arg_t >(callback), reinterpret_cast <void *>(arg));
7799 }
78100
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+
79108 void detach ();
80109 bool active () const ;
81110
82111protected:
83112 static void _static_callback (void * arg);
84- void _attach_s (float seconds, bool repeat, callback_with_arg_t callback, void * arg);
85113 void _attach_ms (uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void * arg);
86114
87115 ETSTimer* _timer;
0 commit comments