PinManager: Make in to namespace

Namespaces are the C++ language construct for grouping global functions.
This commit is contained in:
Will Miles 2024-10-20 10:48:31 -04:00
parent 95b4bde918
commit 32eee3365a
2 changed files with 45 additions and 53 deletions

View File

@ -13,6 +13,16 @@
#endif #endif
#endif #endif
// Pin management state variables
#ifdef ESP8266
static uint32_t pinAlloc = 0UL; // 1 bit per pin, we use first 17bits
#else
static uint64_t pinAlloc = 0ULL; // 1 bit per pin, we use 50 bits on ESP32-S3
static uint16_t ledcAlloc = 0; // up to 16 LEDC channels (WLED_MAX_ANALOG_CHANNELS)
#endif
static uint8_t i2cAllocCount = 0; // allow multiple allocation of I2C bus pins but keep track of allocations
static uint8_t spiAllocCount = 0; // allow multiple allocation of SPI bus pins but keep track of allocations
static PinOwner ownerTag[WLED_NUM_PINS] = { PinOwner::None };
/// Actual allocation/deallocation routines /// Actual allocation/deallocation routines
bool PinManager::deallocatePin(byte gpio, PinOwner tag) bool PinManager::deallocatePin(byte gpio, PinOwner tag)
@ -273,13 +283,3 @@ void PinManager::deallocateLedc(byte pos, byte channels)
} }
} }
#endif #endif
#ifdef ESP8266
uint32_t PinManager::pinAlloc = 0UL;
#else
uint64_t PinManager::pinAlloc = 0ULL;
uint16_t PinManager::ledcAlloc = 0;
#endif
uint8_t PinManager::i2cAllocCount = 0;
uint8_t PinManager::spiAllocCount = 0;
PinOwner PinManager::ownerTag[WLED_NUM_PINS] = { PinOwner::None };

View File

@ -9,6 +9,12 @@
#endif #endif
#include "const.h" // for USERMOD_* values #include "const.h" // for USERMOD_* values
#ifdef ESP8266
#define WLED_NUM_PINS (GPIO_PIN_COUNT+1) // somehow they forgot GPIO 16 (0-16==17)
#else
#define WLED_NUM_PINS (GPIO_PIN_COUNT)
#endif
typedef struct PinManagerPinType { typedef struct PinManagerPinType {
int8_t pin; int8_t pin;
bool isOutput; bool isOutput;
@ -70,53 +76,39 @@ enum struct PinOwner : uint8_t {
}; };
static_assert(0u == static_cast<uint8_t>(PinOwner::None), "PinOwner::None must be zero, so default array initialization works as expected"); static_assert(0u == static_cast<uint8_t>(PinOwner::None), "PinOwner::None must be zero, so default array initialization works as expected");
class PinManager { namespace PinManager {
private: // De-allocates a single pin
#ifdef ESP8266 bool deallocatePin(byte gpio, PinOwner tag);
#define WLED_NUM_PINS (GPIO_PIN_COUNT+1) // somehow they forgot GPIO 16 (0-16==17) // De-allocates multiple pins but only if all can be deallocated (PinOwner has to be specified)
static uint32_t pinAlloc; // 1 bit per pin, we use first 17bits bool deallocateMultiplePins(const uint8_t *pinArray, byte arrayElementCount, PinOwner tag);
#else bool deallocateMultiplePins(const managed_pin_type *pinArray, byte arrayElementCount, PinOwner tag);
#define WLED_NUM_PINS (GPIO_PIN_COUNT) // Allocates a single pin, with an owner tag.
static uint64_t pinAlloc; // 1 bit per pin, we use 50 bits on ESP32-S3 // De-allocation requires the same owner tag (or override)
static uint16_t ledcAlloc; // up to 16 LEDC channels (WLED_MAX_ANALOG_CHANNELS) bool allocatePin(byte gpio, bool output, PinOwner tag);
#endif // Allocates all the pins, or allocates none of the pins, with owner tag.
static uint8_t i2cAllocCount; // allow multiple allocation of I2C bus pins but keep track of allocations // Provided to simplify error condition handling in clients
static uint8_t spiAllocCount; // allow multiple allocation of SPI bus pins but keep track of allocations // using more than one pin, such as I2C, SPI, rotary encoders,
static PinOwner ownerTag[WLED_NUM_PINS]; // ethernet, etc..
bool allocateMultiplePins(const managed_pin_type * mptArray, byte arrayElementCount, PinOwner tag );
public: [[deprecated("Replaced by three-parameter allocatePin(gpio, output, ownerTag), for improved debugging")]]
// De-allocates a single pin inline bool allocatePin(byte gpio, bool output = true) { return allocatePin(gpio, output, PinOwner::None); }
static bool deallocatePin(byte gpio, PinOwner tag); [[deprecated("Replaced by two-parameter deallocatePin(gpio, ownerTag), for improved debugging")]]
// De-allocates multiple pins but only if all can be deallocated (PinOwner has to be specified) inline void deallocatePin(byte gpio) { deallocatePin(gpio, PinOwner::None); }
static bool deallocateMultiplePins(const uint8_t *pinArray, byte arrayElementCount, PinOwner tag);
static bool deallocateMultiplePins(const managed_pin_type *pinArray, byte arrayElementCount, PinOwner tag);
// Allocates a single pin, with an owner tag.
// De-allocation requires the same owner tag (or override)
static bool allocatePin(byte gpio, bool output, PinOwner tag);
// Allocates all the pins, or allocates none of the pins, with owner tag.
// Provided to simplify error condition handling in clients
// using more than one pin, such as I2C, SPI, rotary encoders,
// ethernet, etc..
static bool allocateMultiplePins(const managed_pin_type * mptArray, byte arrayElementCount, PinOwner tag );
[[deprecated("Replaced by three-parameter allocatePin(gpio, output, ownerTag), for improved debugging")]] // will return true for reserved pins
static inline bool allocatePin(byte gpio, bool output = true) { return allocatePin(gpio, output, PinOwner::None); } bool isPinAllocated(byte gpio, PinOwner tag = PinOwner::None);
[[deprecated("Replaced by two-parameter deallocatePin(gpio, ownerTag), for improved debugging")]] // will return false for reserved pins
static inline void deallocatePin(byte gpio) { deallocatePin(gpio, PinOwner::None); } bool isPinOk(byte gpio, bool output = true);
// will return true for reserved pins bool isReadOnlyPin(byte gpio);
static bool isPinAllocated(byte gpio, PinOwner tag = PinOwner::None);
// will return false for reserved pins
static bool isPinOk(byte gpio, bool output = true);
static bool isReadOnlyPin(byte gpio); PinOwner getPinOwner(byte gpio);
static PinOwner getPinOwner(byte gpio); #ifdef ARDUINO_ARCH_ESP32
byte allocateLedc(byte channels);
#ifdef ARDUINO_ARCH_ESP32 void deallocateLedc(byte pos, byte channels);
static byte allocateLedc(byte channels); #endif
static void deallocateLedc(byte pos, byte channels);
#endif
}; };
//extern PinManager pinManager; //extern PinManager pinManager;