Merge pull request 'timer-callbacks' (#10) from timer-callbacks into main

Reviewed-on: https://git.intern.spaceteamaachen.de/ALPAKA/rtos2-utils/pulls/10
Reviewed-by: carlwachter <carlwachter@noreply.git.intern.spaceteamaachen.de>
This commit is contained in:
carlwachter
2023-09-27 12:10:42 +00:00
3 changed files with 102 additions and 16 deletions

View File

@@ -9,6 +9,9 @@
#include <cmsis_os2.h>
#include <sta/timer.hpp>
#include <functional>
namespace sta
{
/**
@@ -19,17 +22,56 @@ namespace sta
class RtosTimer : public Timer
{
public:
RtosTimer();
/**
* @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;
/**
* @returns True of the timmer is currently running.
*/
bool isRunning() override;
private:
static void timeoutHandler(void* arg);
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. */
bool running_; /**< The current state of the timer. Running or not? */
};
} // namespace sta