add a lock for the leds
This commit is contained in:
parent
0b79eb8399
commit
bd5a07924a
@ -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);
|
||||
|
||||
}
|
||||
|
||||
@ -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.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user