ESP32-PaxCounter/src/led.cpp

173 lines
4.4 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
2018-10-03 16:24:45 +02:00
TaskHandle_t ledLoopTask;
2022-10-31 16:56:48 +01:00
static uint16_t LEDBlinkDuration = 0; // state machine variables
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];
2018-07-15 14:28:05 +02:00
2022-10-31 16:56:48 +01:00
void led_setcolor(CRGB color) {
for (int i = 0; i < RGB_LED_COUNT; i++)
leds[i] = color;
FastLED.show();
2018-07-15 14:28:05 +02:00
}
2022-10-31 16:56:48 +01:00
#endif
2018-07-15 14:28:05 +02:00
2022-10-31 16:56:48 +01:00
void led_sethue(uint8_t hue) {
#ifdef HAS_RGB_LED
for (int i = 0; i < RGB_LED_COUNT; i++)
leds[i] = CHSV(hue, 0XFF, 100);
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;
led_setcolor(CRGB::Green);
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) {
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)
2022-10-31 16:56:48 +01:00
if (state == LED_ON) {
// switch LED on
2018-10-24 18:07:41 +02:00
#ifdef LED_ACTIVE_LOW
2022-10-31 16:56:48 +01:00
digitalWrite(HAS_LED, LOW);
2018-10-24 18:07:41 +02:00
#else
2022-10-31 16:56:48 +01:00
digitalWrite(HAS_LED, HIGH);
2018-10-24 18:07:41 +02:00
#endif
2022-10-31 16:56:48 +01:00
#ifdef HAS_RGB_LED
led_setcolor(CRGB::White);
#endif
} else if (state == LED_OFF) {
// switch LED off
2018-10-24 18:07:41 +02:00
#ifdef LED_ACTIVE_LOW
2022-10-31 16:56:48 +01:00
digitalWrite(HAS_LED, HIGH);
2018-10-24 18:07:41 +02:00
#else
2022-10-31 16:56:48 +01:00
digitalWrite(HAS_LED, LOW);
2018-10-24 18:07:41 +02:00
#endif
2022-10-31 16:56:48 +01:00
#ifdef HAS_RGB_LED
led_setcolor(CRGB::Black);
2018-10-24 18:07:41 +02:00
#endif
2022-10-31 16:56:48 +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 16:56:48 +01:00
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) {
// switch LED on
#ifdef LED_ACTIVE_LOW
digitalWrite(HAS_TWO_LED, LOW);
2019-03-24 01:05:13 +01:00
#else
2022-10-31 16:56:48 +01:00
digitalWrite(HAS_TWO_LED, HIGH);
2019-03-24 01:05:13 +01:00
#endif
2022-10-31 16:56:48 +01:00
} else if (state == LED_OFF) {
// switch LED off
#ifdef LED_ACTIVE_LOW
digitalWrite(HAS_TWO_LED, HIGH);
2019-03-24 01:05:13 +01:00
#else
2022-10-31 16:56:48 +01:00
digitalWrite(HAS_TWO_LED, LOW);
#endif
}
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
}
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 16:56:48 +01:00
#ifdef HAS_RGB_LED
led_setcolor(CRGB::Black);
#endif
} 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 16:56:48 +01:00
#ifdef HAS_RGB_LED
led_setcolor(CRGB::Yellow);
#endif
// 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 16:56:48 +01:00
#ifdef HAS_RGB_LED
led_setcolor(CRGB::Pink);
#endif
2018-10-16 08:48:09 +02:00
break;
case CONFIGPORT:
2022-10-31 16:56:48 +01:00
#ifdef HAS_RGB_LED
led_setcolor(CRGB::Cyan);
#endif
2018-10-16 08:48:09 +02:00
break;
default:
2022-10-31 16:56:48 +01:00
#ifdef HAS_RGB_LED
led_setcolor(CRGB::Blue);
#endif
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 16:56:48 +01:00
#ifdef HAS_RGB_LED
led_setcolor(CRGB::Red);
#endif
// heartbeat long blink 200ms on each 2 seconds
LEDState = ((millis() % 2000) < 200) ? LED_ON : LED_OFF;
} else
2022-10-31 16:56:48 +01:00
#elif (defined(HAS_RGB_LED) && ((WIFICOUNTER) || (BLECOUNTER)))
struct count_payload_t count;
libpax_counter_count(&count);
led_sethue(count.pax);
2018-07-15 14:28:05 +02:00
#endif // HAS_LORA
{
// led off
LEDState = LED_OFF;
}
2018-07-15 14:28:05 +02:00
}
2022-10-31 16:56:48 +01:00
switch_LED(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)