diff --git a/drivers/leds.cpp b/drivers/leds.cpp index 2e44d85..443c882 100644 --- a/drivers/leds.cpp +++ b/drivers/leds.cpp @@ -3,6 +3,8 @@ #include "esp_log.h" #include "esp_err.h" #include "led_strip.h" +#include "freertos/FreeRTOS.h" +#include "freertos/semphr.h" const LEDColor LEDColor::OFF = LEDColor::from_rgb(0x00, 0x00, 0x00); const LEDColor LEDColor::RED = LEDColor::from_rgb(0x17, 0x00, 0x00); @@ -23,8 +25,14 @@ const LEDColor LEDColor::WHITE_STRONG = LEDColor::from_rgb(0xFF, 0xFF, 0xFF); const static char* TAG = "leds"; static led_strip_handle_t led_strip = NULL; +static SemaphoreHandle_t led_mutex = NULL; void init_leds() { + led_mutex = xSemaphoreCreateMutex(); + if (led_mutex == NULL) { + ESP_LOGE(TAG, "Failed to create LED mutex"); + return; + } /// LED strip common configuration led_strip_config_t strip_config = { .strip_gpio_num = PIN_NEOPIXEL, @@ -52,13 +60,20 @@ void init_leds() { } void LEDController::set_led(uint32_t led, uint32_t color) { + xSemaphoreTake(led_mutex, portMAX_DELAY); ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, led, color >> 16 & 0xFF, color >> 8 & 0xFF, color & 0xFF)); + xSemaphoreGive(led_mutex); } void LEDController::flush() { + xSemaphoreTake(led_mutex, portMAX_DELAY); ESP_ERROR_CHECK(led_strip_refresh(led_strip)); + xSemaphoreGive(led_mutex); } void LEDController::clear() { + xSemaphoreTake(led_mutex, portMAX_DELAY); ESP_ERROR_CHECK(led_strip_clear(led_strip)); + xSemaphoreGive(led_mutex); + } diff --git a/include/blk_box_drivers/leds.hpp b/include/blk_box_drivers/leds.hpp index bd33a1b..c28270d 100644 --- a/include/blk_box_drivers/leds.hpp +++ b/include/blk_box_drivers/leds.hpp @@ -83,8 +83,6 @@ void init_leds(); class LEDController { private: - uint32_t led_colors[LED_COUNT]; - static void set_led(uint32_t led, uint32_t color); public: /// Sets the color of an indicator LED.