Skip to content

Commit

Permalink
bug fix on pin already defined check
Browse files Browse the repository at this point in the history
  • Loading branch information
PaoloTK committed Sep 12, 2024
1 parent c5435ec commit daf0bcf
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
9 changes: 5 additions & 4 deletions wled00/FX_fcn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions wled00/pin_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion wled00/pin_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit daf0bcf

Please sign in to comment.