Skip to content

Commit

Permalink
Include support for wakingup with touch pins (mainly for e-Ink withou…
Browse files Browse the repository at this point in the history
…t physical buttons)
  • Loading branch information
melkati committed Apr 8, 2024
1 parent 9d55fa6 commit 216da48
Show file tree
Hide file tree
Showing 4 changed files with 355 additions and 168 deletions.
44 changes: 31 additions & 13 deletions CO2_Gadget.ino
Original file line number Diff line number Diff line change
Expand Up @@ -589,12 +589,12 @@ void initHighPerformanceMode() {

if (ESP.getFlashChipSize() > 0) {
Serial.println("-->[STUP] Flash size: " + String(ESP.getFlashChipSize()));
Serial.println("-->[STUP] Flash speed: " + String(ESP.getFlashChipSpeed()));
Serial.println("-->[STUP] Flash mode: " + String(ESP.getFlashChipMode()));
// Serial.println("-->[STUP] Flash speed: " + String(ESP.getFlashChipSpeed()));
// Serial.println("-->[STUP] Flash mode: " + String(ESP.getFlashChipMode()));
}
delay(50);

printResetReason();
// printResetReason();
initImprov();
initBattery();
initGPIO();
Expand Down Expand Up @@ -625,6 +625,8 @@ void initHighPerformanceMode() {
timeInitializationCompleted = millis();
startTimerToDeepSleep = timeInitializationCompleted;
Serial.println("-->[STUP] Initialization in HIGH PERFORMANCE MODE Ready.");
Serial.println("");
Serial.flush();
}

void initGPIOLowPower() {
Expand Down Expand Up @@ -662,7 +664,7 @@ void initGPIOLowPower() {
timeInitializationCompleted = millis();
restartTimerToDeepSleep();
Serial.println("-->[STUP] Going to deep sleep in: " + String((deepSleepData.waitToGoDeepSleepOn1stBoot * 1000 - (millis() - startTimerToDeepSleep)) / 1000) + " seconds");
Serial.println("-->[STUP] deepSleepData.waitToGoDeepSleepOn1stBoot: " + String(deepSleepData.waitToGoDeepSleepOn1stBoot * 1000) + "startTimerToDeepSleep: " + String(startTimerToDeepSleep) + "millis: " + String(millis()));
Serial.println("-->[STUP] deepSleepData.waitToGoDeepSleepOn1stBoot: " + String(deepSleepData.waitToGoDeepSleepOn1stBoot * 1000) + " startTimerToDeepSleep: " + String(startTimerToDeepSleep) + " millis: " + String(millis()));
Serial.println("-->**********************************************************");
Serial.println("-->[STUP]--> INITIALIZING LOW POWER MODE GPIO WAKE UP READY *");
Serial.println("-->**********************************************************");
Expand Down Expand Up @@ -723,14 +725,29 @@ void setup() {
fromDeepSleep();
break;
case ESP_SLEEP_WAKEUP_EXT0:
Serial.println("-->[STUP] Initializing from deep sleep GPIO (WAKEUP_EXT0)");
initGPIOLowPower();
fromDeepSleep();
Serial.println("-->[STUP] Initialization from deep sleep GPIO (WAKEUP_EXT0) completed");
break;
case ESP_SLEEP_WAKEUP_EXT1:
Serial.println("-->[STUP] Initializing from deep sleep GPIO");
Serial.println("-->[STUP] Initializing from deep sleep GPIO (WAKEUP_EXT1)");
Serial.print("-->[STUP] Wake up caused because of GPIO: ");
Serial.println((log(esp_sleep_get_ext1_wakeup_status())) / log(2), 0);
initGPIOLowPower();
fromDeepSleep();
Serial.println("-->[STUP] Initialization from deep sleep GPIO completed");
Serial.println("-->[STUP] Initialization from deep sleep GPIO (WAKEUP_EXT1) completed");
break;
case ESP_SLEEP_WAKEUP_TOUCHPAD:
Serial.println("-->[STUP] Initializing from deep sleep touchpad");
printWakeupTouchpad();
initGPIOLowPower();
fromDeepSleep();
Serial.println("-->[STUP] Initialization from deep sleep touchpad completed");
break;
default:
Serial.println("-->[STUP] Initializing from unknow deep sleep cause: " + String(esp_sleep_get_wakeup_cause()));
Serial.print("-->[STUP][ERROR] Initializing from unknown deep sleep cause: ");
Serial.println(esp_sleep_get_wakeup_cause());
delay(5000);
initHighPerformanceMode();
break;
Expand Down Expand Up @@ -766,7 +783,7 @@ void setup() {
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, brown_reg_temp); // enable brownout detector
}

void loop() { // Only reached in HIGH PERFORMANCE MODE
void loopOLD() { // Only reached in HIGH PERFORMANCE MODE
batteryLoop();
utilityLoop();
improvLoop();
Expand All @@ -780,12 +797,12 @@ void loop() { // Only reached in HIGH PERFORMANCE MODE
adjustBrightnessLoop();
buttonsLoop();
menuLoop();
BLELoop();
BLELoop();
deepSleepLoop();
}

void loopOLD() { // Old loop function. Not used anymore. Just for reference
bool showDebug = false;
void loop() { // Old loop function. Not used anymore. Just for reference
bool showDebug = true;
static unsigned long lastDotPrintTime = 0;
if ((showDebug) && (millis() - lastDotPrintTime > 3000)) {
lastDotPrintTime = millis();
Expand All @@ -801,7 +818,8 @@ void loopOLD() { // Old loop function. Not used anymore. Just for reference
improvLoop();
wifiClientLoop();
mqttClientLoop();
if (deepSleepEnabled && (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_EXT0 || esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_EXT1)) {
// if (deepSleepEnabled && (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_EXT0 || esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_EXT1 || esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_TOUCHPAD)) {
if (deepSleepEnabled) {
if (showDebug) Serial.println("-->[MAIN] Reading sensors in low power mode. ");
deepSleepLoop();
handleLowPowerSensors();
Expand All @@ -817,5 +835,5 @@ void loopOLD() { // Old loop function. Not used anymore. Just for reference
buttonsLoop();
menuLoop();
BLELoop();
deepSleepLoop();
// deepSleepLoop();
}
98 changes: 79 additions & 19 deletions CO2_Gadget_DeepSleep.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,55 @@ String getDeepSleepDataCo2SensorName() {
}
}

void printWakeupTouchpad() {
touch_pad_t touchPin;
touchPin = esp_sleep_get_touchpad_wakeup_status();

#if CONFIG_IDF_TARGET_ESP32
switch (touchPin) {
case 0:
Serial.println("-->[DEEP] Touch detected on GPIO 4");
break;
case 1:
Serial.println("-->[DEEP] Touch detected on GPIO 0");
break;
case 2:
Serial.println("-->[DEEP] Touch detected on GPIO 2");
break;
case 3:
Serial.println("-->[DEEP] Touch detected on GPIO 15");
break;
case 4:
Serial.println("-->[DEEP] Touch detected on GPIO 13");
break;
case 5:
Serial.println("-->[DEEP] Touch detected on GPIO 12");
break;
case 6:
Serial.println("-->[DEEP] Touch detected on GPIO 14");
break;
case 7:
Serial.println("-->[DEEP] Touch detected on GPIO 27");
break;
case 8:
Serial.println("-->[DEEP] Touch detected on GPIO 33");
break;
case 9:
Serial.println("-->[DEEP] Touch detected on GPIO 32");
break;
default:
Serial.println("-->[DEEP] Wakeup not by touchpad");
break;
}
#else
if (touchPin < TOUCH_PAD_MAX) {
Serial.printf("-->[DEEP] Touch detected on GPIO %d\n", touchPin);
} else {
Serial.println("-->[DEEP] Wakeup not by touchpad");
}
#endif
}

String getWakeupCause() {
switch (esp_sleep_get_wakeup_cause()) {
case ESP_SLEEP_WAKEUP_UNDEFINED:
Expand Down Expand Up @@ -130,7 +179,7 @@ String getResetReason() {

void printResetReason() {
#ifdef DEEP_SLEEP_DEBUG
Serial.println("-->[DEEP] Reason for reset: " + getResetReason());
// Serial.println("-->[DEEP] Reason for reset: " + getResetReason());
#endif
}

Expand Down Expand Up @@ -178,16 +227,19 @@ void printRTCMemoryExit() {

void restartTimerToDeepSleep() {
startTimerToDeepSleep = millis();
#ifdef DEEP_SLEEP_DEBUG
#ifdef DEEP_SLEEP_DEBUG
Serial.println("-->[DEEP] Restarting timer to deep sleep.");
#endif
#endif
}

void toDeepSleep() {
void callbackTouch() {
// placeholder callback function
}

#ifdef SUPPORT_EINK
// display.hibernate();
#endif
void toDeepSleep() {
#ifdef SUPPORT_EINK
// display.hibernate();
#endif

if (deepSleepData.co2Sensor == static_cast<CO2SENSORS_t>(CO2Sensor_SCD30)) {
sensors.scd30.stopContinuousMeasurement();
Expand All @@ -205,6 +257,11 @@ void toDeepSleep() {
Serial.println("");
printRTCMemoryEnter();

// Setup interrupt on Touch Pad 0 (GPIO4)
touchAttachInterrupt(T3, callbackTouch, 40);

esp_sleep_enable_touchpad_wakeup();

// Experimental: Turn off green LED and display on S3 board
// #if defined(CONFIG_IDF_TARGET_ESP32S3)
// digitalWrite(TFT_POWER_ON_BATTERY, LOW);
Expand All @@ -219,18 +276,20 @@ void toDeepSleep() {

#ifdef TIMEDEBUG
Serial.println("-->[DEEP] Time awake: " + String(timerAwake.read()) + " ms (in light sleep for " + String(timerLightSleep.read()) + " ms)");
Serial.println("");
Serial.println("");
#endif
Serial.flush();
esp_deep_sleep_disable_rom_logging();
#ifdef BTN_WAKEUP
esp_sleep_enable_ext0_wakeup(static_cast<gpio_num_t>(BTN_WAKEUP), BTN_WAKEUP_ON); // 1 = High, 0 = Low
#else
if (BTN_DWN != -1) {
esp_sleep_enable_ext0_wakeup(static_cast<gpio_num_t>(BTN_DWN), LOW); // 1 = High, 0 = Low
} else if (BTN_UP != -1) {
esp_sleep_enable_ext0_wakeup(static_cast<gpio_num_t>(BTN_UP), LOW); // 1 = High, 0 = Low
}
#endif
// #ifdef BTN_WAKEUP
// esp_sleep_enable_ext0_wakeup(static_cast<gpio_num_t>(BTN_WAKEUP), BTN_WAKEUP_ON); // 1 = High, 0 = Low
// #else
// if (BTN_DWN != -1) {
// esp_sleep_enable_ext0_wakeup(static_cast<gpio_num_t>(BTN_DWN), LOW); // 1 = High, 0 = Low
// } else if (BTN_UP != -1) {
// esp_sleep_enable_ext0_wakeup(static_cast<gpio_num_t>(BTN_UP), LOW); // 1 = High, 0 = Low
// }
// #endif
esp_sleep_enable_timer_wakeup(deepSleepData.timeSleeping * 1000000);
delay(5);
gpio_deep_sleep_hold_en();
Expand Down Expand Up @@ -420,14 +479,13 @@ bool scd40HandleFromDeepSleep(bool blockingMode = true) {
if (currentMillis - previousMillis >= 1000) {
previousMillis = currentMillis;
Serial.print("+");
delay(10);
}
if (currentMillis - startTimeoutMillis >= 31000) {
Serial.println("-->[DEEP] Timeout waiting for data from sensor SCD40");
return (false);
}
esp_sleep_enable_timer_wakeup(0.1 * 1000000);
Serial.flush();
esp_sleep_enable_timer_wakeup(0.3 * 1000000);
timerLightSleep.resume();
esp_light_sleep_start();
timerLightSleep.pause();
Expand Down Expand Up @@ -640,16 +698,18 @@ void fromDeepSleep() {
#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");
interactiveMode = true;
#endif
interactiveMode = true;
break;
case ESP_SLEEP_WAKEUP_TOUCHPAD:
#ifdef DEEP_SLEEP_DEBUG
Serial.println("-->[DEEP] Wakeup caused by touchpad");
interactiveMode = true;
#endif
break;
case ESP_SLEEP_WAKEUP_ULP:
Expand Down
Loading

0 comments on commit 216da48

Please sign in to comment.