From f84e2c2ac78dbd38b4758de93c424b86367df990 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Sat, 4 Sep 2021 16:45:08 +0200 Subject: [PATCH] Debug conditional compile in pin manager. --- wled00/pin_manager.cpp | 14 +++++++++++++- wled00/pin_manager.h | 6 ++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/wled00/pin_manager.cpp b/wled00/pin_manager.cpp index 38e6e162..b0b84ced 100644 --- a/wled00/pin_manager.cpp +++ b/wled00/pin_manager.cpp @@ -1,6 +1,7 @@ #include "pin_manager.h" #include "wled.h" +#ifdef WLED_DEBUG static void DebugPrintOwnerTag(PinOwner tag) { uint32_t q = static_cast(tag); @@ -10,6 +11,7 @@ static void DebugPrintOwnerTag(PinOwner tag) DEBUG_PRINT(F("(no owner)")); } } +#endif /// Actual allocation/deallocation routines bool PinManagerClass::deallocatePin(byte gpio, PinOwner tag) @@ -19,12 +21,14 @@ bool PinManagerClass::deallocatePin(byte gpio, PinOwner tag) // if a non-zero ownerTag, only allow de-allocation if the owner's tag is provided if ((ownerTag[gpio] != PinOwner::None) && (ownerTag[gpio] != tag)) { + #ifdef WLED_DEBUG DEBUG_PRINT(F("PIN DEALLOC: IO ")); DEBUG_PRINT(gpio); DEBUG_PRINT(F(" allocated by ")); DebugPrintOwnerTag(ownerTag[gpio]); DEBUG_PRINT(F(", but attempted de-allocation by ")); DebugPrintOwnerTag(tag); + #endif return false; } @@ -34,6 +38,7 @@ bool PinManagerClass::deallocatePin(byte gpio, PinOwner tag) ownerTag[gpio] = PinOwner::None; return true; } + bool PinManagerClass::allocateMultiplePins(const managed_pin_type * mptArray, byte arrayElementCount, PinOwner tag) { bool shouldFail = false; @@ -46,17 +51,21 @@ bool PinManagerClass::allocateMultiplePins(const managed_pin_type * mptArray, by continue; } if (!isPinOk(gpio, mptArray[i].isOutput)) { + #ifdef WLED_DEBUG DEBUG_PRINT(F("PIN ALLOC: Invalid pin attempted to be allocated: ")); DEBUG_PRINT(gpio); DEBUG_PRINTLN(F("")); + #endif shouldFail = true; } if (isPinAllocated(gpio)) { + #ifdef WLED_DEBUG DEBUG_PRINT(F("PIN ALLOC: FAIL: IO ")); DEBUG_PRINT(gpio); DEBUG_PRINT(F(" already allocated by ")); DebugPrintOwnerTag(ownerTag[gpio]); DEBUG_PRINTLN(F("")); + #endif shouldFail = true; } } @@ -79,15 +88,18 @@ bool PinManagerClass::allocateMultiplePins(const managed_pin_type * mptArray, by } return true; } + bool PinManagerClass::allocatePin(byte gpio, bool output, PinOwner tag) { if (!isPinOk(gpio, output)) return false; if (isPinAllocated(gpio)) { + #ifdef WLED_DEBUG DEBUG_PRINT(F("PIN ALLOC: Pin ")); DEBUG_PRINT(gpio); DEBUG_PRINT(F(" already allocated by ")); DebugPrintOwnerTag(ownerTag[gpio]); DEBUG_PRINTLN(F("")); + #endif return false; } @@ -106,7 +118,7 @@ bool PinManagerClass::isPinAllocated(byte gpio, PinOwner tag) if (!isPinOk(gpio, false)) return true; if ((tag != PinOwner::None) && (ownerTag[gpio] != tag)) return false; byte by = gpio >> 3; - byte bi = gpio - 8*by; + byte bi = gpio - (by<<3); return bitRead(pinAlloc[by], bi); } diff --git a/wled00/pin_manager.h b/wled00/pin_manager.h index aba1ae9d..43d19203 100644 --- a/wled00/pin_manager.h +++ b/wled00/pin_manager.h @@ -7,8 +7,8 @@ #include "const.h" // for USERMOD_* values typedef struct PinManagerPinType { - int8_t pin; - uint8_t isOutput; + int8_t pin; + bool isOutput; } managed_pin_type; /* @@ -87,7 +87,9 @@ class PinManagerClass { #endif inline void deallocatePin(byte gpio) { deallocatePin(gpio, PinOwner::None); } + // will return true for reserved pins bool isPinAllocated(byte gpio, PinOwner tag = PinOwner::None); + // will return false for reserved pins bool isPinOk(byte gpio, bool output = true); #ifdef ARDUINO_ARCH_ESP32