Skip to content

Commit f830ea4

Browse files
authored
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.
1 parent ce31d80 commit f830ea4

19 files changed

Lines changed: 87 additions & 65 deletions

usermods/Analog_Clock/Analog_Clock.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
/*
44
* Usermod for analog clock
55
*/
6-
extern Timezone* tz;
76

87
class AnalogClockUsermod : public Usermod {
98
private:

wled00/FX.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@
8282
//#define MAX_FREQ_LOG10 3.71f
8383

8484
// effect utility functions
85-
uint8_t sin_gap(uint16_t in) {
85+
static uint8_t sin_gap(uint16_t in) {
8686
if (in & 0x100) return 0;
8787
return sin8_t(in + 192); // correct phase shift of sine so that it starts and stops at 0
8888
}
8989

90-
uint16_t triwave16(uint16_t in) {
90+
static uint16_t triwave16(uint16_t in) {
9191
if (in < 0x8000) return in *2;
9292
return 0xFFFF - (in - 0x8000)*2;
9393
}
@@ -99,7 +99,7 @@ uint16_t triwave16(uint16_t in) {
9999
* @param attdec attack & decay, max. pulsewidth / 2
100100
* @returns signed waveform value
101101
*/
102-
int8_t tristate_square8(uint8_t x, uint8_t pulsewidth, uint8_t attdec) {
102+
static int8_t tristate_square8(uint8_t x, uint8_t pulsewidth, uint8_t attdec) {
103103
int8_t a = 127;
104104
if (x > 127) {
105105
a = -127;

wled00/bus_manager.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,17 @@
1717
#include "bus_wrapper.h"
1818
#include "wled.h"
1919

20-
extern char cmDNS[];
21-
extern bool cctICused;
22-
extern bool useParallelI2S;
23-
2420
// functions to get/set bits in an array - based on functions created by Brandon for GOL
2521
// toDo : make this a class that's completely defined in a header file
2622
// note: these functions are automatically inline by the compiler
27-
bool getBitFromArray(const uint8_t* byteArray, size_t position) { // get bit value
23+
static inline bool getBitFromArray(const uint8_t* byteArray, size_t position) { // get bit value
2824
size_t byteIndex = position >> 3; // divide by 8
2925
unsigned bitIndex = position & 0x07; // modulo 8
3026
uint8_t byteValue = byteArray[byteIndex];
3127
return (byteValue >> bitIndex) & 1;
3228
}
3329

34-
void setBitInArray(uint8_t* byteArray, size_t position, bool value) { // set bit - with error handling for nullptr
30+
static inline void setBitInArray(uint8_t* byteArray, size_t position, bool value) { // set bit - with error handling for nullptr
3531
//if (byteArray == nullptr) return;
3632
size_t byteIndex = position >> 3; // divide by 8
3733
unsigned bitIndex = position & 0x07; // modulo 8
@@ -41,11 +37,11 @@ void setBitInArray(uint8_t* byteArray, size_t position, bool value) { // set bi
4137
byteArray[byteIndex] &= ~(1 << bitIndex);
4238
}
4339

44-
size_t getBitArrayBytes(size_t num_bits) { // number of bytes needed for an array with num_bits bits
40+
static inline size_t getBitArrayBytes(size_t num_bits) { // number of bytes needed for an array with num_bits bits
4541
return (num_bits + 7) >> 3;
4642
}
4743

48-
void setBitArray(uint8_t* byteArray, size_t numBits, bool value) { // set all bits to same value
44+
static inline void setBitArray(uint8_t* byteArray, size_t numBits, bool value) { // set all bits to same value
4945
if (byteArray == nullptr) return;
5046
size_t len = getBitArrayBytes(numBits);
5147
if (value) memset(byteArray, 0xFF, len);

wled00/cfg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static constexpr bool validatePinsAndTypes(const unsigned* types, unsigned numTy
3838
//simple macro for ArduinoJSON's or syntax
3939
#define CJSON(a,b) a = b | a
4040

41-
void getStringFromJson(char* dest, const char* src, size_t len) {
41+
static inline void getStringFromJson(char* dest, const char* src, size_t len) {
4242
if (src != nullptr) strlcpy(dest, src, len);
4343
}
4444

wled00/e131.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@
44
#define MAX_4_CH_LEDS_PER_UNIVERSE 128
55
#define MAX_CHANNELS_PER_UNIVERSE 512
66

7+
// forward declarations
8+
static void handleDDPPacket(e131_packet_t* p);
9+
static void handleArtnetPollReply(IPAddress ipAddress);
10+
static void prepareArtnetPollReply(ArtPollReply *reply);
11+
static void sendArtnetPollReply(ArtPollReply *reply, IPAddress ipAddress, uint16_t portAddress);
12+
13+
714
/*
815
* E1.31 handler
916
*/
1017

1118
//DDP protocol support, called by handleE131Packet
1219
//handles RGB data only
13-
void handleDDPPacket(e131_packet_t* p) {
20+
static void handleDDPPacket(e131_packet_t* p) {
1421
static bool ddpSeenPush = false; // have we seen a push yet?
1522
int lastPushSeq = e131LastSequenceNumber[0];
1623

@@ -336,7 +343,7 @@ void handleDMXData(uint16_t uni, uint16_t dmxChannels, uint8_t* e131_data, uint8
336343
e131NewData = true;
337344
}
338345

339-
void handleArtnetPollReply(IPAddress ipAddress) {
346+
static void handleArtnetPollReply(IPAddress ipAddress) {
340347
ArtPollReply artnetPollReply;
341348
prepareArtnetPollReply(&artnetPollReply);
342349

@@ -402,7 +409,7 @@ void handleArtnetPollReply(IPAddress ipAddress) {
402409
#endif
403410
}
404411

405-
void prepareArtnetPollReply(ArtPollReply *reply) {
412+
static void prepareArtnetPollReply(ArtPollReply *reply) {
406413
// Art-Net
407414
reply->reply_id[0] = 0x41;
408415
reply->reply_id[1] = 0x72;
@@ -521,7 +528,7 @@ void prepareArtnetPollReply(ArtPollReply *reply) {
521528
}
522529
}
523530

524-
void sendArtnetPollReply(ArtPollReply *reply, IPAddress ipAddress, uint16_t portAddress) {
531+
static void sendArtnetPollReply(ArtPollReply *reply, IPAddress ipAddress, uint16_t portAddress) {
525532
reply->reply_net_sw = (uint8_t)((portAddress >> 8) & 0x007F);
526533
reply->reply_sub_sw = (uint8_t)((portAddress >> 4) & 0x000F);
527534
reply->reply_sw_out[0] = (uint8_t)(portAddress & 0x000F);

wled00/fcn_declare.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ void handleDMXInput();
9999
//e131.cpp
100100
void handleE131Packet(e131_packet_t* p, IPAddress clientIP, byte protocol);
101101
void handleDMXData(uint16_t uni, uint16_t dmxChannels, uint8_t* e131_data, uint8_t mde, uint8_t previousUniverses);
102-
void handleArtnetPollReply(IPAddress ipAddress);
103-
void prepareArtnetPollReply(ArtPollReply* reply);
104-
void sendArtnetPollReply(ArtPollReply* reply, IPAddress ipAddress, uint16_t portAddress);
102+
// void handleArtnetPollReply(IPAddress ipAddress); // local function, only used in e131.cpp
103+
// void prepareArtnetPollReply(ArtPollReply* reply); // local function, only used in e131.cpp
104+
// void sendArtnetPollReply(ArtPollReply* reply, IPAddress ipAddress, uint16_t portAddress); // local function, only used in e131.cpp
105105

106106
//file.cpp
107107
bool handleFileRead(AsyncWebServerRequest*, String path);
@@ -207,8 +207,8 @@ void publishMqtt();
207207
//ntp.cpp
208208
void handleTime();
209209
void handleNetworkTime();
210-
void sendNTPPacket();
211-
bool checkNTPResponse();
210+
// void sendNTPPacket(); // local function, only used in ntp.cpp
211+
// bool checkNTPResponse(); // local function, only used in ntp.cpp
212212
void updateLocalTime();
213213
void getTimeString(char* out);
214214
bool checkCountdown();
@@ -221,8 +221,8 @@ void setTimeFromAPI(uint32_t timein);
221221

222222
//overlay.cpp
223223
void handleOverlayDraw();
224-
void _overlayAnalogCountdown();
225-
void _overlayAnalogClock();
224+
// void _overlayAnalogCountdown(); // local function, only used in overlay.cpp
225+
// void _overlayAnalogClock(); // local function, only used in overlay.cpp
226226

227227
//playlist.cpp
228228
void shufflePlaylist();

wled00/improv.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
#endif
1717

1818
#define IMPROV_VERSION 1
19-
20-
void parseWiFiCommand(char *rpcData);
19+
// forward declarations
20+
static void parseWiFiCommand(char* rpcData);
2121

2222
enum ImprovPacketType {
2323
Current_State = 0x01,
@@ -252,7 +252,7 @@ void startImprovWifiScan() {}
252252
void handleImprovWifiScan() {}
253253
#endif
254254

255-
void parseWiFiCommand(char* rpcData) {
255+
static void parseWiFiCommand(char* rpcData) {
256256
unsigned len = rpcData[0];
257257
if (!len || len > 126) return;
258258

wled00/ir.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
* Infrared sensor support for several generic RGB remotes and custom JSON remote
88
*/
99

10-
IRrecv* irrecv;
11-
decode_results results;
12-
unsigned long irCheckedTime = 0;
13-
uint32_t lastValidCode = 0;
14-
byte lastRepeatableAction = ACTION_NONE;
15-
uint8_t lastRepeatableValue = 0;
16-
uint16_t irTimesRepeated = 0;
17-
uint8_t lastIR6ColourIdx = 0;
10+
static IRrecv* irrecv;
11+
static decode_results results;
12+
static unsigned long irCheckedTime = 0;
13+
static uint32_t lastValidCode = 0;
14+
static byte lastRepeatableAction = ACTION_NONE;
15+
static uint8_t lastRepeatableValue = 0;
16+
static uint16_t irTimesRepeated = 0;
17+
static uint8_t lastIR6ColourIdx = 0;
1818

1919

2020
// brightnessSteps: a static array of brightness levels following a geometric

wled00/json.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ void serializeInfo(JsonObject root)
898898
root["ip"] = s;
899899
}
900900

901-
void setPaletteColors(JsonArray json, CRGBPalette16 palette)
901+
static void setPaletteColors(JsonArray json, CRGBPalette16 palette)
902902
{
903903
for (int i = 0; i < 16; i++) {
904904
JsonArray colors = json.createNestedArray();
@@ -910,7 +910,7 @@ void setPaletteColors(JsonArray json, CRGBPalette16 palette)
910910
}
911911
}
912912

913-
void setPaletteColors(JsonArray json, byte* tcp)
913+
static void setPaletteColors(JsonArray json, byte* tcp)
914914
{
915915
TRGBGradientPaletteEntryUnion* ent = (TRGBGradientPaletteEntryUnion*)(tcp);
916916
TRGBGradientPaletteEntryUnion u;

wled00/ntp.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
#include "wled.h"
33
#include "fcn_declare.h"
44

5+
// forward declarations
6+
static void sendNTPPacket();
7+
static bool checkNTPResponse();
8+
9+
510
// WARNING: may cause errors in sunset calculations on ESP8266, see #3400
611
// building with `-D WLED_USE_REAL_MATH` will prevent those errors at the expense of flash and RAM
712

@@ -11,7 +16,7 @@
1116
//#define WLED_DEBUG_NTP
1217
#define NTP_SYNC_INTERVAL 42000UL //Get fresh NTP time about twice per day
1318

14-
Timezone* tz;
19+
static Timezone* tz;
1520

1621
#define TZ_UTC 0
1722
#define TZ_UK 1
@@ -42,7 +47,7 @@ Timezone* tz;
4247
#define TZ_COUNT 25
4348
#define TZ_INIT 255
4449

45-
byte tzCurrent = TZ_INIT; //uninitialized
50+
static byte tzCurrent = TZ_INIT; //uninitialized
4651

4752
/* C++11 form -- static std::array<std::pair<TimeChangeRule, TimeChangeRule>, TZ_COUNT> TZ_TABLE PROGMEM = {{ */
4853
static const std::pair<TimeChangeRule, TimeChangeRule> TZ_TABLE[] PROGMEM = {
@@ -199,7 +204,7 @@ void handleNetworkTime()
199204
}
200205
}
201206

202-
void sendNTPPacket()
207+
static void sendNTPPacket()
203208
{
204209
if (!ntpServerIP.fromString(ntpServerName)) //see if server is IP or domain
205210
{
@@ -245,7 +250,7 @@ static bool isValidNtpResponse(const byte* ntpPacket) {
245250
return true;
246251
}
247252

248-
bool checkNTPResponse()
253+
static bool checkNTPResponse()
249254
{
250255
int cb = ntpUdp.parsePacket();
251256
if (cb < NTP_MIN_PACKET_SIZE) {

0 commit comments

Comments
 (0)