Debug conditional compile in pin manager.

This commit is contained in:
Blaz Kristan 2021-09-04 16:45:08 +02:00
parent 4e8c94fd2d
commit f84e2c2ac7
2 changed files with 17 additions and 3 deletions

View File

@ -1,6 +1,7 @@
#include "pin_manager.h" #include "pin_manager.h"
#include "wled.h" #include "wled.h"
#ifdef WLED_DEBUG
static void DebugPrintOwnerTag(PinOwner tag) static void DebugPrintOwnerTag(PinOwner tag)
{ {
uint32_t q = static_cast<uint8_t>(tag); uint32_t q = static_cast<uint8_t>(tag);
@ -10,6 +11,7 @@ static void DebugPrintOwnerTag(PinOwner tag)
DEBUG_PRINT(F("(no owner)")); DEBUG_PRINT(F("(no owner)"));
} }
} }
#endif
/// Actual allocation/deallocation routines /// Actual allocation/deallocation routines
bool PinManagerClass::deallocatePin(byte gpio, PinOwner tag) 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 a non-zero ownerTag, only allow de-allocation if the owner's tag is provided
if ((ownerTag[gpio] != PinOwner::None) && (ownerTag[gpio] != tag)) { if ((ownerTag[gpio] != PinOwner::None) && (ownerTag[gpio] != tag)) {
#ifdef WLED_DEBUG
DEBUG_PRINT(F("PIN DEALLOC: IO ")); DEBUG_PRINT(F("PIN DEALLOC: IO "));
DEBUG_PRINT(gpio); DEBUG_PRINT(gpio);
DEBUG_PRINT(F(" allocated by ")); DEBUG_PRINT(F(" allocated by "));
DebugPrintOwnerTag(ownerTag[gpio]); DebugPrintOwnerTag(ownerTag[gpio]);
DEBUG_PRINT(F(", but attempted de-allocation by ")); DEBUG_PRINT(F(", but attempted de-allocation by "));
DebugPrintOwnerTag(tag); DebugPrintOwnerTag(tag);
#endif
return false; return false;
} }
@ -34,6 +38,7 @@ bool PinManagerClass::deallocatePin(byte gpio, PinOwner tag)
ownerTag[gpio] = PinOwner::None; ownerTag[gpio] = PinOwner::None;
return true; return true;
} }
bool PinManagerClass::allocateMultiplePins(const managed_pin_type * mptArray, byte arrayElementCount, PinOwner tag) bool PinManagerClass::allocateMultiplePins(const managed_pin_type * mptArray, byte arrayElementCount, PinOwner tag)
{ {
bool shouldFail = false; bool shouldFail = false;
@ -46,17 +51,21 @@ bool PinManagerClass::allocateMultiplePins(const managed_pin_type * mptArray, by
continue; continue;
} }
if (!isPinOk(gpio, mptArray[i].isOutput)) { if (!isPinOk(gpio, mptArray[i].isOutput)) {
#ifdef WLED_DEBUG
DEBUG_PRINT(F("PIN ALLOC: Invalid pin attempted to be allocated: ")); DEBUG_PRINT(F("PIN ALLOC: Invalid pin attempted to be allocated: "));
DEBUG_PRINT(gpio); DEBUG_PRINT(gpio);
DEBUG_PRINTLN(F("")); DEBUG_PRINTLN(F(""));
#endif
shouldFail = true; shouldFail = true;
} }
if (isPinAllocated(gpio)) { if (isPinAllocated(gpio)) {
#ifdef WLED_DEBUG
DEBUG_PRINT(F("PIN ALLOC: FAIL: IO ")); DEBUG_PRINT(F("PIN ALLOC: FAIL: IO "));
DEBUG_PRINT(gpio); DEBUG_PRINT(gpio);
DEBUG_PRINT(F(" already allocated by ")); DEBUG_PRINT(F(" already allocated by "));
DebugPrintOwnerTag(ownerTag[gpio]); DebugPrintOwnerTag(ownerTag[gpio]);
DEBUG_PRINTLN(F("")); DEBUG_PRINTLN(F(""));
#endif
shouldFail = true; shouldFail = true;
} }
} }
@ -79,15 +88,18 @@ bool PinManagerClass::allocateMultiplePins(const managed_pin_type * mptArray, by
} }
return true; return true;
} }
bool PinManagerClass::allocatePin(byte gpio, bool output, PinOwner tag) bool PinManagerClass::allocatePin(byte gpio, bool output, PinOwner tag)
{ {
if (!isPinOk(gpio, output)) return false; if (!isPinOk(gpio, output)) return false;
if (isPinAllocated(gpio)) { if (isPinAllocated(gpio)) {
#ifdef WLED_DEBUG
DEBUG_PRINT(F("PIN ALLOC: Pin ")); DEBUG_PRINT(F("PIN ALLOC: Pin "));
DEBUG_PRINT(gpio); DEBUG_PRINT(gpio);
DEBUG_PRINT(F(" already allocated by ")); DEBUG_PRINT(F(" already allocated by "));
DebugPrintOwnerTag(ownerTag[gpio]); DebugPrintOwnerTag(ownerTag[gpio]);
DEBUG_PRINTLN(F("")); DEBUG_PRINTLN(F(""));
#endif
return false; return false;
} }
@ -106,7 +118,7 @@ bool PinManagerClass::isPinAllocated(byte gpio, PinOwner tag)
if (!isPinOk(gpio, false)) return true; if (!isPinOk(gpio, false)) return true;
if ((tag != PinOwner::None) && (ownerTag[gpio] != tag)) return false; if ((tag != PinOwner::None) && (ownerTag[gpio] != tag)) return false;
byte by = gpio >> 3; byte by = gpio >> 3;
byte bi = gpio - 8*by; byte bi = gpio - (by<<3);
return bitRead(pinAlloc[by], bi); return bitRead(pinAlloc[by], bi);
} }

View File

@ -7,8 +7,8 @@
#include "const.h" // for USERMOD_* values #include "const.h" // for USERMOD_* values
typedef struct PinManagerPinType { typedef struct PinManagerPinType {
int8_t pin; int8_t pin;
uint8_t isOutput; bool isOutput;
} managed_pin_type; } managed_pin_type;
/* /*
@ -87,7 +87,9 @@ class PinManagerClass {
#endif #endif
inline void deallocatePin(byte gpio) { deallocatePin(gpio, PinOwner::None); } inline void deallocatePin(byte gpio) { deallocatePin(gpio, PinOwner::None); }
// will return true for reserved pins
bool isPinAllocated(byte gpio, PinOwner tag = PinOwner::None); bool isPinAllocated(byte gpio, PinOwner tag = PinOwner::None);
// will return false for reserved pins
bool isPinOk(byte gpio, bool output = true); bool isPinOk(byte gpio, bool output = true);
#ifdef ARDUINO_ARCH_ESP32 #ifdef ARDUINO_ARCH_ESP32