LED control improved

This commit is contained in:
Klaus K Wilting 2018-10-24 18:07:41 +02:00
parent efaba3775b
commit b4cbf8bdd9
4 changed files with 38 additions and 12 deletions

View File

@ -37,5 +37,8 @@ extern TaskHandle_t ledLoopTask;
void rgb_set_color(uint16_t hue); void rgb_set_color(uint16_t hue);
void blink_LED(uint16_t set_color, uint16_t set_blinkduration); void blink_LED(uint16_t set_color, uint16_t set_blinkduration);
void ledLoop(void *parameter); void ledLoop(void *parameter);
#if (HAS_LED != NOT_A_PIN)
void switch_LED(uint8_t state);
#endif
#endif #endif

View File

@ -12,5 +12,6 @@
#include "beacon_array.h" #include "beacon_array.h"
#include "ota.h" #include "ota.h"
#include "irqhandler.h" #include "irqhandler.h"
#include "led.h"
#endif #endif

View File

@ -87,6 +87,28 @@ void rgb_set_color(uint16_t hue) {}
#endif #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) #if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED)
void blink_LED(uint16_t set_color, uint16_t set_blinkduration) { 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) { void ledLoop(void *parameter) {
while (1) { 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) { if (LEDBlinkStarted && LEDBlinkDuration) {
// Custom blink is finished, let this order, avoid millis() overflow // Custom blink is finished, let this order, avoid millis() overflow
if ((millis() - LEDBlinkStarted) >= LEDBlinkDuration) { if ((millis() - LEDBlinkStarted) >= LEDBlinkDuration) {
@ -154,20 +177,14 @@ void ledLoop(void *parameter) {
if (LEDState != previousLEDState) { if (LEDState != previousLEDState) {
if (LEDState == LED_ON) { if (LEDState == LED_ON) {
rgb_set_color(LEDColor); rgb_set_color(LEDColor);
// if we have only single LED we use it to blink for status
#ifdef LED_ACTIVE_LOW #ifndef HAS_RGB_LED
digitalWrite(HAS_LED, LOW); switch_LED(LED_ON);
#else
digitalWrite(HAS_LED, HIGH);
#endif #endif
} else { } else {
rgb_set_color(COLOR_NONE); rgb_set_color(COLOR_NONE);
#ifndef HAS_RGB_LED
#ifdef LED_ACTIVE_LOW switch_LED(LED_OFF);
digitalWrite(HAS_LED, HIGH);
#else
digitalWrite(HAS_LED, LOW);
#endif #endif
} }
previousLEDState = LEDState; previousLEDState = LEDState;

View File

@ -96,7 +96,12 @@ void setup() {
#if (HAS_LED != NOT_A_PIN) #if (HAS_LED != NOT_A_PIN)
pinMode(HAS_LED, OUTPUT); pinMode(HAS_LED, OUTPUT);
strcat_P(features, " LED"); 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
#endif
#ifdef HAS_RGB_LED #ifdef HAS_RGB_LED
rgb_set_color(COLOR_PINK); rgb_set_color(COLOR_PINK);
strcat_P(features, " RGB"); strcat_P(features, " RGB");