Added modifiable callback for timers

This commit is contained in:
dario
2023-09-23 22:42:47 +02:00
parent 5b8c97cf5c
commit 0f02168b25
2 changed files with 70 additions and 12 deletions

View File

@@ -9,6 +9,9 @@
#include <cmsis_os2.h>
#include <sta/timer.hpp>
#include <functional>
namespace sta
{
/**
@@ -19,16 +22,55 @@ namespace sta
class RtosTimer : public Timer
{
public:
/**
* @brief
*
* @param callback The callback function to call upon timer timeout.
* @param arg The argument to pass to the callback function.
*/
RtosTimer(void (*callback)(void *arg), void *arg);
~RtosTimer();
/**
* @brief Initializes the timer with a callback that has no effect.
*/
RtosTimer();
~RtosTimer();
public:
/**
* @brief Change the timer's callback to a new function.
*
* @param callback The callback function to call upon timer timeout.
* @param arg The argument to pass to the callback function.
*/
void setCallback(void (*callback)(void *arg), void *arg);
/**
* @brief Run the timer for a given number of milliseconds.
*
* @param millis The number of milliseconds to run the timer for.
*/
void start(uint32_t millis) override;
/**
* @brief Stop the timer.
*/
void stop() override;
private:
static void timeoutHandler(void* arg)
{
RtosTimer* timer = static_cast<RtosTimer*>(arg);
timer->callback_(timer->callbackArg_);
}
private:
osTimerId_t timer_id_; /**< CMSIS RTOS2 Timer */
osTimerAttr_t timer_attr_; /**< CMSIS RTOS2 Timer attributes */
osTimerAttr_t timer_attr_; /**< CMSIS RTOS2 Timer attributes */
std::function<void(void*)> callback_; /**< The callback to call on timeout. */
void* callbackArg_; /**< The argument to pass to the callback. */
};
} // namespace sta
#endif // STA_RTOS_TIMER_HPP
#endif // STA_RTOS_TIMER_HPP