ESP32-PaxCounter/src/led.cpp

152 lines
3.9 KiB
C++
Raw Normal View History

2018-07-15 14:28:05 +02:00
// Basic Config
#include "globals.h"
2018-07-31 10:05:32 +02:00
#include "led.h"
2018-07-15 14:28:05 +02:00
2022-10-31 16:56:48 +01:00
static led_states LEDState = LED_OFF; // LED state global for state machine
2022-10-31 18:41:29 +01:00
led_states previousLEDState =
LED_ON; // This will force LED to be off at boot since State is OFF
2018-10-03 16:24:45 +02:00
TaskHandle_t ledLoopTask;
2022-10-31 18:41:29 +01:00
uint32_t LEDColor = COLOR_NONE, LEDBlinkDuration = 0; // state machine variables
2022-10-31 16:56:48 +01:00
static unsigned long LEDBlinkStarted =
0; // When (in millis() led blink started)
2018-07-15 14:28:05 +02:00
#ifdef HAS_RGB_LED
2022-10-31 16:56:48 +01:00
CRGB leds[RGB_LED_COUNT];
#endif
2018-07-15 14:28:05 +02:00
2022-10-31 18:41:29 +01:00
void rgb_set_color(uint32_t color) {
2022-10-31 16:56:48 +01:00
#ifdef HAS_RGB_LED
for (int i = 0; i < RGB_LED_COUNT; i++)
2022-10-31 18:41:29 +01:00
leds[i] = CRGB(color);
2022-10-31 16:56:48 +01:00
FastLED.show();
#endif
2018-07-15 14:28:05 +02:00
}
2022-10-31 16:56:48 +01:00
void rgb_led_init(void) {
#ifdef HAS_RGB_LED
HAS_RGB_LED;
2022-10-31 18:41:29 +01:00
rgb_set_color(COLOR_NONE);
2018-07-15 14:28:05 +02:00
#endif
2022-10-31 16:56:48 +01:00
}
2018-07-15 14:28:05 +02:00
2022-10-31 16:56:48 +01:00
void switch_LED(led_states state) {
#if (HAS_LED != NOT_A_PIN)
2022-10-31 18:41:29 +01:00
if (state == LED_ON) {
// switch LED on
2018-10-24 18:07:41 +02:00
#ifdef LED_ACTIVE_LOW
2022-10-31 18:41:29 +01:00
digitalWrite(HAS_LED, LOW);
2018-10-24 18:07:41 +02:00
#else
2022-10-31 18:41:29 +01:00
digitalWrite(HAS_LED, HIGH);
2022-10-31 16:56:48 +01:00
#endif
2022-10-31 18:41:29 +01:00
} else if (state == LED_OFF) {
// switch LED off
2018-10-24 18:07:41 +02:00
#ifdef LED_ACTIVE_LOW
2022-10-31 18:41:29 +01:00
digitalWrite(HAS_LED, HIGH);
2018-10-24 18:07:41 +02:00
#else
2022-10-31 18:41:29 +01:00
digitalWrite(HAS_LED, LOW);
2022-10-31 16:56:48 +01:00
#endif
}
2022-10-31 18:41:29 +01:00
#endif
}
2018-10-24 18:07:41 +02:00
2022-10-31 16:56:48 +01:00
void switch_LED1(led_states state) {
2019-03-24 16:18:08 +01:00
#ifdef HAS_TWO_LED
2022-10-31 18:41:29 +01:00
if (state == LED_ON) {
// switch LED on
#ifdef LED1_ACTIVE_LOW
digitalWrite(HAS_TWO_LED, LOW);
2019-03-24 01:05:13 +01:00
#else
2022-10-31 18:41:29 +01:00
digitalWrite(HAS_TWO_LED, HIGH);
2019-03-24 01:05:13 +01:00
#endif
2022-10-31 18:41:29 +01:00
} else if (state == LED_OFF) {
// switch LED off
#ifdef LED1_ACTIVE_LOW
digitalWrite(HAS_TWO_LED, HIGH);
2019-03-24 01:05:13 +01:00
#else
2022-10-31 18:41:29 +01:00
digitalWrite(HAS_TWO_LED, LOW);
2019-03-24 01:05:13 +01:00
#endif
}
2019-03-24 16:18:08 +01:00
#endif // HAS_TWO_LED
2019-03-24 01:05:13 +01:00
}
2022-10-31 18:41:29 +01:00
2018-11-25 18:27:20 +01:00
#if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED)
void ledLoop(void *parameter) {
while (1) {
2018-10-24 18:07:41 +02:00
// Custom blink running always have priority other LoRaWAN led
// management
if (LEDBlinkStarted && LEDBlinkDuration) {
// Custom blink is finished, let this order, avoid millis() overflow
2020-12-21 22:11:41 +01:00
if ((long)(millis() - LEDBlinkStarted) >= LEDBlinkDuration) {
// Led becomes off, and stop blink
LEDState = LED_OFF;
LEDBlinkStarted = 0;
LEDBlinkDuration = 0;
2022-10-31 18:41:29 +01:00
LEDColor = COLOR_NONE;
} else {
// In case of LoRaWAN led management blinked off
LEDState = LED_ON;
}
// No custom blink, check LoRaWAN state
2018-07-15 14:28:05 +02:00
} else {
2019-03-24 16:18:08 +01:00
#if (HAS_LORA)
// LED indicators for viusalizing LoRaWAN state
if (LMIC.opmode & (OP_JOINING | OP_REJOIN)) {
2022-10-31 18:41:29 +01:00
LEDColor = COLOR_YELLOW;
// quick blink 20ms on each 1/5 second
LEDState =
((millis() % 200) < 20) ? LED_ON : LED_OFF; // TX data pending
} else if (LMIC.opmode & (OP_TXDATA | OP_TXRXPEND)) {
2018-10-16 08:48:09 +02:00
// select color to blink by message port
switch (LMIC.pendTxPort) {
case STATUSPORT:
2022-10-31 18:41:29 +01:00
LEDColor = COLOR_PINK;
2018-10-16 08:48:09 +02:00
break;
case CONFIGPORT:
2022-10-31 18:41:29 +01:00
LEDColor = COLOR_CYAN;
2018-10-16 08:48:09 +02:00
break;
default:
2022-10-31 18:41:29 +01:00
LEDColor = COLOR_BLUE;
2018-10-16 08:48:09 +02:00
break;
}
// small blink 10ms on each 1/2sec (not when joining)
LEDState = ((millis() % 500) < 10) ? LED_ON : LED_OFF;
// This should not happen so indicate a problem
} else if (LMIC.opmode &
((OP_TXDATA | OP_TXRXPEND | OP_JOINING | OP_REJOIN) == 0)) {
2022-10-31 18:41:29 +01:00
LEDColor = COLOR_RED;
// heartbeat long blink 200ms on each 2 seconds
LEDState = ((millis() % 2000) < 200) ? LED_ON : LED_OFF;
} else
2018-07-15 14:28:05 +02:00
#endif // HAS_LORA
{
// led off
2022-10-31 18:41:29 +01:00
LEDColor = COLOR_NONE;
LEDState = LED_OFF;
}
2018-07-15 14:28:05 +02:00
}
2022-10-31 18:41:29 +01:00
// led need to change state? avoid digitalWrite() for nothing
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
2022-01-26 22:45:34 +01:00
delay(5);
2020-04-06 11:53:55 +02:00
} // while(1)
}; // ledloop()
2018-07-15 14:28:05 +02:00
2022-10-31 16:56:48 +01:00
#endif // #if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED)