Skip to content

Commit a214180

Browse files
authored
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
1 parent 9e9515b commit a214180

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

cores/esp8266/Esp.cpp

+18-17
Original file line numberDiff line numberDiff line change
@@ -468,23 +468,24 @@ bool EspClass::checkFlashCRC() {
468468

469469

470470
String EspClass::getResetReason(void) {
471-
char buff[32];
472-
if (resetInfo.reason == REASON_DEFAULT_RST) { // normal startup by power on
473-
strcpy_P(buff, PSTR("Power on"));
474-
} else if (resetInfo.reason == REASON_WDT_RST) { // hardware watch dog reset
475-
strcpy_P(buff, PSTR("Hardware Watchdog"));
476-
} else if (resetInfo.reason == REASON_EXCEPTION_RST) { // exception reset, GPIO status won’t change
477-
strcpy_P(buff, PSTR("Exception"));
478-
} else if (resetInfo.reason == REASON_SOFT_WDT_RST) { // software watch dog reset, GPIO status won’t change
479-
strcpy_P(buff, PSTR("Software Watchdog"));
480-
} else if (resetInfo.reason == REASON_SOFT_RESTART) { // software restart ,system_restart , GPIO status won’t change
481-
strcpy_P(buff, PSTR("Software/System restart"));
482-
} else if (resetInfo.reason == REASON_DEEP_SLEEP_AWAKE) { // wake up from deep-sleep
483-
strcpy_P(buff, PSTR("Deep-Sleep Wake"));
484-
} else if (resetInfo.reason == REASON_EXT_SYS_RST) { // external system reset
485-
strcpy_P(buff, PSTR("External System"));
486-
} else {
487-
strcpy_P(buff, PSTR("Unknown"));
471+
const __FlashStringHelper* buff;
472+
473+
switch(resetInfo.reason) {
474+
// normal startup by power on
475+
case REASON_DEFAULT_RST: buff = F("Power On"); break;
476+
// hardware watch dog reset
477+
case REASON_WDT_RST: buff = F("Hardware Watchdog"); break;
478+
// exception reset, GPIO status won’t change
479+
case REASON_EXCEPTION_RST: buff = F("Exception"); break;
480+
// software watch dog reset, GPIO status won’t change
481+
case REASON_SOFT_WDT_RST: buff = F("Software Watchdog"); break;
482+
// software restart ,system_restart , GPIO status won’t change
483+
case REASON_SOFT_RESTART: buff = F("Software/System restart"); break;
484+
// wake up from deep-sleep
485+
case REASON_DEEP_SLEEP_AWAKE: buff = F("Deep-Sleep Wake"); break;
486+
// // external system reset
487+
case REASON_EXT_SYS_RST: buff = F("External System"); break;
488+
default: buff = F("Unknown"); break;
488489
}
489490
return String(buff);
490491
}

0 commit comments

Comments
 (0)