forked from sensorium/Mozzi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CircularBuffer.h
75 lines (63 loc) · 1.39 KB
/
CircularBuffer.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
/*
Modified from https://en.wikipedia.org/wiki/Circular_buffer
Mirroring version
On 18 April 2014, the simplified version on the Wikipedia page for power of 2 sized buffers
doesn't work - cbIsEmpty() returns true whether the buffer is full or empty.
*/
/** Circular buffer object. Has a fixed number of cells, set to 256.
@tparam ITEM_TYPE the kind of data to store, eg. int, int8_t etc.
*/
template <class ITEM_TYPE>
class CircularBuffer
{
public:
/** Constructor
*/
CircularBuffer(): start(0),end(0),s_msb(0),e_msb(0)
{
}
inline
bool isFull() {
return end == start && e_msb != s_msb;
}
inline
bool isEmpty() {
return end == start && e_msb == s_msb;
}
inline
void write(ITEM_TYPE in) {
items[end] = in;
//if (isFull()) cbIncrStart(); /* full, overwrite moves start pointer */
cbIncrEnd();
}
inline
ITEM_TYPE read() {
ITEM_TYPE out = items[start];
cbIncrStart();
return out;
}
inline
unsigned long count() {
return (num_buffers_read << 8) + start;
}
private:
ITEM_TYPE items[256];
uint8_t start; /* index of oldest itement */
uint8_t end; /* index at which to write new itement */
uint8_t s_msb;
uint8_t e_msb;
unsigned long num_buffers_read;
inline
void cbIncrStart() {
start++;
if (start == 0) {
s_msb ^= 1;
num_buffers_read++;
}
}
inline
void cbIncrEnd() {
end++;
if (end == 0) e_msb ^= 1;
}
};