7#include "TinyRobotics/utils/Config.h"
8#include "TinyRobotics/utils/LoggerClass.h"
11#define LOGD TRLogger.debug
12#define LOGE TRLogger.error
13#define LOGW TRLogger.warn
14#define LOGI TRLogger.info
17namespace tinyrobotics {
20
21
22
23
24
25template <
typename T = int16_t>
28 BaseBuffer() =
default;
29 virtual ~BaseBuffer() =
default;
34 virtual bool read(T& result) = 0;
37 virtual int readArray(T data[],
int len) {
38 if (data ==
nullptr) {
43 for (
int j = 0; j < lenResult; j++) {
46 LOGD(
"readArray %d -> %d", len, lenResult);
59 virtual int writeArray(
const T data[],
int len) {
64 for (
int j = 0; j < len; j++) {
71 LOGD(
"writeArray %d -> %d", len, result);
85 virtual bool peek(T& result) = 0;
93 virtual bool write(T data) = 0;
96 virtual void reset() = 0;
110 virtual size_t size() = 0;
115 if (size() == 0)
return 0.0f;
116 return 100.0f *
static_cast<
float>(available()) /
117 static_cast<
float>(size());
121 virtual bool resize(
int bytes) {
122 LOGE(
"resize not implemented for this buffer");
128
129
130
131
132
133
135template <
typename T = int16_t>
139
140
141
142
152
153
158 this->owns_buffer =
false;
159 this->buffer = (uint8_t*)data;
160 this->current_read_pos = 0;
161 this->current_write_pos = len;
164 int writeArray(
const T data[],
int len)
override {
169 bool write(T sample)
override {
171 if (current_write_pos < buffer.size()) {
172 buffer[current_write_pos++] = sample;
178 bool read(T& result)
override {
179 bool success =
false;
180 if (current_read_pos < current_write_pos) {
181 result = buffer[current_read_pos++];
187 bool peek(T& result)
override {
188 bool success =
false;
189 if (current_read_pos < current_write_pos) {
190 result = buffer[current_read_pos];
197 int result = current_write_pos - current_read_pos;
198 return std::max(result, 0);
205 int peekArray(uint8_t* data,
int len) {
207 if (len > len_available) {
210 memcpy(data, buffer.data() + current_read_pos, len);
219 return len_available;
221 current_read_pos += len;
222 len_available -= len;
223 memmove(buffer.data(), buffer.data() + current_read_pos,
224 len_available *
sizeof(T));
225 current_read_pos = 0;
226 current_write_pos = len_available;
228 if (is_clear_with_zero) {
229 memset(buffer.data() + current_write_pos, 0,
230 buffer.size() - current_write_pos);
239 memmove(buffer.data(), buffer.data() + current_read_pos, av *
sizeof(T));
240 current_write_pos = av;
241 current_read_pos = 0;
245 T*
address()
override {
return buffer.data(); }
248 T*
data() {
return buffer.data() + current_read_pos; }
250 void reset()
override {
251 current_read_pos = 0;
252 current_write_pos = 0;
253 if (is_clear_with_zero) {
254 memset(buffer.data(), 0, buffer.size());
261 size_t result = std::min(available_size, (size_t)buffer.size());
262 current_read_pos = 0;
263 current_write_pos = result;
267 size_t size()
override {
return buffer.size(); }
270 if (buffer.size() < size) {
281 void setWritePos(
int pos) { current_write_pos = pos; }
291 int current_read_pos = 0;
292 int current_write_pos = 0;
293 bool owns_buffer =
true;
294 bool is_clear_with_zero =
false;
295 std::vector<T> buffer{0};
299
300
301
302
303template <
typename T = int16_t>
306 RingBuffer(
int size) {
311 bool read(T& result)
override {
316 result = _aucBuffer[_iTail];
317 _iTail = nextIndex(_iTail);
324 bool peek(T& result)
override {
329 result = _aucBuffer[_iTail];
333 virtual int peekArray(T* data,
int n) {
334 if (isEmpty())
return -1;
336 int count = _numElems;
338 for (
int j = 0; j < n; j++) {
339 data[j] = _aucBuffer[tail];
340 tail = nextIndex(tail);
343 if (count == 0)
break;
354 virtual bool write(T data)
override {
357 _aucBuffer[_iHead] = data;
358 _iHead = nextIndex(_iHead);
366 virtual void reset()
override {
373 virtual int available()
override {
return _numElems; }
379 virtual T*
address()
override {
return _aucBuffer.data(); }
381 virtual bool resize(
int len) {
382 if (max_size != len && len > 0) {
383 LOGI(
"resize: %d", len);
384 _aucBuffer.resize(len);
391 virtual size_t size()
override {
return max_size; }
394 std::vector<T> _aucBuffer;
400 int nextIndex(
int index) {
401 if (max_size == 0)
return 0;
402 return (uint32_t)(index + 1) % max_size;
#define LOGI
Definition: ESPNowStream.h:11
#define LOGD
Definition: ESPNowStream.h:12
#define LOGE
Definition: ESPNowStream.h:9
Shared functionality of all buffers.
Definition: Buffers.h:26
virtual bool read(T &result)=0
reads a single value
virtual int readArray(T data[], int len)
reads multiple values
Definition: Buffers.h:37
virtual void reset()=0
clears the buffer
virtual int writeArray(const T data[], int len)
Fills the buffer data.
Definition: Buffers.h:59
virtual T * address()=0
returns the address of the start of the physical read buffer
virtual int writeArrayOverwrite(const T data[], int len)
Fills the buffer data and overwrites the oldest data if the buffer is full.
Definition: Buffers.h:76
virtual int clearArray(int len)
Removes the next len entries.
Definition: Buffers.h:51
virtual int availableForWrite()=0
provides the number of entries that are available to write
virtual bool isFull()
checks if the buffer is full
Definition: Buffers.h:88
virtual bool peek(T &result)=0
peeks the actual entry from the buffer
virtual float levelPercent()
Returns the level of the buffer in %.
Definition: Buffers.h:113
void clear()
same as reset
Definition: Buffers.h:99
virtual bool write(T data)=0
write add an entry to the buffer
virtual int available()=0
provides the number of entries that are available to read
virtual bool resize(int bytes)
Resizes the buffer if supported: returns false if not supported.
Definition: Buffers.h:121
Implements a typed Ringbuffer.
Definition: Buffers.h:304
bool peek(T &result) override
peeks the actual entry from the buffer
Definition: Buffers.h:324
bool read(T &result) override
reads a single value
Definition: Buffers.h:311
virtual T * address() override
returns the address of the start of the physical read buffer
Definition: Buffers.h:379
virtual int availableForWrite() override
provides the number of entries that are available to write
Definition: Buffers.h:376
virtual bool write(T data) override
write add an entry to the buffer
Definition: Buffers.h:354
virtual size_t size() override
Returns the maximum capacity of the buffer.
Definition: Buffers.h:391
virtual void reset() override
clears the buffer
Definition: Buffers.h:366
virtual bool isFull() override
checks if the buffer is full
Definition: Buffers.h:349
virtual bool resize(int len)
Resizes the buffer if supported: returns false if not supported.
Definition: Buffers.h:381
virtual int available() override
provides the number of entries that are available to read
Definition: Buffers.h:373
A simple Buffer implementation which just uses a (dynamically sized) array.
Definition: Buffers.h:136
bool active
Optional active/inactive status.
Definition: Buffers.h:286
size_t setAvailable(size_t available_size)
Definition: Buffers.h:260
void trim()
Moves the unprocessed data to the beginning of the buffer.
Definition: Buffers.h:237
bool write(T sample) override
write add an entry to the buffer
Definition: Buffers.h:169
SingleBuffer(int size)
Construct a new Single Buffer object.
Definition: Buffers.h:143
void setClearWithZero(bool flag)
Sets the buffer to 0 on clear.
Definition: Buffers.h:278
bool peek(T &result) override
peeks the actual entry from the buffer
Definition: Buffers.h:187
uint64_t timestamp
Optional timestamp.
Definition: Buffers.h:288
void setWritePos(int pos)
Updates the actual available data size.
Definition: Buffers.h:281
bool read(T &result) override
reads a single value
Definition: Buffers.h:178
int available() override
provides the number of entries that are available to read
Definition: Buffers.h:196
int id
Optional ID.
Definition: Buffers.h:284
int availableForWrite() override
provides the number of entries that are available to write
Definition: Buffers.h:201
T * address() override
Provides address to beginning of the buffer.
Definition: Buffers.h:245
bool isFull() override
checks if the buffer is full
Definition: Buffers.h:203
void onExternalBufferRefilled(void *data, int len)
notifies that the external buffer has been refilled
Definition: Buffers.h:157
bool resize(int size)
Resizes the buffer if supported: returns false if not supported.
Definition: Buffers.h:269
int writeArray(const T data[], int len) override
Fills the buffer data.
Definition: Buffers.h:164
SingleBuffer()
Construct a new Single Buffer w/o allocating any memory.
Definition: Buffers.h:154
T * data()
Provides address of actual data.
Definition: Buffers.h:248
void reset() override
clears the buffer
Definition: Buffers.h:250
int clearArray(int len) override
consumes len bytes and moves current data to the beginning
Definition: Buffers.h:215