arduino-audio-tools
Loading...
Searching...
No Matches
BufferZephyr.h
Go to the documentation of this file.
1#pragma once
2
7#include "AudioToolsConfig.h"
8
9#include <zephyr/kernel.h>
10
11namespace audio_tools {
12
22template <typename T>
23class BufferZephyr : public BaseBuffer<T> {
24 public:
41
43
45 bool resize(size_t size) {
46 bool result = true;
47 size_t req_size = size + 1;
49 end();
52 result = setup();
53 }
54 return result;
55 }
56
58
60
61 void setWriteFromISR(bool active) { write_from_isr = active; }
62
63 void setReadFromISR(bool active) { read_from_isr = active; }
64
65 // reads a single value
66 bool read(T &result) override { return readArray(&result, 1) == 1; }
67
68 // reads multiple values
69 int readArray(T data[], int len) override {
70 if (p_data == nullptr || data == nullptr || len <= 0) return 0;
71
72 int result = 0;
73 for (int j = 0; j < len; j++) {
74 if (!readOne(data[j])) break;
75 result++;
76 }
77 return result;
78 }
79
80 int writeArray(const T data[], int len) override {
81 LOGD("%s: %d", LOG_METHOD, len);
82 if (p_data == nullptr || data == nullptr || len <= 0) return 0;
83
84 int result = 0;
85 for (int j = 0; j < len; j++) {
86 if (!writeOne(data[j])) break;
87 result++;
88 }
89 return result;
90 }
91
92 // peeks the actual entry from the buffer
93 bool peek(T &result) override {
94 if (p_data == nullptr) return false;
95 if (k_sem_count_get(&items_sem) == 0) return false;
96
98 result = p_data[tail];
100 return true;
101 }
102
103 // checks if the buffer is full
104 bool isFull() override { return availableForWrite() == 0; }
105
106 bool isEmpty() {
107 return available() == 0;
108 }
109
110 // write add an entry to the buffer
111 bool write(T data) override { return writeArray(&data, 1) == 1; }
112
113 // clears the buffer
114 void reset() override {
115 if (p_data == nullptr) return;
116
118 head = 0;
119 tail = 0;
123 }
124
125 // provides the number of entries that are available to read
126 int available() override { return k_sem_count_get(&items_sem); }
127
128 // provides the number of entries that are available to write
129 int availableForWrite() override { return k_sem_count_get(&spaces_sem); }
130
131 // returns the address of the start of the physical read buffer
132 T *address() override {
133 LOGE("address() not implemented");
134 return nullptr;
135 }
136
137 size_t size() override { return buffer_size_public; }
138
139 operator bool() { return p_data != nullptr && size() > 0; }
140
141 protected:
142 T *p_data = nullptr;
146 bool read_from_isr = false;
147 bool write_from_isr = false;
148 size_t current_size_entries = 0; // internal ring-buffer capacity (public size + 1)
149 size_t buffer_size_public = 0; // user-visible capacity
150
154 size_t head = 0;
155 size_t tail = 0;
156
159 bool setup() {
160 if (current_size_entries == 0) return true;
161
162 if (p_data == nullptr) {
163 p_data = static_cast<T *>(
165 if (p_data == nullptr) {
166 LOGE("allocate failed for %d bytes",
167 (int)(current_size_entries * sizeof(T)));
168 return false;
169 }
170 }
171
175 head = 0;
176 tail = 0;
177 return true;
178 }
179
181 void end() {
182 if (p_data != nullptr) {
184 }
187 p_data = nullptr;
188 head = 0;
189 tail = 0;
190 }
191
192 bool readOne(T &value) {
194 if (k_sem_take(&items_sem, timeout) != 0) return false;
195
197 value = p_data[tail];
200
202 return true;
203 }
204
205 bool writeOne(const T &value) {
207 if (k_sem_take(&spaces_sem, timeout) != 0) return false;
208
210 p_data[head] = value;
213
215 return true;
216 }
217};
218
220template <class T>
222
225template <class T>
227
228} // namespace audio_tools
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define LOG_METHOD
Definition AudioToolsConfig.h:74
#define portMAX_DELAY
Definition QueueZephyr.h:14
uint32_t TickType_t
Definition QueueZephyr.h:11
Memory allocateator which uses malloc.
Definition Allocator.h:24
virtual void free(void *memory)
frees memory
Definition Allocator.h:83
virtual void * allocate(size_t size)
Allocates memory.
Definition Allocator.h:71
Shared functionality of all buffers.
Definition Buffers.h:23
Buffer implementation which is using a FreeRTOS StreamBuffer. The default allocator uses psram is ava...
Definition BufferRTOS.h:33
Buffer implementation using Zephyr semaphores + mutex as a bounded ring buffer.
Definition BufferZephyr.h:23
struct k_sem items_sem
Definition BufferZephyr.h:152
size_t size() override
Definition BufferZephyr.h:137
struct k_mutex buffer_mutex
Definition BufferZephyr.h:151
Allocator * p_allocator
Definition BufferZephyr.h:143
bool readOne(T &value)
Definition BufferZephyr.h:192
void setReadFromISR(bool active)
Definition BufferZephyr.h:63
bool peek(T &result) override
peeks the actual entry from the buffer
Definition BufferZephyr.h:93
bool read(T &result) override
reads a single value
Definition BufferZephyr.h:66
struct k_sem spaces_sem
Definition BufferZephyr.h:153
size_t current_size_entries
Definition BufferZephyr.h:148
void setWriteMaxWait(TickType_t ticks)
Definition BufferZephyr.h:59
bool write(T data) override
write add an entry to the buffer
Definition BufferZephyr.h:111
int available() override
provides the number of entries that are available to read
Definition BufferZephyr.h:126
size_t head
Definition BufferZephyr.h:154
TickType_t writeWait
Definition BufferZephyr.h:145
int availableForWrite() override
provides the number of entries that are available to write
Definition BufferZephyr.h:129
bool writeOne(const T &value)
Definition BufferZephyr.h:205
T * address() override
returns the address of the start of the physical read buffer
Definition BufferZephyr.h:132
TickType_t readWait
Definition BufferZephyr.h:144
bool isFull() override
checks if the buffer is full
Definition BufferZephyr.h:104
bool read_from_isr
Definition BufferZephyr.h:146
void setReadMaxWait(TickType_t ticks)
Definition BufferZephyr.h:57
int writeArray(const T data[], int len) override
Fills the buffer data.
Definition BufferZephyr.h:80
void end()
Release resources: call resize to restart again.
Definition BufferZephyr.h:181
size_t buffer_size_public
Definition BufferZephyr.h:149
T * p_data
Definition BufferZephyr.h:142
void setWriteFromISR(bool active)
Definition BufferZephyr.h:61
size_t tail
Definition BufferZephyr.h:155
bool resize(size_t size)
Re-Allocates the memory and queue.
Definition BufferZephyr.h:45
void reset() override
clears the buffer
Definition BufferZephyr.h:114
~BufferZephyr()
Definition BufferZephyr.h:42
int readArray(T data[], int len) override
reads multiple values
Definition BufferZephyr.h:69
bool isEmpty()
Definition BufferZephyr.h:106
bool setup()
Definition BufferZephyr.h:159
bool write_from_isr
Definition BufferZephyr.h:147
BufferZephyr(size_t streamBufferSize, size_t xTriggerLevel=1, TickType_t writeMaxWait=portMAX_DELAY, TickType_t readMaxWait=portMAX_DELAY, Allocator &allocator=DefaultAllocator)
Definition BufferZephyr.h:25
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
static TAllocatorExt DefaultAllocator
Definition Allocator.h:207
k_timeout_t rtosTimeoutFromTicks(TickType_t ticks)
Definition QueueZephyr.h:29
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508