Skip to content

Commit e1714ec

Browse files
committed
Require integer to specify callback interval with Ticker API
* Create new `s_timestamp_t` type to specify timestamp is seconds * Alter `attach()` API to expect `s_timestamp_t` for interval value * Create helper macro to convert seconds to milliseconds and help code readability * Modify Greentea tests accordingly
1 parent 101ae73 commit e1714ec

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

TESTS/mbed_drivers/lp_ticker/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ void test_detach(void)
142142
{
143143
LowPowerTicker ticker;
144144
bool ret;
145-
const float ticker_time_s = 0.1f;
145+
const s_timestamp_t ticker_time_s = 1;
146146
const uint32_t wait_time_ms = 500;
147147
Semaphore sem(0, 1);
148148

@@ -171,7 +171,7 @@ void test_attach_time(void)
171171

172172
gtimer.reset();
173173
gtimer.start();
174-
ticker.attach(callback(stop_gtimer_set_flag), ((float)DELAY_US) / 1000000.0f);
174+
ticker.attach(callback(stop_gtimer_set_flag), MICROSECONDS_TO_SECONDS(DELAY_US));
175175
while (!ticker_callback_flag);
176176
ticker.detach();
177177
const int time_diff = gtimer.read_us();

TESTS/mbed_drivers/ticker/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ void test_detach(void)
264264
{
265265
Ticker ticker;
266266
bool ret;
267-
const float ticker_time_s = 0.1f;
267+
const s_timestamp_t ticker_time_s = 1;
268268
const uint32_t wait_time_ms = 500;
269269
Semaphore sem(0, 1);
270270

@@ -293,7 +293,7 @@ void test_attach_time(void)
293293

294294
gtimer.reset();
295295
gtimer.start();
296-
ticker.attach(callback(stop_gtimer_set_flag), ((float)DELAY_US) / 1000000.0f);
296+
ticker.attach(callback(stop_gtimer_set_flag), MICROSECONDS_TO_SECONDS(DELAY_US));
297297
while (!ticker_callback_flag);
298298
ticker.detach();
299299
const int time_diff = gtimer.read_us();

drivers/Ticker.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ class Ticker : public TimerEvent, private NonCopyable<Ticker> {
7878
* @param func pointer to the function to be called
7979
* @param t the time between calls in seconds
8080
*/
81-
void attach(Callback<void()> func, float t)
81+
void attach(Callback<void()> func, const s_timestamp_t t)
8282
{
83-
attach_us(func, t * 1000000.0f);
83+
attach_us(func, SECONDS_TO_MICROSECONDS(t));
8484
}
8585

8686
/** Attach a member function to be called by the Ticker, specifying the interval in seconds
@@ -96,10 +96,10 @@ class Ticker : public TimerEvent, private NonCopyable<Ticker> {
9696
MBED_DEPRECATED_SINCE("mbed-os-5.1",
9797
"The attach function does not support cv-qualifiers. Replaced by "
9898
"attach(callback(obj, method), t).")
99-
void attach(T *obj, M method, float t)
100-
{
101-
attach(callback(obj, method), t);
102-
}
99+
void attach(T *obj, M method, float t)
100+
{
101+
attach(callback(obj, method), (s_timestamp_t)t);
102+
}
103103

104104
/** Attach a function to be called by the Ticker, specifying the interval in microseconds
105105
*

hal/ticker_api.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@
2424
#include <stdbool.h>
2525
#include "device.h"
2626

27+
/**
28+
* Number of microseconds in a second
29+
*/
30+
#define MICROSECONDS_IN_SECOND (us_timestamp_t)1000000
31+
32+
/**
33+
* Converts seconds to microseconds
34+
*/
35+
#define SECONDS_TO_MICROSECONDS(SECONDS) (us_timestamp_t)(MICROSECONDS_IN_SECOND * SECONDS)
36+
37+
/**
38+
* Converts microseconds to seconds
39+
*/
40+
#define MICROSECONDS_TO_SECONDS(MICROSECONDS) (s_timestamp_t)(MICROSECONDS / MICROSECONDS_IN_SECOND)
41+
2742
/**
2843
* Legacy format representing a timestamp in us.
2944
* Given it is modeled as a 32 bit integer, this type can represent timestamp
@@ -38,6 +53,11 @@ typedef uint32_t timestamp_t;
3853
*/
3954
typedef uint64_t us_timestamp_t;
4055

56+
/**
57+
* A second timestamp stored in a 64 bit integer.
58+
*/
59+
typedef uint64_t s_timestamp_t;
60+
4161
/** Ticker's event structure
4262
*/
4363
typedef struct ticker_event_s {

0 commit comments

Comments
 (0)