-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathQueueCPP.h
executable file
·385 lines (352 loc) · 11.9 KB
/
QueueCPP.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/**
* @file QueueCPP.h
* @brief FreeRTOS Queue Wrapper
*
* This file contains a set of lightweight wrappers for queues using FreeRTOS
*
* @copyright (c) 2007-2024 Richard Damon
* @author Richard Damon <richard.damon@gmail.com>
* @parblock
* MIT License:
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* It is requested (but not required by license) that any bugs found or
* improvements made be shared, preferably to the author.
* @endparblock
*
* @ingroup FreeRTOSCpp
*/
#ifndef QUEUECPP_H
#define QUEUECPP_H
#include "FreeRTOScpp.h"
#include "queue.h"
#if FREERTOSCPP_USE_NAMESPACE
namespace FreeRTOScpp {
#endif
//#include <type_traits>
/**
* @brief Base Queue Wrapper
*
* This Base Class provides the Type Independent functionality for a Queue
* @ingroup FreeRTOSCpp
*/
class QueueBase {
protected:
/**
* @brief Constructor
*
* Effectively Abstract class so protected base.
* @param handle_ The queueHandle for the queue/
*/
QueueBase(QueueHandle_t handle_) : queueHandle(handle_) {};
public:
/**
* @brief Destructor.
*/
virtual ~QueueBase() {
vQueueDelete(queueHandle);
}
/**
* @brief Get number of items in the Queue.
* @return The number of item in the Queue.
*/
unsigned waiting() const {
return uxQueueMessagesWaiting(queueHandle);
}
/**
* @brief Return number of spaces available in Queue
* @return the number of spaces available in the Queue.
*/
unsigned available() const {
return uxQueueSpacesAvailable(queueHandle);
}
/**
* @brief Reset the Queue.
* Resets the Queue to an empty state.
*/
void reset() {
xQueueReset(queueHandle);
}
/**
* @brief Check if Queue is Full.
* @return True if Queue is Full.
*/
bool full() {
return 0 == uxQueueSpacesAvailable(queueHandle);
}
/**
* @brief Check if Queue is Empty
* @return True if Queue is Empty.
*/
bool empty() {
return uxQueueMessagesWaiting(queueHandle) == 0;
}
/**
* @brief Is Queue Full.
* Note: Interrupt service routines should only call _ISR routines.
* @return True if Queue is Full.
*/
bool full_ISR() {
return xQueueIsQueueFullFromISR(queueHandle);
}
/**
* @brief Is Queue Empty.
* Note: Interrupt service routines should only call _ISR routines.
* @return True if Queue is Empty.
*/
bool empty_ISR() {
return xQueueIsQueueEmptyFromISR(queueHandle);
}
/**
* @brief Get number of message waiting.
* Note: Interrupt service routines should only call _ISR routines.
* @return The number of messages waiting.
*/
unsigned waiting_ISR() {
return uxQueueMessagesWaitingFromISR(queueHandle);
}
protected:
QueueHandle_t queueHandle;
private:
#if __cplusplus < 201101L
QueueBase(QueueBase const&); ///< We are not copyable.
void operator =(QueueBase const&); ///< We are not assignable.
#else
QueueBase(QueueBase const&) = delete; ///< We are not copyable.
void operator =(QueueBase const&) = delete; ///< We are not assignable.
#endif // __cplusplus
};
/**
* @brief Typed Queue Wrapper
*
* This Base Class provides the Type Dependent functionality for a Queue
* @ingroup FreeRTOSCpp
*/
template<class T> class QueueTypeBase : public QueueBase {
protected:
QueueTypeBase(QueueHandle_t handle_) : QueueBase(handle_) {}
public:
/**
* @brief Push an item onto the Queue.
* Puts an item onto the Queue so it will be the next item to remove.
* @param item The item to put on the Queue.
* @param time How long to wait for room if Queue is full.
* @return True if successful
*/
bool push(T const& item, TickType_t time = portMAX_DELAY){
return xQueueSendToFront(queueHandle, &item, time);
}
#if FREERTOSCPP_USE_CHRONO
/**
* @brief Push an item onto the Queue.
* Puts an item onto the Queue so it will be the next item to remove.
* @param item The item to put on the Queue.
* @param time How long to wait for room if Queue is full.
* @return True if successful
*/
bool push(T const& item, Time_ms time){
return xQueueSendToFront(queueHandle, &item, ms2ticks(time));
}
#endif
/**
* @brief add an item at end of the Queue.
* Puts an item onto the Queue so it will be the last item to remove.
* @param item The item to put on the Queue.
* @param time How long to wait for room if Queue is full.
* @return True if successful
*/
bool add(T const& item, TickType_t time = portMAX_DELAY){
return xQueueSendToBack(queueHandle, &item, time);
}
#if FREERTOSCPP_USE_CHRONO
/**
* @brief add an item at end of the Queue.
* Puts an item onto the Queue so it will be the last item to remove.
* @param item The item to put on the Queue.
* @param time How long to wait for room if Queue is full.
* @return True if successful
*/
bool add(T const& item, Time_ms time){
return xQueueSendToBack(queueHandle, &item, ms2ticks(time));
}
#endif
/**
* @brief Get an item from the Queue.
* Gets the first item from the Queue
* @param var Variable to place the item
* @param time How long to wait for an item to be available.
* @return True if an item returned.
*/
bool pop(T& var, TickType_t time = portMAX_DELAY) {
return xQueueReceive(queueHandle, &var, time);
}
#if FREERTOSCPP_USE_CHRONO
/**
* @brief Get an item from the Queue.
* Gets the first item from the Queue
* @param var Variable to place the item
* @param time How long to wait for an item to be available.
* @return True if an item returned.
*/
bool pop(T& var, Time_ms time) {
return xQueueReceive(queueHandle, &var, ms2ticks(time));
}
#endif
/**
* @brief Look at the first item in the Queue.
* Gets the first item from the Queue leaving it there.
* @param var Variable to place the item
* @param time How long to wait for an item to be available.
* @return True if an item returned.
*/
bool peek(T& var, TickType_t time = 0) {
return xQueuePeek(queueHandle, &var, time);
}
#if FREERTOSCPP_USE_CHRONO
/**
* @brief Look at the first item in the Queue.
* Gets the first item from the Queue leaving it there.
* @param var Variable to place the item
* @param time How long to wait for an item to be available.
* @return True if an item returned.
*/
bool peek(T& var, Time_ms time) {
return xQueuePeek(queueHandle, &var, ms2ticks(time));
}
#endif
/**
* @brief Push an item onto the Queue.
* Puts an item onto the Queue so it will be the next item to remove.
*
* Note: Interrupt service routines should only call _ISR routines.
* @param item The item to put on the Queue.
* @param waswoken Flag variable to determine if context switch is needed.
* @return True if successful
*/
bool push_ISR(T const& item, portBASE_TYPE& waswoken){
return xQueueSendToFrontFromISR(queueHandle, &item, &waswoken);
}
/**
* @brief add an item at end of the Queue.
* Puts an item onto the Queue so it will be the last item to remove.
*
* Note: Interrupt service routines should only call _ISR routines.
* @param item The item to put on the Queue.
* @param waswoken Flag variable to determine if context switch is needed.
* @return True if successful
*/
bool add_ISR(T const& item, portBASE_TYPE& waswoken){
return xQueueSendToBackFromISR(queueHandle, &item, &waswoken);
}
/**
* @brief Get an item from the Queue.
* Gets the first item from the Queue
*
* Note: Interrupt service routines should only call _ISR routines.
* @param var Variable to place the item
* @param waswoken Flag variable to determine if context switch is needed.
* @return True if an item returned.
*/
bool pop_ISR(T& var, portBASE_TYPE& waswoken) {
return xQueueReceiveFromISR(queueHandle, &var, &waswoken);
}
/**
* @brief Look at the first item in the Queue.
* Gets the first item from the Queue leaving it there.
*
* Note: Interrupt service routines should only call _ISR routines.
* @param var Variable to place the item
* @param waswoken Flag variable to determine if context switch is needed.
* @return True if an item returned.
*/
bool peek_ISR(T& var, portBASE_TYPE& waswoken) {
return xQueuePeekFromISR(queueHandle, &var);
}
};
/**
* @brief Queue Wrapper.
*
* Note, is a template on the type of object to place on the queue,
* which makes the Queue more typesafe.
*
* @tparam T The type of object to be placed on the queue.
* Note also, this type needs to be trivially copyable, and preferably a POD
* as the FreeRTOS queue code will copy it with memcpy().
* @tparam queuelength The number of elements to reserve space for in the queue.
* If 0 (which is the default value) then length will be provided to the constructor dynamically.
*
* @todo add Overwrite operation
* @todo add QueueSet Functionality
* @ingroup FreeRTOSCpp
*/
template<class T, unsigned queueLength
#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
= 0
#endif
> class Queue : public QueueTypeBase<T> {
// static_assert(::std::is_pod<T>, "Queues only work with PODs"); ///@todo need to get later version of C++ compile working
public:
/**
* @brief Constructor.
* @param name The name to register the Queue with.
*/
Queue(char const* name = 0) :
QueueTypeBase<T>(
#if( configSUPPORT_STATIC_ALLOCATION == 1 )
xQueueCreateStatic(queueLength, sizeof(T), reinterpret_cast<uint8_t*>(&queueStore), &queueBuff)
#else
xQueueCreate(queueLength, sizeof(T))
#endif
)
{
#if configQUEUE_REGISTRY_SIZE > 0
if(name)
vQueueAddToRegistry(this->queueHandle, name);
#endif
};
private:
#if( configSUPPORT_STATIC_ALLOCATION == 1 )
StaticQueue_t queueBuff;
T queueStore[queueLength];
#endif
};
#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
template<class T> class Queue<T, 0> : public QueueTypeBase<T> {
// static_assert(::std::is_pod<T>, "Queues only work with PODs"); ///@todo need to get later version of C++ compile working
public:
/**
* @brief Constructor.
* @param length How many object to make room for in the Queue.
* @param name The name to register the Queue with.
*/
Queue(unsigned portBASE_TYPE length, char const* name = 0) :
QueueTypeBase<T>(xQueueCreate(length, sizeof(T)))
{
#if configQUEUE_REGISTRY_SIZE > 0
if(name)
vQueueAddToRegistry(this->queueHandle, name);
#endif
};
};
#endif
#if FREERTOSCPP_USE_NAMESPACE
} // namespace FreeRTOScpp
#endif
#endif