2#include "TinyRobotics/utils/Config.h"
3#include "TinyRobotics/utils/Buffers.h"
4#include "TinyRobotics/utils/LoggerClass.h"
5#include "TinyRobotics/utils/AllocatorPSRAM.h"
8# include <freertos/stream_buffer.h>
9# include "freertos/FreeRTOS.h"
10#elif defined(__linux__
)
13# include "stream_buffer.h"
16namespace tinyrobotics {
19
20
21
22
23
24
25
29 BufferRTOS(size_t streamBufferSize, size_t xTriggerLevel = 1,
30 TickType_t writeMaxWait = portMAX_DELAY,
31 TickType_t readMaxWait = portMAX_DELAY)
33 readWait = readMaxWait;
34 writeWait = writeMaxWait;
35 current_size_bytes = (streamBufferSize+1) *
sizeof(T);
36 trigger_level = xTriggerLevel;
38 if (streamBufferSize > 0) {
43 ~BufferRTOS() { end(); }
48 int req_size_bytes = (size + 1)*
sizeof(T);
49 if (current_size_bytes != req_size_bytes) {
51 current_size_bytes = req_size_bytes;
57 void setReadMaxWait(TickType_t ticks) { readWait = ticks; }
59 void setWriteMaxWait(TickType_t ticks) { writeWait = ticks; }
61 void setWriteFromISR(
bool active) { write_from_isr = active; }
63 void setReadFromISR(
bool active) { read_from_isr = active; }
66 bool read(T &result)
override {
73 xHigherPriorityTaskWoken = pdFALSE;
74 int result = xStreamBufferReceiveFromISR(xStreamBuffer, (
void *)data,
76 &xHigherPriorityTaskWoken);
80 portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
82 return result /
sizeof(T);
84 return xStreamBufferReceive(xStreamBuffer, (
void *)data,
sizeof(T) * len,
85 readWait) /
sizeof(T);
92 xHigherPriorityTaskWoken = pdFALSE;
94 xStreamBufferSendFromISR(xStreamBuffer, (
void *)data,
sizeof(T) * len,
95 &xHigherPriorityTaskWoken);
99 portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
101 return result /
sizeof(T);
103 return xStreamBufferSend(xStreamBuffer, (
void *)data,
sizeof(T) * len,
104 writeWait) /
sizeof(T);
109 bool peek(T &result)
override {
110 LOGE(
"peek not implemented");
116 return xStreamBufferIsFull(xStreamBuffer) == pdTRUE;
119 bool isEmpty() {
return xStreamBufferIsEmpty(xStreamBuffer) == pdTRUE; }
122 bool write(T data)
override {
128 void reset()
override { xStreamBufferReset(xStreamBuffer); }
132 return xStreamBufferBytesAvailable(xStreamBuffer) /
sizeof(T);
137 return xStreamBufferSpacesAvailable(xStreamBuffer) /
sizeof(T);
142 LOGE(
"address() not implemented");
146 size_t size() {
return current_size_bytes /
sizeof(T); }
148 operator
bool() {
return xStreamBuffer !=
nullptr && size()>0;}
151 if (xStreamBuffer !=
nullptr) vStreamBufferDelete(xStreamBuffer);
152 allocator.deallocate((T*)p_data, allocated_size);
153 current_size_bytes = 0;
155 xStreamBuffer =
nullptr;
159 TAllocator allocator;
160 StreamBufferHandle_t xStreamBuffer =
nullptr;
161 StaticStreamBuffer_t static_stream_buffer;
162 uint8_t *p_data =
nullptr;
163 BaseType_t xHigherPriorityTaskWoken =
pdFALSE;
166 bool read_from_isr =
false;
167 bool write_from_isr =
false;
168 size_t current_size_bytes = 0;
169 size_t trigger_level = 0;
170 size_t allocated_size = 0;
175 if (current_size_bytes == 0)
return true;
178 allocated_size = (current_size_bytes + 1) *
sizeof(T);
179 if (p_data ==
nullptr) {
180 p_data = (uint8_t *)allocator.allocate(allocated_size);
182 if (p_data ==
nullptr) {
183 LOGE(
"allocate falied for %d bytes", allocated_size);
189 if (xStreamBuffer ==
nullptr) {
190 xStreamBuffer = xStreamBufferCreateStatic(current_size_bytes, trigger_level,
191 p_data, &static_stream_buffer);
193 if (xStreamBuffer ==
nullptr) {
194 LOGE(
"xStreamBufferCreateStatic failed");
207using SynchronizedBufferRTOS =
BufferRTOS<T, TAllocator>;
#define LOGE
Definition: ESPNowStream.h:9
Custom allocator that uses ESP32's PSRAM for memory allocation.
Definition: AllocatorPSRAM.h:21
Shared functionality of all buffers.
Definition: Buffers.h:26
Buffer implementation which is using a FreeRTOS StreamBuffer. The default allocator uses psram is ava...
Definition: BufferRTOS.h:27
bool peek(T &result) override
peeks the actual entry from the buffer
Definition: BufferRTOS.h:109
bool read(T &result) override
reads a single value
Definition: BufferRTOS.h:66
bool write(T data) override
write add an entry to the buffer
Definition: BufferRTOS.h:122
int available() override
provides the number of entries that are available to read
Definition: BufferRTOS.h:131
int availableForWrite() override
provides the number of entries that are available to write
Definition: BufferRTOS.h:136
T * address() override
returns the address of the start of the physical read buffer
Definition: BufferRTOS.h:141
bool isFull() override
checks if the buffer is full
Definition: BufferRTOS.h:115
int writeArray(const T data[], int len)
Fills the buffer data.
Definition: BufferRTOS.h:89
bool resize(size_t size)
Re-Allocats the memory and the queue.
Definition: BufferRTOS.h:46
void reset() override
clears the buffer
Definition: BufferRTOS.h:128
bool setup()
Definition: BufferRTOS.h:174
int readArray(T data[], int len)
reads multiple values
Definition: BufferRTOS.h:71