arduino-audio-tools
Loading...
Searching...
No Matches
QueueRTOS.h
1#pragma once
2#include "AudioTools/CoreAudio/AudioBasic/Collections/Allocator.h"
3#include "AudioToolsConfig.h"
4
5#ifdef ESP32
6# include <freertos/queue.h>
7# include "freertos/FreeRTOS.h"
8#else
9#ifndef __linux__
10# include "FreeRTOS.h"
11# include "queue.h"
12#endif
13#endif
14
15namespace audio_tools {
16
26template <class T>
27class QueueRTOS {
28 public:
29 QueueRTOS(int size, TickType_t writeMaxWait = portMAX_DELAY,
30 TickType_t readMaxWait = portMAX_DELAY,
31 Allocator& allocator = DefaultAllocator) {
32 TRACED();
33 p_allocator = &allocator;
34 read_max_wait = readMaxWait;
35 write_max_wait = writeMaxWait;
36 queue_size = size;
37 setup();
38 };
39
40 ~QueueRTOS() {
41 TRACED();
42 end();
43 }
44
45 void setReadMaxWait(TickType_t ticks) { read_max_wait = ticks; }
46
47 void setWriteMaxWait(TickType_t ticks) { write_max_wait = ticks; }
48
50 bool resize(int size) {
51 bool result = true;
52 TRACED();
53 if (size != queue_size) {
54 end();
55 queue_size = size;
56 result = setup();
57 }
58 return result;
59 }
60
61 bool enqueue(T& data) {
62 TRACED();
63 if (xQueue==nullptr) return false;
64 return xQueueSend(xQueue, (void*)&data, (TickType_t)write_max_wait);
65 }
66
67 bool peek(T& data) {
68 TRACED();
69 if (xQueue==nullptr) return false;
70 return xQueuePeek(xQueue, &data, (TickType_t)read_max_wait);
71 }
72
73 bool dequeue(T& data) {
74 TRACED();
75 if (xQueue==nullptr) return false;
76 return xQueueReceive(xQueue, &data, (TickType_t)read_max_wait);
77 }
78
79 size_t size() { return queue_size; }
80
81 bool clear() {
82 TRACED();
83 if (xQueue==nullptr) return false;
84 xQueueReset(xQueue);
85 return true;
86 }
87
88 bool empty() { return size() == 0; }
89
90 protected:
91 QueueHandle_t xQueue = nullptr;
92 TickType_t write_max_wait = portMAX_DELAY;
93 TickType_t read_max_wait = portMAX_DELAY;
94 Allocator* p_allocator = nullptr;
95 int queue_size;
96 uint8_t* p_data = nullptr;
97 StaticQueue_t queue_buffer;
98
99 bool setup() {
100 if (queue_size > 0) {
101#if configSUPPORT_STATIC_ALLOCATION
102 p_data = (uint8_t*)p_allocator->allocate((queue_size + 1) * sizeof(T));
103 if (p_data == nullptr) return false;
104 xQueue = xQueueCreateStatic(queue_size, sizeof(T), p_data, &queue_buffer);
105#else
106 xQueue = xQueueCreate(queue_size, sizeof(T));
107#endif
108 if (xQueue == nullptr) return false;
109 }
110 return true;
111 }
112
113 void end() {
114 if (xQueue != nullptr) vQueueDelete(xQueue);
115 if (p_data != nullptr) p_allocator->free(p_data);
116 }
117};
118
119} // namespace audio_tools
120
Memory allocateator which uses malloc.
Definition Allocator.h:23
FIFO Queue whch is based on the FreeRTOS queue API. The default allocator will allocate the memory fr...
Definition QueueRTOS.h:27
bool resize(int size)
(Re-)defines the size
Definition QueueRTOS.h:50
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10