mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/rtos2-utils.git
synced 2025-06-12 10:45:58 +00:00
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:
commit
1028264eef
@ -9,6 +9,9 @@
|
|||||||
#include <cmsis_os2.h>
|
#include <cmsis_os2.h>
|
||||||
#include <sta/timer.hpp>
|
#include <sta/timer.hpp>
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
|
||||||
namespace sta
|
namespace sta
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@ -19,17 +22,56 @@ namespace sta
|
|||||||
class RtosTimer : public Timer
|
class RtosTimer : public Timer
|
||||||
{
|
{
|
||||||
public:
|
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(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;
|
void start(uint32_t millis) override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Stop the timer.
|
||||||
|
*/
|
||||||
void stop() override;
|
void stop() override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns True of the timmer is currently running.
|
||||||
|
*/
|
||||||
bool isRunning() override;
|
bool isRunning() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
static void timeoutHandler(void* arg);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
osTimerId_t timer_id_; /**< CMSIS RTOS2 Timer */
|
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
|
} // namespace sta
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#ifdef STA_RTOS_WATCHDOG_ENABLE
|
#ifdef STA_RTOS_WATCHDOG_ENABLE
|
||||||
|
|
||||||
#include <sta/debug/assert.hpp>
|
#include <sta/debug/assert.hpp>
|
||||||
|
#include <sta/debug/debug.hpp>
|
||||||
#include <sta/lang.hpp>
|
#include <sta/lang.hpp>
|
||||||
#include <sta/rtos/defs.hpp>
|
#include <sta/rtos/defs.hpp>
|
||||||
#include <sta/rtos/system/events.hpp>
|
#include <sta/rtos/system/events.hpp>
|
||||||
|
@ -2,31 +2,74 @@
|
|||||||
#include <sta/debug/debug.hpp>
|
#include <sta/debug/debug.hpp>
|
||||||
#include <sta/debug/assert.hpp>
|
#include <sta/debug/assert.hpp>
|
||||||
|
|
||||||
namespace sta {
|
|
||||||
RtosTimer::RtosTimer(){}
|
|
||||||
|
|
||||||
RtosTimer::RtosTimer(void (*callback)(void *arg), void *arg) {
|
namespace sta {
|
||||||
timer_id_ = osTimerNew(callback, osTimerOnce, arg, NULL);
|
RtosTimer::RtosTimer(void (*callback)(void *arg), void *arg)
|
||||||
STA_ASSERT_MSG(timer_id_ != NULL, "Failed to initialize timer");
|
: timer_id_{NULL},
|
||||||
|
timer_attr_{.name="Timer", .attr_bits=osTimerOnce},
|
||||||
|
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_);
|
||||||
|
|
||||||
|
STA_ASSERT_MSG(timer_id_ != NULL, "Failed to create timer!");
|
||||||
}
|
}
|
||||||
|
|
||||||
RtosTimer::~RtosTimer() {
|
RtosTimer::RtosTimer()
|
||||||
|
: RtosTimer([](void *){}, NULL)
|
||||||
|
{}
|
||||||
|
|
||||||
|
RtosTimer::~RtosTimer()
|
||||||
|
{
|
||||||
|
if (isRunning()) stop();
|
||||||
|
|
||||||
osTimerDelete(timer_id_);
|
osTimerDelete(timer_id_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RtosTimer::start(uint32_t millis) {
|
void RtosTimer::setCallback(void (*callback)(void *arg), void *arg)
|
||||||
|
{
|
||||||
|
callback_ = callback;
|
||||||
|
callbackArg_ = arg;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RtosTimer::start(uint32_t millis)
|
||||||
|
{
|
||||||
osStatus_t status = osTimerStart(timer_id_, millis);
|
osStatus_t status = osTimerStart(timer_id_, millis);
|
||||||
|
|
||||||
if (status != osOK) STA_DEBUG_PRINTLN("Timer start failed");
|
if (status != osOK)
|
||||||
|
{
|
||||||
|
STA_DEBUG_PRINTLN("Timer start failed");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RtosTimer::stop() {
|
running_ = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RtosTimer::stop()
|
||||||
|
{
|
||||||
osStatus_t status = osTimerStop(timer_id_);
|
osStatus_t status = osTimerStop(timer_id_);
|
||||||
|
|
||||||
if (status != osOK) STA_DEBUG_PRINTLN("Timer stop failed");
|
if (status != osOK)
|
||||||
|
{
|
||||||
|
STA_DEBUG_PRINTLN("Timer stop failed");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RtosTimer::isRunning() {
|
running_ = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RtosTimer::isRunning()
|
||||||
|
{
|
||||||
return osTimerIsRunning(timer_id_);
|
return osTimerIsRunning(timer_id_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RtosTimer::timeoutHandler(void* arg)
|
||||||
|
{
|
||||||
|
RtosTimer* timer = static_cast<RtosTimer*>(arg);
|
||||||
|
timer->running_ = false;
|
||||||
|
timer->callback_(timer->callbackArg_);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace sta
|
} // namespace sta
|
||||||
|
Loading…
x
Reference in New Issue
Block a user