From 422733c463446ed97d6196836c2feec104f004fa Mon Sep 17 00:00:00 2001 From: Dirk Mueller Date: Tue, 21 Jan 2020 23:32:41 +0100 Subject: [PATCH 1/2] Code size optimisation of ESP.getResetReason() doing if/else snakes for something that is a switch/case is wasteful, as it repeatedly evaluates the same if() condition. Also repeating strcpy_P is adding code bloat. This simplification reduces size from 111 to 41 bytes. --- cores/esp8266/Esp.cpp | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/cores/esp8266/Esp.cpp b/cores/esp8266/Esp.cpp index f59513be40..031d95fc09 100644 --- a/cores/esp8266/Esp.cpp +++ b/cores/esp8266/Esp.cpp @@ -468,23 +468,24 @@ bool EspClass::checkFlashCRC() { String EspClass::getResetReason(void) { - char buff[32]; - if (resetInfo.reason == REASON_DEFAULT_RST) { // normal startup by power on - strcpy_P(buff, PSTR("Power on")); - } else if (resetInfo.reason == REASON_WDT_RST) { // hardware watch dog reset - strcpy_P(buff, PSTR("Hardware Watchdog")); - } else if (resetInfo.reason == REASON_EXCEPTION_RST) { // exception reset, GPIO status won’t change - strcpy_P(buff, PSTR("Exception")); - } else if (resetInfo.reason == REASON_SOFT_WDT_RST) { // software watch dog reset, GPIO status won’t change - strcpy_P(buff, PSTR("Software Watchdog")); - } else if (resetInfo.reason == REASON_SOFT_RESTART) { // software restart ,system_restart , GPIO status won’t change - strcpy_P(buff, PSTR("Software/System restart")); - } else if (resetInfo.reason == REASON_DEEP_SLEEP_AWAKE) { // wake up from deep-sleep - strcpy_P(buff, PSTR("Deep-Sleep Wake")); - } else if (resetInfo.reason == REASON_EXT_SYS_RST) { // external system reset - strcpy_P(buff, PSTR("External System")); - } else { - strcpy_P(buff, PSTR("Unknown")); + const __FlashStringHelper* buff; + + switch(resetInfo.reason) { + // normal startup by power on + case REASON_DEFAULT_RST: buff = F("Power On"); break; + // hardware watch dog reset + case REASON_WDT_RST: buff = F("Hardware Watchdog"); break; + // exception reset, GPIO status won’t change + case REASON_EXCEPTION_RST: buff = F("Exception"); break; + // software watch dog reset, GPIO status won’t change + case REASON_SOFT_WDT_RST: buff = F("Software Watchdog"); break; + // software restart ,system_restart , GPIO status won’t change + case REASON_SOFT_RESTART: buff = F("Software/System restart"); break; + // wake up from deep-sleep + case REASON_DEEP_SLEEP_AWAKE: buff = F("Deep-Sleep Wake"); break; + // // external system reset + case REASON_EXT_SYS_RST: buff = F("External System"); break; + default: buff = F("Unknown"); } return String(buff); } From 0db0347a134b3df350090b509b364fff9c818791 Mon Sep 17 00:00:00 2001 From: Dirk Mueller Date: Thu, 30 Jan 2020 21:06:50 +0100 Subject: [PATCH 2/2] add break statement also to default case --- cores/esp8266/Esp.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp8266/Esp.cpp b/cores/esp8266/Esp.cpp index 031d95fc09..a0e525f2bb 100644 --- a/cores/esp8266/Esp.cpp +++ b/cores/esp8266/Esp.cpp @@ -485,7 +485,7 @@ String EspClass::getResetReason(void) { case REASON_DEEP_SLEEP_AWAKE: buff = F("Deep-Sleep Wake"); break; // // external system reset case REASON_EXT_SYS_RST: buff = F("External System"); break; - default: buff = F("Unknown"); + default: buff = F("Unknown"); break; } return String(buff); }