ESP32 Transceiver IEEE 802.15.4 Library
Loading...
Searching...
No Matches
RingBuffer.h
Go to the documentation of this file.
1#pragma once
2
3namespace ieee802154 {
4
19public:
20 RingBuffer(int size = 128) {
21 resize(size);
22 }
23
24 void resize(size_t new_size) {
25 buffer.resize(new_size);
26 capacity = new_size;
27 clear();
28 }
29
30 bool write(uint8_t byte) {
31 if (isFull()) return false;
32 buffer[tail] = byte;
33 tail = (tail + 1) % capacity;
34 ++count;
35 return true;
36 }
37
38 int writeArray(const uint8_t* data, size_t len) {
39 int written = 0;
40 for (size_t i = 0; i < len; ++i) {
41 if (write(data[i])) {
42 ++written;
43 } else {
44 break;
45 }
46 }
47 return written;
48 }
49
50
51 int available() const {
52 return count;
53 }
54
55 void clear() {
56 head = 0;
57 tail = 0;
58 count = 0;
59 }
60
61 bool isFull() const {
62 return count == capacity;
63 }
64
65 bool isEmpty() const {
66 return count == 0;
67 }
68
69 size_t size() const {
70 return capacity;
71 }
72
73 int read() {
74 if (available() > 0) {
75 uint8_t byte = buffer[head];
76 head = (head + 1) % capacity;
77 --count;
78 return byte;
79 }
80 return 0; // No data available
81 }
82
83 // Read up to len bytes into dest, returns number of bytes read
84 int readArray(uint8_t* dest, size_t len) {
85 int n = 0;
86 while (n < (int)len && available() > 0) {
87 dest[n++] = read();
88 }
89 return n;
90 }
91
92 // Peek at the next byte without removing it
93 bool peek(uint8_t& out) const {
94 if (available() > 0) {
95 out = buffer[head];
96 return true;
97 }
98 return false;
99 }
100
101 // Returns available space for writing
102 int availableForWrite() const {
103 return capacity - count;
104 }
105
106private:
107 std::vector<uint8_t> buffer;
108 size_t capacity = 0;
109 size_t head = 0;
110 size_t tail = 0;
111 int count = 0;
112};
113
114} // namespace ieee802154
Efficient ring buffer for storing frame data.
Definition RingBuffer.h:18
bool isEmpty() const
Definition RingBuffer.h:65
int readArray(uint8_t *dest, size_t len)
Definition RingBuffer.h:84
int read()
Definition RingBuffer.h:73
bool write(uint8_t byte)
Definition RingBuffer.h:30
void clear()
Definition RingBuffer.h:55
int available() const
Definition RingBuffer.h:51
bool isFull() const
Definition RingBuffer.h:61
int availableForWrite() const
Definition RingBuffer.h:102
size_t size() const
Definition RingBuffer.h:69
void resize(size_t new_size)
Definition RingBuffer.h:24
RingBuffer(int size=128)
Definition RingBuffer.h:20
bool peek(uint8_t &out) const
Definition RingBuffer.h:93
int writeArray(const uint8_t *data, size_t len)
Definition RingBuffer.h:38
Definition ESP32TransceiverIEEE802_15_4.cpp:15