Zephyr C++ Toolkit
Loading...
Searching...
No Matches
Timer.hpp
Go to the documentation of this file.
1#pragma once
2
3//================================================================================================//
4// FORWARD DECLARATIONS
5//================================================================================================//
6
7class Timer;
8
9//================================================================================================//
10// INCLUDES
11//================================================================================================//
12
13#include <cstdint>
14#include <cstddef>
15#include <functional>
16
17#include <zephyr/kernel.h>
18#include <zephyr/logging/log.h>
19
20namespace zct {
21
22// Don't use constexpr here, it seg faults!
23// static constexpr int LOG_LEVEL = LOG_LEVEL_DBG;
24#define ZCT_TIMER_LOG_LEVEL LOG_LEVEL_WRN
25
26//================================================================================================//
27// CLASS DECLARATION
28//================================================================================================//
29
39class Timer {
40public:
41
51 Timer(const char* name, std::function<void()> expiryCallback) :
52 m_name(name),
53 m_expiryCallback(expiryCallback)
54 {
55 }
56
63 void start(int64_t period_ms) {
64 // Convert ms to ticks
65 start(period_ms, period_ms);
66 }
67
74 void start(int64_t startDuration_ms, int64_t period_ms) {
75 LOG_MODULE_DECLARE(zct_Timer, ZCT_TIMER_LOG_LEVEL);
76 __ASSERT_NO_MSG(startDuration_ms >= 0); // Start time can be 0, which means the timer will fire immediately. Can't be negative!
77 __ASSERT_NO_MSG(period_ms >= -1); // Period can be -1, which means the timer will not repeat
78
79 if (!this->m_isRegistered) {
80 LOG_WRN("Timer \"%s\" is not registered with a timer manager. Expiry events will not be handled.", this->m_name);
81 }
82
83 this->startTime_ticks = k_uptime_ticks();
84 // Use ceil and not floor to guarantee a minimum delay
85 this->nextExpiryTime_ticks = this->startTime_ticks + k_ms_to_ticks_ceil64(startDuration_ms);
86 if (period_ms == -1) {
87 this->period_ticks = -1;
88 } else {
89 this->period_ticks = k_ms_to_ticks_ceil64(period_ms);
90 }
91 this->m_isRunning = true;
92 }
93
101 void stop() {
102 this->m_isRunning = false;
103 this->period_ticks = -1;
104 this->startTime_ticks = 0;
105 this->nextExpiryTime_ticks = 0;
106 }
107
113 bool isRunning() const { return this->m_isRunning; }
114
123 LOG_MODULE_DECLARE(zct_Timer, ZCT_TIMER_LOG_LEVEL);
124 if (this->period_ticks == -1)
125 {
126 // Timer was one-shot, so stop it
127 this->m_isRunning = false;
128 }
129 else
130 {
131 // Update expiry time based on the period
132 LOG_DBG("Updating timer expiry time. Period: %lld. Next expiry time before update: %lld.", this->period_ticks, this->nextExpiryTime_ticks);
133 this->nextExpiryTime_ticks += this->period_ticks;
134 LOG_DBG("Next expiry time after update: %lld.", this->nextExpiryTime_ticks);
135 }
136 }
137
138
144 int64_t getNextExpiryTimeTicks() const { return this->nextExpiryTime_ticks; }
145
152 void setIsRegistered(bool isRegistered) { this->m_isRegistered = isRegistered; }
153
159 bool getIsRegistered() const { return this->m_isRegistered; }
160
166 void setExpiryCallback(std::function<void()> callback) { m_expiryCallback = callback; }
167
173 const std::function<void()>& getExpiryCallback() const { return m_expiryCallback; }
174
175protected:
176 int64_t period_ticks = 0;
177 int64_t startTime_ticks = 0;
179 bool m_isRunning = false;
180 bool m_isRegistered = false;
181 const char* m_name;
182 std::function<void()> m_expiryCallback;
183};
184
185} // namespace zct
#define ZCT_TIMER_LOG_LEVEL
Definition Timer.hpp:24
A timer that can be used to execute callbacks at regular intervals in an event driven application.
Definition Timer.hpp:39
const std::function< void()> & getExpiryCallback() const
Definition Timer.hpp:173
void setExpiryCallback(std::function< void()> callback)
Definition Timer.hpp:166
bool getIsRegistered() const
Definition Timer.hpp:159
bool m_isRegistered
Definition Timer.hpp:180
void updateAfterExpiry()
Definition Timer.hpp:122
Timer(const char *name, std::function< void()> expiryCallback)
Definition Timer.hpp:51
std::function< void()> m_expiryCallback
Definition Timer.hpp:182
void setIsRegistered(bool isRegistered)
Definition Timer.hpp:152
const char * m_name
Definition Timer.hpp:181
int64_t getNextExpiryTimeTicks() const
Definition Timer.hpp:144
bool m_isRunning
Definition Timer.hpp:179
void start(int64_t startDuration_ms, int64_t period_ms)
Definition Timer.hpp:74
int64_t nextExpiryTime_ticks
Definition Timer.hpp:178
void start(int64_t period_ms)
Definition Timer.hpp:63
int64_t period_ticks
Definition Timer.hpp:176
void stop()
Definition Timer.hpp:101
int64_t startTime_ticks
Definition Timer.hpp:177
bool isRunning() const
Definition Timer.hpp:113
Definition Mutex.hpp:6