libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
lock_queue.hpp
1#pragma once
2
3#include <cstdint>
4
5#include "libxr_def.hpp"
6#include "libxr_system.hpp"
7
8namespace LibXR
9{
24template <typename Data>
26{
27 public:
37
43
50 ErrorCode Push(const Data &data)
51 {
52 auto ans = xQueueSend(queue_handle_, &data, 0);
53 if (ans == pdTRUE)
54 {
55 return ErrorCode::OK;
56 }
57 else
58 {
59 return ErrorCode::FULL;
60 }
61 }
62
70 ErrorCode Pop(Data &data, uint32_t timeout = UINT32_MAX)
71 {
72 auto ans = xQueueReceive(queue_handle_, &data, timeout);
73 if (ans == pdTRUE)
74 {
75 return ErrorCode::OK;
76 }
77 else
78 {
79 return ErrorCode::EMPTY;
80 }
81 }
82
89 ErrorCode Pop(uint32_t timeout)
90 {
91 Data data;
92 auto ans = xQueueReceive(queue_handle_, &data, timeout);
93 if (ans == pdTRUE)
94 {
95 return ErrorCode::OK;
96 }
97 else
98 {
99 return ErrorCode::EMPTY;
100 }
101 }
102
109 ErrorCode Overwrite(const Data &data)
110 {
112 return Push(data);
113 }
114
122 ErrorCode PushFromCallback(const Data &data, bool in_isr)
123 {
124 if (!in_isr)
125 {
126 return Push(data) == pdTRUE ? ErrorCode::OK : ErrorCode::FULL;
127 }
128 else
129 {
133 {
134 portYIELD(); // NOLINT
135 }
136 if (ans == pdTRUE)
137 {
138 return ErrorCode::OK;
139 }
140 else
141 {
142 return ErrorCode::FULL;
143 }
144 }
145 }
146
154 ErrorCode PopFromCallback(Data &data, bool in_isr)
155 {
156 if (!in_isr)
157 {
158 return Pop(data, 0);
159 }
160 else
161 {
165 {
166 portYIELD(); // NOLINT
167 }
168 if (ans == pdTRUE)
169 {
170 return ErrorCode::OK;
171 }
172 else
173 {
174 return ErrorCode::EMPTY;
175 }
176 }
177 }
178
195 ErrorCode PopFromCallback(bool in_isr)
196 {
197 if (!in_isr)
198 {
199 return Pop(0);
200 }
201 else
202 {
203 Data data;
207 {
208 portYIELD(); // NOLINT
209 }
210 if (ans == pdTRUE)
211 {
212 return ErrorCode::OK;
213 }
214 else
215 {
216 return ErrorCode::EMPTY;
217 }
218 }
219 }
220
234 ErrorCode OverwriteFromCallback(const Data &data, bool in_isr)
235 {
236 UNUSED(in_isr);
237 return Overwrite(data);
238 }
239
246 ErrorCode Peek(Data &data)
247 {
248 auto ans = xQueuePeek(queue_handle_, &data, 0);
249 if (ans == pdTRUE)
250 {
251 return ErrorCode::OK;
252 }
253 else
254 {
255 return ErrorCode::EMPTY;
256 }
257 }
258
266 ErrorCode PeekFromCallback(Data &data, bool in_isr)
267 {
268 if (!in_isr)
269 {
270 return Peek(data);
271 }
272 else
273 {
274 return xQueuePeekFromISR(queue_handle_, &data) == pdTRUE ? ErrorCode::OK
275 : ErrorCode::EMPTY;
276 }
277 }
278
284
291
298
306 {
307 if (!in_isr)
308 {
309 return Size();
310 }
311 else
312 {
314 }
315 }
316
324 {
325 if (!in_isr)
326 {
327 return EmptySize();
328 }
329 else
330 {
332 }
333 }
334
335 private:
338};
339} // namespace LibXR
线程安全的队列实现,基于 FreeRTOS 消息队列 Thread-safe queue implementation based on FreeRTOS message queue
ErrorCode Pop(Data &data, uint32_t timeout=UINT32_MAX)
从队列弹出数据(阻塞) Pops data from the queue (blocking)
~LockQueue()
析构函数,删除队列 Destructor that deletes the queue
ErrorCode PeekFromCallback(Data &data, bool in_isr)
从 ISR 查看队列中的数据 Peeks at the data in the queue from an ISR
size_t Size()
获取队列中的数据项数量 Gets the number of items in the queue
ErrorCode Pop()
无参数弹出数据 Pops data from the queue without storing it
const uint32_t LENGTH
队列最大长度 Maximum queue length
ErrorCode Push(const Data &data)
将数据推入队列(非阻塞) Pushes data into the queue (non-blocking)
void Reset()
重置队列,清空所有数据 Resets the queue, clearing all data
ErrorCode Peek(Data &data)
查看队列中的数据(非阻塞) Peeks at the data in the queue (non-blocking)
ErrorCode PopFromCallback(Data &data, bool in_isr)
从 ISR(中断服务例程)弹出数据 Pops data from the queue in an ISR (Interrupt Service Routine)
size_t SizeFromCallback(bool in_isr)
从 ISR 获取队列大小 Gets the queue size from an ISR
size_t EmptySize()
获取队列剩余的可用空间 Gets the remaining available space in the queue
ErrorCode OverwriteFromCallback(const Data &data, bool in_isr)
从 ISR 覆盖队列中的数据 Overwrites the data in the queue from an ISR
ErrorCode PopFromCallback(bool in_isr)
从 ISR(中断服务例程)弹出数据 Pops data from the queue in an ISR (Interrupt Service Routine)
QueueHandle_t queue_handle_
FreeRTOS 队列句柄 FreeRTOS queue handle.
size_t EmptySizeFromCallback(bool in_isr)
从 ISR 获取队列剩余的可用空间 Gets the remaining available space in the queue from an ISR
ErrorCode PushFromCallback(const Data &data, bool in_isr)
从 ISR(中断服务例程)推入数据 Pushes data into the queue from an ISR (Interrupt Service Routine)
ErrorCode Pop(uint32_t timeout)
从队列弹出数据(不关心数据值) Pops data from the queue without retrieving its value
LockQueue(size_t length)
构造函数,创建指定长度的队列 Constructor that creates a queue of specified length
ErrorCode Overwrite(const Data &data)
覆盖队列中的数据 Overwrites the data in the queue
LibXR Color Control Library / LibXR终端颜色控制库
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值