diff --git a/CLC-qthing/SiliconTorch/NVSExplorer.hpp b/CLC-qthing/SiliconTorch/NVSExplorer.hpp index cad8a61dcc204e97b5ecb2102afff6afd00655a8..9dddddee825f35fab63cfe514943204b4f148d62 100644 --- a/CLC-qthing/SiliconTorch/NVSExplorer.hpp +++ b/CLC-qthing/SiliconTorch/NVSExplorer.hpp @@ -14,7 +14,7 @@ // #include "driver/uart.h" // project specific -// #include "" +#include "SpiderLib/ManagedMutex.hpp" // qthing stuff // #include <qthing> @@ -56,7 +56,7 @@ namespace SiliconTorch { NVSExplorer(); - + SpiderLib::ManagedMutex mutex; }; diff --git a/CLC-qthing/SpiderLib/ManagedMutex.hpp b/CLC-qthing/SpiderLib/ManagedMutex.hpp new file mode 100644 index 0000000000000000000000000000000000000000..726e9005acd0eb102540804ca29ce7e240f22931 --- /dev/null +++ b/CLC-qthing/SpiderLib/ManagedMutex.hpp @@ -0,0 +1,51 @@ +#pragma once + +// C++ system level +#include <functional> + +// ESP32 specific +#include "esp_log.h" +#include "FreeRTOS.h" +#include "freertos/semphr.h" + +// project specific +// #include <SpiderLib/Util.hpp> + + +namespace SpiderLib { + + class ManagedMutex { + + public: + ManagedMutex() { + + semphr = xSemaphoreCreateBinary(); + + if (semphr == NULL) { + ESP_LOGE("ManagedMutex", "Memory allocation FAILED"); + abort(); + } + + xSemaphoreGive(semphr); + } + + template<typename T> + T run(std::function<T()> body) { + + // TODO: how infinitely is portMAX_DELAY really…? + while (xSemaphoreTake(semphr, portMAX_DELAY) == pdFALSE) {}; + + T result = body(); + + xSemaphoreGive(semphr); + + return result; + } + + private: + + SemaphoreHandle_t semphr; + + }; + +}