TinyRobotics
Loading...
Searching...
No Matches
QueueRTOS.h
1#pragma once
2#include "TinyRobotics/utils/AllocatorPSRAM.h"
3#include "TinyRobotics/utils/Config.h"
4
5#ifdef ESP32
6#include <freertos/queue.h>
7
8#include "freertos/FreeRTOS.h"
9#else
10#ifndef __linux__
11#include "FreeRTOS.h"
12#include "queue.h"
13#endif
14#endif
15
16namespace tinyrobotics {
17
18/**
19 * @brief FIFO Queue whch is based on the FreeRTOS queue API.
20 * The default allocator will allocate the memory from psram if available
21 * @ingroup collections
22 * @ingroup concurrency
23 * @author Phil Schatzmann
24
25 * @tparam T
26 */
27template <class T, class TAllocator = AllocatorPSRAM<T>>
28class QueueRTOS {
29 public:
30 QueueRTOS(int size, TickType_t writeMaxWait = portMAX_DELAY,
31 TickType_t readMaxWait = portMAX_DELAY) {
32
33 read_max_wait = readMaxWait;
34 write_max_wait = writeMaxWait;
35 queue_size = size;
36 setup();
37 }
38
39 ~QueueRTOS() {
40
41 end();
42 }
43
44 void setReadMaxWait(TickType_t ticks) { read_max_wait = ticks; }
45
46 void setWriteMaxWait(TickType_t ticks) { write_max_wait = ticks; }
47
48 /// (Re-)defines the size
49 bool resize(int size) {
50 bool result = true;
51
52 if (size != queue_size) {
53 end();
54 queue_size = size;
55 result = setup();
56 }
57 return result;
58 }
59
60 bool enqueue(T& data) {
61
62 if (xQueue == nullptr) return false;
63 return xQueueSend(xQueue, (void*)&data, (TickType_t)write_max_wait);
64 }
65
66 bool peek(T& data) {
67
68 if (xQueue == nullptr) return false;
69 return xQueuePeek(xQueue, &data, (TickType_t)read_max_wait);
70 }
71
72 bool dequeue(T& data) {
73
74 if (xQueue == nullptr) return false;
75 return xQueueReceive(xQueue, &data, (TickType_t)read_max_wait);
76 }
77
78 size_t size() { return queue_size; }
79
80 bool clear() {
81
82 if (xQueue == nullptr) return false;
83 xQueueReset(xQueue);
84 return true;
85 }
86
87 void end() {
88 if (xQueue != nullptr) vQueueDelete(xQueue);
89 if (p_data != nullptr) allocator.deallocate((T*)p_data, allocated_size);
90 }
91
92 bool isEmpty() { return size() == 0; }
93
94 protected:
95 QueueHandle_t xQueue = nullptr;
96 TickType_t write_max_wait = portMAX_DELAY;
97 TickType_t read_max_wait = portMAX_DELAY;
98 int queue_size;
99 StaticQueue_t queue_buffer;
100 TAllocator allocator;
101 size_t allocated_size = 0;
102 uint8_t* p_data = nullptr;
103
104 bool setup() {
105 if (queue_size > 0) {
106#if configSUPPORT_STATIC_ALLOCATION
107 allocated_size = (queue_size + 1) * sizeof(T);
108 p_data = (uint8_t*)allocator.allocate(allocated_size);
109 if (p_data == nullptr) return false;
110 xQueue = xQueueCreateStatic(queue_size, sizeof(T), p_data, &queue_buffer);
111#else
112 xQueue = xQueueCreate(queue_size, sizeof(T));
113#endif
114 if (xQueue == nullptr) return false;
115 }
116 return true;
117 }
118};
119
120} // namespace tinyrobotics
Custom allocator that uses ESP32's PSRAM for memory allocation.
Definition: AllocatorPSRAM.h:21
FIFO Queue whch is based on the FreeRTOS queue API. The default allocator will allocate the memory fr...
Definition: QueueRTOS.h:28
bool resize(int size)
(Re-)defines the size
Definition: QueueRTOS.h:49