add a lock for the leds

This commit is contained in:
Mitchell Marino 2026-04-02 00:25:49 -05:00
parent 0b79eb8399
commit bd5a07924a
2 changed files with 15 additions and 2 deletions

View File

@ -3,6 +3,8 @@
#include "esp_log.h" #include "esp_log.h"
#include "esp_err.h" #include "esp_err.h"
#include "led_strip.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::OFF = LEDColor::from_rgb(0x00, 0x00, 0x00);
const LEDColor LEDColor::RED = LEDColor::from_rgb(0x17, 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"; const static char* TAG = "leds";
static led_strip_handle_t led_strip = NULL; static led_strip_handle_t led_strip = NULL;
static SemaphoreHandle_t led_mutex = NULL;
void init_leds() { 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 common configuration
led_strip_config_t strip_config = { led_strip_config_t strip_config = {
.strip_gpio_num = PIN_NEOPIXEL, .strip_gpio_num = PIN_NEOPIXEL,
@ -52,13 +60,20 @@ void init_leds() {
} }
void LEDController::set_led(uint32_t led, uint32_t color) { 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)); ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, led, color >> 16 & 0xFF, color >> 8 & 0xFF, color & 0xFF));
xSemaphoreGive(led_mutex);
} }
void LEDController::flush() { void LEDController::flush() {
xSemaphoreTake(led_mutex, portMAX_DELAY);
ESP_ERROR_CHECK(led_strip_refresh(led_strip)); ESP_ERROR_CHECK(led_strip_refresh(led_strip));
xSemaphoreGive(led_mutex);
} }
void LEDController::clear() { void LEDController::clear() {
xSemaphoreTake(led_mutex, portMAX_DELAY);
ESP_ERROR_CHECK(led_strip_clear(led_strip)); ESP_ERROR_CHECK(led_strip_clear(led_strip));
xSemaphoreGive(led_mutex);
} }

View File

@ -83,8 +83,6 @@ void init_leds();
class LEDController { class LEDController {
private: private:
uint32_t led_colors[LED_COUNT];
static void set_led(uint32_t led, uint32_t color); static void set_led(uint32_t led, uint32_t color);
public: public:
/// Sets the color of an indicator LED. /// Sets the color of an indicator LED.