forked from mrwastl/SmartMatrix
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCircularBuffer.cpp
More file actions
45 lines (35 loc) · 1012 Bytes
/
CircularBuffer.cpp
File metadata and controls
45 lines (35 loc) · 1012 Bytes
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
/* CircularBuffer.cpp
modified from example code from Wikipedia:
http://en.wikipedia.org/wiki/Circular_queue#Use_a_Fill_Count
modified to only contain indexes and not any data elements, and allow for peeking at the next read/write index
*/
#include "CircularBuffer.h"
void cbInit(CircularBuffer *cb, int size) {
cb->size = size;
cb->start = 0;
cb->count = 0;
}
/* below from fill count mods */
int cbIsFull(CircularBuffer *cb) {
return cb->count == cb->size;
}
int cbIsEmpty(CircularBuffer *cb) {
return cb->count == 0;
}
// returns index of next free element
int cbGetNextWrite(CircularBuffer *cb) {
return (cb->start + cb->count) % cb->size;
}
void cbRead(CircularBuffer *cb) {
cb->start = (cb->start + 1) % cb->size;
-- cb->count;
}
void cbWrite(CircularBuffer *cb) {
if (cb->count == cb->size)
cb->start = (cb->start + 1) % cb->size; /* full, overwrite */
else
++ cb->count;
}
int cbGetNextRead(CircularBuffer *cb) {
return cb->start;
}