#include "led_ring.h" #include "driver/i2c_master.h" #include "driver/i2c_types.h" #include "esp_err.h" #include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "led_strip.h" #include typedef struct { const uint8_t *leds; uint8_t count; } digit_definition_t; static const char *TAG = "[LED_RING]"; static led_strip_handle_t led_ring; #define RING_LEDS 95 #define LED_RING_PIN 7 #define LED_RING_BLINK_ON_MS 350 #define LED_RING_BLINK_OFF_MS 150 #define LED_RING_FIND_ME_ON_MS 300 #define LED_RING_FIND_ME_OFF_MS 150 #define LED_RING_FIND_ME_BLINKS_PER_COLOR 3 static QueueHandle_t led_queue; // Led Matrix Maps const uint8_t d0[] = {46, 47, 60, 61, 62, 75, 78, 79, 80, 81, 82, 86, 87, 88, 89, 90}; const uint8_t d1[] = {23, 46, 47, 48, 61, 62, 74, 75, 76, 84, 86, 93, 95}; const uint8_t d2[] = {21, 22, 23, 24, 25, 26, 46, 47, 48, 49, 59, 64, 71, 72, 73, 74, 75, 83, 89, 92, 95}; const uint8_t d3[] = {1, 2, 21, 22, 23, 24, 25, 26, 27, 41, 42, 43, 44, 45, 48, 59, 77, 83, 92, 93, 95}; const uint8_t d4[] = {21, 26, 47, 59, 64, 77, 82, 86, 94, 95}; const uint8_t d5[] = {19, 20, 21, 22, 23, 24, 25, 26, 63, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 90, 91}; const uint8_t d6[] = {19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 65, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91}; const uint8_t d7[] = {1, 47, 58, 59, 60, 61, 62, 63, 64, 65, 77, 80, 82, 92, 94}; const uint8_t d8[] = {1, 2, 3, 4, 5, 19, 20, 21, 22, 23, 24, 25, 26, 27, 41, 42, 43, 44, 45, 49, 58, 64, 73, 78, 82, 86, 90, 92, 93, 94, 95}; const uint8_t d9[] = {19, 20, 21, 22, 23, 24, 25, 26, 27, 46, 47, 58, 64, 71, 72, 73, 74, 75, 77, 82, 86, 94, 95}; const uint8_t d10[] = {46, 50, 57, 61, 65, 72, 76, 78, 80, 82, 84, 86, 88, 90, 92, 93, 94, 95}; const digit_definition_t digit_lookup[] = { {d0, sizeof(d0)}, {d1, sizeof(d1)}, {d2, sizeof(d2)}, {d3, sizeof(d3)}, {d4, sizeof(d4)}, {d5, sizeof(d5)}, {d6, sizeof(d6)}, {d7, sizeof(d7)}, {d8, sizeof(d8)}, {d9, sizeof(d9)}, {d10, sizeof(d10)}}; void led_ring_scale_rgb(uint8_t *r, uint8_t *g, uint8_t *b, uint8_t intensity) { if (intensity == 0) { intensity = LED_RING_DEFAULT_INTENSITY; } *r = (uint16_t)(*r) * intensity / 255; *g = (uint16_t)(*g) * intensity / 255; *b = (uint16_t)(*b) * intensity / 255; } static void ring_fill_color(uint8_t r, uint8_t g, uint8_t b) { for (uint32_t i = 0; i < RING_LEDS; i++) { led_strip_set_pixel(led_ring, i, r, g, b); } } static void ring_blink_scaled(uint8_t r, uint8_t g, uint8_t b, uint8_t intensity, uint8_t count, uint16_t on_ms, uint16_t off_ms) { led_ring_scale_rgb(&r, &g, &b, intensity); for (uint8_t n = 0; n < count; n++) { ring_fill_color(r, g, b); led_strip_refresh(led_ring); vTaskDelay(pdMS_TO_TICKS(on_ms)); led_strip_clear(led_ring); led_strip_refresh(led_ring); if (n + 1 < count) { vTaskDelay(pdMS_TO_TICKS(off_ms)); } } } void vTaskLedRing(void *pvParameters) { led_strip_config_t ring_config = { .strip_gpio_num = LED_RING_PIN, .max_leds = RING_LEDS, }; led_strip_rmt_config_t rmt_ring_config = { .resolution_hz = 10 * 1000 * 1000, }; esp_err_t err = led_strip_new_rmt_device(&ring_config, &rmt_ring_config, &led_ring); if (err == ESP_OK) { ESP_LOGI(TAG, "GPIO_RING_TASK started"); } led_command_t cmd; while (1) { if (xQueueReceive(led_queue, &cmd, portMAX_DELAY)) { uint8_t r = cmd.r; uint8_t g = cmd.g; uint8_t b = cmd.b; led_ring_scale_rgb(&r, &g, &b, cmd.intensity); led_strip_clear(led_ring); if (cmd.mode == LED_CMD_CLEAR) { /* ring already cleared */ } else if (cmd.mode == LED_CMD_SET_DIGIT && cmd.value <= 10) { digit_definition_t digit = digit_lookup[cmd.value]; for (int i = 0; i < digit.count; i++) { led_strip_set_pixel(led_ring, RING_LEDS - digit.leds[i], r, g, b); } } else if (cmd.mode == LED_CMD_SET_COLOR) { ring_fill_color(r, g, b); } else if (cmd.mode == LED_CMD_PROGRESS) { uint32_t lit = ((uint32_t)cmd.progress * RING_LEDS + 50) / 100; if (lit > RING_LEDS) { lit = RING_LEDS; } for (uint32_t i = 0; i < lit; i++) { led_strip_set_pixel(led_ring, i, r, g, b); } } else if (cmd.mode == LED_CMD_BLINK) { uint16_t on_ms = cmd.blink_ms > 0 ? cmd.blink_ms : LED_RING_BLINK_ON_MS; uint8_t count = cmd.blink_count > 0 ? cmd.blink_count : 1; ring_blink_scaled(cmd.r, cmd.g, cmd.b, cmd.intensity, count, on_ms, LED_RING_BLINK_OFF_MS); continue; } else if (cmd.mode == LED_CMD_FIND_ME) { static const struct { uint8_t r, g, b; } colors[] = {{255, 0, 0}, {0, 255, 0}, {0, 0, 255}}; for (size_t c = 0; c < sizeof(colors) / sizeof(colors[0]); c++) { ring_blink_scaled(colors[c].r, colors[c].g, colors[c].b, LED_RING_FULL_INTENSITY, LED_RING_FIND_ME_BLINKS_PER_COLOR, LED_RING_FIND_ME_ON_MS, LED_RING_FIND_ME_OFF_MS); if (c + 1 < sizeof(colors) / sizeof(colors[0])) { vTaskDelay(pdMS_TO_TICKS(LED_RING_FIND_ME_OFF_MS)); } } continue; } led_strip_refresh(led_ring); } } } void led_ring_init(void) { led_queue = xQueueCreate(10, sizeof(led_command_t)); xTaskCreate(vTaskLedRing, "led_task", 4096, NULL, 5, NULL); } void led_ring_send_command(led_command_t *cmd) { if (led_queue != NULL) { (void)xQueueSend(led_queue, cmd, 0); } } void led_ring_show_ota_clear(void) { led_command_t cmd = {.mode = LED_CMD_CLEAR}; led_ring_send_command(&cmd); } void led_ring_show_ota_progress(uint32_t bytes_done, uint32_t total_bytes, uint8_t r, uint8_t g, uint8_t b) { static struct { uint8_t pct; uint8_t r, g, b; } last = {255, 0, 0, 0}; if (total_bytes == 0) { return; } uint32_t pct32 = (bytes_done * 100u + total_bytes / 2) / total_bytes; if (pct32 > 100) { pct32 = 100; } uint8_t pct = (uint8_t)pct32; if (pct == last.pct && r == last.r && g == last.g && b == last.b) { return; } last.pct = pct; last.r = r; last.g = g; last.b = b; led_command_t cmd = { .mode = LED_CMD_PROGRESS, .progress = pct, .r = r, .g = g, .b = b, .intensity = LED_RING_DEFAULT_INTENSITY, }; led_ring_send_command(&cmd); } void led_ring_blink_once(uint8_t r, uint8_t g, uint8_t b) { led_command_t cmd = { .mode = LED_CMD_BLINK, .r = r, .g = g, .b = b, .intensity = LED_RING_DEFAULT_INTENSITY, .blink_ms = LED_RING_BLINK_ON_MS, .blink_count = 1, }; led_ring_send_command(&cmd); } void led_ring_ota_success(void) { led_ring_blink_once(0, 255, 0); } void led_ring_ota_failed(void) { led_ring_blink_once(255, 0, 0); } void led_ring_find_me(void) { led_command_t cmd = {.mode = LED_CMD_FIND_ME}; led_ring_send_command(&cmd); }