From daf0bcfac364846928a2277c1f16475c8d32ac42 Mon Sep 17 00:00:00 2001 From: PaoloTK Date: Thu, 12 Sep 2024 18:12:38 +0200 Subject: [PATCH] bug fix on pin already defined check --- wled00/FX_fcn.cpp | 9 +++++---- wled00/pin_manager.cpp | 8 +++++--- wled00/pin_manager.h | 2 +- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 435998cdcd..6b01d9a323 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -1248,15 +1248,16 @@ void WS2812FX::finalizeInit(void) { // 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]; + // Assign all pins first so we can check for conflicts on this bus + for (unsigned j = 0; j < busPins && j < OUTPUT_MAX_PINS; j++) defPin[j] = defDataPins[pinsIndex + j]; + for (unsigned j = 0; j < busPins && j < OUTPUT_MAX_PINS; j++) { 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. // 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)) { + while (pinManager.isPinAllocated(defPin[j]) || pinManager.isReadOnlyPin(defPin[j]) || pinManager.isPinDefined(defPin[j], defPin, j)) { + DEBUG_PRINTLN(F("Some of the provided pins cannot be used to configure this LED output.")); if (validPin) { defPin[j] = 1; // start with GPIO1 and work upwards validPin = false; diff --git a/wled00/pin_manager.cpp b/wled00/pin_manager.cpp index 9398ec94fd..815bddee36 100644 --- a/wled00/pin_manager.cpp +++ b/wled00/pin_manager.cpp @@ -281,9 +281,11 @@ bool PinManagerClass::isReadOnlyPin(byte gpio) return false; } -bool PinManagerClass::isPinDefined(byte gpio, const unsigned *pins, unsigned start, unsigned end) { - for (unsigned i = start; i < end; i++) { - if (pins[i] == gpio) return true; +// Given an array of pins, check if a given pin is defined except at given index +bool PinManagerClass::isPinDefined(const byte gpio, const uint8_t *pins, const unsigned index) { + unsigned numPins = ((sizeof pins) / (sizeof pins[0])); + for (unsigned i = 0; i < numPins; i++) { + if ((pins[i] == gpio) && (i != index)) return true; } return false; } diff --git a/wled00/pin_manager.h b/wled00/pin_manager.h index 1263dc4f4c..6c57261308 100644 --- a/wled00/pin_manager.h +++ b/wled00/pin_manager.h @@ -113,7 +113,7 @@ class PinManagerClass { bool isPinOk(byte gpio, bool output = true) const; static bool isReadOnlyPin(byte gpio); - static bool isPinDefined(byte gpio, const unsigned* pins, unsigned start = 0, unsigned end = WLED_NUM_PINS); + static bool isPinDefined(const byte gpio, const uint8_t* pins, const unsigned index = 255); PinOwner getPinOwner(byte gpio) const;