From a2141803f11d91a5654c7a7065b552302cac527c Mon Sep 17 00:00:00 2001 From: Dirk Mueller Date: Thu, 30 Jan 2020 22:07:34 +0100 Subject: [PATCH] Code size optimisation of ESP.getResetReason() (#7029) * 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. * add break statement also to default case --- 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 f59513be4..a0e525f2b 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"); break; } return String(buff); }