1717#ifndef MBED_TICKER_H
1818#define MBED_TICKER_H
1919
20+ #include < chrono>
2021#include < mstd_utility>
22+ #include " drivers/TickerDataClock.h"
2123#include " drivers/TimerEvent.h"
2224#include " platform/Callback.h"
2325#include " platform/mbed_toolchain.h"
2426#include " platform/NonCopyable.h"
2527#include " hal/lp_ticker_api.h"
2628
2729namespace mbed {
30+
2831/* *
2932 * \defgroup drivers_Ticker Ticker class
3033 * \ingroup drivers-public-api-ticker
@@ -42,6 +45,7 @@ namespace mbed {
4245 * // Toggle the blinking LED after 5 seconds
4346 *
4447 * #include "mbed.h"
48+ * using namespace std::chrono;
4549 *
4650 * Ticker timer;
4751 * DigitalOut led1(LED1);
@@ -54,26 +58,20 @@ namespace mbed {
5458 * }
5559 *
5660 * int main() {
57- * timer.attach(&attime, 5 );
61+ * timer.attach(&attime, 5us );
5862 * while(1) {
5963 * if(flip == 0) {
6064 * led1 = !led1;
6165 * } else {
6266 * led2 = !led2;
6367 * }
64- * ThisThread::sleep_for(200 );
68+ * ThisThread::sleep_for(200ms );
6569 * }
6670 * }
6771 * @endcode
6872 */
69- class Ticker : public TimerEvent , private NonCopyable <Ticker> {
70-
73+ class TickerBase : public TimerEvent , private NonCopyable <TickerBase> {
7174public:
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-
7775 /* * Attach a function to be called by the Ticker, specifying the interval in seconds
7876 *
7977 * The method forwards its arguments to attach_us() rather than copying them which
@@ -82,18 +80,21 @@ class Ticker : public TimerEvent, private NonCopyable<Ticker> {
8280 * possible given attach_us() expects an integer value for the callback interval.
8381 * @param func pointer to the function to be called
8482 * @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`.
8584 */
8685#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`." )
8787 MBED_FORCEINLINE template <typename F>
8888#else
8989 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`." )
9091#endif
9192 void attach (F &&func, float t)
9293 {
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));
9496 }
9597
96-
9798 /* * Attach a function to be called by the Ticker, specifying the interval in microseconds
9899 *
99100 * @param func pointer to the function to be called
@@ -102,32 +103,65 @@ class Ticker : public TimerEvent, private NonCopyable<Ticker> {
102103 * @note setting @a t to a value shorter than it takes to process the ticker callback
103104 * causes the system to hang. Ticker callback is called constantly with no time
104105 * for threads scheduling.
106+ * @deprecated Pass a chrono duration, not an integer microsecond count. For example use `10ms` rather than `10000`.
105107 *
106108 */
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`." )
107110 void attach_us (Callback<void ()> func, us_timestamp_t t);
108111
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);
114123
115124 /* * Detach the function
116125 */
117126 void detach ();
118127
119128#if !defined(DOXYGEN_ONLY)
120129protected:
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 );
123132
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. */
126153 Callback<void ()> _function; /* *< Callback. */
127154 bool _lock_deepsleep; /* *< Flag which indicates if deep sleep should be disabled. */
128155#endif
156+ private:
157+ void setup (std::chrono::microseconds t);
158+ void setup_absolute (TickerDataClock::time_point t);
129159};
130160
161+ class Ticker : public TickerBase {
162+ public:
163+ Ticker ();
164+ };
131165/* * @}*/
132166
133167} // namespace mbed
0 commit comments