Merge pull request #4213 from willmmiles/static-class-to-namespace

Complete transition of UsermodManager and PinManager away from classes
This commit is contained in:
Will Miles 2025-01-08 21:38:10 -05:00 committed by GitHub
commit 204e72e9eb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 70 additions and 83 deletions

View File

@ -417,36 +417,33 @@ class Usermod {
#endif #endif
}; };
class UsermodManager { namespace UsermodManager {
private: extern byte numMods;
static Usermod* ums[WLED_MAX_USERMODS];
static byte numMods;
public: void loop();
static void loop(); void handleOverlayDraw();
static void handleOverlayDraw(); bool handleButton(uint8_t b);
static bool handleButton(uint8_t b); bool getUMData(um_data_t **um_data, uint8_t mod_id = USERMOD_ID_RESERVED); // USERMOD_ID_RESERVED will poll all usermods
static bool getUMData(um_data_t **um_data, uint8_t mod_id = USERMOD_ID_RESERVED); // USERMOD_ID_RESERVED will poll all usermods void setup();
static void setup(); void connected();
static void connected(); void appendConfigData(Print&);
static void appendConfigData(Print&); void addToJsonState(JsonObject& obj);
static void addToJsonState(JsonObject& obj); void addToJsonInfo(JsonObject& obj);
static void addToJsonInfo(JsonObject& obj); void readFromJsonState(JsonObject& obj);
static void readFromJsonState(JsonObject& obj); void addToConfig(JsonObject& obj);
static void addToConfig(JsonObject& obj); bool readFromConfig(JsonObject& obj);
static bool readFromConfig(JsonObject& obj);
#ifndef WLED_DISABLE_MQTT #ifndef WLED_DISABLE_MQTT
static void onMqttConnect(bool sessionPresent); void onMqttConnect(bool sessionPresent);
static bool onMqttMessage(char* topic, char* payload); bool onMqttMessage(char* topic, char* payload);
#endif #endif
#ifndef WLED_DISABLE_ESPNOW #ifndef WLED_DISABLE_ESPNOW
static bool onEspNowMessage(uint8_t* sender, uint8_t* payload, uint8_t len); bool onEspNowMessage(uint8_t* sender, uint8_t* payload, uint8_t len);
#endif #endif
static void onUpdateBegin(bool); void onUpdateBegin(bool);
static void onStateChange(uint8_t); void onStateChange(uint8_t);
static bool add(Usermod* um); bool add(Usermod* um);
static Usermod* lookup(uint16_t mod_id); Usermod* lookup(uint16_t mod_id);
static inline byte getModCount() {return numMods;}; inline byte getModCount() {return numMods;};
}; };
//usermods_list.cpp //usermods_list.cpp

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)
@ -290,13 +300,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,52 +76,38 @@ 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:
#ifdef ESP8266
#define WLED_NUM_PINS (GPIO_PIN_COUNT+1) // somehow they forgot GPIO 16 (0-16==17)
static uint32_t pinAlloc; // 1 bit per pin, we use first 17bits
#else
#define WLED_NUM_PINS (GPIO_PIN_COUNT)
static uint64_t pinAlloc; // 1 bit per pin, we use 50 bits on ESP32-S3
static uint16_t ledcAlloc; // up to 16 LEDC channels (WLED_MAX_ANALOG_CHANNELS)
#endif
static uint8_t i2cAllocCount; // allow multiple allocation of I2C bus pins but keep track of allocations
static uint8_t spiAllocCount; // allow multiple allocation of SPI bus pins but keep track of allocations
static PinOwner ownerTag[WLED_NUM_PINS];
public:
// De-allocates a single pin // De-allocates a single pin
static bool deallocatePin(byte gpio, PinOwner tag); bool deallocatePin(byte gpio, PinOwner tag);
// De-allocates multiple pins but only if all can be deallocated (PinOwner has to be specified) // De-allocates multiple pins but only if all can be deallocated (PinOwner has to be specified)
static bool deallocateMultiplePins(const uint8_t *pinArray, byte arrayElementCount, PinOwner tag); bool deallocateMultiplePins(const uint8_t *pinArray, byte arrayElementCount, PinOwner tag);
static bool deallocateMultiplePins(const managed_pin_type *pinArray, byte arrayElementCount, PinOwner tag); bool deallocateMultiplePins(const managed_pin_type *pinArray, byte arrayElementCount, PinOwner tag);
// Allocates a single pin, with an owner tag. // Allocates a single pin, with an owner tag.
// De-allocation requires the same owner tag (or override) // De-allocation requires the same owner tag (or override)
static bool allocatePin(byte gpio, bool output, PinOwner tag); bool allocatePin(byte gpio, bool output, PinOwner tag);
// Allocates all the pins, or allocates none of the pins, with owner tag. // Allocates all the pins, or allocates none of the pins, with owner tag.
// Provided to simplify error condition handling in clients // Provided to simplify error condition handling in clients
// using more than one pin, such as I2C, SPI, rotary encoders, // using more than one pin, such as I2C, SPI, rotary encoders,
// ethernet, etc.. // ethernet, etc..
static bool allocateMultiplePins(const managed_pin_type * mptArray, byte arrayElementCount, PinOwner tag ); bool allocateMultiplePins(const managed_pin_type * mptArray, byte arrayElementCount, PinOwner tag );
[[deprecated("Replaced by three-parameter allocatePin(gpio, output, ownerTag), for improved debugging")]] [[deprecated("Replaced by three-parameter allocatePin(gpio, output, ownerTag), for improved debugging")]]
static inline bool allocatePin(byte gpio, bool output = true) { return allocatePin(gpio, output, PinOwner::None); } inline bool allocatePin(byte gpio, bool output = true) { return allocatePin(gpio, output, PinOwner::None); }
[[deprecated("Replaced by two-parameter deallocatePin(gpio, ownerTag), for improved debugging")]] [[deprecated("Replaced by two-parameter deallocatePin(gpio, ownerTag), for improved debugging")]]
static inline void deallocatePin(byte gpio) { deallocatePin(gpio, PinOwner::None); } inline void deallocatePin(byte gpio) { deallocatePin(gpio, PinOwner::None); }
// will return true for reserved pins // will return true for reserved pins
static bool isPinAllocated(byte gpio, PinOwner tag = PinOwner::None); bool isPinAllocated(byte gpio, PinOwner tag = PinOwner::None);
// will return false for reserved pins // will return false for reserved pins
static bool isPinOk(byte gpio, bool output = true); bool isPinOk(byte gpio, bool output = true);
static bool isReadOnlyPin(byte gpio); bool isReadOnlyPin(byte gpio);
static PinOwner getPinOwner(byte gpio); PinOwner getPinOwner(byte gpio);
#ifdef ARDUINO_ARCH_ESP32 #ifdef ARDUINO_ARCH_ESP32
static byte allocateLedc(byte channels); byte allocateLedc(byte channels);
static void deallocateLedc(byte pos, byte channels); void deallocateLedc(byte pos, byte channels);
#endif #endif
}; };

View File

@ -3,6 +3,9 @@
* Registration and management utility for v2 usermods * Registration and management utility for v2 usermods
*/ */
static Usermod* ums[WLED_MAX_USERMODS] = {nullptr};
byte UsermodManager::numMods = 0;
//Usermod Manager internals //Usermod Manager internals
void UsermodManager::setup() { for (unsigned i = 0; i < numMods; i++) ums[i]->setup(); } void UsermodManager::setup() { for (unsigned i = 0; i < numMods; i++) ums[i]->setup(); }
void UsermodManager::connected() { for (unsigned i = 0; i < numMods; i++) ums[i]->connected(); } void UsermodManager::connected() { for (unsigned i = 0; i < numMods; i++) ums[i]->connected(); }
@ -69,8 +72,6 @@ bool UsermodManager::add(Usermod* um)
return true; return true;
} }
Usermod* UsermodManager::ums[WLED_MAX_USERMODS] = {nullptr};
byte UsermodManager::numMods = 0;
/* Usermod v2 interface shim for oappend */ /* Usermod v2 interface shim for oappend */
Print* Usermod::oappend_shim = nullptr; Print* Usermod::oappend_shim = nullptr;

View File

@ -895,9 +895,6 @@ WLED_GLOBAL uint32_t ledMaps _INIT(0); // bitfield representation of available l
WLED_GLOBAL uint16_t ledMaps _INIT(0); // bitfield representation of available ledmaps WLED_GLOBAL uint16_t ledMaps _INIT(0); // bitfield representation of available ledmaps
#endif #endif
// Usermod manager
WLED_GLOBAL UsermodManager usermods _INIT(UsermodManager());
// global I2C SDA pin (used for usermods) // global I2C SDA pin (used for usermods)
#ifndef I2CSDAPIN #ifndef I2CSDAPIN
WLED_GLOBAL int8_t i2c_sda _INIT(-1); WLED_GLOBAL int8_t i2c_sda _INIT(-1);