From f830ea498cbb4057de4d436c72a5d2aab49e8e50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20M=C3=B6hle?= <91616163+softhack007@users.noreply.github.com> Date: Wed, 11 Feb 2026 22:24:06 +0100 Subject: [PATCH] Clean up global variables namespace, save a few 100 bytes of flash (#5368) * reduce scope of some variables to "static" these are not used anywhere else. Making them static avoid name conflicts, cleans up the global scope and in some cases allows for better optimization by the compiler. * remove unused reference ``tz``from analog clock usermod * side-catch: remove two "local var shadows global var" warnings * reduce scope of functions declared globally, but not used anywhere else Safe to make static * declared in fcn_declare.h, only used locally in one file * not declared in fcn_declare.h, only used locally * HUB75 small optimization make bit array functions "static inline" -> better for optimization, saves some bytes because the compiler does not need to preserve a non-inline function copy for external references. * a few more static functions as suggested by the rabbit. --- usermods/Analog_Clock/Analog_Clock.cpp | 1 - wled00/FX.cpp | 6 +++--- wled00/bus_manager.cpp | 12 ++++-------- wled00/cfg.cpp | 2 +- wled00/e131.cpp | 15 +++++++++++---- wled00/fcn_declare.h | 14 +++++++------- wled00/improv.cpp | 6 +++--- wled00/ir.cpp | 16 ++++++++-------- wled00/json.cpp | 4 ++-- wled00/ntp.cpp | 13 +++++++++---- wled00/overlay.cpp | 7 +++++-- wled00/remote.cpp | 2 +- wled00/set.cpp | 10 +++++----- wled00/udp.cpp | 2 +- wled00/util.cpp | 2 +- wled00/wled_serial.cpp | 13 ++++++++----- wled00/wled_server.cpp | 5 ++++- wled00/ws.cpp | 11 +++++++---- wled00/xml.cpp | 11 +++++++---- 19 files changed, 87 insertions(+), 65 deletions(-) diff --git a/usermods/Analog_Clock/Analog_Clock.cpp b/usermods/Analog_Clock/Analog_Clock.cpp index d3a2b73b..3e0a1e99 100644 --- a/usermods/Analog_Clock/Analog_Clock.cpp +++ b/usermods/Analog_Clock/Analog_Clock.cpp @@ -3,7 +3,6 @@ /* * Usermod for analog clock */ -extern Timezone* tz; class AnalogClockUsermod : public Usermod { private: diff --git a/wled00/FX.cpp b/wled00/FX.cpp index ddffeb03..54f441da 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -82,12 +82,12 @@ //#define MAX_FREQ_LOG10 3.71f // effect utility functions -uint8_t sin_gap(uint16_t in) { +static uint8_t sin_gap(uint16_t in) { if (in & 0x100) return 0; return sin8_t(in + 192); // correct phase shift of sine so that it starts and stops at 0 } -uint16_t triwave16(uint16_t in) { +static uint16_t triwave16(uint16_t in) { if (in < 0x8000) return in *2; return 0xFFFF - (in - 0x8000)*2; } @@ -99,7 +99,7 @@ uint16_t triwave16(uint16_t in) { * @param attdec attack & decay, max. pulsewidth / 2 * @returns signed waveform value */ -int8_t tristate_square8(uint8_t x, uint8_t pulsewidth, uint8_t attdec) { +static int8_t tristate_square8(uint8_t x, uint8_t pulsewidth, uint8_t attdec) { int8_t a = 127; if (x > 127) { a = -127; diff --git a/wled00/bus_manager.cpp b/wled00/bus_manager.cpp index 017f1dd1..2ae0f5d4 100644 --- a/wled00/bus_manager.cpp +++ b/wled00/bus_manager.cpp @@ -17,21 +17,17 @@ #include "bus_wrapper.h" #include "wled.h" -extern char cmDNS[]; -extern bool cctICused; -extern bool useParallelI2S; - // functions to get/set bits in an array - based on functions created by Brandon for GOL // toDo : make this a class that's completely defined in a header file // note: these functions are automatically inline by the compiler -bool getBitFromArray(const uint8_t* byteArray, size_t position) { // get bit value +static inline bool getBitFromArray(const uint8_t* byteArray, size_t position) { // get bit value size_t byteIndex = position >> 3; // divide by 8 unsigned bitIndex = position & 0x07; // modulo 8 uint8_t byteValue = byteArray[byteIndex]; return (byteValue >> bitIndex) & 1; } -void setBitInArray(uint8_t* byteArray, size_t position, bool value) { // set bit - with error handling for nullptr +static inline void setBitInArray(uint8_t* byteArray, size_t position, bool value) { // set bit - with error handling for nullptr //if (byteArray == nullptr) return; size_t byteIndex = position >> 3; // divide by 8 unsigned bitIndex = position & 0x07; // modulo 8 @@ -41,11 +37,11 @@ void setBitInArray(uint8_t* byteArray, size_t position, bool value) { // set bi byteArray[byteIndex] &= ~(1 << bitIndex); } -size_t getBitArrayBytes(size_t num_bits) { // number of bytes needed for an array with num_bits bits +static inline size_t getBitArrayBytes(size_t num_bits) { // number of bytes needed for an array with num_bits bits return (num_bits + 7) >> 3; } -void setBitArray(uint8_t* byteArray, size_t numBits, bool value) { // set all bits to same value +static inline void setBitArray(uint8_t* byteArray, size_t numBits, bool value) { // set all bits to same value if (byteArray == nullptr) return; size_t len = getBitArrayBytes(numBits); if (value) memset(byteArray, 0xFF, len); diff --git a/wled00/cfg.cpp b/wled00/cfg.cpp index 6e26b222..37028130 100644 --- a/wled00/cfg.cpp +++ b/wled00/cfg.cpp @@ -38,7 +38,7 @@ static constexpr bool validatePinsAndTypes(const unsigned* types, unsigned numTy //simple macro for ArduinoJSON's or syntax #define CJSON(a,b) a = b | a -void getStringFromJson(char* dest, const char* src, size_t len) { +static inline void getStringFromJson(char* dest, const char* src, size_t len) { if (src != nullptr) strlcpy(dest, src, len); } diff --git a/wled00/e131.cpp b/wled00/e131.cpp index 357e7841..8029feec 100644 --- a/wled00/e131.cpp +++ b/wled00/e131.cpp @@ -4,13 +4,20 @@ #define MAX_4_CH_LEDS_PER_UNIVERSE 128 #define MAX_CHANNELS_PER_UNIVERSE 512 +// forward declarations +static void handleDDPPacket(e131_packet_t* p); +static void handleArtnetPollReply(IPAddress ipAddress); +static void prepareArtnetPollReply(ArtPollReply *reply); +static void sendArtnetPollReply(ArtPollReply *reply, IPAddress ipAddress, uint16_t portAddress); + + /* * E1.31 handler */ //DDP protocol support, called by handleE131Packet //handles RGB data only -void handleDDPPacket(e131_packet_t* p) { +static void handleDDPPacket(e131_packet_t* p) { static bool ddpSeenPush = false; // have we seen a push yet? int lastPushSeq = e131LastSequenceNumber[0]; @@ -336,7 +343,7 @@ void handleDMXData(uint16_t uni, uint16_t dmxChannels, uint8_t* e131_data, uint8 e131NewData = true; } -void handleArtnetPollReply(IPAddress ipAddress) { +static void handleArtnetPollReply(IPAddress ipAddress) { ArtPollReply artnetPollReply; prepareArtnetPollReply(&artnetPollReply); @@ -402,7 +409,7 @@ void handleArtnetPollReply(IPAddress ipAddress) { #endif } -void prepareArtnetPollReply(ArtPollReply *reply) { +static void prepareArtnetPollReply(ArtPollReply *reply) { // Art-Net reply->reply_id[0] = 0x41; reply->reply_id[1] = 0x72; @@ -521,7 +528,7 @@ void prepareArtnetPollReply(ArtPollReply *reply) { } } -void sendArtnetPollReply(ArtPollReply *reply, IPAddress ipAddress, uint16_t portAddress) { +static void sendArtnetPollReply(ArtPollReply *reply, IPAddress ipAddress, uint16_t portAddress) { reply->reply_net_sw = (uint8_t)((portAddress >> 8) & 0x007F); reply->reply_sub_sw = (uint8_t)((portAddress >> 4) & 0x000F); reply->reply_sw_out[0] = (uint8_t)(portAddress & 0x000F); diff --git a/wled00/fcn_declare.h b/wled00/fcn_declare.h index 8430e45c..84b6da9e 100644 --- a/wled00/fcn_declare.h +++ b/wled00/fcn_declare.h @@ -99,9 +99,9 @@ void handleDMXInput(); //e131.cpp void handleE131Packet(e131_packet_t* p, IPAddress clientIP, byte protocol); void handleDMXData(uint16_t uni, uint16_t dmxChannels, uint8_t* e131_data, uint8_t mde, uint8_t previousUniverses); -void handleArtnetPollReply(IPAddress ipAddress); -void prepareArtnetPollReply(ArtPollReply* reply); -void sendArtnetPollReply(ArtPollReply* reply, IPAddress ipAddress, uint16_t portAddress); +// void handleArtnetPollReply(IPAddress ipAddress); // local function, only used in e131.cpp +// void prepareArtnetPollReply(ArtPollReply* reply); // local function, only used in e131.cpp +// void sendArtnetPollReply(ArtPollReply* reply, IPAddress ipAddress, uint16_t portAddress); // local function, only used in e131.cpp //file.cpp bool handleFileRead(AsyncWebServerRequest*, String path); @@ -207,8 +207,8 @@ void publishMqtt(); //ntp.cpp void handleTime(); void handleNetworkTime(); -void sendNTPPacket(); -bool checkNTPResponse(); +// void sendNTPPacket(); // local function, only used in ntp.cpp +// bool checkNTPResponse(); // local function, only used in ntp.cpp void updateLocalTime(); void getTimeString(char* out); bool checkCountdown(); @@ -221,8 +221,8 @@ void setTimeFromAPI(uint32_t timein); //overlay.cpp void handleOverlayDraw(); -void _overlayAnalogCountdown(); -void _overlayAnalogClock(); +// void _overlayAnalogCountdown(); // local function, only used in overlay.cpp +// void _overlayAnalogClock(); // local function, only used in overlay.cpp //playlist.cpp void shufflePlaylist(); diff --git a/wled00/improv.cpp b/wled00/improv.cpp index 0bc7a669..2d2d876d 100644 --- a/wled00/improv.cpp +++ b/wled00/improv.cpp @@ -16,8 +16,8 @@ #endif #define IMPROV_VERSION 1 - -void parseWiFiCommand(char *rpcData); +// forward declarations +static void parseWiFiCommand(char* rpcData); enum ImprovPacketType { Current_State = 0x01, @@ -252,7 +252,7 @@ void startImprovWifiScan() {} void handleImprovWifiScan() {} #endif -void parseWiFiCommand(char* rpcData) { +static void parseWiFiCommand(char* rpcData) { unsigned len = rpcData[0]; if (!len || len > 126) return; diff --git a/wled00/ir.cpp b/wled00/ir.cpp index fe0950ab..6c9ea3ad 100644 --- a/wled00/ir.cpp +++ b/wled00/ir.cpp @@ -7,14 +7,14 @@ * Infrared sensor support for several generic RGB remotes and custom JSON remote */ -IRrecv* irrecv; -decode_results results; -unsigned long irCheckedTime = 0; -uint32_t lastValidCode = 0; -byte lastRepeatableAction = ACTION_NONE; -uint8_t lastRepeatableValue = 0; -uint16_t irTimesRepeated = 0; -uint8_t lastIR6ColourIdx = 0; +static IRrecv* irrecv; +static decode_results results; +static unsigned long irCheckedTime = 0; +static uint32_t lastValidCode = 0; +static byte lastRepeatableAction = ACTION_NONE; +static uint8_t lastRepeatableValue = 0; +static uint16_t irTimesRepeated = 0; +static uint8_t lastIR6ColourIdx = 0; // brightnessSteps: a static array of brightness levels following a geometric diff --git a/wled00/json.cpp b/wled00/json.cpp index 3e053708..15b7f759 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -898,7 +898,7 @@ void serializeInfo(JsonObject root) root["ip"] = s; } -void setPaletteColors(JsonArray json, CRGBPalette16 palette) +static void setPaletteColors(JsonArray json, CRGBPalette16 palette) { for (int i = 0; i < 16; i++) { JsonArray colors = json.createNestedArray(); @@ -910,7 +910,7 @@ void setPaletteColors(JsonArray json, CRGBPalette16 palette) } } -void setPaletteColors(JsonArray json, byte* tcp) +static void setPaletteColors(JsonArray json, byte* tcp) { TRGBGradientPaletteEntryUnion* ent = (TRGBGradientPaletteEntryUnion*)(tcp); TRGBGradientPaletteEntryUnion u; diff --git a/wled00/ntp.cpp b/wled00/ntp.cpp index abad5c3c..8244961d 100644 --- a/wled00/ntp.cpp +++ b/wled00/ntp.cpp @@ -2,6 +2,11 @@ #include "wled.h" #include "fcn_declare.h" +// forward declarations +static void sendNTPPacket(); +static bool checkNTPResponse(); + + // WARNING: may cause errors in sunset calculations on ESP8266, see #3400 // building with `-D WLED_USE_REAL_MATH` will prevent those errors at the expense of flash and RAM @@ -11,7 +16,7 @@ //#define WLED_DEBUG_NTP #define NTP_SYNC_INTERVAL 42000UL //Get fresh NTP time about twice per day -Timezone* tz; +static Timezone* tz; #define TZ_UTC 0 #define TZ_UK 1 @@ -42,7 +47,7 @@ Timezone* tz; #define TZ_COUNT 25 #define TZ_INIT 255 -byte tzCurrent = TZ_INIT; //uninitialized +static byte tzCurrent = TZ_INIT; //uninitialized /* C++11 form -- static std::array, TZ_COUNT> TZ_TABLE PROGMEM = {{ */ static const std::pair TZ_TABLE[] PROGMEM = { @@ -199,7 +204,7 @@ void handleNetworkTime() } } -void sendNTPPacket() +static void sendNTPPacket() { if (!ntpServerIP.fromString(ntpServerName)) //see if server is IP or domain { @@ -245,7 +250,7 @@ static bool isValidNtpResponse(const byte* ntpPacket) { return true; } -bool checkNTPResponse() +static bool checkNTPResponse() { int cb = ntpUdp.parsePacket(); if (cb < NTP_MIN_PACKET_SIZE) { diff --git a/wled00/overlay.cpp b/wled00/overlay.cpp index 3f6e6312..f5366f3b 100644 --- a/wled00/overlay.cpp +++ b/wled00/overlay.cpp @@ -1,10 +1,13 @@ #include "wled.h" +// forward declarations +static void _overlayAnalogCountdown(); + /* * Used to draw clock overlays over the strip */ -void _overlayAnalogClock() +static void _overlayAnalogClock() { int overlaySize = overlayMax - overlayMin +1; if (countdownMode) @@ -47,7 +50,7 @@ void _overlayAnalogClock() } -void _overlayAnalogCountdown() +static void _overlayAnalogCountdown() { if ((unsigned long)toki.second() < countdownTime) { diff --git a/wled00/remote.cpp b/wled00/remote.cpp index b5aaa521..9685e52f 100644 --- a/wled00/remote.cpp +++ b/wled00/remote.cpp @@ -109,7 +109,7 @@ static void setOff() { } } -void presetWithFallback(uint8_t presetID, uint8_t effectID, uint8_t paletteID) { +static void presetWithFallback(uint8_t presetID, uint8_t effectID, uint8_t paletteID) { resetNightMode(); applyPresetWithFallback(presetID, CALL_MODE_BUTTON_PRESET, effectID, paletteID); } diff --git a/wled00/set.cpp b/wled00/set.cpp index ab3060d0..d69bda90 100644 --- a/wled00/set.cpp +++ b/wled00/set.cpp @@ -570,16 +570,16 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage) macroAlexaOff = request->arg(F("A1")).toInt(); macroCountdown = request->arg(F("MC")).toInt(); macroNl = request->arg(F("MN")).toInt(); - int i = 0; + int ii = 0; for (auto &button : buttons) { - char mp[4] = "MP"; mp[2] = (i<10?'0':'A'-10)+i; mp[3] = 0; // short - char ml[4] = "ML"; ml[2] = (i<10?'0':'A'-10)+i; ml[3] = 0; // long - char md[4] = "MD"; md[2] = (i<10?'0':'A'-10)+i; md[3] = 0; // double + char mp[4] = "MP"; mp[2] = (ii<10?'0':'A'-10)+ii; mp[3] = 0; // short + char ml[4] = "ML"; ml[2] = (ii<10?'0':'A'-10)+ii; ml[3] = 0; // long + char md[4] = "MD"; md[2] = (ii<10?'0':'A'-10)+ii; md[3] = 0; // double //if (!request->hasArg(mp)) break; button.macroButton = request->arg(mp).toInt(); // these will default to 0 if not present button.macroLongPress = request->arg(ml).toInt(); button.macroDoublePress = request->arg(md).toInt(); - i++; + ii++; } char k[3]; k[2] = 0; diff --git a/wled00/udp.cpp b/wled00/udp.cpp index f0e0ea7e..fab67bc6 100644 --- a/wled00/udp.cpp +++ b/wled00/udp.cpp @@ -457,7 +457,7 @@ void exitRealtime() { #define TMP2NET_OUT_PORT 65442 -void sendTPM2Ack() { +static void sendTPM2Ack() { notifierUdp.beginPacket(notifierUdp.remoteIP(), TMP2NET_OUT_PORT); uint8_t response_ack = 0xac; notifierUdp.write(&response_ack, 1); diff --git a/wled00/util.cpp b/wled00/util.cpp index bccb2d69..f3c1b318 100644 --- a/wled00/util.cpp +++ b/wled00/util.cpp @@ -370,7 +370,7 @@ int16_t extractModeDefaults(uint8_t mode, const char *segVar) void checkSettingsPIN(const char* pin) { if (!pin) return; if (!correctPIN && millis() - lastEditTime < PIN_RETRY_COOLDOWN) return; // guard against PIN brute force - bool correctBefore = correctPIN; + //bool correctBefore = correctPIN; // unused correctPIN = (strlen(settingsPIN) == 0 || strncmp(settingsPIN, pin, 4) == 0); lastEditTime = millis(); } diff --git a/wled00/wled_serial.cpp b/wled00/wled_serial.cpp index 7675976b..dd04788d 100644 --- a/wled00/wled_serial.cpp +++ b/wled00/wled_serial.cpp @@ -1,5 +1,8 @@ #include "wled.h" +// forward declarations +static void sendBytes(); + /* * Adalight and TPM2 handler */ @@ -19,9 +22,9 @@ enum class AdaState { TPM2_Header_CountLo, }; -uint16_t currentBaud = 1152; //default baudrate 115200 (divided by 100) -bool continuousSendLED = false; -uint32_t lastUpdate = 0; +static uint16_t currentBaud = 1152; //default baudrate 115200 (divided by 100) +static bool continuousSendLED = false; +static uint32_t lastUpdate = 0; void updateBaudRate(uint32_t rate){ unsigned rate100 = rate/100; @@ -37,7 +40,7 @@ void updateBaudRate(uint32_t rate){ } // RGB LED data return as JSON array. Slow, but easy to use on the other end. -void sendJSON(){ +static inline void sendJSON(){ if (serialCanTX) { unsigned used = strip.getLengthTotal(); Serial.write('['); @@ -50,7 +53,7 @@ void sendJSON(){ } // RGB LED data returned as bytes in TPM2 format. Faster, and slightly less easy to use on the other end. -void sendBytes(){ +static void sendBytes(){ if (serialCanTX) { Serial.write(0xC9); Serial.write(0xDA); unsigned used = strip.getLengthTotal(); diff --git a/wled00/wled_server.cpp b/wled00/wled_server.cpp index 687d7348..bcf7487f 100644 --- a/wled00/wled_server.cpp +++ b/wled00/wled_server.cpp @@ -19,6 +19,9 @@ #include "html_cpal.h" #include "html_edit.h" +// forward declarations +static void createEditHandler(); + // define flash strings once (saves flash memory) static const char s_redirecting[] PROGMEM = "Redirecting..."; @@ -222,7 +225,7 @@ static void handleUpload(AsyncWebServerRequest *request, const String& filename, static const char _edit_htm[] PROGMEM = "/edit.htm"; -void createEditHandler() { +static void createEditHandler() { if (editHandler != nullptr) server.removeHandler(editHandler); editHandler = &server.on(F("/edit"), static_cast(HTTP_GET), [](AsyncWebServerRequest *request) { diff --git a/wled00/ws.cpp b/wled00/ws.cpp index 6d74a5a0..5c4edcd3 100644 --- a/wled00/ws.cpp +++ b/wled00/ws.cpp @@ -5,15 +5,18 @@ */ #ifdef WLED_ENABLE_WEBSOCKETS +// forward declarations +static bool sendLiveLedsWs(uint32_t wsClient); + // define some constants for binary protocols, dont use defines but C++ style constexpr constexpr uint8_t BINARY_PROTOCOL_GENERIC = 0xFF; // generic / auto detect NOT IMPLEMENTED constexpr uint8_t BINARY_PROTOCOL_E131 = P_E131; // = 0, untested! constexpr uint8_t BINARY_PROTOCOL_ARTNET = P_ARTNET; // = 1, untested! constexpr uint8_t BINARY_PROTOCOL_DDP = P_DDP; // = 2 -uint16_t wsLiveClientId = 0; -unsigned long wsLastLiveTime = 0; -//uint8_t* wsFrameBuffer = nullptr; +static uint16_t wsLiveClientId = 0; +static unsigned long wsLastLiveTime = 0; +//static uint8_t* wsFrameBuffer = nullptr; #define WS_LIVE_INTERVAL 40 @@ -185,7 +188,7 @@ void sendDataWs(AsyncWebSocketClient * client) releaseJSONBufferLock(); } -bool sendLiveLedsWs(uint32_t wsClient) +static bool sendLiveLedsWs(uint32_t wsClient) { AsyncWebSocketClient * wsc = ws.client(wsClient); if (!wsc || wsc->queueLength() > 0) return false; //only send if queue free diff --git a/wled00/xml.cpp b/wled00/xml.cpp index 0b6eafa4..ce5dec73 100644 --- a/wled00/xml.cpp +++ b/wled00/xml.cpp @@ -5,6 +5,9 @@ * Sending XML status files to client */ +// forward declarations +static void appendGPIOinfo(Print& settingsScript); + //build XML response to HTTP /win API request void XML_response(Print& dest) { @@ -38,7 +41,7 @@ static void extractPin(Print& settingsScript, const JsonObject &obj, const char } } -void fillWLEDVersion(char *buf, size_t len) +static void fillWLEDVersion(char *buf, size_t len) { if (!buf || len == 0) return; @@ -89,7 +92,7 @@ static void fillUMPins(Print& settingsScript, const JsonObject &mods) } } -void appendGPIOinfo(Print& settingsScript) +static void appendGPIOinfo(Print& settingsScript) { settingsScript.print(F("d.um_p=[-1")); // has to have 1 element if (i2c_sda > -1 && i2c_scl > -1) { @@ -593,9 +596,9 @@ void getSettingsJS(byte subPage, Print& settingsScript) printSetFormValue(settingsScript,PSTR("A1"),macroAlexaOff); printSetFormValue(settingsScript,PSTR("MC"),macroCountdown); printSetFormValue(settingsScript,PSTR("MN"),macroNl); - int i = 0; + int ii = 0; for (const auto &button : buttons) { - settingsScript.printf_P(PSTR("addRow(%d,%d,%d,%d);"), i++, button.macroButton, button.macroLongPress, button.macroDoublePress); + settingsScript.printf_P(PSTR("addRow(%d,%d,%d,%d);"), ii++, button.macroButton, button.macroLongPress, button.macroDoublePress); } char k[4];