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) {
|
2018-11-04 19:25:11 +01:00
|
|
|
#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-11-04 19:25:11 +01:00
|
|
|
}
|
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)
|
|
|
|
|
2018-10-03 00:25:05 +02:00
|
|
|
void ledLoop(void *parameter) {
|
|
|
|
while (1) {
|
2018-10-24 18:07:41 +02:00
|
|
|
// Custom blink running always have priority other LoRaWAN led
|
|
|
|
// management
|
2018-10-03 00:25:05 +02:00
|
|
|
if (LEDBlinkStarted && LEDBlinkDuration) {
|
2020-12-09 20:37:03 +01:00
|
|
|
// Custom blink is finished, let this order, avoid millis() overflow
|
2020-12-21 22:11:41 +01:00
|
|
|
if ((long)(millis() - LEDBlinkStarted) >= LEDBlinkDuration) {
|
2018-10-03 00:25:05 +02:00
|
|
|
// Led becomes off, and stop blink
|
|
|
|
LEDState = LED_OFF;
|
|
|
|
LEDBlinkStarted = 0;
|
|
|
|
LEDBlinkDuration = 0;
|
2022-10-31 18:41:29 +01:00
|
|
|
LEDColor = COLOR_NONE;
|
2018-10-03 00:25:05 +02:00
|
|
|
} 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)
|
2018-10-03 00:25:05 +02:00
|
|
|
// LED indicators for viusalizing LoRaWAN state
|
|
|
|
if (LMIC.opmode & (OP_JOINING | OP_REJOIN)) {
|
2022-10-31 18:41:29 +01:00
|
|
|
LEDColor = COLOR_YELLOW;
|
2018-10-03 00:25:05 +02:00
|
|
|
// quick blink 20ms on each 1/5 second
|
|
|
|
LEDState =
|
2020-12-09 20:37:03 +01:00
|
|
|
((millis() % 200) < 20) ? LED_ON : LED_OFF; // TX data pending
|
2018-10-03 00:25:05 +02:00
|
|
|
} 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;
|
|
|
|
}
|
2018-10-03 00:25:05 +02:00
|
|
|
// small blink 10ms on each 1/2sec (not when joining)
|
2020-12-09 20:37:03 +01:00
|
|
|
LEDState = ((millis() % 500) < 10) ? LED_ON : LED_OFF;
|
2018-10-03 00:25:05 +02:00
|
|
|
// 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;
|
2018-10-03 00:25:05 +02:00
|
|
|
// heartbeat long blink 200ms on each 2 seconds
|
2020-12-09 20:37:03 +01:00
|
|
|
LEDState = ((millis() % 2000) < 200) ? LED_ON : LED_OFF;
|
2018-10-03 00:25:05 +02:00
|
|
|
} else
|
2018-07-15 14:28:05 +02:00
|
|
|
#endif // HAS_LORA
|
2018-10-03 00:25:05 +02:00
|
|
|
{
|
|
|
|
// led off
|
2022-10-31 18:41:29 +01:00
|
|
|
LEDColor = COLOR_NONE;
|
2018-10-03 00:25:05 +02:00
|
|
|
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;
|
|
|
|
}
|
2018-10-03 00:25:05 +02:00
|
|
|
// 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)
|