fix: ensure proper reset behavior after flashing by verifying wakeup conditions

This commit is contained in:
Arthur Tazhitdinov 2026-01-20 04:25:33 +05:00
parent 6d68466891
commit 676aef19e7

View File

@ -174,7 +174,7 @@ void verifyWakeupLongPress() {
delay(10);
inputManager.update();
} while (inputManager.isPressed(InputManager::BTN_POWER) && inputManager.getHeldTime() < calibratedPressDuration);
abort = inputManager.getHeldTime() < calibratedPressDuration;
abort = false;
} else {
abort = true;
}
@ -263,13 +263,30 @@ void setupDisplayAndFonts() {
Serial.printf("[%lu] [ ] Fonts setup\n", millis());
}
bool isUsbConnected() {
// U0RXD/GPIO20 reads HIGH when USB is connected
return digitalRead(UART0_RXD) == HIGH;
}
bool isWakeupAfterFlashing() {
const auto wakeupCause = esp_sleep_get_wakeup_cause();
const auto resetReason = esp_reset_reason();
return isUsbConnected() && (wakeupCause == ESP_SLEEP_WAKEUP_UNDEFINED) && (resetReason == ESP_RST_UNKNOWN);
}
void setup() {
t1 = millis();
// Only start serial if USB connected
pinMode(UART0_RXD, INPUT);
if (digitalRead(UART0_RXD) == HIGH) {
if (isUsbConnected()) {
Serial.begin(115200);
// Wait up to 3 seconds for Serial to be ready to catch early logs
unsigned long start = millis();
while (!Serial && (millis() - start) < 3000) {
delay(10);
}
}
inputManager.begin();
@ -292,8 +309,10 @@ void setup() {
SETTINGS.loadFromFile();
KOREADER_STORE.loadFromFile();
// verify power button press duration after we've read settings.
verifyWakeupLongPress();
if (!isWakeupAfterFlashing()) {
// If we woke up from sleep, verify long press
verifyWakeupLongPress();
}
// First serial output only here to avoid timing inconsistencies for power button press duration verification
Serial.printf("[%lu] [ ] Starting CrossPoint version " CROSSPOINT_VERSION "\n", millis());