Fix sleep bug

This commit is contained in:
Christoph Hagen 2023-06-05 13:37:46 +02:00
parent 7f3ec0841e
commit 3a54de3729

View File

@ -15,7 +15,7 @@ RTC_DATA_ATTR uint32_t nextTimeToMeasureTemperatureSeconds = 0;
// Indicate the time until the device should stay awake // Indicate the time until the device should stay awake
// Updated when button is pressed // Updated when button is pressed
uint32_t timeUntilSleep = 0; uint32_t sleepStartAfterButtonPressOrConnection = 0;
/** /**
* @brief Check if enough time has passed for the next temperature measurement * @brief Check if enough time has passed for the next temperature measurement
@ -50,7 +50,7 @@ uint32_t secondsUntilNextTemperatureMeasurement() {
void deepSleepUntilNextTemperatureMeasurement() { void deepSleepUntilNextTemperatureMeasurement() {
uint32_t seconds = secondsUntilNextTemperatureMeasurement(); uint32_t seconds = secondsUntilNextTemperatureMeasurement();
if (seconds = 0) { if (seconds == 0) {
// The time until next measurement is too short, // The time until next measurement is too short,
// so don't sleep and just wait a little // so don't sleep and just wait a little
// May run the loop multiple times until the time is elapsed // May run the loop multiple times until the time is elapsed
@ -75,14 +75,14 @@ bool wakeupButtonIsPressed() {
} }
void updateStayAwakeTime() { void updateStayAwakeTime() {
timeUntilSleep = time(NULL) + wakeupDurationAfterButtonPress; sleepStartAfterButtonPressOrConnection = time(NULL) + wakeupDurationAfterButtonPress;
} }
bool shouldGoToSleep() { bool shouldGoToSleep() {
if (timeUntilSleep > time(NULL)) { if (bluetoothIsConnected()) {
return false; return false;
} }
if (bluetoothIsConnected()) { if (time(NULL) <= sleepStartAfterButtonPressOrConnection) {
return false; return false;
} }
return true; return true;