Zephyr C++ Toolkit
Loading...
Searching...
No Matches
TimerManager.hpp
Go to the documentation of this file.
1#pragma once
2
3//================================================================================================//
4// FORWARD DECLARATIONS
5//================================================================================================//
6
7
8//================================================================================================//
9// INCLUDES
10//================================================================================================//
11
12// System includes
13#include <stdint.h>
14
15// 3rd party includes
16#include <zephyr/kernel.h>
17
18// Local includes
19#include "Timer.hpp"
20
21//================================================================================================//
22// MACROS
23//================================================================================================//
24
25// Don't use constexpr here, it seg faults!
26// static constexpr int LOG_LEVEL = LOG_LEVEL_DBG;
27#define ZCT_TIMER_MANAGER_LOG_LEVEL LOG_LEVEL_WRN
28
29//================================================================================================//
30// CLASS DECLARATION
31//================================================================================================//
32
33namespace zct {
34
36public:
37
46 TimerManager(uint32_t maxNumTimers) {
47 LOG_MODULE_DECLARE(TimerManager, ZCT_TIMER_MANAGER_LOG_LEVEL);
48 LOG_DBG("TimerManager constructor called.");
49 m_timers = new Timer*[maxNumTimers];
50 for (uint32_t i = 0; i < maxNumTimers; i++) {
51 m_timers[i] = nullptr;
52 }
53 m_maxNumTimers = maxNumTimers;
54 LOG_DBG("TimerManager constructor finished.");
55 }
56
58 // Free the memory allocated in constructor.
59 delete[] m_timers;
60 }
61
64 Timer* m_timer = nullptr;
65 uint64_t m_durationToWaitUs = 0;
66
67 // Explicit constructor
68 TimerExpiryInfo(Timer* timer, uint64_t durationToWaitUs) : m_timer(timer), m_durationToWaitUs(durationToWaitUs) {}
69 };
70
71
76 void registerTimer(Timer& timer) {
77 __ASSERT(m_numTimers < m_maxNumTimers, "Max number of timers of %u reached.", m_maxNumTimers);
78 m_timers[m_numTimers] = &timer;
80 // Make sure to set the isRegistered flag to true. This will prevent log warnings
81 // if the timer is started when it is not registered with any timer managers.
82 timer.setIsRegistered(true);
83 }
84
95 LOG_MODULE_DECLARE(TimerManager, ZCT_TIMER_MANAGER_LOG_LEVEL);
96 LOG_DBG("getNextExpiringTimer() called. this: %p, m_numTimers: %u.", this, m_numTimers);
97
98 // Set output to null in case no timer expired
99 Timer* expiredTimer = nullptr;
100 uint64_t durationToWaitUs = 0;
101 // Iterate through all registered timers, and find the one that is expiring next (if any)
102 for(uint32_t i = 0; i < this->m_numTimers; i++) {
103 Timer* timer = this->m_timers[i];
104 if (timer->isRunning()) {
105 if (expiredTimer == nullptr || timer->getNextExpiryTimeTicks() < expiredTimer->getNextExpiryTimeTicks()) {
106 // LOG_DBG("Setting expired timer to %p.", timer);
107 expiredTimer = timer;
108 }
109 }
110 }
111 LOG_DBG("Expired timer: %p.\n", expiredTimer);
112
113 // Convert the expiry time to a duration from now
114 // Calculate time to wait for next timeout event
115 durationToWaitUs = 0;
116
117 // Ticks is the fundemental resolution that the kernel does operations at
118 int64_t uptime_ticks = k_uptime_ticks();
119 if (expiredTimer != nullptr) {
120 if (expiredTimer->getNextExpiryTimeTicks() <= uptime_ticks) {
121 durationToWaitUs = 0;
122 LOG_DBG("Timer expired.");
123 // Need to update the timer now that we have detected it has expired.
124 // This will either stop the timer if it is a one-shot, or update the next expiry time
125 // expiredTimer->updateAfterExpiry();
126 } else {
127 durationToWaitUs = k_ticks_to_us_ceil64(expiredTimer->getNextExpiryTimeTicks() - uptime_ticks);
128 LOG_DBG("Time to wait in us: %llu.", durationToWaitUs);
129 }
130 }
131 else
132 {
133 LOG_DBG("No timers running.");
134 }
135 // Save outputs
136 return TimerManager::TimerExpiryInfo{expiredTimer, durationToWaitUs};
137 }
138
139
140protected:
142 uint32_t m_numTimers = 0;
144};
145
146} // namespace zct
#define ZCT_TIMER_MANAGER_LOG_LEVEL
Definition TimerManager.hpp:27
Definition TimerManager.hpp:35
uint32_t m_maxNumTimers
Definition TimerManager.hpp:143
TimerExpiryInfo getNextExpiringTimer()
Definition TimerManager.hpp:94
Timer ** m_timers
Definition TimerManager.hpp:141
~TimerManager()
Definition TimerManager.hpp:57
TimerManager(uint32_t maxNumTimers)
Definition TimerManager.hpp:46
void registerTimer(Timer &timer)
Definition TimerManager.hpp:76
uint32_t m_numTimers
Definition TimerManager.hpp:142
A timer that can be used to execute callbacks at regular intervals in an event driven application.
Definition Timer.hpp:39
void setIsRegistered(bool isRegistered)
Definition Timer.hpp:152
int64_t getNextExpiryTimeTicks() const
Definition Timer.hpp:144
bool isRunning() const
Definition Timer.hpp:113
Definition Mutex.hpp:6
Definition TimerManager.hpp:63
uint64_t m_durationToWaitUs
Definition TimerManager.hpp:65
TimerExpiryInfo(Timer *timer, uint64_t durationToWaitUs)
Definition TimerManager.hpp:68
Timer * m_timer
Definition TimerManager.hpp:64