MCPcopy Create free account
hub / github.com/crosspoint-reader/crosspoint-reader / verifyPowerButtonDuration

Function verifyPowerButtonDuration

src/main.cpp:164–204  ·  view source on GitHub ↗

Verify power button press duration on wake-up from deep sleep Pre-condition: isWakeupByPowerButton() == true

Source from the content-addressed store, hash-verified

162// Verify power button press duration on wake-up from deep sleep
163// Pre-condition: isWakeupByPowerButton() == true
164void verifyPowerButtonDuration() {
165 if (SETTINGS.shortPwrBtn == CrossPointSettings::SHORT_PWRBTN::SLEEP) {
166 // Fast path for short press
167 // Needed because inputManager.isPressed() may take up to ~500ms to return the correct state
168 return;
169 }
170
171 // Give the user up to 1000ms to start holding the power button, and must hold for SETTINGS.getPowerButtonDuration()
172 const auto start = millis();
173 bool abort = false;
174 // Subtract the current time, because inputManager only starts counting the HeldTime from the first update()
175 // This way, we remove the time we already took to reach here from the duration,
176 // assuming the button was held until now from millis()==0 (i.e. device start time).
177 const uint16_t calibration = start;
178 const uint16_t calibratedPressDuration =
179 (calibration < SETTINGS.getPowerButtonDuration()) ? SETTINGS.getPowerButtonDuration() - calibration : 1;
180
181 gpio.update();
182 // Needed because inputManager.isPressed() may take up to ~500ms to return the correct state
183 while (!gpio.isPressed(HalGPIO::BTN_POWER) && millis() - start < 1000) {
184 delay(10); // only wait 10ms each iteration to not delay too much in case of short configured duration.
185 gpio.update();
186 }
187
188 t2 = millis();
189 if (gpio.isPressed(HalGPIO::BTN_POWER)) {
190 do {
191 delay(10);
192 gpio.update();
193 } while (gpio.isPressed(HalGPIO::BTN_POWER) && gpio.getPowerButtonHeldTime() < calibratedPressDuration);
194 abort = gpio.getPowerButtonHeldTime() < calibratedPressDuration;
195 } else {
196 abort = true;
197 }
198
199 if (abort) {
200 // Button released too early. Returning to sleep.
201 // IMPORTANT: Re-arm the wakeup trigger before sleeping again
202 powerManager.startDeepSleep(gpio);
203 }
204}
205void waitForPowerRelease() {
206 gpio.update();
207 while (gpio.isPressed(HalGPIO::BTN_POWER)) {

Callers

nothing calls this directly

Calls 5

updateMethod · 0.45
isPressedMethod · 0.45
startDeepSleepMethod · 0.45

Tested by

no test coverage detected