From dcc647a062cd63679cb6055790886c502a5f6a2c Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Fri, 31 May 2024 13:54:09 +0200 Subject: [PATCH] Added periodic timers --- include/sta/rtos/timer.hpp | 3 ++- src/timer.cpp | 11 ++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/include/sta/rtos/timer.hpp b/include/sta/rtos/timer.hpp index 6447772..c0b1525 100644 --- a/include/sta/rtos/timer.hpp +++ b/include/sta/rtos/timer.hpp @@ -10,6 +10,7 @@ #include #include +#include "cmsis_os2.h" namespace sta @@ -30,7 +31,7 @@ namespace sta * @param callback The callback function to call upon timer timeout. * @param arg The argument to pass to the callback function. */ - RtosTimer(std::function, void *arg); + RtosTimer(std::function, void *arg, osTimerType_t type = osTimerOnce); /** * @brief Initializes the timer with a callback that has no effect. diff --git a/src/timer.cpp b/src/timer.cpp index 7a7d6d3..d372294 100644 --- a/src/timer.cpp +++ b/src/timer.cpp @@ -2,17 +2,18 @@ #include #include +#include "cmsis_os2.h" namespace sta { - RtosTimer::RtosTimer(std::function callback, void *arg) + RtosTimer::RtosTimer(std::function callback, void *arg, osTimerType_t type /*= osTimerOnce */) : timer_id_{NULL}, - timer_attr_{.name="Timer", .attr_bits=osTimerOnce}, + timer_attr_{.name="Timer", .attr_bits=type}, callback_{callback}, callbackArg_{arg}, running_{false} { - // Pass an anonymous function as the callback which will invoke the currently registered callback. - 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!"); } @@ -68,7 +69,7 @@ namespace sta { void RtosTimer::timeoutHandler(void* arg) { RtosTimer* timer = static_cast(arg); - timer->running_ = false; + timer->running_ = timer->isRunning(); timer->callback_(timer->callbackArg_); }