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