Skip to content

Commit 511117a

Browse files
committed
[RTOS] Updated RtosTimer to use Callback
Updated the RtosTimer class to use Callback in order to be consistent with the updated Thread class.
1 parent fd757d3 commit 511117a

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

rtos/rtos/RtosTimer.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,21 @@
2323

2424
#include <string.h>
2525

26+
#include "mbed.h"
2627
#include "cmsis_os.h"
2728
#include "mbed_error.h"
2829

2930
namespace rtos {
3031

31-
RtosTimer::RtosTimer(void (*periodic_task)(void const *argument), os_timer_type type, void *argument) {
32+
void RtosTimer::constructor(mbed::Callback<void()> func, os_timer_type type) {
3233
#ifdef CMSIS_OS_RTX
33-
_timer.ptimer = periodic_task;
34+
_timer.ptimer = (void (*)(const void *))Callback<void()>::thunk;
3435

3536
memset(_timer_data, 0, sizeof(_timer_data));
3637
_timer.timer = _timer_data;
3738
#endif
38-
_timer_id = osTimerCreate(&_timer, type, argument);
39+
_function = func;
40+
_timer_id = osTimerCreate(&_timer, type, &_function);
3941
}
4042

4143
osStatus RtosTimer::start(uint32_t millisec) {

rtos/rtos/RtosTimer.h

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include <stdint.h>
2626
#include "cmsis_os.h"
27+
#include "Callback.h"
2728

2829
namespace rtos {
2930

@@ -36,21 +37,30 @@ namespace rtos {
3637
*/
3738
class RtosTimer {
3839
public:
39-
/** Create and Start timer.
40-
@param task name of the timer call back function.
40+
/** Create timer.
41+
@param func function to be executed by this timer.
4142
@param type osTimerOnce for one-shot or osTimerPeriodic for periodic behaviour. (default: osTimerPeriodic)
42-
@param argument argument to the timer call back function. (default: NULL)
4343
*/
44-
RtosTimer(void (*task)(void const *argument),
45-
os_timer_type type=osTimerPeriodic,
46-
void *argument=NULL);
44+
RtosTimer(mbed::Callback<void()> func, os_timer_type type=osTimerPeriodic) {
45+
constructor(func, type);
46+
}
47+
48+
/** Create timer.
49+
@param obj pointer to the object to call the member function on.
50+
@param method member function to be executed by this timer.
51+
@param type osTimerOnce for one-shot or osTimerPeriodic for periodic behaviour. (default: osTimerPeriodic)
52+
*/
53+
template <typename T, typename M>
54+
RtosTimer(T *obj, M method, os_timer_type type=osTimerPeriodic) {
55+
constructor(mbed::Callback<void()>(obj, method), type);
56+
}
4757

4858
/** Stop the timer.
4959
@return status code that indicates the execution status of the function.
5060
*/
5161
osStatus stop(void);
5262

53-
/** start a timer.
63+
/** Start the timer.
5464
@param millisec time delay value of the timer.
5565
@return status code that indicates the execution status of the function.
5666
*/
@@ -59,6 +69,11 @@ class RtosTimer {
5969
~RtosTimer();
6070

6171
private:
72+
// Required to share definitions without
73+
// delegated constructors
74+
void constructor(mbed::Callback<void()> func, os_timer_type type);
75+
76+
mbed::Callback<void()> _function;
6277
osTimerId _timer_id;
6378
osTimerDef_t _timer;
6479
#if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)

0 commit comments

Comments
 (0)