/* * status_led.c * * Copyright (c) 2025 Umber Networks & Robert McMahon * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * 3. Neither the name of the copyright holder nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #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; // Helper to set color safely static void set_color(uint8_t r, uint8_t g, uint8_t b) { if (s_is_rgb && s_led_strip) { 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 logic: If any color is requested, turn ON. // NOTE: If your LED is active-low (VCC->LED->Pin), invert this to !((r+g+b)>0) gpio_set_level(s_gpio_pin, (r + g + b) > 0 ? 1 : 0); } } static void led_task(void *arg) { int toggle = 0; // --- Startup Diagnostic Sequence --- // Cycle R -> G -> B to prove hardware is working ESP_LOGW(TAG, "Running LED Diagnostic Sequence on GPIO %d...", s_gpio_pin); set_color(50, 0, 0); // Red vTaskDelay(pdMS_TO_TICKS(300)); set_color(0, 50, 0); // Green vTaskDelay(pdMS_TO_TICKS(300)); set_color(0, 0, 50); // Blue vTaskDelay(pdMS_TO_TICKS(300)); set_color(0, 0, 0); // Off vTaskDelay(pdMS_TO_TICKS(100)); while (1) { // Brightness set to 30-50 (out of 255) for visibility switch (s_current_state) { case LED_STATE_NO_CONFIG: // Yellow (Solid RGB / Blink Simple) if (s_is_rgb) { set_color(40, 30, 0); vTaskDelay(pdMS_TO_TICKS(1000)); } else { set_color(1,1,1); vTaskDelay(pdMS_TO_TICKS(100)); set_color(0,0,0); vTaskDelay(pdMS_TO_TICKS(100)); } break; case LED_STATE_WAITING: // Blue Blink set_color(0, 0, toggle ? 50 : 0); toggle = !toggle; vTaskDelay(pdMS_TO_TICKS(500)); break; case LED_STATE_CONNECTED: // Green Solid set_color(0, 30, 0); vTaskDelay(pdMS_TO_TICKS(1000)); break; case LED_STATE_MONITORING: // Cyan Solid set_color(0, 30, 30); vTaskDelay(pdMS_TO_TICKS(1000)); break; case LED_STATE_TRANSMITTING: // Purple Fast Flash set_color(toggle ? 40 : 0, 0, toggle ? 40 : 0); toggle = !toggle; vTaskDelay(pdMS_TO_TICKS(100)); break; case LED_STATE_TRANSMITTING_SLOW: // Purple Slow Pulse set_color(toggle ? 40 : 0, 0, toggle ? 40 : 0); toggle = !toggle; vTaskDelay(pdMS_TO_TICKS(500)); break; case LED_STATE_STALLED: // Red/Purple Solid set_color(50, 0, 20); vTaskDelay(pdMS_TO_TICKS(1000)); break; case LED_STATE_FAILED: // Red Blink set_color(toggle ? 50 : 0, 0, 0); toggle = !toggle; vTaskDelay(pdMS_TO_TICKS(200)); break; default: vTaskDelay(pdMS_TO_TICKS(100)); 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 Status LED: GPIO=%d, Type=%s", gpio_pin, is_rgb_strip ? "RGB Strip (WS2812)" : "Simple GPIO"); if (s_is_rgb) { led_strip_config_t s_cfg = { .strip_gpio_num = gpio_pin, .max_leds = 1, .led_pixel_format = LED_PIXEL_FORMAT_GRB, .led_model = LED_MODEL_WS2812, .flags.invert_out = false, }; led_strip_rmt_config_t r_cfg = { .resolution_hz = 10 * 1000 * 1000, .flags.with_dma = false, }; esp_err_t ret = led_strip_new_rmt_device(&s_cfg, &r_cfg, &s_led_strip); if (ret != ESP_OK) { ESP_LOGE(TAG, "Failed to create RMT LED strip: %s", esp_err_to_name(ret)); return; } 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; }