From decd45687f1446f3cd8dff817f4c542ace66c137 Mon Sep 17 00:00:00 2001 From: TD-er Date: Mon, 16 Nov 2020 13:20:45 +0100 Subject: [PATCH] [Cleanup] Save resources by moving static strings to flash --- lib/IRremoteESP8266/src/IRutils.cpp | 2 +- lib/RN2483-Arduino-Library/src/rn2xx3_helper.cpp | 4 ++-- lib/WakeOnLan-1.1.6/src/WakeOnLan.cpp | 6 +++--- lib/ccronexpr/ccronexpr.c | 2 +- src/_C002.ino | 7 ++++--- src/_C004.ino | 2 +- src/_C005.ino | 2 +- src/_C014.ino | 2 +- src/_P042_Candle.ino | 8 ++++---- src/_P073_7DGT.ino | 2 +- src/src/DataStructs/ESPEasy_packed_raw_data.cpp | 2 +- src/src/ESPEasyCore/ESPEasyRules.cpp | 2 +- src/src/Helpers/StringConverter.cpp | 2 +- src/src/Helpers/SystemVariables.cpp | 2 +- src/src/Helpers/_CPlugin_Helper.h | 8 ++++++-- 15 files changed, 29 insertions(+), 24 deletions(-) diff --git a/lib/IRremoteESP8266/src/IRutils.cpp b/lib/IRremoteESP8266/src/IRutils.cpp index beec2fbfb..7256d57de 100644 --- a/lib/IRremoteESP8266/src/IRutils.cpp +++ b/lib/IRremoteESP8266/src/IRutils.cpp @@ -274,7 +274,7 @@ String resultToSourceCode(const decode_results * const results) { /// @deprecated This is only for those that want this legacy format. String resultToTimingInfo(const decode_results * const results) { String output = ""; - String value = ""; + String value; // Reserve some space for the string to reduce heap fragmentation. output.reserve(2048); // 2KB should cover most cases. value.reserve(6); // Max value should be 2^17 = 131072 diff --git a/lib/RN2483-Arduino-Library/src/rn2xx3_helper.cpp b/lib/RN2483-Arduino-Library/src/rn2xx3_helper.cpp index 64b7abc0b..647eba8dc 100644 --- a/lib/RN2483-Arduino-Library/src/rn2xx3_helper.cpp +++ b/lib/RN2483-Arduino-Library/src/rn2xx3_helper.cpp @@ -79,7 +79,7 @@ String rn2xx3_helper::base16encode(const String& input_c) if (input[i] == '\0') { break; } char buffer[3]; - sprintf(buffer, "%02x", static_cast(input[i])); + sprintf_P(buffer, PSTR("%02x"), static_cast(input[i])); output += buffer[0]; output += buffer[1]; } @@ -95,7 +95,7 @@ String rn2xx3_helper::base16encode(const byte *data, uint8_t size) for (unsigned i = 0; i < size; i++) { - sprintf(buffer, "%02X", data[i]); + sprintf_P(buffer, PSTR("%02X"), data[i]); dataToTx += buffer[0]; dataToTx += buffer[1]; } diff --git a/lib/WakeOnLan-1.1.6/src/WakeOnLan.cpp b/lib/WakeOnLan-1.1.6/src/WakeOnLan.cpp index f325349d5..84a14931c 100644 --- a/lib/WakeOnLan-1.1.6/src/WakeOnLan.cpp +++ b/lib/WakeOnLan-1.1.6/src/WakeOnLan.cpp @@ -25,11 +25,11 @@ bool WakeOnLan::stringToArray(uint8_t* _macAddress, const char* _macString) { unsigned int tempMACAddress[6]; if (strlen(_macString) == 12) // FFFFFFFFFFFF - sprintf(macFormat, "%%2x%%2x%%2x%%2x%%2x%%2x"); + sprintf_P(macFormat, PSTR("%%2x%%2x%%2x%%2x%%2x%%2x")); else if (strlen(_macString) == 14) // FFFF-FFFF-FFFF - sprintf(macFormat, "%%2x%%2x%c%%2x%%2x%c%%2x%%2x", _macString[4], _macString[9]); + sprintf_P(macFormat, PSTR("%%2x%%2x%c%%2x%%2x%c%%2x%%2x"), _macString[4], _macString[9]); else if (strlen(_macString) == 17) // FF-FF-FF-FF-FF-FF - sprintf(macFormat, "%%2x%c%%2x%c%%2x%c%%2x%c%%2x%c%%2x", _macString[2], _macString[5], _macString[8], _macString[11], _macString[14]); + sprintf_P(macFormat, PSTR("%%2x%c%%2x%c%%2x%c%%2x%c%%2x%c%%2x"), _macString[2], _macString[5], _macString[8], _macString[11], _macString[14]); else return false; diff --git a/lib/ccronexpr/ccronexpr.c b/lib/ccronexpr/ccronexpr.c index 1933eb144..b342e8713 100644 --- a/lib/ccronexpr/ccronexpr.c +++ b/lib/ccronexpr/ccronexpr.c @@ -545,7 +545,7 @@ static char* to_string(int num) { if (abs(num) >= CRON_MAX_NUM_TO_SRING) return NULL; char* str = (char*) cron_malloc(CRON_NUM_OF_DIGITS(num) + 1); if (!str) return NULL; - int res = sprintf(str, "%d", num); + int res = sprintf_P(str, PSTR("%d"), num); if (res < 0) { cron_free(str); return NULL; diff --git a/src/_C002.ino b/src/_C002.ino index 23c423e8d..d7b3c1ee6 100644 --- a/src/_C002.ino +++ b/src/_C002.ino @@ -12,7 +12,9 @@ #define CPLUGIN_NAME_002 "Domoticz MQTT" #include "src/Commands/InternalCommands.h" - +#include "src/ESPEasyCore/ESPEasyRules.h" +#include "src/Globals/Settings.h" +#include "src/Helpers/PeriodicalActions.h" #include "src/Helpers/StringParser.h" #include @@ -102,7 +104,7 @@ bool CPlugin_002(CPlugin::Function function, struct EventStruct *event, String& // We need the index of the controller we are: 0...CONTROLLER_MAX if (Settings.TaskDeviceEnabled[x] && (Settings.TaskDeviceID[ControllerID][x] == idx)) // get idx for our controller index { - String action = ""; + String action; switch (Settings.TaskDeviceNumber[x]) { case 1: // temp solution, if input switch, update state @@ -115,7 +117,6 @@ bool CPlugin_002(CPlugin::Function function, struct EventStruct *event, String& } case 29: // temp solution, if plugin 029, set gpio { - action = ""; int baseVar = x * VARS_PER_TASK; if (strcasecmp_P(switchtype, PSTR("dimmer")) == 0) diff --git a/src/_C004.ino b/src/_C004.ino index 6afa34ceb..1971cacc3 100644 --- a/src/_C004.ino +++ b/src/_C004.ino @@ -108,7 +108,7 @@ bool do_process_c004_delay_queue(int controller_number, const C004_queue_element { postDataStr += F("&field"); postDataStr += element.idx + x; - postDataStr += "="; + postDataStr += '='; postDataStr += formatUserVarNoCheck(element.TaskIndex, x); } } diff --git a/src/_C005.ino b/src/_C005.ino index 29f162fb5..43271e7e1 100644 --- a/src/_C005.ino +++ b/src/_C005.ino @@ -138,7 +138,7 @@ bool CPlugin_005(CPlugin::Function function, struct EventStruct *event, String& String tmppubname = pubname; tmppubname.replace(F("%valname%"), ExtraTaskSettings.TaskDeviceValueNames[x]); - String value = ""; + String value; // Small optimization so we don't try to copy potentially large strings if (event->sensorType == Sensor_VType::SENSOR_TYPE_STRING) { MQTTpublish(event->ControllerIndex, tmppubname.c_str(), event->String2.c_str(), mqtt_retainFlag); diff --git a/src/_C014.ino b/src/_C014.ino index e9f3f0cc1..ee9900b37 100644 --- a/src/_C014.ino +++ b/src/_C014.ino @@ -700,7 +700,7 @@ bool CPlugin_014(CPlugin::Function function, struct EventStruct *event, String& parseControllerVariables(pubname, event, false); LoadTaskSettings(event->TaskIndex); - String value = ""; + String value; byte valueCount = getValueCountForTask(event->TaskIndex); for (byte x = 0; x < valueCount; x++) { diff --git a/src/_P042_Candle.ino b/src/_P042_Candle.ino index 97325f556..060c4d24e 100644 --- a/src/_P042_Candle.ino +++ b/src/_P042_Candle.ino @@ -188,10 +188,10 @@ boolean Plugin_042(byte function, struct EventStruct *event, String& string) // Color Selection char hexvalue[7] = {0}; - sprintf(hexvalue, "%02X%02X%02X", // Create Hex value for color - PCONFIG(0), - PCONFIG(1), - PCONFIG(2)); + sprintf_P(hexvalue, F("%02X%02X%02X"), // Create Hex value for color + PCONFIG(0), + PCONFIG(1), + PCONFIG(2)); // http://jscolor.com/examples/ addHtml(F("Color: