Skip to content

Commit cd1ede3

Browse files
committed
Size & speed optimisations
1 parent 1ec68c4 commit cd1ede3

File tree

16 files changed

+72
-70
lines changed

16 files changed

+72
-70
lines changed

usermods/Temperature/usermod_temperature.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ float UsermodTemperature::readDallas() {
113113
#ifdef WLED_DEBUG
114114
if (OneWire::crc8(data,8) != data[8]) {
115115
DEBUG_PRINTLN(F("CRC error reading temperature."));
116-
for (byte i=0; i < 9; i++) DEBUG_PRINTF_P(PSTR("0x%02X "), data[i]);
116+
for (unsigned i=0; i < 9; i++) DEBUG_PRINTF_P(PSTR("0x%02X "), data[i]);
117117
DEBUG_PRINT(F(" => "));
118118
DEBUG_PRINTF_P(PSTR("0x%02X\n"), OneWire::crc8(data,8));
119119
}
@@ -133,7 +133,7 @@ float UsermodTemperature::readDallas() {
133133
break;
134134
}
135135
}
136-
for (byte i=1; i<9; i++) data[0] &= data[i];
136+
for (unsigned i=1; i<9; i++) data[0] &= data[i];
137137
return data[0]==0xFF ? -127.0f : retVal;
138138
}
139139

usermods/usermod_v2_four_line_display_ALT/usermod_v2_four_line_display_ALT.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,8 @@ void FourLineDisplayUsermod::setPowerSave(uint8_t save) {
445445

446446
void FourLineDisplayUsermod::center(String &line, uint8_t width) {
447447
int len = line.length();
448-
if (len<width) for (byte i=(width-len)/2; i>0; i--) line = ' ' + line;
449-
for (byte i=line.length(); i<width; i++) line += ' ';
448+
if (len<width) for (unsigned i=(width-len)/2; i>0; i--) line = ' ' + line;
449+
for (unsigned i=line.length(); i<width; i++) line += ' ';
450450
}
451451

452452
void FourLineDisplayUsermod::draw2x2GlyphIcons() {
@@ -819,28 +819,28 @@ void FourLineDisplayUsermod::showCurrentEffectOrPalette(int inputEffPal, const c
819819
if (overlayUntil == 0) {
820820
lockRedraw = true;
821821
// Find the mode name in JSON
822-
uint8_t printedChars = extractModeName(inputEffPal, qstring, lineBuffer, MAX_JSON_CHARS-1);
822+
unsigned printedChars = extractModeName(inputEffPal, qstring, lineBuffer, MAX_JSON_CHARS-1);
823823
if (lineBuffer[0]=='*' && lineBuffer[1]==' ') {
824824
// remove "* " from dynamic palettes
825-
for (byte i=2; i<=printedChars; i++) lineBuffer[i-2] = lineBuffer[i]; //include '\0'
825+
for (unsigned i=2; i<=printedChars; i++) lineBuffer[i-2] = lineBuffer[i]; //include '\0'
826826
printedChars -= 2;
827827
} else if ((lineBuffer[0]==' ' && lineBuffer[1]>127)) {
828828
// remove note symbol from effect names
829-
for (byte i=5; i<=printedChars; i++) lineBuffer[i-5] = lineBuffer[i]; //include '\0'
829+
for (unsigned i=5; i<=printedChars; i++) lineBuffer[i-5] = lineBuffer[i]; //include '\0'
830830
printedChars -= 5;
831831
}
832832
if (lineHeight == 2) { // use this code for 8 line display
833833
char smallBuffer1[MAX_MODE_LINE_SPACE];
834834
char smallBuffer2[MAX_MODE_LINE_SPACE];
835-
uint8_t smallChars1 = 0;
836-
uint8_t smallChars2 = 0;
835+
unsigned smallChars1 = 0;
836+
unsigned smallChars2 = 0;
837837
if (printedChars < MAX_MODE_LINE_SPACE) { // use big font if the text fits
838838
while (printedChars < (MAX_MODE_LINE_SPACE-1)) lineBuffer[printedChars++]=' ';
839839
lineBuffer[printedChars] = 0;
840840
drawString(1, row*lineHeight, lineBuffer);
841841
} else { // for long names divide the text into 2 lines and print them small
842842
bool spaceHit = false;
843-
for (uint8_t i = 0; i < printedChars; i++) {
843+
for (unsigned i = 0; i < printedChars; i++) {
844844
switch (lineBuffer[i]) {
845845
case ' ':
846846
if (i > 4 && !spaceHit) {
@@ -865,8 +865,8 @@ void FourLineDisplayUsermod::showCurrentEffectOrPalette(int inputEffPal, const c
865865
}
866866
} else { // use this code for 4 ling displays
867867
char smallBuffer3[MAX_MODE_LINE_SPACE+1]; // uses 1x1 icon for mode/palette
868-
uint8_t smallChars3 = 0;
869-
for (uint8_t i = 0; i < MAX_MODE_LINE_SPACE; i++) smallBuffer3[smallChars3++] = (i >= printedChars) ? ' ' : lineBuffer[i];
868+
unsigned smallChars3 = 0;
869+
for (unsigned i = 0; i < MAX_MODE_LINE_SPACE; i++) smallBuffer3[smallChars3++] = (i >= printedChars) ? ' ' : lineBuffer[i];
870870
smallBuffer3[smallChars3] = 0;
871871
drawString(1, row*lineHeight, smallBuffer3, true);
872872
}
@@ -1265,7 +1265,7 @@ void FourLineDisplayUsermod::addToConfig(JsonObject& root) {
12651265
bool FourLineDisplayUsermod::readFromConfig(JsonObject& root) {
12661266
bool needsRedraw = false;
12671267
DisplayType newType = type;
1268-
int8_t oldPin[3]; for (byte i=0; i<3; i++) oldPin[i] = ioPin[i];
1268+
int8_t oldPin[3]; for (unsigned i=0; i<3; i++) oldPin[i] = ioPin[i];
12691269

12701270
JsonObject top = root[FPSTR(_name)];
12711271
if (top.isNull()) {
@@ -1276,7 +1276,7 @@ bool FourLineDisplayUsermod::readFromConfig(JsonObject& root) {
12761276

12771277
enabled = top[FPSTR(_enabled)] | enabled;
12781278
newType = top["type"] | newType;
1279-
for (byte i=0; i<3; i++) ioPin[i] = top["pin"][i] | ioPin[i];
1279+
for (unsigned i=0; i<3; i++) ioPin[i] = top["pin"][i] | ioPin[i];
12801280
flip = top[FPSTR(_flip)] | flip;
12811281
contrast = top[FPSTR(_contrast)] | contrast;
12821282
#ifndef ARDUINO_ARCH_ESP32
@@ -1302,7 +1302,7 @@ bool FourLineDisplayUsermod::readFromConfig(JsonObject& root) {
13021302
DEBUG_PRINTLN(F(" config (re)loaded."));
13031303
// changing parameters from settings page
13041304
bool pinsChanged = false;
1305-
for (byte i=0; i<3; i++) if (ioPin[i] != oldPin[i]) { pinsChanged = true; break; }
1305+
for (unsigned i=0; i<3; i++) if (ioPin[i] != oldPin[i]) { pinsChanged = true; break; }
13061306
if (pinsChanged || type!=newType) {
13071307
bool isSPI = (type == SSD1306_SPI || type == SSD1306_SPI64 || type == SSD1309_SPI64);
13081308
bool newSPI = (newType == SSD1306_SPI || newType == SSD1306_SPI64 || newType == SSD1309_SPI64);

usermods/usermod_v2_rotary_encoder_ui_ALT/usermod_v2_rotary_encoder_ui_ALT.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ void RotaryEncoderUIUsermod::sortModesAndPalettes() {
416416

417417
byte *RotaryEncoderUIUsermod::re_initIndexArray(int numModes) {
418418
byte *indexes = (byte *)malloc(sizeof(byte) * numModes);
419-
for (byte i = 0; i < numModes; i++) {
419+
for (unsigned i = 0; i < numModes; i++) {
420420
indexes[i] = i;
421421
}
422422
return indexes;
@@ -700,7 +700,7 @@ void RotaryEncoderUIUsermod::findCurrentEffectAndPalette() {
700700

701701
effectPaletteIndex = 0;
702702
DEBUG_PRINTLN(effectPalette);
703-
for (uint8_t i = 0; i < strip.getPaletteCount()+strip.customPalettes.size(); i++) {
703+
for (unsigned i = 0; i < strip.getPaletteCount()+strip.customPalettes.size(); i++) {
704704
if (palettes_alpha_indexes[i] == effectPalette) {
705705
effectPaletteIndex = i;
706706
DEBUG_PRINTLN(F("Found palette."));
@@ -764,7 +764,7 @@ void RotaryEncoderUIUsermod::changeEffect(bool increase) {
764764
effectCurrent = modes_alpha_indexes[effectCurrentIndex];
765765
stateChanged = true;
766766
if (applyToAll) {
767-
for (byte i=0; i<strip.getSegmentsNum(); i++) {
767+
for (unsigned i=0; i<strip.getSegmentsNum(); i++) {
768768
Segment& seg = strip.getSegment(i);
769769
if (!seg.isActive()) continue;
770770
seg.setMode(effectCurrent);
@@ -792,7 +792,7 @@ void RotaryEncoderUIUsermod::changeEffectSpeed(bool increase) {
792792
effectSpeed = max(min((increase ? effectSpeed+fadeAmount : effectSpeed-fadeAmount), 255), 0);
793793
stateChanged = true;
794794
if (applyToAll) {
795-
for (byte i=0; i<strip.getSegmentsNum(); i++) {
795+
for (unsigned i=0; i<strip.getSegmentsNum(); i++) {
796796
Segment& seg = strip.getSegment(i);
797797
if (!seg.isActive()) continue;
798798
seg.speed = effectSpeed;
@@ -820,7 +820,7 @@ void RotaryEncoderUIUsermod::changeEffectIntensity(bool increase) {
820820
effectIntensity = max(min((increase ? effectIntensity+fadeAmount : effectIntensity-fadeAmount), 255), 0);
821821
stateChanged = true;
822822
if (applyToAll) {
823-
for (byte i=0; i<strip.getSegmentsNum(); i++) {
823+
for (unsigned i=0; i<strip.getSegmentsNum(); i++) {
824824
Segment& seg = strip.getSegment(i);
825825
if (!seg.isActive()) continue;
826826
seg.intensity = effectIntensity;
@@ -855,7 +855,7 @@ void RotaryEncoderUIUsermod::changeCustom(uint8_t par, bool increase) {
855855
case 2: val = sid.custom2 = max(min((increase ? sid.custom2+fadeAmount : sid.custom2-fadeAmount), 255), 0); break;
856856
default: val = sid.custom1 = max(min((increase ? sid.custom1+fadeAmount : sid.custom1-fadeAmount), 255), 0); break;
857857
}
858-
for (byte i=0; i<strip.getSegmentsNum(); i++) {
858+
for (unsigned i=0; i<strip.getSegmentsNum(); i++) {
859859
Segment& seg = strip.getSegment(i);
860860
if (!seg.isActive() || i == id) continue;
861861
switch (par) {
@@ -894,7 +894,7 @@ void RotaryEncoderUIUsermod::changePalette(bool increase) {
894894
effectPalette = palettes_alpha_indexes[effectPaletteIndex];
895895
stateChanged = true;
896896
if (applyToAll) {
897-
for (byte i=0; i<strip.getSegmentsNum(); i++) {
897+
for (unsigned i=0; i<strip.getSegmentsNum(); i++) {
898898
Segment& seg = strip.getSegment(i);
899899
if (!seg.isActive()) continue;
900900
seg.setPalette(effectPalette);
@@ -923,7 +923,7 @@ void RotaryEncoderUIUsermod::changeHue(bool increase){
923923
colorHStoRGB(currentHue1*256, currentSat1, col);
924924
stateChanged = true;
925925
if (applyToAll) {
926-
for (byte i=0; i<strip.getSegmentsNum(); i++) {
926+
for (unsigned i=0; i<strip.getSegmentsNum(); i++) {
927927
Segment& seg = strip.getSegment(i);
928928
if (!seg.isActive()) continue;
929929
seg.colors[0] = RGBW32(col[0], col[1], col[2], col[3]);
@@ -952,7 +952,7 @@ void RotaryEncoderUIUsermod::changeSat(bool increase){
952952
currentSat1 = max(min((increase ? currentSat1+fadeAmount : currentSat1-fadeAmount), 255), 0);
953953
colorHStoRGB(currentHue1*256, currentSat1, col);
954954
if (applyToAll) {
955-
for (byte i=0; i<strip.getSegmentsNum(); i++) {
955+
for (unsigned i=0; i<strip.getSegmentsNum(); i++) {
956956
Segment& seg = strip.getSegment(i);
957957
if (!seg.isActive()) continue;
958958
seg.colors[0] = RGBW32(col[0], col[1], col[2], col[3]);
@@ -1012,7 +1012,7 @@ void RotaryEncoderUIUsermod::changeCCT(bool increase){
10121012
#endif
10131013
currentCCT = max(min((increase ? currentCCT+fadeAmount : currentCCT-fadeAmount), 255), 0);
10141014
// if (applyToAll) {
1015-
for (byte i=0; i<strip.getSegmentsNum(); i++) {
1015+
for (unsigned i=0; i<strip.getSegmentsNum(); i++) {
10161016
Segment& seg = strip.getSegment(i);
10171017
if (!seg.isActive()) continue;
10181018
seg.setCCT(currentCCT);

wled00/FX_fcn.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,8 +1215,9 @@ void WS2812FX::finalizeInit(void) {
12151215
const unsigned defDataPins[] = {DATA_PINS};
12161216
const unsigned defCounts[] = {PIXEL_COUNTS};
12171217
const unsigned defNumPins = ((sizeof defDataPins) / (sizeof defDataPins[0]));
1218-
const unsigned defNumCounts = ((sizeof defCounts) / (sizeof defCounts[0]));
1219-
const unsigned defNumBusses = defNumPins > defNumCounts && defNumCounts > 1 && defNumPins%defNumCounts == 0 ? defNumCounts : defNumPins;
1218+
const unsigned defNumCounts = ((sizeof defCounts) / (sizeof defCounts[0]));
1219+
// if number of pins is divisible by counts, use number of counts to determine number of buses, otherwise use pins
1220+
const unsigned defNumBusses = defNumPins > defNumCounts && defNumPins%defNumCounts == 0 ? defNumCounts : defNumPins;
12201221
const unsigned pinsPerBus = defNumPins / defNumBusses;
12211222
unsigned prevLen = 0;
12221223
for (unsigned i = 0; i < defNumBusses && i < WLED_MAX_BUSSES+WLED_MIN_VIRTUAL_BUSSES; i++) {
@@ -1229,6 +1230,7 @@ void WS2812FX::finalizeInit(void) {
12291230
while (pinManager.isPinAllocated(defPin[0]) && defPin[0] < WLED_NUM_PINS) defPin[0]++;
12301231
}
12311232
unsigned start = prevLen;
1233+
// if we have less counts than pins and they do not align, use last known count to set current count
12321234
unsigned count = defCounts[(i < defNumCounts) ? i : defNumCounts -1];
12331235
prevLen += count;
12341236
BusConfig defCfg = BusConfig(DEFAULT_LED_TYPE, defPin, start, count, DEFAULT_LED_COLOR_ORDER, false, 0, RGBW_MODE_MANUAL_ONLY, 0, useGlobalLedBuffer);

wled00/NodeStruct.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct NodeStruct
3434

3535
NodeStruct() : age(0), nodeType(0), build(0)
3636
{
37-
for (uint8_t i = 0; i < 4; ++i) { ip[i] = 0; }
37+
for (unsigned i = 0; i < 4; ++i) { ip[i] = 0; }
3838
}
3939
};
4040
typedef std::map<uint8_t, NodeStruct> NodesMap;

wled00/alexa.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void alexaInit()
2525
// names are identical as the preset names, switching off can be done by switching off any of them
2626
if (alexaNumPresets) {
2727
String name = "";
28-
for (byte presetIndex = 1; presetIndex <= alexaNumPresets; presetIndex++)
28+
for (unsigned presetIndex = 1; presetIndex <= alexaNumPresets; presetIndex++)
2929
{
3030
if (!getPresetName(presetIndex, name)) break; // no more presets
3131
EspalexaDevice* dev = new EspalexaDevice(name.c_str(), onAlexaChange, EspalexaDeviceType::extendedcolor);
@@ -64,7 +64,7 @@ void onAlexaChange(EspalexaDevice* dev)
6464
} else // switch-on behavior for preset devices
6565
{
6666
// turn off other preset devices
67-
for (byte i = 1; i < espalexa.getDeviceCount(); i++)
67+
for (unsigned i = 1; i < espalexa.getDeviceCount(); i++)
6868
{
6969
if (i == dev->getId()) continue;
7070
espalexa.getDevice(i)->setValue(0); // turn off other presets
@@ -87,7 +87,7 @@ void onAlexaChange(EspalexaDevice* dev)
8787
applyPreset(macroAlexaOff, CALL_MODE_ALEXA);
8888
// below for loop stops Alexa from complaining if macroAlexaOff does not actually turn off
8989
}
90-
for (byte i = 0; i < espalexa.getDeviceCount(); i++)
90+
for (unsigned i = 0; i < espalexa.getDeviceCount(); i++)
9191
{
9292
espalexa.getDevice(i)->setValue(0);
9393
}

wled00/cfg.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ bool deserializeConfig(JsonObject doc, bool fromFS) {
8686
CJSON(apBehavior, ap[F("behav")]);
8787
/*
8888
JsonArray ap_ip = ap["ip"];
89-
for (byte i = 0; i < 4; i++) {
89+
for (unsigned i = 0; i < 4; i++) {
9090
apIP[i] = ap_ip;
9191
}
9292
*/
@@ -565,7 +565,7 @@ bool deserializeConfig(JsonObject doc, bool fromFS) {
565565

566566
JsonArray if_hue_ip = if_hue["ip"];
567567

568-
for (byte i = 0; i < 4; i++)
568+
for (unsigned i = 0; i < 4; i++)
569569
CJSON(hueIP[i], if_hue_ip[i]);
570570
#endif
571571

@@ -793,7 +793,7 @@ void serializeConfig() {
793793
ethernet["type"] = ethernetType;
794794
if (ethernetType != WLED_ETH_NONE && ethernetType < WLED_NUM_ETH_TYPES) {
795795
JsonArray pins = ethernet.createNestedArray("pin");
796-
for (uint8_t p=0; p<WLED_ETH_RSVD_PINS_COUNT; p++) pins.add(esp32_nonconfigurable_ethernet_pins[p].pin);
796+
for (unsigned p=0; p<WLED_ETH_RSVD_PINS_COUNT; p++) pins.add(esp32_nonconfigurable_ethernet_pins[p].pin);
797797
if (ethernetBoards[ethernetType].eth_power>=0) pins.add(ethernetBoards[ethernetType].eth_power);
798798
if (ethernetBoards[ethernetType].eth_mdc>=0) pins.add(ethernetBoards[ethernetType].eth_mdc);
799799
if (ethernetBoards[ethernetType].eth_mdio>=0) pins.add(ethernetBoards[ethernetType].eth_mdio);
@@ -1046,7 +1046,7 @@ void serializeConfig() {
10461046
if_hue_recv["col"] = hueApplyColor;
10471047

10481048
JsonArray if_hue_ip = if_hue.createNestedArray("ip");
1049-
for (byte i = 0; i < 4; i++) {
1049+
for (unsigned i = 0; i < 4; i++) {
10501050
if_hue_ip.add(hueIP[i]);
10511051
}
10521052
#endif
@@ -1081,7 +1081,7 @@ void serializeConfig() {
10811081

10821082
JsonArray timers_ins = timers.createNestedArray("ins");
10831083

1084-
for (byte i = 0; i < 10; i++) {
1084+
for (unsigned i = 0; i < 10; i++) {
10851085
if (timerMacro[i] == 0 && timerHours[i] == 0 && timerMinutes[i] == 0) continue; // sunrise/sunset get saved always (timerHours=255)
10861086
JsonObject timers_ins0 = timers_ins.createNestedObject();
10871087
timers_ins0["en"] = (timerWeekday[i] & 0x01);
@@ -1113,7 +1113,7 @@ void serializeConfig() {
11131113
dmx[F("start-led")] = DMXStartLED;
11141114

11151115
JsonArray dmx_fixmap = dmx.createNestedArray(F("fixmap"));
1116-
for (byte i = 0; i < 15; i++) {
1116+
for (unsigned i = 0; i < 15; i++) {
11171117
dmx_fixmap.add(DMXFixtureMap[i]);
11181118
}
11191119

wled00/dmx.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void handleDMX()
2222
bool calc_brightness = true;
2323

2424
// check if no shutter channel is set
25-
for (byte i = 0; i < DMXChannels; i++)
25+
for (unsigned i = 0; i < DMXChannels; i++)
2626
{
2727
if (DMXFixtureMap[i] == 5) calc_brightness = false;
2828
}

wled00/led.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ void handleNightlight()
226226
nightlightDelayMs = (unsigned)(nightlightDelayMins*60000);
227227
nightlightActiveOld = true;
228228
briNlT = bri;
229-
for (byte i=0; i<4; i++) colNlT[i] = col[i]; // remember starting color
229+
for (unsigned i=0; i<4; i++) colNlT[i] = col[i]; // remember starting color
230230
if (nightlightMode == NL_MODE_SUN)
231231
{
232232
//save current
@@ -251,7 +251,7 @@ void handleNightlight()
251251
bri = briNlT + ((nightlightTargetBri - briNlT)*nper);
252252
if (nightlightMode == NL_MODE_COLORFADE) // color fading only is enabled with "NF=2"
253253
{
254-
for (byte i=0; i<4; i++) col[i] = colNlT[i]+ ((colSec[i] - colNlT[i])*nper); // fading from actual color to secondary color
254+
for (unsigned i=0; i<4; i++) col[i] = colNlT[i]+ ((colSec[i] - colNlT[i])*nper); // fading from actual color to secondary color
255255
}
256256
colorUpdated(CALL_MODE_NO_NOTIFY);
257257
}

wled00/ntp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ void checkTimers()
377377
if (!hour(localTime) && minute(localTime)==1) calculateSunriseAndSunset();
378378

379379
DEBUG_PRINTF_P(PSTR("Local time: %02d:%02d\n"), hour(localTime), minute(localTime));
380-
for (uint8_t i = 0; i < 8; i++)
380+
for (unsigned i = 0; i < 8; i++)
381381
{
382382
if (timerMacro[i] != 0
383383
&& (timerWeekday[i] & 0x01) //timer is enabled

0 commit comments

Comments
 (0)