diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 6f218c93f..435998cdc 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -53,7 +53,7 @@ #endif #ifndef DATA_PINS - #define DATA_PINS LEDPIN + #define DATA_PINS DEFAULT_LED_PIN #endif #ifndef LED_TYPES @@ -1245,32 +1245,27 @@ void WS2812FX::finalizeInit(void) { unsigned dataType = defDataTypes[(i < defNumTypes) ? i : defNumTypes -1]; unsigned busPins = Bus::getNumberOfPins(dataType); - // check if we have enough pins left to configure an output of this type - // should never happen due to static assert above - if (pinsIndex + busPins > defNumPins) { - DEBUG_PRINTLN(F("LED outputs misaligned with defined pins. Some pins will remain unused.")); - break; - } + // if we need more pins than available all outputs have been configured + if (pinsIndex + busPins > defNumPins) break; for (unsigned j = 0; j < busPins && j < OUTPUT_MAX_PINS; j++) { defPin[j] = defDataPins[pinsIndex + j]; - // when booting without config (1st boot) we need to make sure GPIOs defined for LED output don't clash with hardware + bool validPin = true; + // When booting without config (1st boot) we need to make sure GPIOs defined for LED output don't clash with hardware // i.e. DEBUG (GPIO1), DMX (2), SPI RAM/FLASH (16&17 on ESP32-WROVER/PICO), read/only pins, etc. - if (pinManager.isPinAllocated(defPin[j]) || pinManager.isReadOnlyPin(defPin[j])) { - defPin[j] = 1; // start with GPIO1 and work upwards - while ( - ( - pinManager.isPinAllocated(defPin[j]) || - pinManager.isReadOnlyPin(defPin[j]) || - // Check if pin is defined for current bus - pinManager.isPinDefined(defPin[j], defDataPins, pinsIndex + j, pinsIndex + busPins) - ) - && - defPin[j] < WLED_NUM_PINS - ) - { - defPin[j]++; + // Pin should not be already allocated, read/only or defined for current bus + while (pinManager.isPinAllocated(defPin[j]) || pinManager.isReadOnlyPin(defPin[j]) || + pinManager.isPinDefined(defPin[j], defDataPins, pinsIndex + j + 1, pinsIndex + busPins)) { + if (validPin) { + defPin[j] = 1; // start with GPIO1 and work upwards + validPin = false; + } + if (defPin[j] < WLED_NUM_PINS) { + defPin[j]++; + } else { + DEBUG_PRINTLN(F("No available pins left! Can't configure output.")); + return; } } } diff --git a/wled00/const.h b/wled00/const.h index ffefb3ca5..1bfca0758 100644 --- a/wled00/const.h +++ b/wled00/const.h @@ -563,15 +563,6 @@ #define WLED_MAX_NODES 150 #endif -//this is merely a default now and can be changed at runtime -#ifndef LEDPIN -#if defined(ESP8266) || defined(CONFIG_IDF_TARGET_ESP32C3) //|| (defined(ARDUINO_ARCH_ESP32) && defined(BOARD_HAS_PSRAM)) || defined(ARDUINO_ESP32_PICO) - #define LEDPIN 2 // GPIO2 (D4) on Wemos D1 mini compatible boards, safe to use on any board -#else - #define LEDPIN 16 // aligns with GPIO2 (D4) on Wemos D1 mini32 compatible boards (if it is unusable it will be reassigned in WS2812FX::finalizeInit()) -#endif -#endif - // List of read only pins. Cannot be used for LED outputs. #if defined(CONFIG_IDF_TARGET_ESP32S2) #define READ_ONLY_PINS 46 @@ -593,13 +584,14 @@ #endif #endif -#ifndef DEFAULT_LED_TYPE - #define DEFAULT_LED_TYPE TYPE_WS2812_RGB -#endif - -#ifndef DEFAULT_LED_COUNT - #define DEFAULT_LED_COUNT 30 +// Defaults pins, type and counts to configure LED output +#if defined(ESP8266) || defined(CONFIG_IDF_TARGET_ESP32C3) + #define DEFAULT_LED_PIN 2 // GPIO2 (D4) on Wemos D1 mini compatible boards, safe to use on any board +#else + #define DEFAULT_LED_PIN 16 // aligns with GPIO2 (D4) on Wemos D1 mini32 compatible boards (if it is unusable it will be reassigned in WS2812FX::finalizeInit()) #endif +#define DEFAULT_LED_TYPE TYPE_WS2812_RGB +#define DEFAULT_LED_COUNT 30 #define INTERFACE_UPDATE_COOLDOWN 1000 // time in ms to wait between websockets, alexa, and MQTT updates