RTOS Wrapper
API reference
Loading...
Searching...
No Matches
PeriodicTimer.h
Go to the documentation of this file.
1#pragma once
7#include "OsUtility.h"
8
14public:
16 PeriodicTimer() noexcept : timer{}, created(false) {}
17
20
31 bool Create(const char* name, void (*callback)(uint32_t), uint32_t arg,
32 uint32_t periodMs, bool autoStart = false) noexcept {
33 if (created) return false;
34 uint32_t ticks = os_convert_msec_to_delay_ticks(periodMs);
35 created = os_timer_create_ex(timer, name, callback, arg, ticks, ticks,
36 autoStart ? OS_AUTO_START : OS_DONT_START);
37 return created;
38 }
39
41 bool Start() noexcept { return os_timer_activate_ex(timer); }
42
44 bool Stop() noexcept { return os_timer_deactivate_ex(timer); }
45
47 bool Destroy() noexcept {
48 if (!created) {
49 return true;
50 }
52 if (res) created = false;
53 return res;
54 }
55
57 bool IsValid() const noexcept { return created; }
58
59private:
61 bool created;
62};
63
C-facing helper functions for delays, cycle counters, and creation of RTOS handles (queues,...
bool os_timer_deactivate_ex(OS_Timer &timer, bool suppressVerbose=true) noexcept
Deactivates a OS timer.
Definition OsUtility.cpp:707
constexpr uint32_t os_convert_msec_to_delay_ticks(uint32_t milliseconds)
Converts milliseconds to delay ticks, rounding down but never to zero.
Definition OsUtility.h:74
bool os_timer_create_ex(OS_Timer &timer, const char *name, void(*callback)(uint32_t), uint32_t callbackExpirationInput, uint32_t initialTimeoutTicks, uint32_t rescheduleTimeoutTicks, OS_Uint autoActivate, bool suppressVerbose=true) noexcept
Creates a OS timer and reports any errors associated with the creation.
Definition OsUtility.cpp:627
bool os_timer_activate_ex(OS_Timer &timer, bool suppressVerbose=true) noexcept
Activates a OS timer.
Definition OsUtility.cpp:683
bool os_timer_deactivate_and_delete_ex(OS_Timer &timer, bool suppressVerbose=true) noexcept
Stops and deletes a OS timer, reporting any errors associated with the operation.
Definition OsUtility.cpp:659
Simple RAII wrapper around OS timers.
Definition PeriodicTimer.h:13
bool Destroy() noexcept
Definition PeriodicTimer.h:47
bool Stop() noexcept
Definition PeriodicTimer.h:44
PeriodicTimer() noexcept
Definition PeriodicTimer.h:16
~PeriodicTimer()
Definition PeriodicTimer.h:19
bool Create(const char *name, void(*callback)(uint32_t), uint32_t arg, uint32_t periodMs, bool autoStart=false) noexcept
Create a periodic timer.
Definition PeriodicTimer.h:31
OS_Timer timer
Definition PeriodicTimer.h:60
bool created
Definition PeriodicTimer.h:61
bool Start() noexcept
Definition PeriodicTimer.h:41
bool IsValid() const noexcept
Definition PeriodicTimer.h:57
@ OS_AUTO_START
Definition os_cmsis.h:54
@ OS_DONT_START
Definition os_cmsis.h:55
osTimerId_t OS_Timer
Definition os_cmsis.h:32