diff --git a/include/led.h b/include/led.h index 2631563c..6527e879 100644 --- a/include/led.h +++ b/include/led.h @@ -37,5 +37,8 @@ extern TaskHandle_t ledLoopTask; void rgb_set_color(uint16_t hue); void blink_LED(uint16_t set_color, uint16_t set_blinkduration); void ledLoop(void *parameter); +#if (HAS_LED != NOT_A_PIN) +void switch_LED(uint8_t state); +#endif #endif \ No newline at end of file diff --git a/include/main.h b/include/main.h index 9c74aef5..f7284720 100644 --- a/include/main.h +++ b/include/main.h @@ -12,5 +12,6 @@ #include "beacon_array.h" #include "ota.h" #include "irqhandler.h" +#include "led.h" #endif \ No newline at end of file diff --git a/src/led.cpp b/src/led.cpp index 76195523..8e3b7ed8 100644 --- a/src/led.cpp +++ b/src/led.cpp @@ -87,6 +87,28 @@ void rgb_set_color(uint16_t hue) {} #endif +#if (HAS_LED != NOT_A_PIN) + +void switch_LED(uint8_t state) { + if (state == LED_ON) { + // switch LED on +#ifdef LED_ACTIVE_LOW + digitalWrite(HAS_LED, LOW); +#else + digitalWrite(HAS_LED, HIGH); +#endif + } else if (state == LED_OFF) { + // switch LED off +#ifdef LED_ACTIVE_LOW + digitalWrite(HAS_LED, HIGH); +#else + digitalWrite(HAS_LED, LOW); +#endif + } +} + +#endif + #if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED) void blink_LED(uint16_t set_color, uint16_t set_blinkduration) { @@ -98,7 +120,8 @@ void blink_LED(uint16_t set_color, uint16_t set_blinkduration) { void ledLoop(void *parameter) { while (1) { - // Custom blink running always have priority other LoRaWAN led management + // Custom blink running always have priority other LoRaWAN led + // management if (LEDBlinkStarted && LEDBlinkDuration) { // Custom blink is finished, let this order, avoid millis() overflow if ((millis() - LEDBlinkStarted) >= LEDBlinkDuration) { @@ -154,20 +177,14 @@ void ledLoop(void *parameter) { if (LEDState != previousLEDState) { if (LEDState == LED_ON) { rgb_set_color(LEDColor); - -#ifdef LED_ACTIVE_LOW - digitalWrite(HAS_LED, LOW); -#else - digitalWrite(HAS_LED, HIGH); + // 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); - -#ifdef LED_ACTIVE_LOW - digitalWrite(HAS_LED, HIGH); -#else - digitalWrite(HAS_LED, LOW); +#ifndef HAS_RGB_LED + switch_LED(LED_OFF); #endif } previousLEDState = LEDState; diff --git a/src/main.cpp b/src/main.cpp index 1cceb71f..af5fdd4c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -96,7 +96,12 @@ void setup() { #if (HAS_LED != NOT_A_PIN) pinMode(HAS_LED, OUTPUT); strcat_P(features, " LED"); +// switch on power LED if we have 2 LEDs, else use it for status +#ifdef HAS_RGB_LED + switch_LED(LED_ON); #endif +#endif + #ifdef HAS_RGB_LED rgb_set_color(COLOR_PINK); strcat_P(features, " RGB");