Skip to content

Commit 239f31b

Browse files
authored
Merge pull request #2199 from neilt6/rtos-timer-update
[RTOS] Updated RtosTimer to use Callback
2 parents 37e254f + 2133dad commit 239f31b

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
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: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
#include <stdint.h>
2626
#include "cmsis_os.h"
27+
#include "Callback.h"
28+
#include "toolchain.h"
2729

2830
namespace rtos {
2931

@@ -36,21 +38,41 @@ namespace rtos {
3638
*/
3739
class RtosTimer {
3840
public:
39-
/** Create and Start timer.
40-
@param task name of the timer call back function.
41+
/** Create timer.
42+
@param func function to be executed by this timer.
4143
@param type osTimerOnce for one-shot or osTimerPeriodic for periodic behaviour. (default: osTimerPeriodic)
4244
@param argument argument to the timer call back function. (default: NULL)
45+
@deprecated Replaced with RtosTimer(Callback<void()>, os_timer_type)
46+
*/
47+
MBED_DEPRECATED("Replaced with RtosTimer(Callback<void()>, os_timer_type)")
48+
RtosTimer(void (*func)(void const *argument), os_timer_type type=osTimerPeriodic, void *argument=NULL) {
49+
constructor(mbed::Callback<void()>(argument, (void (*)(void *))func), type);
50+
}
51+
52+
/** Create timer.
53+
@param func function to be executed by this timer.
54+
@param type osTimerOnce for one-shot or osTimerPeriodic for periodic behaviour. (default: osTimerPeriodic)
55+
*/
56+
RtosTimer(mbed::Callback<void()> func, os_timer_type type=osTimerPeriodic) {
57+
constructor(func, type);
58+
}
59+
60+
/** Create timer.
61+
@param obj pointer to the object to call the member function on.
62+
@param method member function to be executed by this timer.
63+
@param type osTimerOnce for one-shot or osTimerPeriodic for periodic behaviour. (default: osTimerPeriodic)
4364
*/
44-
RtosTimer(void (*task)(void const *argument),
45-
os_timer_type type=osTimerPeriodic,
46-
void *argument=NULL);
65+
template <typename T, typename M>
66+
RtosTimer(T *obj, M method, os_timer_type type=osTimerPeriodic) {
67+
constructor(mbed::Callback<void()>(obj, method), type);
68+
}
4769

4870
/** Stop the timer.
4971
@return status code that indicates the execution status of the function.
5072
*/
5173
osStatus stop(void);
5274

53-
/** start a timer.
75+
/** Start the timer.
5476
@param millisec time delay value of the timer.
5577
@return status code that indicates the execution status of the function.
5678
*/
@@ -59,6 +81,11 @@ class RtosTimer {
5981
~RtosTimer();
6082

6183
private:
84+
// Required to share definitions without
85+
// delegated constructors
86+
void constructor(mbed::Callback<void()> func, os_timer_type type);
87+
88+
mbed::Callback<void()> _function;
6289
osTimerId _timer_id;
6390
osTimerDef_t _timer;
6491
#if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)

0 commit comments

Comments
 (0)