fix led color control

This commit is contained in:
cyberman54 2022-10-31 18:41:29 +01:00
parent c55b7e8709
commit 681118c2ec
2 changed files with 65 additions and 73 deletions

View File

@ -2,6 +2,7 @@
#define _LED_H #define _LED_H
#ifdef HAS_RGB_LED #ifdef HAS_RGB_LED
#define FASTLED_INTERNAL
#include <FastLED.h> #include <FastLED.h>
#include "libpax_helpers.h" #include "libpax_helpers.h"
#endif #endif
@ -16,10 +17,22 @@
enum led_states { LED_OFF, LED_ON }; enum led_states { LED_OFF, LED_ON };
enum colors {
COLOR_WHITE = 0xFFFFFF,
COLOR_NONE = 0x000000,
COLOR_CYAN = 0x00FFFF,
COLOR_BLUE = 0x0000FF,
COLOR_GREEN = 0x008000,
COLOR_YELLOW = 0xFFFF00,
COLOR_ORANGE = 0xFFA500,
COLOR_RED = 0xFF0000,
COLOR_PINK = 0xFFC0CB
};
extern TaskHandle_t ledLoopTask; extern TaskHandle_t ledLoopTask;
void rgb_led_init(void); void rgb_led_init(void);
void led_sethue(uint8_t hue); void rgb_set_color(uint32_t color);
void ledLoop(void *parameter); void ledLoop(void *parameter);
void switch_LED(led_states state); void switch_LED(led_states state);
void switch_LED1(led_states state); void switch_LED1(led_states state);

View File

@ -3,25 +3,23 @@
#include "led.h" #include "led.h"
static led_states LEDState = LED_OFF; // LED state global for state machine static led_states LEDState = LED_OFF; // LED state global for state machine
led_states previousLEDState =
LED_ON; // This will force LED to be off at boot since State is OFF
TaskHandle_t ledLoopTask; TaskHandle_t ledLoopTask;
static uint16_t LEDBlinkDuration = 0; // state machine variables
uint32_t LEDColor = COLOR_NONE, LEDBlinkDuration = 0; // state machine variables
static unsigned long LEDBlinkStarted = static unsigned long LEDBlinkStarted =
0; // When (in millis() led blink started) 0; // When (in millis() led blink started)
#ifdef HAS_RGB_LED #ifdef HAS_RGB_LED
CRGB leds[RGB_LED_COUNT]; CRGB leds[RGB_LED_COUNT];
void led_setcolor(CRGB color) {
for (int i = 0; i < RGB_LED_COUNT; i++)
leds[i] = color;
FastLED.show();
}
#endif #endif
void led_sethue(uint8_t hue) { void rgb_set_color(uint32_t color) {
#ifdef HAS_RGB_LED #ifdef HAS_RGB_LED
for (int i = 0; i < RGB_LED_COUNT; i++) for (int i = 0; i < RGB_LED_COUNT; i++)
leds[i] = CHSV(hue, 0XFF, 100); leds[i] = CRGB(color);
FastLED.show(); FastLED.show();
#endif #endif
} }
@ -29,15 +27,11 @@ void led_sethue(uint8_t hue) {
void rgb_led_init(void) { void rgb_led_init(void) {
#ifdef HAS_RGB_LED #ifdef HAS_RGB_LED
HAS_RGB_LED; HAS_RGB_LED;
led_setcolor(CRGB::Green); rgb_set_color(COLOR_NONE);
#endif #endif
} }
void switch_LED(led_states state) { void switch_LED(led_states state) {
static led_states previousLEDState = LED_OFF;
// led need to change state? avoid digitalWrite() for nothing
if (state != previousLEDState) {
previousLEDState = state;
#if (HAS_LED != NOT_A_PIN) #if (HAS_LED != NOT_A_PIN)
if (state == LED_ON) { if (state == LED_ON) {
// switch LED on // switch LED on
@ -45,9 +39,6 @@ void switch_LED(led_states state) {
digitalWrite(HAS_LED, LOW); digitalWrite(HAS_LED, LOW);
#else #else
digitalWrite(HAS_LED, HIGH); digitalWrite(HAS_LED, HIGH);
#endif
#ifdef HAS_RGB_LED
led_setcolor(CRGB::White);
#endif #endif
} else if (state == LED_OFF) { } else if (state == LED_OFF) {
// switch LED off // switch LED off
@ -55,42 +46,32 @@ void switch_LED(led_states state) {
digitalWrite(HAS_LED, HIGH); digitalWrite(HAS_LED, HIGH);
#else #else
digitalWrite(HAS_LED, LOW); digitalWrite(HAS_LED, LOW);
#endif
#ifdef HAS_RGB_LED
led_setcolor(CRGB::Black);
#endif #endif
} }
#endif #endif
} }
}
void switch_LED1(led_states state) { void switch_LED1(led_states state) {
#ifdef HAS_TWO_LED #ifdef HAS_TWO_LED
static led_states previousLEDState = LED_OFF;
// led need to change state? avoid digitalWrite() for nothing
if (state != previousLEDState) {
previousLEDState = state;
#if (HAS_LED != NOT_A_PIN)
if (state == LED_ON) { if (state == LED_ON) {
// switch LED on // switch LED on
#ifdef LED_ACTIVE_LOW #ifdef LED1_ACTIVE_LOW
digitalWrite(HAS_TWO_LED, LOW); digitalWrite(HAS_TWO_LED, LOW);
#else #else
digitalWrite(HAS_TWO_LED, HIGH); digitalWrite(HAS_TWO_LED, HIGH);
#endif #endif
} else if (state == LED_OFF) { } else if (state == LED_OFF) {
// switch LED off // switch LED off
#ifdef LED_ACTIVE_LOW #ifdef LED1_ACTIVE_LOW
digitalWrite(HAS_TWO_LED, HIGH); digitalWrite(HAS_TWO_LED, HIGH);
#else #else
digitalWrite(HAS_TWO_LED, LOW); digitalWrite(HAS_TWO_LED, LOW);
#endif #endif
} }
#endif
}
#endif // HAS_TWO_LED #endif // HAS_TWO_LED
} }
#if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED) #if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED)
void ledLoop(void *parameter) { void ledLoop(void *parameter) {
@ -104,9 +85,7 @@ void ledLoop(void *parameter) {
LEDState = LED_OFF; LEDState = LED_OFF;
LEDBlinkStarted = 0; LEDBlinkStarted = 0;
LEDBlinkDuration = 0; LEDBlinkDuration = 0;
#ifdef HAS_RGB_LED LEDColor = COLOR_NONE;
led_setcolor(CRGB::Black);
#endif
} else { } else {
// In case of LoRaWAN led management blinked off // In case of LoRaWAN led management blinked off
LEDState = LED_ON; LEDState = LED_ON;
@ -116,9 +95,7 @@ void ledLoop(void *parameter) {
#if (HAS_LORA) #if (HAS_LORA)
// LED indicators for viusalizing LoRaWAN state // LED indicators for viusalizing LoRaWAN state
if (LMIC.opmode & (OP_JOINING | OP_REJOIN)) { if (LMIC.opmode & (OP_JOINING | OP_REJOIN)) {
#ifdef HAS_RGB_LED LEDColor = COLOR_YELLOW;
led_setcolor(CRGB::Yellow);
#endif
// quick blink 20ms on each 1/5 second // quick blink 20ms on each 1/5 second
LEDState = LEDState =
((millis() % 200) < 20) ? LED_ON : LED_OFF; // TX data pending ((millis() % 200) < 20) ? LED_ON : LED_OFF; // TX data pending
@ -126,19 +103,13 @@ void ledLoop(void *parameter) {
// select color to blink by message port // select color to blink by message port
switch (LMIC.pendTxPort) { switch (LMIC.pendTxPort) {
case STATUSPORT: case STATUSPORT:
#ifdef HAS_RGB_LED LEDColor = COLOR_PINK;
led_setcolor(CRGB::Pink);
#endif
break; break;
case CONFIGPORT: case CONFIGPORT:
#ifdef HAS_RGB_LED LEDColor = COLOR_CYAN;
led_setcolor(CRGB::Cyan);
#endif
break; break;
default: default:
#ifdef HAS_RGB_LED LEDColor = COLOR_BLUE;
led_setcolor(CRGB::Blue);
#endif
break; break;
} }
// small blink 10ms on each 1/2sec (not when joining) // small blink 10ms on each 1/2sec (not when joining)
@ -146,25 +117,33 @@ void ledLoop(void *parameter) {
// This should not happen so indicate a problem // This should not happen so indicate a problem
} else if (LMIC.opmode & } else if (LMIC.opmode &
((OP_TXDATA | OP_TXRXPEND | OP_JOINING | OP_REJOIN) == 0)) { ((OP_TXDATA | OP_TXRXPEND | OP_JOINING | OP_REJOIN) == 0)) {
#ifdef HAS_RGB_LED LEDColor = COLOR_RED;
led_setcolor(CRGB::Red);
#endif
// heartbeat long blink 200ms on each 2 seconds // heartbeat long blink 200ms on each 2 seconds
LEDState = ((millis() % 2000) < 200) ? LED_ON : LED_OFF; LEDState = ((millis() % 2000) < 200) ? LED_ON : LED_OFF;
} else } else
#elif (defined(HAS_RGB_LED) && ((WIFICOUNTER) || (BLECOUNTER)))
struct count_payload_t count;
libpax_counter_count(&count);
led_sethue(count.pax);
#endif // HAS_LORA #endif // HAS_LORA
{ {
// led off // led off
LEDColor = COLOR_NONE;
LEDState = LED_OFF; LEDState = LED_OFF;
} }
} }
// led need to change state? avoid digitalWrite() for nothing
switch_LED(LEDState); if (LEDState != previousLEDState) {
if (LEDState == LED_ON) {
rgb_set_color(LEDColor);
// if we have only single LED we use it to blink for status
#ifndef HAS_RGB_LED
switch_LED(LED_ON);
#endif
} else {
rgb_set_color(COLOR_NONE);
#ifndef HAS_RGB_LED
switch_LED(LED_OFF);
#endif
}
previousLEDState = LEDState;
}
// give yield to CPU // give yield to CPU
delay(5); delay(5);
} // while(1) } // while(1)