25
25
#include < stdint.h>
26
26
#include < stdbool.h>
27
27
#include < stddef.h>
28
+ #include < functional>
29
+ #include < Schedule.h>
28
30
29
31
extern " C" {
30
32
typedef struct _ETSTIMER_ ETSTimer;
@@ -37,15 +39,28 @@ class Ticker
37
39
~Ticker ();
38
40
typedef void (*callback_t )(void );
39
41
typedef void (*callback_with_arg_t )(void *);
42
+ typedef std::function<void (void )> callback_function_t ;
40
43
41
- void attach (float seconds, callback_t callback)
44
+ void attach_scheduled (float seconds, callback_function_t callback)
42
45
{
43
- _attach_ms (seconds * 1000 , true , reinterpret_cast < callback_with_arg_t >( callback), 0 );
46
+ attach (seconds, std::bind (schedule_function, callback));
44
47
}
45
48
46
- void attach_ms ( uint32_t milliseconds, callback_t callback)
49
+ void attach ( float seconds, callback_function_t callback)
47
50
{
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 );
49
64
}
50
65
51
66
template <typename TArg>
@@ -67,14 +82,26 @@ class Ticker
67
82
_attach_ms (milliseconds, true , reinterpret_cast <callback_with_arg_t >(callback), arg32);
68
83
}
69
84
70
- void once (float seconds, callback_t callback)
85
+ void once_scheduled (float seconds, callback_function_t callback)
71
86
{
72
- _attach_ms (seconds * 1000 , false , reinterpret_cast < callback_with_arg_t >( callback), 0 );
87
+ once (seconds, std::bind (schedule_function, callback));
73
88
}
74
89
75
- void once_ms ( uint32_t milliseconds, callback_t callback)
90
+ void once ( float seconds, callback_function_t callback)
76
91
{
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 );
78
105
}
79
106
80
107
template <typename TArg>
@@ -98,10 +125,11 @@ class Ticker
98
125
99
126
protected:
100
127
void _attach_ms (uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg);
101
-
128
+ static void _static_callback ( void * arg);
102
129
103
130
protected:
104
131
ETSTimer* _timer;
132
+ callback_function_t _callback_function = nullptr ;
105
133
};
106
134
107
135
0 commit comments