mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/rtos2-utils.git
synced 2025-06-10 01:55:59 +00:00
83 lines
2.1 KiB
C++
83 lines
2.1 KiB
C++
/**
|
|
* @file
|
|
* @brief RTOS timer implementation.
|
|
*/
|
|
|
|
#ifndef STA_RTOS_TIMER_HPP
|
|
#define STA_RTOS_TIMER_HPP
|
|
|
|
#include <cmsis_os2.h>
|
|
#include <sta/timer.hpp>
|
|
|
|
#include <functional>
|
|
#include "cmsis_os2.h"
|
|
|
|
|
|
namespace sta
|
|
{
|
|
/**
|
|
* @brief Implementation of Timer using CMSIS RTOS2.
|
|
*
|
|
* @ingroup STA_RTOS_API
|
|
*
|
|
* @defgroup STA_RTOS_TIMER Timer
|
|
*/
|
|
class RtosTimer : public Timer
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Creates a new timer with a given callback function.
|
|
*
|
|
* @param callback The callback function to call upon timer timeout.
|
|
* @param arg The argument to pass to the callback function.
|
|
* @param repeating True if the timer should repeat. False if it should only run once.
|
|
*/
|
|
RtosTimer(std::function<void(void*)>, void *arg, bool repeating = false);
|
|
|
|
/**
|
|
* @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(std::function<void(void*)> callback, 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 */
|
|
|
|
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
|
|
|
|
#endif // STA_RTOS_TIMER_HPP
|