#include "FreeRTOS.h" #include "queue.h" #include #include QueueHandle_t xQueueCreate(const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize) { QueueHandle_t xQueue; if (uxItemSize != 1) { throw std::runtime_error("uxItemSize must be 1"); } xQueue.queue.reserve(uxQueueLength); return xQueue; } BaseType_t xQueueSend(QueueHandle_t &xQueue, const void * const pvItemToQueue, TickType_t xTicksToWait) { std::lock_guard guard(xQueue.mutex); xQueue.queue.push_back(*reinterpret_cast(pvItemToQueue)); return true; } BaseType_t xQueueSendFromISR(QueueHandle_t &xQueue, const void * const pvItemToQueue, BaseType_t *xHigherPriorityTaskWoken) { TickType_t xTicksToWait = 0; *xHigherPriorityTaskWoken = pdFALSE; return xQueueSend(xQueue, pvItemToQueue, 0.0); } BaseType_t xQueueReceive(QueueHandle_t &xQueue, void * const pvBuffer, TickType_t xTicksToWait) { while (xQueue.queue.empty()) { if (xTicksToWait <= 25) { return false; } SDL_Delay(25); xTicksToWait -= 25; } if (xQueue.queue.empty()) { return false; } std::lock_guard guard(xQueue.mutex); uint8_t *buf = reinterpret_cast(pvBuffer); *buf = xQueue.queue.at(0); xQueue.queue.erase(xQueue.queue.begin()); return true; }