2525#include < stdint.h>
2626#include < stdbool.h>
2727#include < stddef.h>
28+ #include < functional>
29+ #include < Schedule.h>
2830
2931extern " C" {
3032 typedef struct _ETSTIMER_ ETSTimer;
@@ -37,15 +39,28 @@ class Ticker
3739 ~Ticker ();
3840 typedef void (*callback_t )(void );
3941 typedef void (*callback_with_arg_t )(void *);
42+ typedef std::function<void (void )> callback_function_t ;
4043
41- void attach (float seconds, callback_t callback)
44+ void attach_scheduled (float seconds, callback_function_t callback)
4245 {
43- _attach_ms (seconds * 1000 , true , reinterpret_cast < callback_with_arg_t >( callback), 0 );
46+ attach (seconds, std::bind (schedule_function, callback));
4447 }
4548
46- void attach_ms ( uint32_t milliseconds, callback_t callback)
49+ void attach ( float seconds, callback_function_t callback)
4750 {
48- _attach_ms (milliseconds, true , reinterpret_cast <callback_with_arg_t >(callback), 0 );
51+ _callback_function = callback;
52+ attach (seconds, _static_callback, (void *)this );
53+ }
54+
55+ void attach_ms_scheduled (uint32_t milliseconds, callback_function_t callback)
56+ {
57+ attach_ms (milliseconds, std::bind (schedule_function, callback));
58+ }
59+
60+ void attach_ms (uint32_t milliseconds, callback_function_t callback)
61+ {
62+ _callback_function = callback;
63+ attach_ms (milliseconds, _static_callback, (void *)this );
4964 }
5065
5166 template <typename TArg>
@@ -67,14 +82,26 @@ class Ticker
6782 _attach_ms (milliseconds, true , reinterpret_cast <callback_with_arg_t >(callback), arg32);
6883 }
6984
70- void once (float seconds, callback_t callback)
85+ void once_scheduled (float seconds, callback_function_t callback)
7186 {
72- _attach_ms (seconds * 1000 , false , reinterpret_cast < callback_with_arg_t >( callback), 0 );
87+ once (seconds, std::bind (schedule_function, callback));
7388 }
7489
75- void once_ms ( uint32_t milliseconds, callback_t callback)
90+ void once ( float seconds, callback_function_t callback)
7691 {
77- _attach_ms (milliseconds, false , reinterpret_cast <callback_with_arg_t >(callback), 0 );
92+ _callback_function = callback;
93+ once (seconds, _static_callback, (void *)this );
94+ }
95+
96+ void once_ms_scheduled (uint32_t milliseconds, callback_function_t callback)
97+ {
98+ once_ms (milliseconds, std::bind (schedule_function, callback));
99+ }
100+
101+ void once_ms (uint32_t milliseconds, callback_function_t callback)
102+ {
103+ _callback_function = callback;
104+ once_ms (milliseconds, _static_callback, (void *)this );
78105 }
79106
80107 template <typename TArg>
@@ -98,10 +125,11 @@ class Ticker
98125
99126protected:
100127 void _attach_ms (uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg);
101-
128+ static void _static_callback ( void * arg);
102129
103130protected:
104131 ETSTimer* _timer;
132+ callback_function_t _callback_function = nullptr ;
105133};
106134
107135
0 commit comments