Added modifiable callback for timers

This commit is contained in:
dario
2023-09-23 22:42:47 +02:00
parent 5b8c97cf5c
commit 0f02168b25
2 changed files with 70 additions and 12 deletions

View File

@@ -1,28 +1,44 @@
#include <sta/rtos/timer.hpp>
#include <sta/debug/debug.hpp>
#include <sta/debug/assert.hpp>
namespace sta {
RtosTimer::RtosTimer(void (*callback)(void *arg), void *arg) {
timer_attr_.name = "Timer";
timer_attr_.attr_bits = osTimerOnce;
timer_attr_.cb_size = sizeof(osTimerAttr_t);
RtosTimer::RtosTimer(void (*callback)(void *arg), void *arg)
: timer_id_{NULL}, timer_attr_{.name="Timer", .attr_bits=osTimerOnce, .cb_size=sizeof(osTimerAttr_t)}, callback_{callback}, callbackArg_{arg}
{
// Pass an anonymous function as the callback which will invoke the currently registered callback.
timer_id_ = osTimerNew(timeoutHandler, osTimerOnce, arg, &timer_attr_);
timer_id_ = osTimerNew(callback, osTimerOnce, arg, &timer_attr_);
STA_ASSERT_MSG(timer_id_ != NULL, "Failed to create timer!");
}
RtosTimer::~RtosTimer() {
RtosTimer::RtosTimer()
: RtosTimer([](void *){}, NULL)
{}
RtosTimer::~RtosTimer()
{
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);
if (status != osOK) STA_DEBUG_PRINTLN("Timer start failed");
}
void RtosTimer::stop() {
void RtosTimer::stop()
{
osStatus_t status = osTimerStop(timer_id_);
if (status != osOK) STA_DEBUG_PRINTLN("Timer stop failed");
}
} // namespace sta
} // namespace sta