17
17
#ifndef MBED_TICKER_H
18
18
#define MBED_TICKER_H
19
19
20
+ #include < chrono>
20
21
#include < mstd_utility>
22
+ #include " drivers/TickerDataClock.h"
21
23
#include " drivers/TimerEvent.h"
22
24
#include " platform/Callback.h"
23
25
#include " platform/mbed_toolchain.h"
24
26
#include " platform/NonCopyable.h"
25
27
#include " hal/lp_ticker_api.h"
26
28
27
29
namespace mbed {
30
+
28
31
/* *
29
32
* \defgroup drivers_Ticker Ticker class
30
33
* \ingroup drivers-public-api-ticker
@@ -42,6 +45,7 @@ namespace mbed {
42
45
* // Toggle the blinking LED after 5 seconds
43
46
*
44
47
* #include "mbed.h"
48
+ * using namespace std::chrono;
45
49
*
46
50
* Ticker timer;
47
51
* DigitalOut led1(LED1);
@@ -54,26 +58,20 @@ namespace mbed {
54
58
* }
55
59
*
56
60
* int main() {
57
- * timer.attach(&attime, 5 );
61
+ * timer.attach(&attime, 5us );
58
62
* while(1) {
59
63
* if(flip == 0) {
60
64
* led1 = !led1;
61
65
* } else {
62
66
* led2 = !led2;
63
67
* }
64
- * ThisThread::sleep_for(200 );
68
+ * ThisThread::sleep_for(200ms );
65
69
* }
66
70
* }
67
71
* @endcode
68
72
*/
69
- class Ticker : public TimerEvent , private NonCopyable <Ticker> {
70
-
73
+ class TickerBase : public TimerEvent , private NonCopyable <TickerBase> {
71
74
public:
72
- Ticker ();
73
-
74
- // When low power ticker is in use, then do not disable deep sleep.
75
- Ticker (const ticker_data_t *data);
76
-
77
75
/* * Attach a function to be called by the Ticker, specifying the interval in seconds
78
76
*
79
77
* The method forwards its arguments to attach_us() rather than copying them which
@@ -82,18 +80,21 @@ class Ticker : public TimerEvent, private NonCopyable<Ticker> {
82
80
* possible given attach_us() expects an integer value for the callback interval.
83
81
* @param func pointer to the function to be called
84
82
* @param t the time between calls in seconds
83
+ * @deprecated Pass a chrono duration, not a float second count. For example use `10ms` rather than `0.01f`.
85
84
*/
86
85
#if defined(__ICCARM__)
86
+ MBED_DEPRECATED_SINCE (" mbed-os-6.0.0" , " Pass a chrono duration, not a float second count. For example use `10ms` rather than `0.01f`." )
87
87
MBED_FORCEINLINE template <typename F>
88
88
#else
89
89
template <typename F> MBED_FORCEINLINE
90
+ MBED_DEPRECATED_SINCE (" mbed-os-6.0.0" , " Pass a chrono duration, not a float second count. For example use `10ms` rather than `0.01f`." )
90
91
#endif
91
92
void attach (F &&func, float t)
92
93
{
93
- attach_us (std::forward<F>(func), t * 1000000 .0f );
94
+ auto float_interval = std::chrono::duration<float >(t);
95
+ attach (std::forward<F>(func), std::chrono::duration_cast<std::chrono::microseconds>(float_interval));
94
96
}
95
97
96
-
97
98
/* * Attach a function to be called by the Ticker, specifying the interval in microseconds
98
99
*
99
100
* @param func pointer to the function to be called
@@ -102,32 +103,65 @@ class Ticker : public TimerEvent, private NonCopyable<Ticker> {
102
103
* @note setting @a t to a value shorter than it takes to process the ticker callback
103
104
* causes the system to hang. Ticker callback is called constantly with no time
104
105
* for threads scheduling.
106
+ * @deprecated Pass a chrono duration, not an integer microsecond count. For example use `10ms` rather than `10000`.
105
107
*
106
108
*/
109
+ MBED_DEPRECATED_SINCE (" mbed-os-6.0.0" , " Pass a chrono duration, not an integer microsecond count. For example use `10ms` rather than `10000`." )
107
110
void attach_us (Callback<void ()> func, us_timestamp_t t);
108
111
109
-
110
- virtual ~Ticker ()
111
- {
112
- detach ();
113
- }
112
+ /* * Attach a function to be called by the Ticker, specifying the interval in microseconds
113
+ *
114
+ * @param func pointer to the function to be called
115
+ * @param t the time between calls in micro-seconds
116
+ *
117
+ * @note setting @a t to a value shorter than it takes to process the ticker callback
118
+ * causes the system to hang. Ticker callback is called constantly with no time
119
+ * for threads scheduling.
120
+ *
121
+ */
122
+ void attach (Callback<void ()> func, std::chrono::microseconds t);
114
123
115
124
/* * Detach the function
116
125
*/
117
126
void detach ();
118
127
119
128
#if !defined(DOXYGEN_ONLY)
120
129
protected:
121
- void setup ( us_timestamp_t t );
122
- virtual void handler ( );
130
+ TickerBase ( const ticker_data_t *data );
131
+ TickerBase ( const ticker_data_t *data, bool lock_deepsleep );
123
132
124
- protected:
125
- us_timestamp_t _delay; /* *< Time delay (in microseconds) for resetting the multishot callback. */
133
+ ~TickerBase ()
134
+ {
135
+ detach ();
136
+ }
137
+
138
+ /* * Attach a function to be called by the Ticker, specifying the absolute call time
139
+ *
140
+ * If used, handler must be overridden, as TickerBase::handler would attempt
141
+ * to reschedule. This is done by `TimeoutBase` (used by `Timeout` and `LowPowerTimeout`).
142
+ *
143
+ * @param func pointer to the function to be called
144
+ * @param abs_time the time for the call
145
+ *
146
+ * @note setting @a abs_time to a time in the past means the event will be scheduled immediately
147
+ * resulting in an instant call to the function.
148
+ */
149
+ void attach_absolute (Callback<void ()> func, TickerDataClock::time_point abs_time);
150
+
151
+ void handler () override ;
152
+ std::chrono::microseconds _delay{0 }; /* *< Time delay (in microseconds) for resetting the multishot callback. */
126
153
Callback<void ()> _function; /* *< Callback. */
127
154
bool _lock_deepsleep; /* *< Flag which indicates if deep sleep should be disabled. */
128
155
#endif
156
+ private:
157
+ void setup (std::chrono::microseconds t);
158
+ void setup_absolute (TickerDataClock::time_point t);
129
159
};
130
160
161
+ class Ticker : public TickerBase {
162
+ public:
163
+ Ticker ();
164
+ };
131
165
/* * @}*/
132
166
133
167
} // namespace mbed
0 commit comments