forked from rogerlinndesign/linnstrument-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathls_bytebuffer.h
51 lines (41 loc) · 1.53 KB
/
ls_bytebuffer.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
/************************* ls_bytebuffer: LinnStrument ByteBuffer class ***************************
This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/
or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
***************************************************************************************************
Circular byte buffer that has independent push and pop locations. This allows a fixed memory usage
for a queue of data, without having to worry about memory allocation.
Once the size of the buffer fills up though, new data will start filling the beginning back up
again. You need to be careful to select the approriate size of the byte buffer for your use-case
in order to prevent losing data.
**************************************************************************************************/
#ifndef BYTEBUFFER_H_
#define BYTEBUFFER_H_
#include <Arduino.h>
template <unsigned int Size>
class ByteBuffer {
public:
ByteBuffer() : write_(buffer_), read_(buffer_), tail_(buffer_+Size) {}
~ByteBuffer() {}
void push(byte value) {
*write_++ = value;
if (write_ == tail_) write_ = buffer_;
}
byte peek() {
return *read_;
}
byte pop() {
byte result = *read_++;
if (read_ == tail_) read_ = buffer_;
return result;
}
boolean empty() const {
return write_ == read_;
}
private:
byte buffer_[Size];
byte* write_;
byte* read_;
byte* tail_;
};
#endif