Rework logic

This commit is contained in:
jpirnay
2026-05-06 21:38:00 +02:00
parent b0abcb3894
commit b8f2ecf23d
2 changed files with 16 additions and 13 deletions
+14 -11
View File
@@ -254,19 +254,26 @@ void HalGPIO::startDeepSleep() {
} }
void HalGPIO::verifyPowerButtonWakeup(uint16_t requiredDurationMs, bool shortPressAllowed) { void HalGPIO::verifyPowerButtonWakeup(uint16_t requiredDurationMs, bool shortPressAllowed) {
// The wakeup reason was already confirmed as a power button press before this is called,
// so we know a real press occurred. When short presses are allowed, nothing more to verify.
if (shortPressAllowed) { if (shortPressAllowed) {
LOG_DBG("GPIO", "verifyPowerButtonWakeup: shortPressAllowed, skipping verification"); LOG_DBG("GPIO", "verifyPowerButtonWakeup: shortPressAllowed, skipping hold verification");
return; return;
} }
// Calibrate: subtract boot time already elapsed, assuming button held since boot
// Calibrate: subtract boot time already elapsed, assuming button held since boot.
// Never collapse to less than BOUNCE_TOLERANCE_MS so the hold loop always has time to
// sample the button and detect a release (early release = unintentional tap).
constexpr unsigned long BOUNCE_TOLERANCE_MS = 100;
const uint16_t calibration = millis(); const uint16_t calibration = millis();
const uint16_t calibratedDuration = (calibration < requiredDurationMs) ? (requiredDurationMs - calibration) : 1; const uint16_t calibratedDuration =
(calibration < requiredDurationMs) ? (requiredDurationMs - calibration) : BOUNCE_TOLERANCE_MS;
LOG_DBG("GPIO", "verifyPowerButtonWakeup: requiredMs=%u, calibration=%u, calibratedMs=%u", requiredDurationMs, LOG_DBG("GPIO", "verifyPowerButtonWakeup: requiredMs=%u, calibration=%u, calibratedMs=%u", requiredDurationMs,
calibration, calibratedDuration); calibration, calibratedDuration);
const auto start = millis(); const auto start = millis();
inputMgr.update(); inputMgr.update();
// inputMgr.isPressed() may take up to ~500ms to return correct state // inputMgr.isPressed() may take up to ~500ms to return correct state after boot
while (!inputMgr.isPressed(BTN_POWER) && millis() - start < 1000) { while (!inputMgr.isPressed(BTN_POWER) && millis() - start < 1000) {
delay(10); delay(10);
inputMgr.update(); inputMgr.update();
@@ -275,9 +282,8 @@ void HalGPIO::verifyPowerButtonWakeup(uint16_t requiredDurationMs, bool shortPre
inputMgr.isPressed(BTN_POWER), digitalRead(InputManager::POWER_BUTTON_PIN) == LOW); inputMgr.isPressed(BTN_POWER), digitalRead(InputManager::POWER_BUTTON_PIN) == LOW);
if (inputMgr.isPressed(BTN_POWER)) { if (inputMgr.isPressed(BTN_POWER)) {
// Use wall-clock elapsed time instead of getHeldTime() which resets on bounce. // Monitor the hold for calibratedDuration, tolerating brief bounces up to BOUNCE_TOLERANCE_MS.
// Tolerate brief release gaps (bouncing switch) up to BOUNCE_TOLERANCE_MS. // Early release beyond the bounce window means an unintentional tap — go back to sleep.
constexpr unsigned long BOUNCE_TOLERANCE_MS = 100;
unsigned long lastSeenPressed = millis(); unsigned long lastSeenPressed = millis();
const auto holdStart = millis(); const auto holdStart = millis();
unsigned long bounceCount = 0; unsigned long bounceCount = 0;
@@ -291,16 +297,13 @@ void HalGPIO::verifyPowerButtonWakeup(uint16_t requiredDurationMs, bool shortPre
} }
lastSeenPressed = millis(); lastSeenPressed = millis();
} else if (millis() - lastSeenPressed >= BOUNCE_TOLERANCE_MS) { } else if (millis() - lastSeenPressed >= BOUNCE_TOLERANCE_MS) {
// Button released for longer than bounce tolerance — truly released LOG_DBG("GPIO", "verifyPowerButtonWakeup: released early after %lu ms (bounces=%lu), going to sleep",
LOG_DBG("GPIO",
"verifyPowerButtonWakeup: released during hold check after %lu ms (bounces=%lu), going to sleep",
millis() - holdStart, bounceCount); millis() - holdStart, bounceCount);
startDeepSleep(); startDeepSleep();
} }
} }
LOG_DBG("GPIO", "verifyPowerButtonWakeup: hold verified after %lu ms (bounces=%lu), proceeding with boot", LOG_DBG("GPIO", "verifyPowerButtonWakeup: hold verified after %lu ms (bounces=%lu), proceeding with boot",
millis() - holdStart, bounceCount); millis() - holdStart, bounceCount);
// Held long enough (tolerating brief bounces) — proceed with boot
} else { } else {
LOG_DBG("GPIO", "verifyPowerButtonWakeup: button not pressed after 1s wait, going to sleep"); LOG_DBG("GPIO", "verifyPowerButtonWakeup: button not pressed after 1s wait, going to sleep");
startDeepSleep(); startDeepSleep();
+2 -2
View File
@@ -511,8 +511,8 @@ void loop() {
activityManager.goHome(); activityManager.goHome();
break; break;
case BA::BTN_SLEEP: case BA::BTN_SLEEP:
activityManager.goToSleep(); enterDeepSleep();
break; return; // enterDeepSleep() never returns, but return here to stop processing
case BA::BTN_FORCE_REFRESH: { case BA::BTN_FORCE_REFRESH: {
RenderLock lock; RenderLock lock;
renderer.displayBuffer(HalDisplay::HALF_REFRESH); renderer.displayBuffer(HalDisplay::HALF_REFRESH);