diff --git a/Firmware/src/FreeRTOS/Mutex.cpp b/Firmware/src/FreeRTOS/Mutex.cpp new file mode 100644 index 0000000..7f8683a --- /dev/null +++ b/Firmware/src/FreeRTOS/Mutex.cpp @@ -0,0 +1,20 @@ +#include "Mutex.h" +#include + +freertos::Mutex::Mutex() : handle{xSemaphoreCreateMutex()} {} +freertos::Mutex::Mutex(Mutex &&other) noexcept { + handle = etl::move(other.handle); +} +freertos::Mutex::~Mutex() { vSemaphoreDelete(handle); } +auto freertos::Mutex::operator=(const Mutex &&other) noexcept -> Mutex & { + handle = etl::move(other.handle); + return *this; +} +auto freertos::Mutex::lock(TickType_t timeout) -> bool{ + BaseType_t success = xSemaphoreTake(handle, timeout); + return success == pdTRUE ? true : false; +} +auto freertos::Mutex::unlock() -> bool { + BaseType_t success = xSemaphoreGive(handle); + return success == pdTRUE ? true : false; +} \ No newline at end of file diff --git a/Firmware/src/FreeRTOS/Mutex.h b/Firmware/src/FreeRTOS/Mutex.h new file mode 100644 index 0000000..77ec4b8 --- /dev/null +++ b/Firmware/src/FreeRTOS/Mutex.h @@ -0,0 +1,19 @@ +#pragma once +#include + +namespace freertos { +class Mutex { +public: + Mutex(); + Mutex(const Mutex &other) = delete; + Mutex(Mutex &&other) noexcept; + ~Mutex(); + Mutex &operator=(const Mutex &other) = delete; + Mutex &operator=(const Mutex &&other) noexcept; + bool lock(TickType_t timeout = portMAX_DELAY); + bool unlock(); + +private: + SemaphoreHandle_t handle; +}; +} // namespace freertos