Sound Byte Libs 29c5ff3
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_PATTERNS_TIMING_NON_BLOCKING_DELAY_HPP_
2#define SBL_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 patterns {
19namespace timing {
20
21/**
22 * @brief Simple non-blocking delay using system time
23 *
24 * Template-based non-blocking delay that uses HAL system time interface.
25 * Applications typically use this through board-provided timing utilities.
26 *
27 * @tparam SystemTimeImpl HAL system time implementation
28 *
29 * @note All public methods are ISR-safe — timer reads only, no blocking.
30 */
31template<typename SystemTimeImpl>
33public:
34 /**
35 * @brief Constructor with delay period
36 * @param delay_ms Delay period in milliseconds
37 */
38 explicit NonBlockingDelay(uint32_t delay_ms)
39 : period_(delay_ms), last_trigger_(0), active_(false) {}
40
41 /**
42 * @brief Start the delay timer
43 */
44 void start() {
45 last_trigger_ = SystemTimeImpl::millis();
46 active_ = true;
47 }
48
49 /**
50 * @brief Check if delay period has elapsed
51 * @return true if delay period has elapsed
52 */
53 bool ready() const {
54 if (!active_) return false;
55 return (SystemTimeImpl::millis() - last_trigger_) >= period_;
56 }
57
58 /**
59 * @brief Reset the delay timer for next period
60 */
61 void reset() {
62 last_trigger_ = SystemTimeImpl::millis();
63 }
64
65 /**
66 * @brief Stop the delay timer
67 */
68 void stop() {
69 active_ = false;
70 }
71
72 /**
73 * @brief Check if timer is active
74 */
75 bool isActive() const {
76 return active_;
77 }
78
79 /**
80 * @brief Update delay period
81 * @param delay_ms New delay period in milliseconds
82 */
83 void setPeriod(uint32_t delay_ms) {
84 period_ = delay_ms;
85 }
86
87 /**
88 * @brief Get current delay period
89 * @return Current delay period in milliseconds
90 */
91 uint32_t getPeriod() const {
92 return period_;
93 }
94
95private:
96 uint32_t period_; // Delay period in milliseconds
97 uint32_t last_trigger_; // Last trigger time
98 bool active_; // Whether timer is active
99};
100
101/**
102 * @brief Interval timer for periodic tasks
103 *
104 * Similar to NonBlockingDelay but automatically resets after each period.
105 * Useful for periodic tasks like blinking LEDs, sampling sensors, etc.
106 *
107 * @tparam SystemTimeImpl HAL system time implementation
108 *
109 * @note All public methods are ISR-safe — timer reads only, no blocking.
110 */
111template<typename SystemTimeImpl>
113public:
114 /**
115 * @brief Constructor with interval period
116 * @param interval_ms Interval period in milliseconds
117 */
118 explicit IntervalTimer(uint32_t interval_ms)
119 : interval_(interval_ms), last_trigger_(0), active_(false) {}
120
121 /**
122 * @brief Start the interval timer
123 */
124 void start() {
125 last_trigger_ = SystemTimeImpl::millis();
126 active_ = true;
127 }
128
129 /**
130 * @brief Check if interval has elapsed and auto-reset
131 * @return true if interval has elapsed (resets automatically)
132 */
133 bool tick() {
134 if (!active_) return false;
135
136 uint32_t now = SystemTimeImpl::millis();
137 if ((now - last_trigger_) >= interval_) {
138 last_trigger_ = now; // Auto-reset
139 return true;
140 }
141 return false;
142 }
143
144 /**
145 * @brief Stop the interval timer
146 */
147 void stop() {
148 active_ = false;
149 }
150
151 /**
152 * @brief Update interval period
153 * @param interval_ms New interval in milliseconds
154 */
155 void setInterval(uint32_t interval_ms) {
156 interval_ = interval_ms;
157 }
158
159 /**
160 * @brief Get current interval
161 * @return Current interval in milliseconds
162 */
163 uint32_t getInterval() const {
164 return interval_;
165 }
166
167private:
168 uint32_t interval_; // Interval in milliseconds
169 uint32_t last_trigger_; // Last trigger time
170 bool active_; // Whether timer is active
171};
172
173} // namespace timing
174} // namespace patterns
175} // namespace sbl
176
177#endif // SBL_PATTERNS_TIMING_NON_BLOCKING_DELAY_HPP_
Interval timer for periodic tasks.
void stop()
Stop the interval timer.
void start()
Start the interval timer.
uint32_t getInterval() const
Get current interval.
void setInterval(uint32_t interval_ms)
Update interval period.
bool tick()
Check if interval has elapsed and auto-reset.
IntervalTimer(uint32_t interval_ms)
Constructor with interval period.
Simple non-blocking delay using system time.
NonBlockingDelay(uint32_t delay_ms)
Constructor with delay period.
uint32_t getPeriod() const
Get current delay period.
bool isActive() const
Check if timer is active.
bool ready() const
Check if delay period has elapsed.
void setPeriod(uint32_t delay_ms)
Update delay period.
void reset()
Reset the delay timer for next period.
Root namespace for all Sound Byte Libs functionality.
Definition aliases.hpp:24
System timing functions for cooperative scheduling.