From 93009419d97e95d88e715e4744b9b0e3ac5cfe4f Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sat, 29 Oct 2022 13:01:17 +0200 Subject: [PATCH 1/7] libpax update --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 380120ee1..00a8bf2e7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -276,7 +276,7 @@ void setup() { libpax_default_config(&configuration); // configure WIFI sniffing - strcpy(configuration.wifi_my_country, WIFI_MY_COUNTRY); + strcpy(configuration.wifi_my_country_str, WIFI_MY_COUNTRY); configuration.wificounter = cfg.wifiscan; configuration.wifi_channel_map = WIFI_CHANNEL_ALL; configuration.wifi_channel_switch_interval = cfg.wifichancycle; From 16d0c03f5ceeb2df21b949c871b53eff20ec406d Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sat, 29 Oct 2022 13:01:28 +0200 Subject: [PATCH 2/7] reduce CI to master --- .github/workflows/build.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 53185297d..87da98e31 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -5,7 +5,6 @@ on: push: branches: - master - - development jobs: build: From fb06fefd6f0cb7e5c06eaf7ee9a95f5f8d33933d Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Mon, 31 Oct 2022 16:19:29 +0100 Subject: [PATCH 3/7] globals.h sanitization --- include/globals.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/globals.h b/include/globals.h index 8d57f1dd8..35adfb7e8 100644 --- a/include/globals.h +++ b/include/globals.h @@ -5,7 +5,9 @@ #include // Time functions +#ifdef HAS_RTC #include +#endif #include // std::set for unified array functions From 40e64ddb03524661d078c1a4382923ff2bc024a5 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Mon, 31 Oct 2022 16:19:56 +0100 Subject: [PATCH 4/7] sdcard add generic slotconfig --- include/sdcard.h | 4 ++++ src/sdcard.cpp | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/include/sdcard.h b/include/sdcard.h index 1f2b0bca7..4b1145eb3 100644 --- a/include/sdcard.h +++ b/include/sdcard.h @@ -33,6 +33,10 @@ #elif HAS_SDCARD == 2 // MMC interface #include "driver/sdmmc_host.h" +#ifndef SDCARD_SLOTCONFIG +#define SDCARD_SLOTCONFIG SDMMC_SLOT_CONFIG_DEFAULT() +#endif + #ifndef SDCARD_SLOTWIDTH #define SDCARD_SLOTWIDTH 1 #endif diff --git a/src/sdcard.cpp b/src/sdcard.cpp index d4c23e462..544142337 100644 --- a/src/sdcard.cpp +++ b/src/sdcard.cpp @@ -132,7 +132,7 @@ bool sdcard_init(bool create) { // has these signals. // Default config for SDMMC_HOST_DEFAULT (4-bit bus width, slot 1) // https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/sdmmc_host.html - sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT(); + sdmmc_slot_config_t slot_config = SDCARD_SLOTCONFIG; // Set 1-line or 4-line SD mode (default is 1-line) slot_config.width = SDCARD_SLOTWIDTH; From b2c054f187653706951247e81cb41cb42f5983fc Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Mon, 31 Oct 2022 16:56:48 +0100 Subject: [PATCH 5/7] RGB led library migration --- include/led.h | 39 ++----- platformio_orig.ini | 2 +- src/hal/eboxtube.h | 2 +- src/hal/fipy.h | 3 +- src/hal/generic.h | 3 +- src/hal/lolin32litelora.h | 3 +- src/hal/lolin32lora.h | 3 +- src/hal/lopy.h | 3 +- src/hal/lopy4.h | 3 +- src/hal/m5fire.h | 3 +- src/hal/octopus32.h | 3 +- src/led.cpp | 217 +++++++++++++++----------------------- 12 files changed, 112 insertions(+), 172 deletions(-) diff --git a/include/led.h b/include/led.h index 6bbef12dc..dd54f6d7f 100644 --- a/include/led.h +++ b/include/led.h @@ -1,9 +1,11 @@ #ifndef _LED_H #define _LED_H -#ifdef RGB_LED_COUNT -#include +#ifdef HAS_RGB_LED +#include +#include "libpax_helpers.h" #endif + #ifdef HAS_LORA #include "lorawan.h" #endif @@ -12,39 +14,14 @@ #define RGB_LED_COUNT 1 #endif -// value for HSL color -// see http://www.workwithcolor.com/blue-color-hue-range-01.htm -#define COLOR_RED 0 -#define COLOR_ORANGE 30 -#define COLOR_ORANGE_YELLOW 45 -#define COLOR_YELLOW 60 -#define COLOR_YELLOW_GREEN 90 -#define COLOR_GREEN 120 -#define COLOR_GREEN_CYAN 165 -#define COLOR_CYAN 180 -#define COLOR_CYAN_BLUE 210 -#define COLOR_BLUE 240 -#define COLOR_BLUE_MAGENTA 275 -#define COLOR_MAGENTA 300 -#define COLOR_PINK 350 -#define COLOR_WHITE 360 -#define COLOR_NONE 999 - -struct RGBColor { - uint8_t R; - uint8_t G; - uint8_t B; -}; - enum led_states { LED_OFF, LED_ON }; extern TaskHandle_t ledLoopTask; -// Exported Functions -void rgb_set_color(uint16_t hue); -void blink_LED(uint16_t set_color, uint16_t set_blinkduration); +void rgb_led_init(void); +void led_sethue(uint8_t hue); void ledLoop(void *parameter); -void switch_LED(uint8_t state); -void switch_LED1(uint8_t state); +void switch_LED(led_states state); +void switch_LED1(led_states state); #endif \ No newline at end of file diff --git a/platformio_orig.ini b/platformio_orig.ini index 3eb41b623..6a02af35d 100644 --- a/platformio_orig.ini +++ b/platformio_orig.ini @@ -67,7 +67,7 @@ lib_deps_display = lib_deps_ledmatrix = seeed-studio/Ultrathin_LED_Matrix @ ^1.0.0 lib_deps_rgbled = - https://github.com/RoboticsBrno/SmartLeds.git + fastled/FastLED @ ^3.5.0 lib_deps_gps = mikalhart/TinyGPSPlus @ ^1.0.3 lib_deps_sensors = diff --git a/src/hal/eboxtube.h b/src/hal/eboxtube.h index e95b69402..7d2f5b86f 100644 --- a/src/hal/eboxtube.h +++ b/src/hal/eboxtube.h @@ -15,7 +15,7 @@ #define HAS_LED (22) // Green LED on board #define RGB_LED_COUNT 1 // we have 1 LED -#define HAS_RGB_LED SmartLed rgb_led(LED_WS2812, RGB_LED_COUNT, GPIO_NUM_2) // WS2812B RGB LED on board +#define HAS_RGB_LED FastLED.addLeds(leds, RGB_LED_COUNT); #define HAS_BUTTON (0) // button "FLASH" on board #define DISABLE_BROWNOUT 1 // comment out if you want to keep brownout feature diff --git a/src/hal/fipy.h b/src/hal/fipy.h index f904d716f..dd8a5da59 100644 --- a/src/hal/fipy.h +++ b/src/hal/fipy.h @@ -13,7 +13,8 @@ #define CFG_sx1272_radio 1 #define HAS_LED NOT_A_PIN // FiPy has no on board LED, so we use RGB LED -#define HAS_RGB_LED SmartLed rgb_led(LED_WS2812, 1, GPIO_NUM_0) // WS2812B RGB LED on GPIO0 +#define RGB_LED_COUNT 1 // we have 1 LED +#define HAS_RGB_LED FastLED.addLeds(leds, RGB_LED_COUNT); #define BOARD_HAS_PSRAM // use extra 4MB extern RAM // Pins for LORA chip SPI interface, reset line and interrupt lines diff --git a/src/hal/generic.h b/src/hal/generic.h index 26d465e3b..ef6913308 100644 --- a/src/hal/generic.h +++ b/src/hal/generic.h @@ -72,7 +72,8 @@ #define HAS_LED (21) // on board LED #define HAS_BUTTON (39) // on board button -#define HAS_RGB_LED SmartLed rgb_led(LED_WS2812, 1, GPIO_NUM_0) // WS2812B RGB LED on GPIO0 +#define RGB_LED_COUNT 1 // we have 1 LED +#define HAS_RGB_LED FastLED.addLeds(leds, RGB_LED_COUNT); // GPS settings #define HAS_GPS 1 // use on board GPS diff --git a/src/hal/lolin32litelora.h b/src/hal/lolin32litelora.h index d3ae2133f..232f9adb2 100644 --- a/src/hal/lolin32litelora.h +++ b/src/hal/lolin32litelora.h @@ -17,7 +17,8 @@ //#define MY_DISPLAY_FLIP 1 // uncomment this for rotated display #define HAS_LED 22 // ESP32 GPIO12 (pin22) On Board LED (set to NOT_A_PIN to disable) #define LED_ACTIVE_LOW 1 // Onboard LED is active when pin is LOW -#define HAS_RGB_LED SmartLed rgb_led(LED_WS2812, 1, GPIO_NUM_13) // ESP32 GPIO13 (pin13) On Board Shield WS2812B RGB LED +#define RGB_LED_COUNT 1 // we have 1 LED +#define HAS_RGB_LED FastLED.addLeds(leds, RGB_LED_COUNT); #define HAS_BUTTON 15 // ESP32 GPIO15 (pin15) Button is on the LoraNode32 shield #define BUTTON_PULLUP 1 // Button need pullup instead of default pulldown diff --git a/src/hal/lolin32lora.h b/src/hal/lolin32lora.h index 15cdb3318..c53aaf47b 100644 --- a/src/hal/lolin32lora.h +++ b/src/hal/lolin32lora.h @@ -18,7 +18,8 @@ #define HAS_LED NOT_A_PIN // Led os on same pin as Lora SS pin, to avoid problems, we don't use it #define LED_ACTIVE_LOW 1 // Onboard LED is active when pin is LOW // Anyway shield is on over the LoLin32 board, so we won't be able to see this LED -#define HAS_RGB_LED SmartLed rgb_led(LED_WS2812, 1, GPIO_NUM_13) // ESP32 GPIO13 (pin13) On Board Shield WS2812B RGB LED +#define RGB_LED_COUNT 1 // we have 1 LED +#define HAS_RGB_LED FastLED.addLeds(leds, RGB_LED_COUNT); #define HAS_BUTTON 15 // ESP32 GPIO15 (pin15) Button is on the LoraNode32 shield #define BUTTON_PULLUP 1 // Button need pullup instead of default pulldown diff --git a/src/hal/lopy.h b/src/hal/lopy.h index e196ee1a4..0946c0463 100644 --- a/src/hal/lopy.h +++ b/src/hal/lopy.h @@ -12,7 +12,8 @@ #define HAS_LORA 1 // comment out if device shall not send data via LoRa #define CFG_sx1272_radio 1 #define HAS_LED NOT_A_PIN // LoPy4 has no on board mono LED, we use on board RGB LED -#define HAS_RGB_LED SmartLed rgb_led(LED_WS2812, 1, GPIO_NUM_0) // WS2812B RGB LED on GPIO0 (P2) +#define RGB_LED_COUNT 1 // we have 1 LED +#define HAS_RGB_LED FastLED.addLeds(leds, RGB_LED_COUNT); // Note: Pins for LORA chip SPI interface come from board file pins_arduino.h diff --git a/src/hal/lopy4.h b/src/hal/lopy4.h index 0c5999f03..cb960fa70 100644 --- a/src/hal/lopy4.h +++ b/src/hal/lopy4.h @@ -21,7 +21,8 @@ #define CFG_sx1276_radio 1 #define HAS_LED NOT_A_PIN // LoPy4 has no on board mono LED, we use on board RGB LED #define RGB_LED_COUNT 1 // we have 1 LEDs -#define HAS_RGB_LED SmartLed rgb_led(LED_WS2812, RGB_LED_COUNT, GPIO_NUM_0) // WS2812B RGB LED on GPIO0 (P2) +#define RGB_LED_COUNT 1 // we have 1 LED +#define HAS_RGB_LED FastLED.addLeds(leds, RGB_LED_COUNT); #define BOARD_HAS_PSRAM // use extra 4MB extern RAM // Note: Pins for LORA chip SPI interface come from board file pins_arduino.h diff --git a/src/hal/m5fire.h b/src/hal/m5fire.h index 156cd4944..16d3b2cbc 100644 --- a/src/hal/m5fire.h +++ b/src/hal/m5fire.h @@ -33,7 +33,8 @@ #define HAS_LED NOT_A_PIN // no on board LED (?) #define RGB_LED_COUNT 10 -#define HAS_RGB_LED SmartLed rgb_led(LED_SK6812, RGB_LED_COUNT, GPIO_NUM_15) // LED_SK6812 RGB LED on GPIO15 +#define RGB_LED_COUNT 1 // we have 1 LED +#define HAS_RGB_LED FastLED.addLeds(leds, RGB_LED_COUNT); #define HAS_BUTTON (39) // on board button A // power management settings diff --git a/src/hal/octopus32.h b/src/hal/octopus32.h index b7de8b12f..6b5b66c47 100644 --- a/src/hal/octopus32.h +++ b/src/hal/octopus32.h @@ -22,7 +22,8 @@ #define HAS_LED 13 // ESP32 GPIO12 (pin22) On Board LED //#define LED_ACTIVE_LOW 1 // Onboard LED is active when pin is LOW -//#define HAS_RGB_LED SmartLed rgb_led(LED_WS2812, 1, GPIO_NUM_13) // ESP32 GPIO13 (pin13) On Board Shield WS2812B RGB LED +//#define RGB_LED_COUNT 1 // we have 1 LED +//#define HAS_RGB_LED FastLED.addLeds(leds, RGB_LED_COUNT); //#define HAS_BUTTON 15 // ESP32 GPIO15 (pin15) Button is on the LoraNode32 shield //#define BUTTON_PULLUP 1 // Button need pullup instead of default pulldown diff --git a/src/led.cpp b/src/led.cpp index ecd5b99a7..b705011f7 100644 --- a/src/led.cpp +++ b/src/led.cpp @@ -2,142 +2,95 @@ #include "globals.h" #include "led.h" -led_states LEDState = LED_OFF; // LED state global for state machine -led_states previousLEDState = - LED_ON; // This will force LED to be off at boot since State is OFF - +static led_states LEDState = LED_OFF; // LED state global for state machine TaskHandle_t ledLoopTask; - -uint16_t LEDColor = COLOR_NONE, LEDBlinkDuration = 0; // state machine variables -unsigned long LEDBlinkStarted = 0; // When (in millis() led blink started) +static uint16_t LEDBlinkDuration = 0; // state machine variables +static unsigned long LEDBlinkStarted = + 0; // When (in millis() led blink started) #ifdef HAS_RGB_LED +CRGB leds[RGB_LED_COUNT]; -// RGB Led instance -HAS_RGB_LED; - -float rgb_CalcColor(float p, float q, float t) { - if (t < 0.0f) - t += 1.0f; - if (t > 1.0f) - t -= 1.0f; - - if (t < 1.0f / 6.0f) - return p + (q - p) * 6.0f * t; - - if (t < 0.5f) - return q; - - if (t < 2.0f / 3.0f) - return p + ((q - p) * (2.0f / 3.0f - t) * 6.0f); - - return p; -} - -// ------------------------------------------------------------------------ -// Hue, Saturation, Lightness color members -// HslColor using H, S, L values (0.0 - 1.0) -// L should be limited to between (0.0 - 0.5) -// ------------------------------------------------------------------------ -RGBColor rgb_hsl2rgb(float h, float s, float l) { - RGBColor RGB_color; - float r; - float g; - float b; - - if (s == 0.0f || l == 0.0f) { - r = g = b = l; // achromatic or black - } else { - float q = l < 0.5f ? l * (1.0f + s) : l + s - (l * s); - float p = 2.0f * l - q; - r = rgb_CalcColor(p, q, h + 1.0f / 3.0f); - g = rgb_CalcColor(p, q, h); - b = rgb_CalcColor(p, q, h - 1.0f / 3.0f); - } - - RGB_color.R = (uint8_t)(r * 255.0f); - RGB_color.G = (uint8_t)(g * 255.0f); - RGB_color.B = (uint8_t)(b * 255.0f); - - return RGB_color; +void led_setcolor(CRGB color) { + for (int i = 0; i < RGB_LED_COUNT; i++) + leds[i] = color; + FastLED.show(); } +#endif -void rgb_set_color(uint16_t hue) { - if (hue == COLOR_NONE) { - // set Off - for (int i = 0; i < RGB_LED_COUNT; i++) - rgb_led[i] = Rgb(0, 0, 0); - } else { - // see http://www.workwithcolor.com/blue-color-hue-range-01.htm - // H (is color from 0..360) should be between 0.0 and 1.0 - // S is saturation keep it to 1 - // L is brightness should be between 0.0 and 0.5 - // cfg.rgblum is between 0 and 100 (percent) - RGBColor target = rgb_hsl2rgb(hue / 360.0f, 1.0f, 0.005f * cfg.rgblum); - // uint32_t color = target.R<<16 | target.G<<8 | target.B; - for (int i = 0; i < RGB_LED_COUNT; i++) - rgb_led[i] = Rgb(target.R, target.G, target.B); - } - // Show - rgb_led.show(); +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 } -#else - -// No RGB LED empty functions -void rgb_set_color(uint16_t hue) {} - +void rgb_led_init(void) { +#ifdef HAS_RGB_LED + HAS_RGB_LED; + led_setcolor(CRGB::Green); #endif +} -void switch_LED(uint8_t state) { +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) - if (state == LED_ON) { - // switch LED on + if (state == LED_ON) { + // switch LED on #ifdef LED_ACTIVE_LOW - digitalWrite(HAS_LED, LOW); + digitalWrite(HAS_LED, LOW); #else - digitalWrite(HAS_LED, HIGH); + digitalWrite(HAS_LED, HIGH); #endif - } else if (state == LED_OFF) { - // switch LED off +#ifdef HAS_RGB_LED + led_setcolor(CRGB::White); +#endif + } else if (state == LED_OFF) { + // switch LED off #ifdef LED_ACTIVE_LOW - digitalWrite(HAS_LED, HIGH); + digitalWrite(HAS_LED, HIGH); #else - digitalWrite(HAS_LED, LOW); + digitalWrite(HAS_LED, LOW); #endif - } +#ifdef HAS_RGB_LED + led_setcolor(CRGB::Black); #endif + } +#endif + } } -void switch_LED1(uint8_t state) { +void switch_LED1(led_states state) { #ifdef HAS_TWO_LED - if (state == LED_ON) { - // switch LED on -#ifdef LED1_ACTIVE_LOW - digitalWrite(HAS_TWO_LED, LOW); + 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); #else - digitalWrite(HAS_TWO_LED, HIGH); + digitalWrite(HAS_TWO_LED, HIGH); #endif - } else if (state == LED_OFF) { - // switch LED off -#ifdef LED1_ACTIVE_LOW - digitalWrite(HAS_TWO_LED, HIGH); + } else if (state == LED_OFF) { + // switch LED off +#ifdef LED_ACTIVE_LOW + digitalWrite(HAS_TWO_LED, HIGH); #else - digitalWrite(HAS_TWO_LED, LOW); + digitalWrite(HAS_TWO_LED, LOW); +#endif + } #endif } #endif // HAS_TWO_LED } -void blink_LED(uint16_t set_color, uint16_t set_blinkduration) { -#if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED) - LEDColor = set_color; // set color for RGB LED - LEDBlinkDuration = set_blinkduration; // duration - LEDBlinkStarted = millis(); // Time Start here - LEDState = LED_ON; // Let main set LED on -#endif -} - #if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED) void ledLoop(void *parameter) { @@ -151,7 +104,9 @@ void ledLoop(void *parameter) { LEDState = LED_OFF; LEDBlinkStarted = 0; LEDBlinkDuration = 0; - LEDColor = COLOR_NONE; +#ifdef HAS_RGB_LED + led_setcolor(CRGB::Black); +#endif } else { // In case of LoRaWAN led management blinked off LEDState = LED_ON; @@ -161,7 +116,9 @@ void ledLoop(void *parameter) { #if (HAS_LORA) // LED indicators for viusalizing LoRaWAN state if (LMIC.opmode & (OP_JOINING | OP_REJOIN)) { - LEDColor = COLOR_YELLOW; +#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 @@ -169,13 +126,19 @@ void ledLoop(void *parameter) { // select color to blink by message port switch (LMIC.pendTxPort) { case STATUSPORT: - LEDColor = COLOR_PINK; +#ifdef HAS_RGB_LED + led_setcolor(CRGB::Pink); +#endif break; case CONFIGPORT: - LEDColor = COLOR_CYAN; +#ifdef HAS_RGB_LED + led_setcolor(CRGB::Cyan); +#endif break; default: - LEDColor = COLOR_BLUE; +#ifdef HAS_RGB_LED + led_setcolor(CRGB::Blue); +#endif break; } // small blink 10ms on each 1/2sec (not when joining) @@ -183,36 +146,28 @@ void ledLoop(void *parameter) { // This should not happen so indicate a problem } else if (LMIC.opmode & ((OP_TXDATA | OP_TXRXPEND | OP_JOINING | OP_REJOIN) == 0)) { - LEDColor = COLOR_RED; +#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 +#elif (defined(HAS_RGB_LED) && ((WIFICOUNTER) || (BLECOUNTER))) + struct count_payload_t count; + libpax_counter_count(&count); + led_sethue(count.pax); #endif // HAS_LORA { // led off - LEDColor = COLOR_NONE; LEDState = LED_OFF; } } - // 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; - } + + switch_LED(LEDState); + // give yield to CPU delay(5); } // while(1) }; // ledloop() -#endif // #if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED) +#endif // #if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED) \ No newline at end of file From c55b7e87098e56758801645be4c06d12f37ac654 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Mon, 31 Oct 2022 18:41:15 +0100 Subject: [PATCH 6/7] fix rgb led init --- src/main.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 00a8bf2e7..a1af483cc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -201,26 +201,26 @@ void setup() { #endif // initialize leds +#ifdef HAS_RGB_LED + rgb_led_init(); + strcat_P(features, " RGB"); +#endif + #if (HAS_LED != NOT_A_PIN) pinMode(HAS_LED, OUTPUT); strcat_P(features, " LED"); - #ifdef LED_POWER_SW pinMode(LED_POWER_SW, OUTPUT); digitalWrite(LED_POWER_SW, LED_POWER_ON); #endif - #ifdef HAS_TWO_LED pinMode(HAS_TWO_LED, OUTPUT); strcat_P(features, " LED2"); #endif - -// use LED for power display if we have additional RGB LED, else for status +// use simple LED for power display if we have additional RGB LED, else for status #ifdef HAS_RGB_LED switch_LED(LED_ON); - strcat_P(features, " RGB"); #endif - #endif // HAS_LED #if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED) From 681118c2ececc32676e0e649e4d70b6dc988b9bd Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Mon, 31 Oct 2022 18:41:29 +0100 Subject: [PATCH 7/7] fix led color control --- include/led.h | 15 +++++- src/led.cpp | 123 +++++++++++++++++++++----------------------------- 2 files changed, 65 insertions(+), 73 deletions(-) diff --git a/include/led.h b/include/led.h index dd54f6d7f..4bf4c0d88 100644 --- a/include/led.h +++ b/include/led.h @@ -2,6 +2,7 @@ #define _LED_H #ifdef HAS_RGB_LED +#define FASTLED_INTERNAL #include #include "libpax_helpers.h" #endif @@ -16,10 +17,22 @@ enum led_states { LED_OFF, LED_ON }; +enum colors { + COLOR_WHITE = 0xFFFFFF, + COLOR_NONE = 0x000000, + COLOR_CYAN = 0x00FFFF, + COLOR_BLUE = 0x0000FF, + COLOR_GREEN = 0x008000, + COLOR_YELLOW = 0xFFFF00, + COLOR_ORANGE = 0xFFA500, + COLOR_RED = 0xFF0000, + COLOR_PINK = 0xFFC0CB +}; + extern TaskHandle_t ledLoopTask; void rgb_led_init(void); -void led_sethue(uint8_t hue); +void rgb_set_color(uint32_t color); void ledLoop(void *parameter); void switch_LED(led_states state); void switch_LED1(led_states state); diff --git a/src/led.cpp b/src/led.cpp index b705011f7..ee6c7e298 100644 --- a/src/led.cpp +++ b/src/led.cpp @@ -3,25 +3,23 @@ #include "led.h" static led_states LEDState = LED_OFF; // LED state global for state machine +led_states previousLEDState = + LED_ON; // This will force LED to be off at boot since State is OFF + TaskHandle_t ledLoopTask; -static uint16_t LEDBlinkDuration = 0; // state machine variables + +uint32_t LEDColor = COLOR_NONE, LEDBlinkDuration = 0; // state machine variables static unsigned long LEDBlinkStarted = 0; // When (in millis() led blink started) #ifdef HAS_RGB_LED CRGB leds[RGB_LED_COUNT]; - -void led_setcolor(CRGB color) { - for (int i = 0; i < RGB_LED_COUNT; i++) - leds[i] = color; - FastLED.show(); -} #endif -void led_sethue(uint8_t hue) { +void rgb_set_color(uint32_t color) { #ifdef HAS_RGB_LED for (int i = 0; i < RGB_LED_COUNT; i++) - leds[i] = CHSV(hue, 0XFF, 100); + leds[i] = CRGB(color); FastLED.show(); #endif } @@ -29,68 +27,51 @@ void led_sethue(uint8_t hue) { void rgb_led_init(void) { #ifdef HAS_RGB_LED HAS_RGB_LED; - led_setcolor(CRGB::Green); + rgb_set_color(COLOR_NONE); #endif } 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) - if (state == LED_ON) { - // switch LED on + if (state == LED_ON) { + // switch LED on #ifdef LED_ACTIVE_LOW - digitalWrite(HAS_LED, LOW); + digitalWrite(HAS_LED, LOW); #else - digitalWrite(HAS_LED, HIGH); -#endif -#ifdef HAS_RGB_LED - led_setcolor(CRGB::White); + digitalWrite(HAS_LED, HIGH); #endif - } else if (state == LED_OFF) { - // switch LED off + } else if (state == LED_OFF) { + // switch LED off #ifdef LED_ACTIVE_LOW - digitalWrite(HAS_LED, HIGH); + digitalWrite(HAS_LED, HIGH); #else - digitalWrite(HAS_LED, LOW); -#endif -#ifdef HAS_RGB_LED - led_setcolor(CRGB::Black); -#endif - } + digitalWrite(HAS_LED, LOW); #endif } +#endif } void switch_LED1(led_states state) { #ifdef HAS_TWO_LED - 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); + if (state == LED_ON) { + // switch LED on +#ifdef LED1_ACTIVE_LOW + digitalWrite(HAS_TWO_LED, LOW); #else - digitalWrite(HAS_TWO_LED, HIGH); + digitalWrite(HAS_TWO_LED, HIGH); #endif - } else if (state == LED_OFF) { - // switch LED off -#ifdef LED_ACTIVE_LOW - digitalWrite(HAS_TWO_LED, HIGH); + } else if (state == LED_OFF) { + // switch LED off +#ifdef LED1_ACTIVE_LOW + digitalWrite(HAS_TWO_LED, HIGH); #else - digitalWrite(HAS_TWO_LED, LOW); -#endif - } + digitalWrite(HAS_TWO_LED, LOW); #endif } #endif // HAS_TWO_LED } + #if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED) void ledLoop(void *parameter) { @@ -104,9 +85,7 @@ void ledLoop(void *parameter) { LEDState = LED_OFF; LEDBlinkStarted = 0; LEDBlinkDuration = 0; -#ifdef HAS_RGB_LED - led_setcolor(CRGB::Black); -#endif + LEDColor = COLOR_NONE; } else { // In case of LoRaWAN led management blinked off LEDState = LED_ON; @@ -116,9 +95,7 @@ void ledLoop(void *parameter) { #if (HAS_LORA) // LED indicators for viusalizing LoRaWAN state if (LMIC.opmode & (OP_JOINING | OP_REJOIN)) { -#ifdef HAS_RGB_LED - led_setcolor(CRGB::Yellow); -#endif + LEDColor = COLOR_YELLOW; // quick blink 20ms on each 1/5 second LEDState = ((millis() % 200) < 20) ? LED_ON : LED_OFF; // TX data pending @@ -126,19 +103,13 @@ void ledLoop(void *parameter) { // select color to blink by message port switch (LMIC.pendTxPort) { case STATUSPORT: -#ifdef HAS_RGB_LED - led_setcolor(CRGB::Pink); -#endif + LEDColor = COLOR_PINK; break; case CONFIGPORT: -#ifdef HAS_RGB_LED - led_setcolor(CRGB::Cyan); -#endif + LEDColor = COLOR_CYAN; break; default: -#ifdef HAS_RGB_LED - led_setcolor(CRGB::Blue); -#endif + LEDColor = COLOR_BLUE; break; } // small blink 10ms on each 1/2sec (not when joining) @@ -146,25 +117,33 @@ void ledLoop(void *parameter) { // This should not happen so indicate a problem } else if (LMIC.opmode & ((OP_TXDATA | OP_TXRXPEND | OP_JOINING | OP_REJOIN) == 0)) { -#ifdef HAS_RGB_LED - led_setcolor(CRGB::Red); -#endif + LEDColor = COLOR_RED; // heartbeat long blink 200ms on each 2 seconds LEDState = ((millis() % 2000) < 200) ? LED_ON : LED_OFF; } else -#elif (defined(HAS_RGB_LED) && ((WIFICOUNTER) || (BLECOUNTER))) - struct count_payload_t count; - libpax_counter_count(&count); - led_sethue(count.pax); #endif // HAS_LORA { // led off + LEDColor = COLOR_NONE; LEDState = LED_OFF; } } - - switch_LED(LEDState); - + // 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 delay(5); } // while(1)