Sound Byte Libs 1ee2ca6
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
non_blocking_delay.hpp
Go to the documentation of this file.
1#ifndef SBL_CORE_PATTERNS_TIMING_NON_BLOCKING_DELAY_HPP_
2#define SBL_CORE_PATTERNS_TIMING_NON_BLOCKING_DELAY_HPP_
3
4/**
5 * @file non_blocking_delay.hpp
6 * @brief Non-blocking delay utilities for cooperative scheduling
7 * @ingroup patterns
8 *
9 * Provides non-blocking delay mechanisms that allow other tasks
10 * to run while waiting. Essential for building responsive embedded
11 * systems without RTOS overhead.
12 */
13
15#include <cstdint>
16
17namespace sbl {
18namespace core {
19namespace patterns {
20namespace timing {
21
22/**
23 * @brief Simple non-blocking delay using system time
24 *
25 * Template-based non-blocking delay that uses HAL system time interface.
26 * Applications typically use this through board-provided timing utilities.
27 *
28 * @tparam SystemTimeImpl HAL system time implementation
29 */
30template<typename SystemTimeImpl>
32public:
33 /**
34 * @brief Constructor with delay period
35 * @param delay_ms Delay period in milliseconds
36 */
38 : period_(delay_ms), last_trigger_(0), active_(false) {}
39
40 /**
41 * @brief Start the delay timer
42 */
43 void start() {
44 last_trigger_ = SystemTimeImpl::millis();
45 active_ = true;
46 }
47
48 /**
49 * @brief Check if delay period has elapsed
50 * @return true if delay period has elapsed
51 */
52 bool ready() const {
53 if (!active_) return false;
54 return (SystemTimeImpl::millis() - last_trigger_) >= period_;
55 }
56
57 /**
58 * @brief Reset the delay timer for next period
59 */
60 void reset() {
61 last_trigger_ = SystemTimeImpl::millis();
62 }
63
64 /**
65 * @brief Stop the delay timer
66 */
67 void stop() {
68 active_ = false;
69 }
70
71 /**
72 * @brief Check if timer is active
73 */
74 bool isActive() const {
75 return active_;
76 }
77
78 /**
79 * @brief Update delay period
80 * @param delay_ms New delay period in milliseconds
81 */
83 period_ = delay_ms;
84 }
85
86 /**
87 * @brief Get current delay period
88 * @return Current delay period in milliseconds
89 */
91 return period_;
92 }
93
94private:
95 uint32_t period_; // Delay period in milliseconds
96 uint32_t last_trigger_; // Last trigger time
97 bool active_; // Whether timer is active
98};
99
100/**
101 * @brief Interval timer for periodic tasks
102 *
103 * Similar to NonBlockingDelay but automatically resets after each period.
104 * Useful for periodic tasks like blinking LEDs, sampling sensors, etc.
105 *
106 * @tparam SystemTimeImpl HAL system time implementation
107 */
108template<typename SystemTimeImpl>
109class IntervalTimer {
110public:
111 /**
112 * @brief Constructor with interval period
113 * @param interval_ms Interval period in milliseconds
114 */
116 : interval_(interval_ms), last_trigger_(0), active_(false) {}
117
118 /**
119 * @brief Start the interval timer
120 */
121 void start() {
122 last_trigger_ = SystemTimeImpl::millis();
123 active_ = true;
124 }
125
126 /**
127 * @brief Check if interval has elapsed and auto-reset
128 * @return true if interval has elapsed (resets automatically)
129 */
130 bool tick() {
131 if (!active_) return false;
132
133 uint32_t now = SystemTimeImpl::millis();
134 if ((now - last_trigger_) >= interval_) {
135 last_trigger_ = now; // Auto-reset
136 return true;
137 }
138 return false;
139 }
140
141 /**
142 * @brief Stop the interval timer
143 */
144 void stop() {
145 active_ = false;
146 }
147
148 /**
149 * @brief Update interval period
150 * @param interval_ms New interval in milliseconds
151 */
153 interval_ = interval_ms;
154 }
155
156 /**
157 * @brief Get current interval
158 * @return Current interval in milliseconds
159 */
161 return interval_;
162 }
163
164private:
165 uint32_t interval_; // Interval in milliseconds
166 uint32_t last_trigger_; // Last trigger time
167 bool active_; // Whether timer is active
168};
169
170} // namespace timing
171} // namespace patterns
172} // namespace core
173} // namespace sbl
174
175#endif // SBL_CORE_PATTERNS_TIMING_NON_BLOCKING_DELAY_HPP_
Generic interrupt-driven interval timer.
bool tick()
Check if interval has elapsed and auto-reset.
uint32_t getInterval() const
Get current interval.
void setInterval(uint32_t interval_ms)
Update interval period.
IntervalTimer(uint32_t interval_ms)
Constructor with interval period.
Simple non-blocking delay using system time.
bool isActive() const
Check if timer is active.
uint32_t getPeriod() const
Get current delay period.
bool ready() const
Check if delay period has elapsed.
NonBlockingDelay(uint32_t delay_ms)
Constructor with delay period.
void reset()
Reset the delay timer for next period.
void setPeriod(uint32_t delay_ms)
Update delay period.
Root namespace for all Sound Byte Libs functionality.
Definition aliases.hpp:24
System timing functions for cooperative scheduling.