diff --git a/src/main.cpp b/src/main.cpp index 43b0f30..489673b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -212,6 +212,30 @@ void setup() { pinMode(POWER_OFF_PIN, INPUT); } +void checkForPowerOffRequest() { + static unsigned long powerOffButtonPressed_ts = 0; + static bool powerOffRequested = false; + + if (!digitalRead(POWER_OFF_PIN)) { // power off pressed + if (!powerOffRequested) { + Serial.println("Switching off? Keep button for 2 seconds to turn off."); + powerOffRequested = true; + powerOffButtonPressed_ts = millis(); + } + if (millis() - powerOffButtonPressed_ts > 2000) { + // pressed for longer than 2 seconds, now turn off + ESP.deepSleep(ESP.deepSleepMax()); + delay(200); + } + } else { + if (powerOffRequested) { + powerOffRequested = false; + Serial.println("Power off cancelled."); + powerOffButtonPressed_ts = 0; + } + } +} + bool requestToRegisterSent = false; void loop(void) @@ -226,23 +250,7 @@ void loop(void) return; } - static unsigned long powerOffButtonPressed_ts = 0; - static bool powerOffMessagePrinted = false; - if (!digitalRead(POWER_OFF_PIN)) { - if (!powerOffMessagePrinted) { - Serial.println("Switching off? Keep button for 2 seconds to turn off."); - powerOffMessagePrinted = true; - } - if (millis() - powerOffButtonPressed_ts > 2000) { - // pressed for longer than 2 seconds, now turn off - ESP.deepSleep(ESP.deepSleepMax()); - delay(200); - } - } else { - powerOffButtonPressed_ts = millis(); - if (powerOffMessagePrinted) Serial.println("Power off cancelled."); - powerOffMessagePrinted = false; - } + checkForPowerOffRequest(); #if defined(AUTOSTART_CLOCK_AFTER_ms) #if AUTOSTART_CLOCK_AFTER_ms > 0