fix led so it works now

This commit is contained in:
Robert McMahon 2025-12-22 15:52:02 -08:00
parent 98b013569d
commit 2590a96b15
2 changed files with 34 additions and 80 deletions

View File

@ -30,7 +30,6 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "status_led.h" #include "status_led.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
@ -38,87 +37,56 @@
#include "led_strip.h" #include "led_strip.h"
#include "esp_log.h" #include "esp_log.h"
static const char *TAG = "status_led"; static const char *TAG = "STATUS_LED"; // Added TAG for logging
static led_strip_handle_t s_led_strip = NULL; static led_strip_handle_t s_led_strip = NULL;
static bool s_is_rgb = false; static bool s_is_rgb = false;
static int s_gpio_pin = -1; static int s_gpio_pin = -1;
static volatile led_state_t s_current_state = LED_STATE_NO_CONFIG; 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) { static void set_color(uint8_t r, uint8_t g, uint8_t b) {
if (s_is_rgb && s_led_strip) { if (s_is_rgb && s_led_strip) {
led_strip_set_pixel(s_led_strip, 0, r, g, b); led_strip_set_pixel(s_led_strip, 0, r, g, b);
led_strip_refresh(s_led_strip); led_strip_refresh(s_led_strip);
} else if (!s_is_rgb && s_gpio_pin >= 0) { } else if (!s_is_rgb && s_gpio_pin >= 0) {
// Simple LED logic: If any color is requested, turn ON. gpio_set_level(s_gpio_pin, (r+g+b) > 0);
// 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) { static void led_task(void *arg) {
int toggle = 0; 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) { while (1) {
// Brightness set to 30-50 (out of 255) for visibility
switch (s_current_state) { switch (s_current_state) {
case LED_STATE_NO_CONFIG: // Yellow (Solid RGB / Blink Simple) case LED_STATE_NO_CONFIG: // Yellow
if (s_is_rgb) { if (s_is_rgb) { set_color(25, 25, 0); vTaskDelay(pdMS_TO_TICKS(1000)); }
set_color(40, 30, 0); else { set_color(1,1,1); vTaskDelay(100); set_color(0,0,0); vTaskDelay(100); }
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; break;
case LED_STATE_WAITING: // Blue Blink // ... rest of cases identical to your code ...
set_color(0, 0, toggle ? 50 : 0); case LED_STATE_WAITING:
toggle = !toggle; set_color(0, 0, toggle ? 50 : 0); toggle = !toggle;
vTaskDelay(pdMS_TO_TICKS(500)); vTaskDelay(pdMS_TO_TICKS(500));
break; break;
case LED_STATE_CONNECTED: // Green Solid case LED_STATE_CONNECTED:
set_color(0, 30, 0); set_color(0, 25, 0); vTaskDelay(pdMS_TO_TICKS(1000));
vTaskDelay(pdMS_TO_TICKS(1000));
break; break;
case LED_STATE_MONITORING: // Cyan Solid case LED_STATE_MONITORING:
set_color(0, 30, 30); set_color(0, 0, 50); vTaskDelay(pdMS_TO_TICKS(1000));
vTaskDelay(pdMS_TO_TICKS(1000));
break; break;
case LED_STATE_TRANSMITTING: // Purple Fast Flash case LED_STATE_TRANSMITTING:
set_color(toggle ? 40 : 0, 0, toggle ? 40 : 0); set_color(toggle ? 50 : 0, 0, toggle ? 50 : 0); toggle = !toggle;
toggle = !toggle; vTaskDelay(pdMS_TO_TICKS(50));
vTaskDelay(pdMS_TO_TICKS(100));
break; break;
case LED_STATE_TRANSMITTING_SLOW: // Purple Slow Pulse case LED_STATE_TRANSMITTING_SLOW:
set_color(toggle ? 40 : 0, 0, toggle ? 40 : 0); set_color(toggle ? 50 : 0, 0, toggle ? 50 : 0); toggle = !toggle;
toggle = !toggle; vTaskDelay(pdMS_TO_TICKS(250));
vTaskDelay(pdMS_TO_TICKS(500));
break; break;
case LED_STATE_STALLED: // Red/Purple Solid case LED_STATE_STALLED:
set_color(50, 0, 20); set_color(50, 0, 50); vTaskDelay(pdMS_TO_TICKS(1000));
vTaskDelay(pdMS_TO_TICKS(1000));
break; break;
case LED_STATE_FAILED: // Red Blink case LED_STATE_FAILED:
set_color(toggle ? 50 : 0, 0, 0); set_color(toggle ? 50 : 0, 0, 0); toggle = !toggle;
toggle = !toggle;
vTaskDelay(pdMS_TO_TICKS(200)); vTaskDelay(pdMS_TO_TICKS(200));
break; break;
default:
vTaskDelay(pdMS_TO_TICKS(100));
break;
} }
} }
} }
@ -127,41 +95,27 @@ void status_led_init(int gpio_pin, bool is_rgb_strip) {
s_gpio_pin = gpio_pin; s_gpio_pin = gpio_pin;
s_is_rgb = is_rgb_strip; s_is_rgb = is_rgb_strip;
ESP_LOGI(TAG, "Initializing Status LED: GPIO=%d, Type=%s", // --- DIAGNOSTIC LOG ---
gpio_pin, is_rgb_strip ? "RGB Strip (WS2812)" : "Simple GPIO"); ESP_LOGW(TAG, "Initializing LED on GPIO %d (RGB: %d)", gpio_pin, is_rgb_strip);
if (s_is_rgb) { if (s_is_rgb) {
led_strip_config_t s_cfg = { led_strip_config_t s_cfg = { .strip_gpio_num = gpio_pin, .max_leds = 1 };
.strip_gpio_num = gpio_pin, led_strip_rmt_config_t r_cfg = { .resolution_hz = 10 * 1000 * 1000 };
.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); esp_err_t ret = led_strip_new_rmt_device(&s_cfg, &r_cfg, &s_led_strip);
if (ret != ESP_OK) { if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to create RMT LED strip: %s", esp_err_to_name(ret)); ESP_LOGE(TAG, "RMT Device Init Failed: %s", esp_err_to_name(ret));
return; } else {
ESP_LOGI(TAG, "RMT Device Init Success");
led_strip_clear(s_led_strip);
} }
led_strip_clear(s_led_strip);
} else { } else {
gpio_reset_pin(gpio_pin); gpio_reset_pin(gpio_pin);
gpio_set_direction(gpio_pin, GPIO_MODE_OUTPUT); gpio_set_direction(gpio_pin, GPIO_MODE_OUTPUT);
gpio_set_level(gpio_pin, 0);
} }
xTaskCreate(led_task, "led_task", 2048, NULL, 5, NULL); xTaskCreate(led_task, "led_task", 2048, NULL, 5, NULL);
} }
void status_led_set_state(led_state_t state) { // ... Setters/Getters ...
s_current_state = state; 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; }
led_state_t status_led_get_state(void) {
return s_current_state;
}

View File

@ -42,7 +42,7 @@
// ============================================================================ // ============================================================================
// ESP32-C5 (DevKitC-1) 3.3V VCC Pin 1 GND PIN 15 // ESP32-C5 (DevKitC-1) 3.3V VCC Pin 1 GND PIN 15
// ============================================================================ // ============================================================================
#define RGB_LED_GPIO 8 // Common addressable LED pin for C5 #define RGB_LED_GPIO 27 // Common addressable LED pin for C5
#define HAS_RGB_LED 1 #define HAS_RGB_LED 1
#define GPS_TX_PIN GPIO_NUM_24 #define GPS_TX_PIN GPIO_NUM_24
#define GPS_RX_PIN GPIO_NUM_23 #define GPS_RX_PIN GPIO_NUM_23