1
- /*
1
+ /*
2
2
Ticker.h - esp8266 library that calls functions periodically
3
3
4
4
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
5
5
This file is part of the esp8266 core for Arduino environment.
6
-
6
+
7
7
This library is free software; you can redistribute it and/or
8
8
modify it under the terms of the GNU Lesser General Public
9
9
License as published by the Free Software Foundation; either
22
22
#ifndef TICKER_H
23
23
#define TICKER_H
24
24
25
- #include < stdint.h>
26
- #include < stdbool.h>
27
- #include < stddef.h>
28
25
#include < functional>
29
26
#include < Schedule.h>
30
-
31
- extern " C" {
32
- typedef struct _ETSTIMER_ ETSTimer;
33
- }
27
+ #include < ets_sys.h>
34
28
35
29
class Ticker
36
30
{
37
31
public:
38
32
Ticker ();
39
33
~Ticker ();
40
- typedef void (* callback_t )( void );
34
+
41
35
typedef void (*callback_with_arg_t )(void *);
42
36
typedef std::function<void (void )> callback_function_t ;
43
37
@@ -48,8 +42,8 @@ class Ticker
48
42
49
43
void attach (float seconds, callback_function_t callback)
50
44
{
51
- _callback_function = callback;
52
- attach (seconds, _static_callback, ( void *) this );
45
+ _callback_function = std::move ( callback) ;
46
+ _attach_s (seconds, true , _static_callback, this );
53
47
}
54
48
55
49
void attach_ms_scheduled (uint32_t milliseconds, callback_function_t callback)
@@ -59,27 +53,25 @@ class Ticker
59
53
60
54
void attach_ms (uint32_t milliseconds, callback_function_t callback)
61
55
{
62
- _callback_function = callback;
63
- attach_ms (milliseconds, _static_callback, ( void *) this );
56
+ _callback_function = std::move ( callback) ;
57
+ _attach_ms (milliseconds, true , _static_callback, this );
64
58
}
65
59
66
60
template <typename TArg>
67
61
void attach (float seconds, void (*callback)(TArg), TArg arg)
68
62
{
69
- 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*) " );
70
64
// C-cast serves two purposes:
71
65
// static_cast for smaller integer types,
72
66
// reinterpret_cast + const_cast for pointer types
73
- uint32_t arg32 = (uint32_t )arg;
74
- _attach_ms (seconds * 1000 , true , reinterpret_cast <callback_with_arg_t >(callback), arg32);
67
+ _attach_s (seconds, true , reinterpret_cast <callback_with_arg_t >(callback), (void *)arg);
75
68
}
76
69
77
70
template <typename TArg>
78
71
void attach_ms (uint32_t milliseconds, void (*callback)(TArg), TArg arg)
79
72
{
80
- static_assert (sizeof (TArg) <= sizeof (uint32_t ), " attach_ms() callback argument size must be <= 4 bytes" );
81
- uint32_t arg32 = (uint32_t )arg;
82
- _attach_ms (milliseconds, true , reinterpret_cast <callback_with_arg_t >(callback), arg32);
73
+ static_assert (sizeof (TArg) <= sizeof (void *), " attach() callback argument size must be <= sizeof(void*)" );
74
+ _attach_ms (milliseconds, true , reinterpret_cast <callback_with_arg_t >(callback), (void *)arg);
83
75
}
84
76
85
77
void once_scheduled (float seconds, callback_function_t callback)
@@ -89,8 +81,8 @@ class Ticker
89
81
90
82
void once (float seconds, callback_function_t callback)
91
83
{
92
- _callback_function = callback;
93
- once (seconds, _static_callback, ( void *) this );
84
+ _callback_function = std::move ( callback) ;
85
+ _attach_s (seconds, false , _static_callback, this );
94
86
}
95
87
96
88
void once_ms_scheduled (uint32_t milliseconds, callback_function_t callback)
@@ -100,36 +92,38 @@ class Ticker
100
92
101
93
void once_ms (uint32_t milliseconds, callback_function_t callback)
102
94
{
103
- _callback_function = callback;
104
- once_ms (milliseconds, _static_callback, ( void *) this );
95
+ _callback_function = std::move ( callback) ;
96
+ _attach_ms (milliseconds, false , _static_callback, this );
105
97
}
106
98
107
99
template <typename TArg>
108
100
void once (float seconds, void (*callback)(TArg), TArg arg)
109
101
{
110
- static_assert (sizeof (TArg) <= sizeof (uint32_t ), " attach() callback argument size must be <= 4 bytes" );
111
- uint32_t arg32 = (uint32_t )(arg);
112
- _attach_ms (seconds * 1000 , false , reinterpret_cast <callback_with_arg_t >(callback), arg32);
102
+ static_assert (sizeof (TArg) <= sizeof (void *), " attach() callback argument size must be <= sizeof(void*)" );
103
+ _attach_s (seconds, false , reinterpret_cast <callback_with_arg_t >(callback), (void *)arg);
113
104
}
114
105
115
106
template <typename TArg>
116
107
void once_ms (uint32_t milliseconds, void (*callback)(TArg), TArg arg)
117
108
{
118
- static_assert (sizeof (TArg) <= sizeof (uint32_t ), " attach_ms() callback argument size must be <= 4 bytes" );
119
- uint32_t arg32 = (uint32_t )(arg);
120
- _attach_ms (milliseconds, false , reinterpret_cast <callback_with_arg_t >(callback), arg32);
109
+ static_assert (sizeof (TArg) <= sizeof (void *), " attach() callback argument size must be <= sizeof(void*)" );
110
+ _attach_ms (milliseconds, false , reinterpret_cast <callback_with_arg_t >(callback), (void *)arg);
121
111
}
122
112
123
113
void detach ();
124
114
bool active () const ;
125
115
126
- protected:
127
- void _attach_ms (uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg);
128
- static void _static_callback (void * arg);
129
-
130
116
protected:
117
+ void _attach_ms (uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void * arg);
118
+ static void _static_callback (void * arg);
119
+
131
120
ETSTimer* _timer;
132
121
callback_function_t _callback_function = nullptr ;
122
+
123
+ private:
124
+ void _attach_s (float seconds, bool repeat, callback_with_arg_t callback, void * arg);
125
+ // char _etsTimerMem[sizeof(ETSTimer)];
126
+ ETSTimer _etsTimer;
133
127
};
134
128
135
129
0 commit comments