-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.hpp
99 lines (88 loc) · 1.67 KB
/
Queue.hpp
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
/*
* Byte Queue.h
*
* Based on Steven de Salas
*
* Defines a class for a queue of byte.
* Used for Arduino projects, just #include "Queue.h" and add this file via the IDE.
*/
#ifndef QUEUE_H
#define QUEUE_H
#include <Arduino.h>
#include "Constant.hpp"
class Queue
{
private:
byte _front;
byte _back;
byte _count;
byte _data[MAX_ELEMENT_SIZE + 1]{0};
byte const _maxitems = MAX_ELEMENT_SIZE;
public:
Queue() : _front(0), _back(0), _count(0)
{
}
~Queue()
{
//delete[] _data;
}
inline byte count() { return _count; }
inline byte front() { return _front; }
inline byte back() { return _back; }
void push(const byte &item);
byte peek();
byte pop();
void clear();
};
void Queue::push(const byte &item)
{
LOG("Queue::push(%d) called front:%d, count:%d", item, _front, _count);
if (_count < _maxitems)
{ // Drops out when full
_data[_back++] = item;
++_count;
// Check wrap around
if (_back > _maxitems)
{
_back -= (_maxitems + 1);
}
}
LOG("Q:push(%d) front:%d, count:%d", item, _front, _count);
}
byte Queue::pop()
{
byte result = 0;
if (_count <= 0)
{
LOG("Queue::pop() empty, result:%d", result);
}
else
{
LOG("Queue::pop() called front:%d, count:%d", _front, _count);
result = _data[_front];
_front++;
--_count;
// Check wrap around
if (_front > _maxitems)
{
_front -= (_maxitems + 1);
}
LOG("Q:pop() front:%d, count:%d, result:%d", _front, _count, result);
}
return result;
}
byte Queue::peek()
{
byte result = 0;
if (_count > 0)
{
result = _data[_front];
}
return result;
}
void Queue::clear()
{
_front = _back;
_count = 0;
}
#endif