From fde53117d630e7fbb38fbfc401ea037ef5b3e8e7 Mon Sep 17 00:00:00 2001
From: Theo Arends <11044339+arendst@users.noreply.github.com>
Date: Sat, 27 Jul 2019 18:37:56 +0200
Subject: [PATCH] Space saving code optimization
Space saving code optimization
---
sonoff/sonoff.ino | 2 +-
sonoff/support.ino | 16 +++++----
sonoff/xdrv_01_webserver.ino | 66 +++++++++++++++++++-----------------
3 files changed, 44 insertions(+), 40 deletions(-)
diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino
index 325690231..ac92c4519 100755
--- a/sonoff/sonoff.ino
+++ b/sonoff/sonoff.ino
@@ -108,6 +108,7 @@ int blinks = 201; // Number of LED blinks
uint32_t uptime = 0; // Counting every second until 4294967295 = 130 year
uint32_t loop_load_avg = 0; // Indicative loop load average
uint32_t global_update = 0; // Timestamp of last global temperature and humidity update
+uint32_t web_log_index = 1; // Index in Web log buffer (should never be 0)
float global_temperature = 9999; // Provide a global temperature to be used by some sensors
float global_humidity = 0; // Provide a global humidity to be used by some sensors
float global_pressure = 0; // Provide a global pressure to be used by some sensors
@@ -137,7 +138,6 @@ uint8_t energy_flg = 0; // Energy monitor configured
uint8_t light_type = 0; // Light types
uint8_t serial_in_byte; // Received byte
uint8_t ota_retry_counter = OTA_ATTEMPTS; // OTA retry counter
-uint8_t web_log_index = 1; // Index in Web log buffer (should never be 0)
uint8_t devices_present = 0; // Max number of devices supported
uint8_t seriallog_level; // Current copy of Settings.seriallog_level
uint8_t syslog_level; // Current copy of Settings.syslog_level
diff --git a/sonoff/support.ino b/sonoff/support.ino
index c0a1ebda2..f4b0d0b4b 100644
--- a/sonoff/support.ino
+++ b/sonoff/support.ino
@@ -502,16 +502,16 @@ bool ParseIp(uint32_t* addr, const char* str)
return (3 == i);
}
-void MakeValidMqtt(uint8_t option, char* str)
+void MakeValidMqtt(uint32_t option, char* str)
{
// option 0 = replace by underscore
// option 1 = delete character
- uint16_t i = 0;
+ uint32_t i = 0;
while (str[i] > 0) {
// if ((str[i] == '/') || (str[i] == '+') || (str[i] == '#') || (str[i] == ' ')) {
if ((str[i] == '+') || (str[i] == '#') || (str[i] == ' ')) {
if (option) {
- uint16_t j = i;
+ uint32_t j = i;
while (str[j] > 0) {
str[j] = str[j +1];
j++;
@@ -529,7 +529,7 @@ void MakeValidMqtt(uint8_t option, char* str)
bool NewerVersion(char* version_str)
{
uint32_t version = 0;
- uint8_t i = 0;
+ uint32_t i = 0;
char *str_ptr;
char* version_dup = strdup(version_str); // Duplicate the version_str as strtok_r will modify it.
@@ -676,7 +676,7 @@ uint32_t RoundSqrtInt(uint32_t num)
return s / 2;
}
-char* GetTextIndexed(char* destination, size_t destination_size, uint16_t index, const char* haystack)
+char* GetTextIndexed(char* destination, size_t destination_size, uint32_t index, const char* haystack)
{
// Returns empty string if not found
// Returns text of found
@@ -1414,7 +1414,7 @@ void SetSyslog(uint8_t loglevel)
}
#ifdef USE_WEBSERVER
-void GetLog(uint8_t idx, char** entry_pp, size_t* len_p)
+void GetLog(uint32_t idx, char** entry_pp, size_t* len_p)
{
char* entry_p = nullptr;
size_t len = 0;
@@ -1422,7 +1422,7 @@ void GetLog(uint8_t idx, char** entry_pp, size_t* len_p)
if (idx) {
char* it = web_log;
do {
- uint8_t cur_idx = *it;
+ uint32_t cur_idx = *it;
it++;
size_t tmp = strchrspn(it, '\1');
tmp++; // Skip terminating '\1'
@@ -1477,6 +1477,7 @@ void AddLog(uint8_t loglevel)
if (Settings.webserver && (loglevel <= Settings.weblog_level)) {
// Delimited, zero-terminated buffer of log lines.
// Each entry has this format: [index][log data]['\1']
+ web_log_index &= 0xFF;
if (!web_log_index) web_log_index++; // Index 0 is not allowed as it is the end of char string
while (web_log_index == web_log[0] || // If log already holds the next index, remove it
strlen(web_log) + strlen(log_data) + 13 > WEB_LOG_SIZE) // 13 = web_log_index + mxtime + '\1' + '\0'
@@ -1488,6 +1489,7 @@ void AddLog(uint8_t loglevel)
memmove(web_log, it, WEB_LOG_SIZE -(it-web_log)); // Move buffer forward to remove oldest log line
}
snprintf_P(web_log, sizeof(web_log), PSTR("%s%c%s%s\1"), web_log, web_log_index++, mxtime, log_data);
+ web_log_index &= 0xFF;
if (!web_log_index) web_log_index++; // Index 0 is not allowed as it is the end of char string
}
#endif // USE_WEBSERVER
diff --git a/sonoff/xdrv_01_webserver.ino b/sonoff/xdrv_01_webserver.ino
index 7b57e9b4d..bc4cf9682 100644
--- a/sonoff/xdrv_01_webserver.ino
+++ b/sonoff/xdrv_01_webserver.ino
@@ -797,7 +797,7 @@ void WSContentSendStyle(void)
WSContentSendStyle_P(nullptr);
}
-void WSContentButton(uint8_t title_index)
+void WSContentButton(uint32_t title_index)
{
char action[4];
char title[100]; // Large to accomodate UTF-16 as used by Russian
@@ -816,7 +816,7 @@ void WSContentButton(uint8_t title_index)
}
}
-void WSContentSpaceButton(uint8_t title_index)
+void WSContentSpaceButton(uint32_t title_index)
{
WSContentSend_P(PSTR("
")); // 5px padding
WSContentButton(title_index);
@@ -842,7 +842,7 @@ void WSContentStop(void)
/*********************************************************************************************/
-void WebRestart(uint8_t type)
+void WebRestart(uint32_t type)
{
// type 0 = restart
// type 1 = restart after config change
@@ -963,7 +963,7 @@ void HandleRoot(void)
if (SONOFF_BRIDGE == my_module_type) {
WSContentSend_P(HTTP_TABLE100);
WSContentSend_P(PSTR(""));
- uint8_t idx = 0;
+ uint32_t idx = 0;
for (uint32_t i = 0; i < 4; i++) {
if (idx > 0) { WSContentSend_P(PSTR("
")); }
for (uint32_t j = 0; j < 4; j++) {
@@ -1010,7 +1010,7 @@ bool HandleRootStatusRefresh(void)
WebGetArg("o", tmp, sizeof(tmp)); // 1 - 16 Device number for button Toggle or Fanspeed
if (strlen(tmp)) {
ShowWebSource(SRC_WEBGUI);
- uint8_t device = atoi(tmp);
+ uint32_t device = atoi(tmp);
#ifdef USE_SONOFF_IFAN
if (IsModuleIfan()) {
if (device < 2) {
@@ -1049,11 +1049,11 @@ bool HandleRootStatusRefresh(void)
if (devices_present) {
WSContentSend_P(PSTR("{t}
"));
- uint8_t fsize = (devices_present < 5) ? 70 - (devices_present * 8) : 32;
+ uint32_t fsize = (devices_present < 5) ? 70 - (devices_present * 8) : 32;
#ifdef USE_SONOFF_IFAN
if (IsModuleIfan()) {
WSContentSend_P(HTTP_DEVICE_STATE, 36, (bitRead(power, 0)) ? "bold" : "normal", 54, GetStateText(bitRead(power, 0)));
- uint8_t fanspeed = GetFanspeed();
+ uint32_t fanspeed = GetFanspeed();
snprintf_P(svalue, sizeof(svalue), PSTR("%d"), fanspeed);
WSContentSend_P(HTTP_DEVICE_STATE, 64, (fanspeed) ? "bold" : "normal", 54, (fanspeed) ? svalue : GetStateText(0));
} else {
@@ -1120,7 +1120,7 @@ void HandleTemplateConfiguration(void)
if (WebServer->hasArg("m")) {
WSContentBegin(200, CT_PLAIN);
for (uint32_t i = 0; i < sizeof(kModuleNiceList); i++) { // "}2'%d'>%s (%d)}3" - "}2'0'>Sonoff Basic (1)}3"
- uint8_t midx = pgm_read_byte(kModuleNiceList + i);
+ uint32_t midx = pgm_read_byte(kModuleNiceList + i);
WSContentSend_P(HTTP_MODULE_TEMPLATE_REPLACE, midx, AnyModuleName(midx).c_str(), midx +1);
}
WSContentEnd();
@@ -1129,8 +1129,8 @@ void HandleTemplateConfiguration(void)
WebGetArg("t", stemp, sizeof(stemp)); // 0 - 69 Template number
if (strlen(stemp)) {
- uint8_t module = atoi(stemp);
- uint8_t module_save = Settings.module;
+ uint32_t module = atoi(stemp);
+ uint32_t module_save = Settings.module;
Settings.module = module;
myio cmodule;
ModuleGpios(&cmodule);
@@ -1143,7 +1143,7 @@ void HandleTemplateConfiguration(void)
if (1 == i) {
WSContentSend_P(HTTP_MODULE_TEMPLATE_REPLACE, 255, D_SENSOR_USER, 255); // }2'255'>User (255)}3
}
- uint8_t midx = pgm_read_byte(kGpioNiceList + i);
+ uint32_t midx = pgm_read_byte(kGpioNiceList + i);
WSContentSend_P(HTTP_MODULE_TEMPLATE_REPLACE, midx, GetTextIndexed(stemp, sizeof(stemp), midx, kSensorNames), midx);
}
WSContentSend_P(PSTR("}1")); // Field separator
@@ -1205,7 +1205,7 @@ void TemplateSaveSettings(void)
WebGetArg("s1", tmp, sizeof(tmp)); // NAME
snprintf_P(svalue, sizeof(svalue), PSTR(D_CMND_TEMPLATE " {\"" D_JSON_NAME "\":\"%s\",\"" D_JSON_GPIO "\":["), tmp);
- uint8_t j = 0;
+ uint32_t j = 0;
for (uint32_t i = 0; i < sizeof(Settings.user_template.gp); i++) {
if (6 == i) { j = 9; }
if (8 == i) { j = 12; }
@@ -1217,14 +1217,14 @@ void TemplateSaveSettings(void)
}
WebGetArg("g17", tmp, sizeof(tmp)); // FLAG - ADC0
- uint8_t flag = atoi(tmp);
+ uint32_t flag = atoi(tmp);
for (uint32_t i = 0; i < GPIO_FLAG_USED; i++) {
snprintf_P(webindex, sizeof(webindex), PSTR("c%d"), i);
- uint8_t state = WebServer->hasArg(webindex) << i +4; // FLAG
+ uint32_t state = WebServer->hasArg(webindex) << i +4; // FLAG
flag += state;
}
WebGetArg("g99", tmp, sizeof(tmp)); // BASE
- uint8_t base = atoi(tmp) +1;
+ uint32_t base = atoi(tmp) +1;
snprintf_P(svalue, sizeof(svalue), PSTR("%s],\"" D_JSON_FLAG "\":%d,\"" D_JSON_BASE "\":%d}"), svalue, flag, base);
ExecuteWebCommand(svalue, SRC_WEBGUI);
@@ -1243,13 +1243,13 @@ void HandleModuleConfiguration(void)
}
char stemp[30]; // Sensor name
- uint8_t midx;
+ uint32_t midx;
myio cmodule;
ModuleGpios(&cmodule);
if (WebServer->hasArg("m")) {
WSContentBegin(200, CT_PLAIN);
- uint8_t vidx = 0;
+ uint32_t vidx = 0;
for (uint32_t i = 0; i <= sizeof(kModuleNiceList); i++) { // "}2'%d'>%s (%d)}3" - "}2'255'>UserTemplate (0)}3" - "}2'0'>Sonoff Basic (1)}3"
if (0 == i) {
midx = USER_MODULE;
@@ -1326,7 +1326,7 @@ void ModuleSaveSettings(void)
char webindex[5]; // WebGetArg name
WebGetArg("g99", tmp, sizeof(tmp));
- uint8_t new_module = (!strlen(tmp)) ? MODULE : atoi(tmp);
+ uint32_t new_module = (!strlen(tmp)) ? MODULE : atoi(tmp);
Settings.last_module = Settings.module;
Settings.module = new_module;
SetModuleType();
@@ -1361,7 +1361,7 @@ const char kEscapeCode[] PROGMEM = "&|>|<|"|'";
String HtmlEscape(const String unescaped) {
char escaped[10];
- uint16_t ulen = unescaped.length();
+ size_t ulen = unescaped.length();
String result = "";
for (size_t i = 0; i < ulen; i++) {
char c = unescaped[i];
@@ -1521,7 +1521,7 @@ void HandleLoggingConfiguration(void)
char stemp2[32];
uint8_t dlevel[3] = { LOG_LEVEL_INFO, LOG_LEVEL_INFO, LOG_LEVEL_NONE };
for (uint32_t idx = 0; idx < 3; idx++) {
- uint8_t llevel = (0==idx)?Settings.seriallog_level:(1==idx)?Settings.weblog_level:Settings.syslog_level;
+ uint32_t llevel = (0==idx)?Settings.seriallog_level:(1==idx)?Settings.weblog_level:Settings.syslog_level;
WSContentSend_P(PSTR("%s (%s)