Skip to content

Commit a140ddd

Browse files
committed
Add Chrono support to Ticker et al
1 parent ad07352 commit a140ddd

14 files changed

+555
-139
lines changed

drivers/LowPowerTicker.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
#include "platform/platform.h"
2121
#include "drivers/Ticker.h"
22-
#include "platform/NonCopyable.h"
2322

2423
#if defined (DEVICE_LPTICKER) || defined(DOXYGEN_ONLY)
2524

@@ -40,16 +39,9 @@ namespace mbed {
4039
*
4140
* @note Synchronization level: Interrupt safe
4241
*/
43-
class LowPowerTicker : public Ticker, private NonCopyable<LowPowerTicker> {
44-
42+
class LowPowerTicker : public TickerBase {
4543
public:
46-
LowPowerTicker() : Ticker(get_lp_ticker_data())
47-
{
48-
}
49-
50-
virtual ~LowPowerTicker()
51-
{
52-
}
44+
LowPowerTicker();
5345
};
5446

5547
/** @}*/

drivers/LowPowerTimeout.h

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@
2121

2222
#if DEVICE_LPTICKER || defined(DOXYGEN_ONLY)
2323

24-
#include "hal/lp_ticker_api.h"
25-
#include "drivers/LowPowerTicker.h"
26-
#include "platform/NonCopyable.h"
24+
#include "drivers/LowPowerClock.h"
25+
#include "drivers/Timeout.h"
2726

2827
namespace mbed {
2928
/**
@@ -36,14 +35,37 @@ namespace mbed {
3635
*
3736
* @note Synchronization level: Interrupt safe
3837
*/
39-
class LowPowerTimeout : public LowPowerTicker, private NonCopyable<LowPowerTimeout> {
40-
#if !defined(DOXYGEN_ONLY)
41-
private:
42-
virtual void handler(void)
38+
class LowPowerTimeout : public TimeoutBase {
39+
public:
40+
LowPowerTimeout();
41+
42+
/** Clock to use with attach_absolute, guaranteeing running only while attached or manually locked */
43+
using clock = LowPowerClock;
44+
45+
/** Clock to use with attach_absolute, running always */
46+
using steady_clock = LowPowerClock;
47+
48+
/** @copydoc TimeoutBase::scheduled_time() */
49+
LowPowerClock::time_point scheduled_time() const
4350
{
44-
_function.call();
51+
/* Massage from virtual TickerDataClock::time_point used internally to true LowPowerClock::time_point */
52+
return LowPowerClock::time_point{TimeoutBase::scheduled_time().time_since_epoch()};
53+
}
54+
55+
/** Attach a function to be called by the Timeout, specifying the absolute time
56+
*
57+
* @param func pointer to the function to be called
58+
* @param abs_time the absolute time for the call, referenced to LowPowerClock
59+
*
60+
* @note setting @a abs_time to a time in the past means the event will be scheduled immediately
61+
* resulting in an instant call to the function.
62+
*/
63+
template <class F>
64+
void attach_absolute(F &&func, LowPowerClock::time_point abs_time)
65+
{
66+
/* Massage from true LowPowerClock::time_point to virtual TickerDataClock::time_point used internally */
67+
TimeoutBase::attach_absolute(std::forward<F>(func), TickerDataClock::time_point{abs_time.time_since_epoch()});
4568
}
46-
#endif
4769
};
4870

4971
/** @}*/

drivers/LowPowerTimer.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,9 @@ namespace mbed {
3636
*
3737
* @note Synchronization level: Interrupt safe
3838
*/
39-
class LowPowerTimer : public Timer, private NonCopyable<LowPowerTimer> {
40-
39+
class LowPowerTimer : public TimerBase {
4140
public:
42-
LowPowerTimer() : Timer(get_lp_ticker_data())
43-
{
44-
}
45-
41+
LowPowerTimer();
4642
};
4743

4844
/** @}*/

drivers/Ticker.h

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@
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

2729
namespace 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> {
7174
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-
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)
120129
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);
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

Comments
 (0)