2 #include "AudioConfig.h"
3 #include "AudioTools/CoreAudio/Buffers.h"
4 #include "AudioTools/CoreAudio/AudioLogger.h"
5 #include "AudioTools/CoreAudio/AudioBasic/Collections/Allocator.h"
8 # include <freertos/stream_buffer.h>
9 # include "freertos/FreeRTOS.h"
11 # include "FreeRTOS.h"
12 # include "stream_buffer.h"
31 BufferRTOS(
size_t streamBufferSize,
size_t xTriggerLevel = 1,
32 TickType_t writeMaxWait = portMAX_DELAY,
33 TickType_t readMaxWait = portMAX_DELAY,
36 readWait = readMaxWait;
37 writeWait = writeMaxWait;
38 current_size_bytes = (streamBufferSize+1) *
sizeof(T);
39 trigger_level = xTriggerLevel;
40 p_allocator = &allocator;
42 if (streamBufferSize > 0) {
52 int req_size_bytes = (size + 1)*
sizeof(T);
53 if (current_size_bytes != req_size_bytes) {
55 current_size_bytes = req_size_bytes;
61 void setReadMaxWait(TickType_t ticks) { readWait = ticks; }
63 void setWriteMaxWait(TickType_t ticks) { writeWait = ticks; }
65 void setWriteFromISR(
bool active) { write_from_isr = active; }
67 void setReadFromISR(
bool active) { read_from_isr = active; }
79 xHigherPriorityTaskWoken = pdFALSE;
80 int result = xStreamBufferReceiveFromISR(xStreamBuffer, (
void *)data,
82 &xHigherPriorityTaskWoken);
86 portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
88 return result /
sizeof(T);
90 return xStreamBufferReceive(xStreamBuffer, (
void *)data,
sizeof(T) * len,
91 readWait) /
sizeof(T);
96 LOGD(
"%s: %d", LOG_METHOD, len);
98 xHigherPriorityTaskWoken = pdFALSE;
100 xStreamBufferSendFromISR(xStreamBuffer, (
void *)data,
sizeof(T) * len,
101 &xHigherPriorityTaskWoken);
103 portYIELD_FROM_ISR();
105 portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
107 return result /
sizeof(T);
109 return xStreamBufferSend(xStreamBuffer, (
void *)data,
sizeof(T) * len,
110 writeWait) /
sizeof(T);
116 LOGE(
"peek not implmented");
122 return xStreamBufferIsFull(xStreamBuffer) == pdTRUE;
125 bool isEmpty() {
return xStreamBufferIsEmpty(xStreamBuffer) == pdTRUE; }
134 void reset()
override { xStreamBufferReset(xStreamBuffer); }
138 return xStreamBufferBytesAvailable(xStreamBuffer) /
sizeof(T);
143 return xStreamBufferSpacesAvailable(xStreamBuffer) /
sizeof(T);
148 LOGE(
"address() not implemented");
152 size_t size() {
return current_size_bytes /
sizeof(T); }
155 StreamBufferHandle_t xStreamBuffer =
nullptr;
156 StaticStreamBuffer_t static_stream_buffer;
157 uint8_t *p_data =
nullptr;
158 Allocator *p_allocator =
nullptr;
159 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
160 int readWait = portMAX_DELAY;
161 int writeWait = portMAX_DELAY;
162 bool read_from_isr =
false;
163 bool write_from_isr =
false;
164 size_t current_size_bytes = 0;
165 size_t trigger_level = 0;
170 if (current_size_bytes == 0)
return true;
173 int size = (current_size_bytes + 1) *
sizeof(T);
174 if (p_data ==
nullptr) {
175 p_data = (uint8_t *)p_allocator->allocate(size);
177 if (p_data ==
nullptr) {
178 LOGE(
"allocate falied for %d bytes", size)
185 if (xStreamBuffer ==
nullptr) {
186 xStreamBuffer = xStreamBufferCreateStatic(current_size_bytes, trigger_level,
187 p_data, &static_stream_buffer);
189 if (xStreamBuffer ==
nullptr) {
190 LOGE(
"xStreamBufferCreateStatic failed");
200 if (xStreamBuffer !=
nullptr) vStreamBufferDelete(xStreamBuffer);
201 p_allocator->free(p_data);
202 current_size_bytes = 0;
204 xStreamBuffer =
nullptr;
210 using SynchronizedBufferRTOS = BufferRTOS<T>;