From 46e435001345fef30e1deadda03f1e9b6bac8686 Mon Sep 17 00:00:00 2001 From: cschwinne Date: Mon, 11 Mar 2019 19:30:49 +0100 Subject: [PATCH] Improved heap usage by 2k --- wled00/WS2812FX.cpp | 11 +++++------ wled00/wled00.ino | 5 ++--- wled00/wled01_eeprom.ino | 10 +++------- wled00/wled02_xml.ino | 19 ++++++++++++------- wled00/wled07_notify.ino | 11 +++++------ wled00/wled17_mqtt.ino | 3 +-- wled00/wled18_server.ino | 12 ++++++++---- 7 files changed, 36 insertions(+), 35 deletions(-) diff --git a/wled00/WS2812FX.cpp b/wled00/WS2812FX.cpp index 23f93c08..843bd858 100644 --- a/wled00/WS2812FX.cpp +++ b/wled00/WS2812FX.cpp @@ -739,16 +739,16 @@ uint16_t WS2812FX::mode_chase_rainbow(void) { /* * Sec flashes running on prim. */ +#define FLASH_COUNT 4 uint16_t WS2812FX::mode_chase_flash(void) { - const static uint8_t flash_count = 4; - uint8_t flash_step = SEGMENT_RUNTIME.counter_mode_call % ((flash_count * 2) + 1); + uint8_t flash_step = SEGMENT_RUNTIME.counter_mode_call % ((FLASH_COUNT * 2) + 1); for(uint16_t i=SEGMENT.start; i < SEGMENT.stop; i++) { setPixelColor(i, color_from_palette(i, true, PALETTE_SOLID_WRAP, 0)); } uint16_t delay = 10 + ((30 * (uint16_t)(255 - SEGMENT.speed)) / SEGMENT_LENGTH); - if(flash_step < (flash_count * 2)) { + if(flash_step < (FLASH_COUNT * 2)) { if(flash_step % 2 == 0) { uint16_t n = SEGMENT_RUNTIME.counter_mode_step; uint16_t m = (SEGMENT_RUNTIME.counter_mode_step + 1) % SEGMENT_LENGTH; @@ -769,15 +769,14 @@ uint16_t WS2812FX::mode_chase_flash(void) { * Prim flashes running, followed by random color. */ uint16_t WS2812FX::mode_chase_flash_random(void) { - const static uint8_t flash_count = 4; - uint8_t flash_step = SEGMENT_RUNTIME.counter_mode_call % ((flash_count * 2) + 1); + uint8_t flash_step = SEGMENT_RUNTIME.counter_mode_call % ((FLASH_COUNT * 2) + 1); for(uint16_t i=0; i < SEGMENT_RUNTIME.counter_mode_step; i++) { setPixelColor(SEGMENT.start + i, color_wheel(SEGMENT_RUNTIME.aux_param)); } uint16_t delay = 1 + ((10 * (uint16_t)(255 - SEGMENT.speed)) / SEGMENT_LENGTH); - if(flash_step < (flash_count * 2)) { + if(flash_step < (FLASH_COUNT * 2)) { uint16_t n = SEGMENT_RUNTIME.counter_mode_step; uint16_t m = (SEGMENT_RUNTIME.counter_mode_step + 1) % SEGMENT_LENGTH; if(flash_step % 2 == 0) { diff --git a/wled00/wled00.ino b/wled00/wled00.ino index 824d65d9..51c960cf 100644 --- a/wled00/wled00.ino +++ b/wled00/wled00.ino @@ -248,7 +248,6 @@ bool aOtaEnabled = true; //ArduinoOTA allows easy updates d uint16_t userVar0 = 0, userVar1 = 0; - //internal global variable declarations //color byte col[]{255, 159, 0, 0}; //target RGB(W) color @@ -400,9 +399,9 @@ IPAddress ntpServerIP; unsigned int ntpLocalPort = 2390; #define NTP_PACKET_SIZE 48 -//string temp buffer +//string temp buffer (now stored in stack locally) #define OMAX 2048 -char obuf[OMAX]; +char* obuf; uint16_t olen = 0; String messageHead, messageSub; diff --git a/wled00/wled01_eeprom.ino b/wled00/wled01_eeprom.ino index f319baa7..ff206c38 100644 --- a/wled00/wled01_eeprom.ino +++ b/wled00/wled01_eeprom.ino @@ -574,16 +574,12 @@ void savePreset(byte index) } -String loadMacro(byte index) +char* loadMacro(byte index) { index-=1; - String m=""; + char m[65]; if (index > 15) return m; - for (int i = 1024+64*index; i < 1088+64*index; i++) - { - if (EEPROM.read(i) == 0) break; - m += char(EEPROM.read(i)); - } + readStringFromEEPROM(1024+64*index, m, 64); return m; } diff --git a/wled00/wled02_xml.ino b/wled00/wled02_xml.ino index e1420248..2437ab70 100644 --- a/wled00/wled02_xml.ino +++ b/wled00/wled02_xml.ino @@ -3,9 +3,10 @@ */ //build XML response to HTTP /win API request -void XML_response(AsyncWebServerRequest *request, bool includeTheme) +char* XML_response(AsyncWebServerRequest *request, bool includeTheme) { - olen = 0; + char sbuf[1024]; + olen = 0; obuf = sbuf; oappend(""); oappendi((nightlightActive && nightlightFade) ? briT : bri); oappend(""); @@ -97,7 +98,8 @@ void XML_response(AsyncWebServerRequest *request, bool includeTheme) oappend(""); } oappend(""); - if (request != nullptr) request->send(200, "text/xml", obuf); + if (request != nullptr) request->send(200, "text/xml", sbuf); + return sbuf; } //append a numeric setting to string buffer @@ -155,13 +157,15 @@ void sappends(char stype, char* key, char* val) //get values for settings form in javascript -void getSettingsJS(byte subPage) +char* getSettingsJS(byte subPage) { //0: menu 1: wifi 2: leds 3: ui 4: sync 5: time 6: sec DEBUG_PRINT("settings resp"); DEBUG_PRINTLN(subPage); - olen = 0; obuf[0] = 0; //clear buffer - if (subPage <1 || subPage >6) return; + char sbuf[2048]; + olen = 0; obuf = sbuf; + + if (subPage <1 || subPage >6) return sbuf; if (subPage == 1) { sappends('s',"CS",clientSSID); @@ -345,7 +349,7 @@ void getSettingsJS(byte subPage) for (int i=1;i<17;i++) { sprintf(k+1,"%i",i); - sappends('s',k,(char*)loadMacro(i).c_str()); + sappends('s',k,loadMacro(i)); } sappend('v',"MB",macroBoot); @@ -381,6 +385,7 @@ void getSettingsJS(byte subPage) oappend(") OK\";"); } oappend("}"); + return sbuf; } diff --git a/wled00/wled07_notify.ino b/wled00/wled07_notify.ino index 1cd35f38..d6a6a9ab 100644 --- a/wled00/wled07_notify.ino +++ b/wled00/wled07_notify.ino @@ -135,13 +135,13 @@ void handleNotifications() if (packetSize > UDP_IN_MAXSIZE || packetSize < 3) return; realtimeIP = rgbUdp.remoteIP(); DEBUG_PRINTLN(rgbUdp.remoteIP()); - olen = 0; - rgbUdp.read(obuf, packetSize); + uint8_t lbuf[packetSize]; + rgbUdp.read(lbuf, packetSize); arlsLock(realtimeTimeoutMs); uint16_t id = 0; for (uint16_t i = 0; i < packetSize -2; i += 3) { - setRealtimePixel(id, obuf[i], obuf[i+1], obuf[i+2], 0); + setRealtimePixel(id, lbuf[i], lbuf[i+1], lbuf[i+2], 0); id++; if (id >= ledCount) break; } @@ -153,9 +153,8 @@ void handleNotifications() if (packetSize > UDP_IN_MAXSIZE) return; if(packetSize && notifierUdp.remoteIP() != WiFi.localIP()) //don't process broadcasts we send ourselves { - olen = 0; - notifierUdp.read(obuf, packetSize); - char* udpIn = obuf; + uint8_t udpIn[packetSize]; + notifierUdp.read(udpIn, packetSize); //wled notifier, block if realtime packets active if (udpIn[0] == 0 && !realtimeActive && receiveNotifications) diff --git a/wled00/wled17_mqtt.ino b/wled00/wled17_mqtt.ino index b09a3e58..d8be91e4 100644 --- a/wled00/wled17_mqtt.ino +++ b/wled00/wled17_mqtt.ino @@ -88,10 +88,9 @@ void publishMqtt() strcat(subuf, "/c"); mqtt->publish(subuf, 0, true, s); - XML_response(nullptr, false); strcpy(subuf, mqttDeviceTopic); strcat(subuf, "/v"); - mqtt->publish(subuf, 0, true, obuf); + mqtt->publish(subuf, 0, true, XML_response(nullptr, false)); } diff --git a/wled00/wled18_server.ino b/wled00/wled18_server.ino index 58667001..efccbdde 100644 --- a/wled00/wled18_server.ino +++ b/wled00/wled18_server.ino @@ -266,6 +266,8 @@ String msgProcessor(const String& var) void serveMessage(AsyncWebServerRequest* request, uint16_t code, String headl, String subl="", byte optionT=255) { + char buf[512]; + obuf = buf; olen = 0; getCSSColors(); messageHead = headl; @@ -278,7 +280,11 @@ void serveMessage(AsyncWebServerRequest* request, uint16_t code, String headl, S String settingsProcessor(const String& var) { - if (var == "CSS") return String(obuf); + if (var == "CSS") { + char* buf = getSettingsJS(optionType); + getCSSColors(); + return buf; + } if (var == "SCSS") return String(PAGE_settingsCss); return String(); } @@ -306,10 +312,8 @@ void serveSettings(AsyncWebServerRequest* request) #ifdef WLED_DISABLE_MOBILE_UI //disable welcome page if not enough storage if (subPage == 255) {serveIndex(request); return;} #endif - - getSettingsJS(subPage); - getCSSColors(); + optionType = subPage; switch (subPage) {