Merge pull request 'feat: periodic timers via rtos timer' (#25) from feat/periodic_timers into main

Reviewed-on: https://git.intern.spaceteamaachen.de/ALPAKA/rtos2-utils/pulls/25
Reviewed-by: dario <dario@noreply.git.intern.spaceteamaachen.de>
This commit is contained in:
dario 2024-06-01 11:55:06 +00:00
commit 10870f0c24
2 changed files with 12 additions and 7 deletions

View File

@ -10,6 +10,7 @@
#include <sta/timer.hpp> #include <sta/timer.hpp>
#include <functional> #include <functional>
#include "cmsis_os2.h"
namespace sta namespace sta
@ -25,12 +26,13 @@ namespace sta
{ {
public: public:
/** /**
* @brief * @brief Creates a new timer with a given callback function.
* *
* @param callback The callback function to call upon timer timeout. * @param callback The callback function to call upon timer timeout.
* @param arg The argument to pass to the callback function. * @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); RtosTimer(std::function<void(void*)>, void *arg, bool repeating = false);
/** /**
* @brief Initializes the timer with a callback that has no effect. * @brief Initializes the timer with a callback that has no effect.

View File

@ -2,17 +2,20 @@
#include <sta/debug/debug.hpp> #include <sta/debug/debug.hpp>
#include <sta/debug/assert.hpp> #include <sta/debug/assert.hpp>
#include "cmsis_os2.h"
namespace sta { namespace sta {
RtosTimer::RtosTimer(std::function<void(void*)> callback, void *arg) RtosTimer::RtosTimer(std::function<void(void*)> callback, void *arg, bool repeating /*= false */)
: timer_id_{NULL}, : timer_id_{NULL},
timer_attr_{.name="Timer", .attr_bits=osTimerOnce}, timer_attr_{.name="Timer", .attr_bits=repeating ? osTimerPeriodic : osTimerOnce},
callback_{callback}, callback_{callback},
callbackArg_{arg}, callbackArg_{arg},
running_{false} running_{false}
{ {
// Pass an anonymous function as the callback which will invoke the currently registered callback. osTimerType_t type = repeating ? osTimerPeriodic : osTimerOnce;
timer_id_ = osTimerNew(timeoutHandler, osTimerOnce, this, &timer_attr_);
// Pass an anonymous function as the callback which will invoke the currently registered callback.
timer_id_ = osTimerNew(timeoutHandler, type, this, &timer_attr_);
STA_ASSERT_MSG(timer_id_ != NULL, "Failed to create timer!"); STA_ASSERT_MSG(timer_id_ != NULL, "Failed to create timer!");
} }
@ -68,7 +71,7 @@ namespace sta {
void RtosTimer::timeoutHandler(void* arg) void RtosTimer::timeoutHandler(void* arg)
{ {
RtosTimer* timer = static_cast<RtosTimer*>(arg); RtosTimer* timer = static_cast<RtosTimer*>(arg);
timer->running_ = false; timer->running_ = timer->isRunning();
timer->callback_(timer->callbackArg_); timer->callback_(timer->callbackArg_);
} }