-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathArduinoQueue.h
224 lines (186 loc) · 4.41 KB
/
ArduinoQueue.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
/*
Author: Einar Arnason
email: [email protected]
A lightweight linked list type queue implementation,
meant for microcontrollers.
Usage and further info:
https://github.com/EinarArnason/ArduinoQueue
*/
#pragma once
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#elif defined(ARDUINO) && ARDUINO < 100
#include "WProgram.h"
#endif
#if __cplusplus <= 199711L
#define nullptr NULL
#endif
template <typename T>
class ArduinoQueue {
class Node {
public:
T item;
Node* next;
Node() { next = nullptr; }
~Node() { next = nullptr; }
};
Node* head;
Node* tail;
unsigned int maxItems;
unsigned int maxMemory;
unsigned int count;
public:
ArduinoQueue(unsigned int maxItems = (unsigned int)-1,
unsigned int maxMemory = (unsigned int)-1) {
this->head = nullptr;
this->tail = nullptr;
this->count = 0;
this->maxMemory = maxMemory;
this->maxItems = maxMemory / sizeof(Node);
if (maxItems != 0 && this->maxItems > maxItems) {
this->maxItems = maxItems;
}
}
~ArduinoQueue() {
for (Node* node = head; node != nullptr; node = head) {
head = node->next;
delete node;
}
}
/*
Push an item to the queue.
Returns false if memory is
full, or true if the item
was added to queue.
*/
bool enqueue(T item) {
if (count == maxItems) {
return false;
}
Node* node = new Node;
if (node == nullptr) {
return false;
}
node->item = item;
if (head == nullptr) {
head = node;
tail = node;
count++;
return true;
}
tail->next = node;
tail = node;
count++;
return true;
}
/*
Pop the front of the queue.
Because exceptions are not
usually implemented for
microcontrollers, if queue
is empty, a dummy item is
returned.
*/
T dequeue() {
if ((count == 0) || (head == nullptr)) {
return T();
}
Node* node = head;
head = node->next;
T item = node->item;
delete node;
node = nullptr;
if (head == nullptr) {
tail = nullptr;
}
count--;
return item;
}
/*
Returns true if the queue
is empty, false otherwise.
*/
bool isEmpty() { return head == nullptr; }
/*
Returns true if the queue
is full, false otherwise.
*/
bool isFull() { return count == maxItems; }
/*
Returns the number of items
currently in the queue.
*/
unsigned int itemCount() { return count; }
/*
Returns the size of the
queue item in bytes.
*/
unsigned int itemSize() { return sizeof(Node); }
/*
Returns the size of the queue
(maximum number of items)
*/
unsigned int maxQueueSize() { return maxItems; }
/*
Returns the size of the queue
(maximum size in bytes)
*/
unsigned int maxMemorySize() { return maxMemory; }
/*
Get the item in the front
of the queue.
Because exceptions are not
usually implemented for
microcontrollers, if queue
is empty, a dummy item is
returned.
*/
T getHead() {
if ((count == 0) || (head == nullptr)) {
return T();
}
T item = head->item;
return item;
}
/*
Get the item in the back
of the queue.
Because exceptions are not
usually implemented for
microcontrollers, if queue
is empty, a dummy item is
returned.
*/
T getTail() {
if ((count == 0) || (head == nullptr)) {
return T();
}
T item = tail->item;
return item;
}
T* getHeadPtr() {
if ((count == 0) || (head == nullptr)) {
return nullptr;
}
return &(head->item);
}
T* getTailPtr() {
if ((count == 0) || (head == nullptr)) {
return nullptr;
}
return &(tail->item);
}
/*
Depricated functions
*/
// Depricated, use getHead() instead
T front() { return getHead(); }
// Depricated, use itemCount() instead
unsigned int item_count() { return itemCount(); }
// Depricated, use itemSize() instead
unsigned int item_size() { return itemSize(); }
// Depricated, use maxQueueSize() instead
unsigned int max_queue_size() { return maxQueueSize(); }
// Depricated, use maxMemorySize() instead
unsigned int max_memory_size() { return maxMemorySize(); }
};