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
pull/11236/head
Hugues Kamba 2019-08-15 07:14:54 +01:00
parent 101ae73b87
commit e1714ec53a
4 changed files with 30 additions and 10 deletions

View File

@ -142,7 +142,7 @@ void test_detach(void)
{
LowPowerTicker ticker;
bool ret;
const float ticker_time_s = 0.1f;
const s_timestamp_t ticker_time_s = 1;
const uint32_t wait_time_ms = 500;
Semaphore sem(0, 1);
@ -171,7 +171,7 @@ void test_attach_time(void)
gtimer.reset();
gtimer.start();
ticker.attach(callback(stop_gtimer_set_flag), ((float)DELAY_US) / 1000000.0f);
ticker.attach(callback(stop_gtimer_set_flag), MICROSECONDS_TO_SECONDS(DELAY_US));
while (!ticker_callback_flag);
ticker.detach();
const int time_diff = gtimer.read_us();

View File

@ -264,7 +264,7 @@ void test_detach(void)
{
Ticker ticker;
bool ret;
const float ticker_time_s = 0.1f;
const s_timestamp_t ticker_time_s = 1;
const uint32_t wait_time_ms = 500;
Semaphore sem(0, 1);
@ -293,7 +293,7 @@ void test_attach_time(void)
gtimer.reset();
gtimer.start();
ticker.attach(callback(stop_gtimer_set_flag), ((float)DELAY_US) / 1000000.0f);
ticker.attach(callback(stop_gtimer_set_flag), MICROSECONDS_TO_SECONDS(DELAY_US));
while (!ticker_callback_flag);
ticker.detach();
const int time_diff = gtimer.read_us();

View File

@ -78,9 +78,9 @@ public:
* @param func pointer to the function to be called
* @param t the time between calls in seconds
*/
void attach(Callback<void()> func, float t)
void attach(Callback<void()> func, const s_timestamp_t t)
{
attach_us(func, t * 1000000.0f);
attach_us(func, SECONDS_TO_MICROSECONDS(t));
}
/** Attach a member function to be called by the Ticker, specifying the interval in seconds
@ -96,10 +96,10 @@ public:
MBED_DEPRECATED_SINCE("mbed-os-5.1",
"The attach function does not support cv-qualifiers. Replaced by "
"attach(callback(obj, method), t).")
void attach(T *obj, M method, float t)
{
attach(callback(obj, method), t);
}
void attach(T *obj, M method, float t)
{
attach(callback(obj, method), (s_timestamp_t)t);
}
/** Attach a function to be called by the Ticker, specifying the interval in microseconds
*

View File

@ -24,6 +24,21 @@
#include <stdbool.h>
#include "device.h"
/**
* Number of microseconds in a second
*/
#define MICROSECONDS_IN_SECOND (us_timestamp_t)1000000
/**
* Converts seconds to microseconds
*/
#define SECONDS_TO_MICROSECONDS(SECONDS) (us_timestamp_t)(MICROSECONDS_IN_SECOND * SECONDS)
/**
* Converts microseconds to seconds
*/
#define MICROSECONDS_TO_SECONDS(MICROSECONDS) (s_timestamp_t)(MICROSECONDS / MICROSECONDS_IN_SECOND)
/**
* Legacy format representing a timestamp in us.
* Given it is modeled as a 32 bit integer, this type can represent timestamp
@ -38,6 +53,11 @@ typedef uint32_t timestamp_t;
*/
typedef uint64_t us_timestamp_t;
/**
* A second timestamp stored in a 64 bit integer.
*/
typedef uint64_t s_timestamp_t;
/** Ticker's event structure
*/
typedef struct ticker_event_s {