FreeRTOS C++ Wrappers  1.6.0
C++ interface to FreeRTOS
cqueue.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2017, Michael Becker (michael.f.becker@gmail.com)
4  *
5  * This file is part of the FreeRTOS Add-ons project.
6  *
7  * Source Code:
8  * https://github.com/michaelbecker/freertos-addons
9  *
10  * Project Page:
11  * http://michaelbecker.github.io/freertos-addons/
12  *
13  * On-line Documentation:
14  * http://michaelbecker.github.io/freertos-addons/docs/html/index.html
15  *
16  * Permission is hereby granted, free of charge, to any person obtaining a
17  * copy of this software and associated documentation files
18  * (the "Software"), to deal in the Software without restriction, including
19  * without limitation the rights to use, copy, modify, merge, publish,
20  * distribute, sublicense, and/or sell copies of the Software, and to
21  * permit persons to whom the Software is furnished to do so,subject to the
22  * following conditions:
23  *
24  * + The above copyright notice and this permission notice shall be included
25  * in all copies or substantial portions of the Software.
26  * + Credit is appreciated, but not required, if you find this project
27  * useful enough to include in your application, product, device, etc.
28  *
29  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
30  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
32  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
33  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
34  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
35  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36  *
37  ***************************************************************************/
38 
39 
40 #include "queue.hpp"
41 
42 
43 using namespace cpp_freertos;
44 
45 
46 Queue::Queue(UBaseType_t maxItems, UBaseType_t itemSize)
47 {
48  handle = xQueueCreate(maxItems, itemSize);
49 
50  if (handle == NULL) {
51 #ifndef CPP_FREERTOS_NO_EXCEPTIONS
52  throw QueueCreateException();
53 #else
54  configASSERT(!"Queue Constructor Failed");
55 #endif
56  }
57 }
58 
59 
61 {
62  vQueueDelete(handle);
63 }
64 
65 
66 bool Queue::Enqueue(void *item)
67 {
68  BaseType_t success;
69 
70  success = xQueueSendToBack(handle, item, portMAX_DELAY);
71 
72  return success == pdTRUE ? true : false;
73 }
74 
75 
76 bool Queue::Enqueue(void *item, TickType_t Timeout)
77 {
78  BaseType_t success;
79 
80  success = xQueueSendToBack(handle, item, Timeout);
81 
82  return success == pdTRUE ? true : false;
83 }
84 
85 
86 bool Queue::Dequeue(void *item, TickType_t Timeout)
87 {
88  BaseType_t success;
89 
90  success = xQueueReceive(handle, item, Timeout);
91 
92  return success == pdTRUE ? true : false;
93 }
94 
95 
96 bool Queue::Peek(void *item, TickType_t Timeout)
97 {
98  BaseType_t success;
99 
100  success = xQueuePeek(handle, item, Timeout);
101 
102  return success == pdTRUE ? true : false;
103 }
104 
105 
106 bool Queue::EnqueueFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken)
107 {
108  BaseType_t success;
109 
110  success = xQueueSendToBackFromISR(handle, item, pxHigherPriorityTaskWoken);
111 
112  return success == pdTRUE ? true : false;
113 }
114 
115 
116 bool Queue::DequeueFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken)
117 {
118  BaseType_t success;
119 
120  success = xQueueReceiveFromISR(handle, item, pxHigherPriorityTaskWoken);
121 
122  return success == pdTRUE ? true : false;
123 }
124 
125 
126 bool Queue::PeekFromISR(void *item)
127 {
128  BaseType_t success;
129 
130  success = xQueuePeekFromISR(handle, item);
131 
132  return success == pdTRUE ? true : false;
133 }
134 
135 
137 {
138  UBaseType_t cnt = uxQueueMessagesWaiting(handle);
139 
140  return cnt == 0 ? true : false;
141 }
142 
143 
145 {
146  UBaseType_t cnt = uxQueueSpacesAvailable(handle);
147 
148  return cnt == 0 ? true : false;
149 }
150 
151 
153 {
154  xQueueReset(handle);
155 }
156 
157 
158 UBaseType_t Queue::NumItems()
159 {
160  return uxQueueMessagesWaiting(handle);
161 }
162 
163 
164 UBaseType_t Queue::NumSpacesLeft()
165 {
166  return uxQueueSpacesAvailable(handle);
167 }
168 
169 
170 Deque::Deque(UBaseType_t maxItems, UBaseType_t itemSize)
171  : Queue(maxItems, itemSize)
172 {
173 }
174 
175 
176 bool Deque::EnqueueToFront(void *item, TickType_t Timeout)
177 {
178  BaseType_t success;
179 
180  success = xQueueSendToFront(handle, item, Timeout);
181 
182  return success == pdTRUE ? true : false;
183 }
184 
185 
186 bool Deque::EnqueueToFrontFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken)
187 {
188  BaseType_t success;
189 
190  success = xQueueSendToFrontFromISR(handle, item, pxHigherPriorityTaskWoken);
191 
192  return success == pdTRUE ? true : false;
193 }
194 
195 
196 BinaryQueue::BinaryQueue(UBaseType_t itemSize)
197  : Queue(1, itemSize)
198 {
199 }
200 
201 
202 bool BinaryQueue::Enqueue(void *item)
203 {
204  (void)xQueueOverwrite(handle, item);
205  return true;
206 }
207 
208 
209 bool BinaryQueue::EnqueueFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken)
210 {
211  (void)xQueueOverwriteFromISR(handle, item, pxHigherPriorityTaskWoken);
212  return true;
213 }
UBaseType_t NumItems()
Definition: cqueue.cpp:158
QueueHandle_t handle
Definition: queue.hpp:247
bool Dequeue(void *item, TickType_t Timeout=portMAX_DELAY)
Definition: cqueue.cpp:86
bool Peek(void *item, TickType_t Timeout=portMAX_DELAY)
Definition: cqueue.cpp:96
UBaseType_t NumSpacesLeft()
Definition: cqueue.cpp:164
virtual bool EnqueueFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken)
Definition: cqueue.cpp:209
bool EnqueueToFront(void *item, TickType_t Timeout=portMAX_DELAY)
Definition: cqueue.cpp:176
Queue(UBaseType_t maxItems, UBaseType_t itemSize)
Definition: cqueue.cpp:46
virtual bool Enqueue(void *item)
Definition: cqueue.cpp:66
bool EnqueueToFrontFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken)
Definition: cqueue.cpp:186
bool DequeueFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken)
Definition: cqueue.cpp:116
BinaryQueue(UBaseType_t itemSize)
Definition: cqueue.cpp:196
virtual ~Queue()
Definition: cqueue.cpp:60
bool PeekFromISR(void *item)
Definition: cqueue.cpp:126
Deque(UBaseType_t maxItems, UBaseType_t itemSize)
Definition: cqueue.cpp:170
virtual bool EnqueueFromISR(void *item, BaseType_t *pxHigherPriorityTaskWoken)
Definition: cqueue.cpp:106
virtual bool Enqueue(void *item)
Definition: cqueue.cpp:202