ESP32/components/status_led/status_led.c

122 lines
4.8 KiB
C

/*
* 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"; // Added TAG for logging
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;
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) {
gpio_set_level(s_gpio_pin, (r+g+b) > 0);
}
}
static void led_task(void *arg) {
int toggle = 0;
while (1) {
switch (s_current_state) {
case LED_STATE_NO_CONFIG: // Yellow
if (s_is_rgb) { set_color(25, 25, 0); vTaskDelay(pdMS_TO_TICKS(1000)); }
else { set_color(1,1,1); vTaskDelay(100); set_color(0,0,0); vTaskDelay(100); }
break;
// ... rest of cases identical to your code ...
case LED_STATE_WAITING:
set_color(0, 0, toggle ? 50 : 0); toggle = !toggle;
vTaskDelay(pdMS_TO_TICKS(500));
break;
case LED_STATE_CONNECTED:
set_color(0, 25, 0); vTaskDelay(pdMS_TO_TICKS(1000));
break;
case LED_STATE_MONITORING:
set_color(0, 0, 50); vTaskDelay(pdMS_TO_TICKS(1000));
break;
case LED_STATE_TRANSMITTING:
set_color(toggle ? 50 : 0, 0, toggle ? 50 : 0); toggle = !toggle;
vTaskDelay(pdMS_TO_TICKS(50));
break;
case LED_STATE_TRANSMITTING_SLOW:
set_color(toggle ? 50 : 0, 0, toggle ? 50 : 0); toggle = !toggle;
vTaskDelay(pdMS_TO_TICKS(250));
break;
case LED_STATE_STALLED:
set_color(50, 0, 50); vTaskDelay(pdMS_TO_TICKS(1000));
break;
case LED_STATE_FAILED:
set_color(toggle ? 50 : 0, 0, 0); toggle = !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;
// --- DIAGNOSTIC LOG ---
ESP_LOGW(TAG, "Initializing LED on GPIO %d (RGB: %d)", gpio_pin, is_rgb_strip);
if (s_is_rgb) {
led_strip_config_t s_cfg = { .strip_gpio_num = gpio_pin, .max_leds = 1 };
led_strip_rmt_config_t r_cfg = { .resolution_hz = 10 * 1000 * 1000 };
esp_err_t ret = led_strip_new_rmt_device(&s_cfg, &r_cfg, &s_led_strip);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "RMT Device Init Failed: %s", esp_err_to_name(ret));
} else {
ESP_LOGI(TAG, "RMT Device Init Success");
led_strip_clear(s_led_strip);
}
} else {
gpio_reset_pin(gpio_pin);
gpio_set_direction(gpio_pin, GPIO_MODE_OUTPUT);
}
xTaskCreate(led_task, "led_task", 2048, NULL, 5, NULL);
}
// ... Setters/Getters ...
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; }