Skip to content

Commit

Permalink
Fix: Display reverse on wake.
Browse files Browse the repository at this point in the history
Refactor code to improve maintainability and readability by modularizing deep sleep wake-up logic and adding "OnWake" suffix to relevant functions.
  • Loading branch information
melkati committed Jul 1, 2024
1 parent 48ad5e0 commit 08244d3
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 93 deletions.
6 changes: 4 additions & 2 deletions CO2_Gadget.ino
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ typedef struct {
bool sendMQTTOnWake;
bool sendESPNowOnWake;
bool displayOnWake;
bool displayReverseOnWake; // Display reverse on wake. Here to avoid having to read preferences on wake
uint16_t timeToDisplayOnWake = 3;
bool measurementsStarted;
uint64_t bootTimes;
Expand Down Expand Up @@ -757,6 +758,7 @@ void initGPIOLowPower() {
interactiveMode = true;
deepSleepEnabled = true;
#if defined(SUPPORT_TFT) || defined(SUPPORT_OLED) || defined(SUPPORT_EINK)
handleDisplayReverseOnWake();
initDisplay(true);
#endif
initBattery();
Expand Down Expand Up @@ -829,8 +831,8 @@ void setup() {
uint32_t brown_reg_temp = READ_PERI_REG(RTC_CNTL_BROWN_OUT_REG); // save WatchDog register
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); // disable brownout detector
Serial.setDebugOutput(true);
// Serial.setTxBufferSize(1024);
// Serial.setRxBufferSize(512);
Serial.setTxBufferSize(1024);
Serial.setRxBufferSize(512);
Serial.begin(115200);
Serial.println();
Serial.println();
Expand Down
196 changes: 106 additions & 90 deletions CO2_Gadget_DeepSleep.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ void printRTCMemoryEnter() {
Serial.println("-->[DEEP][ENTER DeepSleep] sendMQTTOnWake: " + String(deepSleepData.sendMQTTOnWake));
Serial.println("-->[DEEP][ENTER DeepSleep] sendESPNowOnWake: " + String(deepSleepData.sendESPNowOnWake));
Serial.println("-->[DEEP][ENTER DeepSleep] displayOnWake: " + String(deepSleepData.displayOnWake));
Serial.println("-->[DEEP][ENTER DeepSleep] displayReverseOnWake: " + String(deepSleepData.displayReverseOnWake));
Serial.println("-->[DEEP][ENTER DeepSleep] Wake up times: " + String(deepSleepData.bootTimes));
#endif
}
Expand All @@ -223,7 +224,8 @@ void printRTCMemoryExit() {
Serial.println("-->[DEEP][EXIT DeepSleep] activeWifiOnWake: " + String(deepSleepData.activeWifiOnWake));
Serial.println("-->[DEEP][EXIT DeepSleep] sendMQTTOnWake: " + String(deepSleepData.sendMQTTOnWake));
Serial.println("-->[DEEP][EXIT DeepSleep] sendESPNowOnWake: " + String(deepSleepData.sendESPNowOnWake));
Serial.println("-->[DEEP][EXIT DeepSleep] displayOnWake: " + String(deepSleepData.displayOnWake));
Serial.println("-->[DEEP][EXIT DeepSleep] displayOnWake: " + String(deepSleepData.displayOnWake));
Serial.println("-->[DEEP][EXIT DeepSleep] displayReverseOnWake: " + String(deepSleepData.displayReverseOnWake));
Serial.println("-->[DEEP][EXIT DeepSleep] Wake up times: " + String(deepSleepData.bootTimes));
#endif
}
Expand All @@ -244,6 +246,10 @@ void toDeepSleep() {
// display.hibernate();
#endif

#if defined(SUPPORT_TFT) || defined(SUPPORT_OLED) || defined(SUPPORT_EINK)
deepSleepData.displayReverseOnWake = displayReverse;
#endif

if (deepSleepData.co2Sensor == static_cast<CO2SENSORS_t>(CO2Sensor_SCD30)) {
// sensors.scd30.stopContinuousMeasurement();
} else if (deepSleepData.co2Sensor == static_cast<CO2SENSORS_t>(CO2Sensor_SCD41)) {
Expand Down Expand Up @@ -622,146 +628,145 @@ bool handleLowPowerSensors() {
return (readOK);
}

void fromDeepSleepTimer() {
void handleCycleCountersOnWake() {
--deepSleepData.cyclesLeftToWiFiConnect;
--deepSleepData.cyclesLeftToRedrawDisplay;
if (deepSleepData.cyclesLeftToWiFiConnect == 65535) deepSleepData.cyclesLeftToWiFiConnect = 0;
if (deepSleepData.cyclesLeftToRedrawDisplay == 65535) deepSleepData.cyclesLeftToRedrawDisplay = 0;

#if defined(DEEP_SLEEP_DEBUG)
Serial.println("-->[DEEP] Cycles left to connect to WiFi: " + String(deepSleepData.cyclesLeftToWiFiConnect));
#endif

#if defined(DEEP_SLEEP_DEBUG) && defined(SUPPORT_EINK)
Serial.println("-->[DEEP] Cycles left to redraw E-Ink display: " + String(deepSleepData.cyclesLeftToRedrawDisplay));
#endif
}

switch (deepSleepData.lowPowerMode) {
case BASIC_LOWPOWER:
void handleLowPowerModeBasicOnWake() {
#ifdef DEEP_SLEEP_DEBUG
Serial.println("-->[DEEP] Waking up from deep sleep. LowPowerMode: BASIC_LOWPOWER");
Serial.println("-->[DEEP] Waking up from deep sleep. LowPowerMode: BASIC_LOWPOWER");
#endif
break;
case MEDIUM_LOWPOWER:
}

void handleBLEOnWake() {
#ifdef SUPPORT_BLE
if (deepSleepData.activeBLEOnWake) {
initBLE();
#ifdef DEEP_SLEEP_DEBUG
Serial.println("-->[DEEP] Waking up from deep sleep. LowPowerMode: MEDIUM_LOWPOWER");
Serial.println("-->[DEEP] BLE initialized. activeBLE: " + String(activeBLE));
#endif
initBattery();
batteryLoop();
if (handleLowPowerSensors()) {
displayFromDeepSleep(deepSleepData.cyclesLeftToRedrawDisplay == 0);
}
#ifdef SUPPORT_BLE
if (deepSleepData.activeBLEOnWake) {
initBLE();
publishBLE();
}
#endif
}

void handleDisplayReverseOnWake() {
#if defined(SUPPORT_TFT) || defined(SUPPORT_OLED) || defined(SUPPORT_EINK)
displayReverse = deepSleepData.displayReverseOnWake;
setDisplayReverse(displayReverse);
reverseButtons(displayReverse);
#endif
}

void handleDisplayRedrawOnWake() {
#if defined(SUPPORT_TFT) || defined(SUPPORT_OLED) || defined(SUPPORT_EINK)
if (deepSleepData.cyclesLeftToRedrawDisplay == 0) {
#ifdef DEEP_SLEEP_DEBUG
Serial.println("-->[DEEP] BLE initialized. activeBLE: " + String(activeBLE));
Serial.println("-->[DEEP] Updating display");
#endif
publishBLE();
// BLELoop();
}
initDisplay(true);
displayShowValues(true);
}
#endif
}

void handleDisplayOnWake() {
#if defined(SUPPORT_TFT) || defined(SUPPORT_OLED)
if (deepSleepData.displayOnWake) {
if (deepSleepData.cyclesLeftToRedrawDisplay == 0) {
if (deepSleepData.displayOnWake && deepSleepData.cyclesLeftToRedrawDisplay == 0) {
#ifdef DEEP_SLEEP_DEBUG
Serial.println("-->[DEEP] Displaying values momentarily for " + String(deepSleepData.timeToDisplayOnWake) + " seconds");
#endif
initPreferences();
initDisplay(true);
displayShowValues(true);
}
esp_sleep_enable_timer_wakeup(deepSleepData.timeToDisplayOnWake * 1000000);
Serial.flush();
#ifdef TIMEDEBUG
timerLightSleep.resume();
Serial.println("-->[DEEP] Displaying values momentarily for " + String(deepSleepData.timeToDisplayOnWake) + " seconds");
#endif
// esp_light_sleep_start();
delay(deepSleepData.timeToDisplayOnWake * 1000);
#ifdef TIMEDEBUG
timerLightSleep.pause();
initDisplay(true);
displayShowValues(true);
esp_sleep_enable_timer_wakeup(deepSleepData.timeToDisplayOnWake * 1000000);
Serial.flush();
delay(deepSleepData.timeToDisplayOnWake * 1000);
}
#endif
}
#endif // defined(SUPPORT_TFT) || defined(SUPPORT_OLED)
#ifdef SUPPORT_EINK
if (deepSleepData.cyclesLeftToRedrawDisplay == 0) {
}

void handleWiFiConnectionOnWake() {
if (deepSleepData.cyclesLeftToWiFiConnect == 0) {
doDeepSleepWiFiConnect();
}
}

void handleMQTTPublishOnWake() {
#ifdef SUPPORT_MQTT
if (deepSleepData.sendMQTTOnWake && WiFi.status() == WL_CONNECTED) {
#ifdef DEEP_SLEEP_DEBUG
Serial.println("-->[DEEP] Updating e-ink display");
Serial.println("-->[DEEP] MQTT connected. Publishing measurements.");
#endif
initPreferences();
initDisplay(true);
displayShowValues(true);
}
#ifdef TIMEDEBUG
timerLightSleep.resume();
#endif
#ifdef TIMEDEBUG
timerLightSleep.pause();
publishMQTT(true);
delay(10);
}
#endif
#endif // SUPPORT_EINK
if (deepSleepData.cyclesLeftToWiFiConnect == 0) {
doDeepSleepWiFiConnect();
}
}

void handleMediumLowPowerModeOnWake() {
#ifdef DEEP_SLEEP_DEBUG
Serial.println("-->[DEEP] CO2: " + String(co2) + " CO2temp: " + String(temp) + " CO2humi: " + String(hum));
Serial.println("-->[DEEP] Waking up from deep sleep. LowPowerMode: MEDIUM_LOWPOWER");
#endif
#ifdef SUPPORT_MQTT
if ((deepSleepData.sendMQTTOnWake) && (WiFi.status() == WL_CONNECTED)) {
Serial.println("-->[DEEP] MQTT connected. Publishing measurements.");
publishMQTT(true);
delay(10);
}
initBattery();
batteryLoop();
if (handleLowPowerSensors()) {
displayFromDeepSleep(deepSleepData.cyclesLeftToRedrawDisplay == 0);
}
handleBLEOnWake();
handleDisplayOnWake();
handleWiFiConnectionOnWake();
#ifdef DEEP_SLEEP_DEBUG
Serial.println("-->[DEEP] CO2: " + String(co2) + " CO2temp: " + String(temp) + " CO2humi: " + String(hum));
#endif
handleMQTTPublishOnWake();
}

void fromDeepSleepTimer() {
handleCycleCountersOnWake();

switch (deepSleepData.lowPowerMode) {
case BASIC_LOWPOWER:
handleLowPowerModeBasicOnWake();
break;
case MEDIUM_LOWPOWER:
handleMediumLowPowerModeOnWake();
break;
case MAXIMUM_LOWPOWER:
break;
}

Serial.flush();
}

void fromDeepSleep() {
esp_sleep_wakeup_cause_t wakeupCause;
wakeupCause = esp_sleep_get_wakeup_cause();
#ifdef DEEP_SLEEP_DEBUG
printRTCMemoryExit();
Serial.println("-->[STUP] Initializing from deep sleep mode working with sensor (" + String(deepSleepData.co2Sensor) + "): " + getDeepSleepDataCo2SensorName());
#endif
#ifdef DEEP_SLEEP_DEBUG
Serial.println("");
Serial.println("");
#endif
void handleWakeupCauseOnWake(esp_sleep_wakeup_cause_t wakeupCause) {
switch (wakeupCause) {
case ESP_SLEEP_WAKEUP_TIMER:
#ifdef DEEP_SLEEP_DEBUG
Serial.println("-->[DEEP] Wakeup caused by timer");
#endif
fromDeepSleepTimer();
#if defined(SUPPORT_OLED) || defined(SUPPORT_EINK) // #if defined(SUPPORT_TFT) || defined(SUPPORT_OLED) || defined(SUPPORT_EINK)
#if defined(SUPPORT_OLED) || defined(SUPPORT_EINK)
Serial.println("-->[DEEP] Turn display off before going to deep sleep *");
// turnOffDisplay();
// Serial.println("-->[DEEP] Display off *");
delay(10);
displaySleep(false);
#endif
toDeepSleep();
break;
case ESP_SLEEP_WAKEUP_EXT0:
#ifdef DEEP_SLEEP_DEBUG
Serial.println("-->[DEEP] Wakeup caused by external signal using RTC_IO");
#endif
interactiveMode = true;
break;
case ESP_SLEEP_WAKEUP_EXT1:
#ifdef DEEP_SLEEP_DEBUG
Serial.println("-->[DEEP] Wakeup caused by external signal using RTC_CNTL");
#endif
interactiveMode = true;
break;
case ESP_SLEEP_WAKEUP_TOUCHPAD:
#ifdef DEEP_SLEEP_DEBUG
Serial.println("-->[DEEP] Wakeup caused by touchpad");
interactiveMode = true;
Serial.println("-->[DEEP] Wakeup caused by external signal");
#endif
interactiveMode = true;
break;
case ESP_SLEEP_WAKEUP_ULP:
#ifdef DEEP_SLEEP_DEBUG
Expand All @@ -774,6 +779,17 @@ void fromDeepSleep() {
}
}

void fromDeepSleep() {
esp_sleep_wakeup_cause_t wakeupCause = esp_sleep_get_wakeup_cause();
#ifdef DEEP_SLEEP_DEBUG
printRTCMemoryExit();
Serial.println("-->[STUP] Initializing from deep sleep mode working with sensor (" + String(deepSleepData.co2Sensor) + "): " + getDeepSleepDataCo2SensorName());
Serial.println();
#endif
handleDisplayReverseOnWake();
handleWakeupCauseOnWake(wakeupCause);
}

void deepSleepLoop() {
// Variable to store the last time Serial.print was called
static unsigned long lastSerialPrintTime = 0;
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extra_configs = platformio_extra_configs.ini
[version]
build_flags =
-D CO2_GADGET_VERSION="\"0.14."\"
-D CO2_GADGET_REV="\"013-development-low-power"\"
-D CO2_GADGET_REV="\"014-development-low-power"\"

;****************************************************************************************
;*** You can disable features by commenting the line with a semicolon at the beginning
Expand Down

0 comments on commit 08244d3

Please sign in to comment.