121 lines
3.8 KiB
C
121 lines
3.8 KiB
C
#include "status_led.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "driver/gpio.h"
|
|
#include "led_strip.h"
|
|
#include "esp_log.h"
|
|
|
|
static const char *TAG = "STATUS_LED";
|
|
|
|
static led_strip_handle_t s_led_strip = NULL;
|
|
static bool s_is_rgb = false;
|
|
static int s_gpio_pin = -1;
|
|
static volatile led_state_t s_current_state = LED_STATE_NO_CONFIG;
|
|
|
|
// Internal helper to abstract hardware differences
|
|
static void set_color(uint8_t r, uint8_t g, uint8_t b) {
|
|
if (s_is_rgb && s_led_strip) {
|
|
// Addressable RGB (WS2812)
|
|
led_strip_set_pixel(s_led_strip, 0, r, g, b);
|
|
led_strip_refresh(s_led_strip);
|
|
} else if (!s_is_rgb && s_gpio_pin >= 0) {
|
|
// Simple LED: Any color > 0 is treated as ON
|
|
bool on = (r + g + b) > 0;
|
|
gpio_set_level(s_gpio_pin, on ? 1 : 0);
|
|
}
|
|
}
|
|
|
|
static void led_task(void *arg) {
|
|
int blink_toggle = 0;
|
|
while (1) {
|
|
switch (s_current_state) {
|
|
case LED_STATE_NO_CONFIG:
|
|
if (s_is_rgb) {
|
|
set_color(25, 25, 0); // Yellow
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
} else {
|
|
set_color(255, 255, 255); vTaskDelay(pdMS_TO_TICKS(100));
|
|
set_color(0, 0, 0); vTaskDelay(pdMS_TO_TICKS(100));
|
|
}
|
|
break;
|
|
|
|
case LED_STATE_WAITING:
|
|
// Blue Blink (Slow)
|
|
if (blink_toggle) set_color(0, 0, 50);
|
|
else set_color(0, 0, 0);
|
|
blink_toggle = !blink_toggle;
|
|
vTaskDelay(pdMS_TO_TICKS(500));
|
|
break;
|
|
|
|
case LED_STATE_CONNECTED:
|
|
// Green Solid
|
|
set_color(0, 25, 0);
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
break;
|
|
|
|
case LED_STATE_MONITORING:
|
|
// Blue Solid
|
|
set_color(0, 0, 50);
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
break;
|
|
|
|
case LED_STATE_TRANSMITTING:
|
|
// Behavior: Purple Flash (Fast) - "Sending"
|
|
if (blink_toggle) set_color(50, 0, 50); // Purple
|
|
else set_color(0, 0, 0); // Off
|
|
blink_toggle = !blink_toggle;
|
|
vTaskDelay(pdMS_TO_TICKS(100)); // Fast toggle
|
|
break;
|
|
|
|
case LED_STATE_STALLED:
|
|
// Behavior: Purple Solid - "Buffered/Error 12"
|
|
set_color(50, 0, 50);
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
break;
|
|
|
|
case LED_STATE_FAILED:
|
|
// Red Blink (Fast)
|
|
if (blink_toggle) set_color(50, 0, 0);
|
|
else set_color(0, 0, 0);
|
|
blink_toggle = !blink_toggle;
|
|
vTaskDelay(pdMS_TO_TICKS(200));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void status_led_init(int gpio_pin, bool is_rgb_strip) {
|
|
s_gpio_pin = gpio_pin;
|
|
s_is_rgb = is_rgb_strip;
|
|
|
|
ESP_LOGI(TAG, "Initializing LED on GPIO %d (Type: %s)",
|
|
gpio_pin, is_rgb_strip ? "RGB Strip" : "Simple GPIO");
|
|
|
|
if (s_is_rgb) {
|
|
led_strip_config_t strip_config = {
|
|
.strip_gpio_num = gpio_pin,
|
|
.max_leds = 1,
|
|
};
|
|
led_strip_rmt_config_t rmt_config = {
|
|
.resolution_hz = 10 * 1000 * 1000,
|
|
.flags.with_dma = false,
|
|
};
|
|
ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &s_led_strip));
|
|
led_strip_clear(s_led_strip);
|
|
} else {
|
|
gpio_reset_pin(gpio_pin);
|
|
gpio_set_direction(gpio_pin, GPIO_MODE_OUTPUT);
|
|
gpio_set_level(gpio_pin, 0);
|
|
}
|
|
|
|
xTaskCreate(led_task, "led_task", 2048, NULL, 5, NULL);
|
|
}
|
|
|
|
void status_led_set_state(led_state_t state) {
|
|
s_current_state = state;
|
|
}
|
|
|
|
led_state_t status_led_get_state(void) {
|
|
return s_current_state;
|
|
}
|