diff --git a/Firmware/src/FreeRTOS/BinarySemaphore.cpp b/Firmware/src/FreeRTOS/BinarySemaphore.cpp new file mode 100644 index 0000000..96464ae --- /dev/null +++ b/Firmware/src/FreeRTOS/BinarySemaphore.cpp @@ -0,0 +1,26 @@ +#include +#include + +freertos::BinarySemaphore::BinarySemaphore() + : handle{xSemaphoreCreateBinary()} {} +freertos::BinarySemaphore::BinarySemaphore(BinarySemaphore &&other) noexcept { + handle = etl::move(other.handle); +} +freertos::BinarySemaphore::~BinarySemaphore() { vSemaphoreDelete(handle); } +auto freertos::BinarySemaphore::operator=( + const BinarySemaphore &&other) noexcept -> BinarySemaphore & { + handle = etl::move(other.handle); + return *this; +} +auto freertos::BinarySemaphore::take(TickType_t timeout) -> bool { + BaseType_t success = xSemaphoreTake(handle, timeout); + return success == pdTRUE ? true : false; +} +auto freertos::BinarySemaphore::give() -> bool { + BaseType_t success = xSemaphoreGive(handle); + return success == pdTRUE ? true : false; +} + +auto freertos::BinarySemaphore::getCount() -> size_t { + return uxSemaphoreGetCount(handle); +} \ No newline at end of file diff --git a/Firmware/src/FreeRTOS/BinarySemaphore.h b/Firmware/src/FreeRTOS/BinarySemaphore.h new file mode 100644 index 0000000..08fb38d --- /dev/null +++ b/Firmware/src/FreeRTOS/BinarySemaphore.h @@ -0,0 +1,20 @@ +#pragma once +#include + +namespace freertos { +class BinarySemaphore { +public: + BinarySemaphore(); + BinarySemaphore(const BinarySemaphore &other) = delete; + BinarySemaphore(BinarySemaphore &&other) noexcept; + ~BinarySemaphore(); + BinarySemaphore &operator=(const BinarySemaphore &other) = delete; + BinarySemaphore &operator=(const BinarySemaphore &&other) noexcept; + bool take(TickType_t timeout = portMAX_DELAY); + bool give(); + size_t getCount(); + +private: + SemaphoreHandle_t handle; +}; +} // namespace freertos