From e4663df50093261091bad7380bbd9d5bf8f015f5 Mon Sep 17 00:00:00 2001 From: Jannes Date: Sat, 19 Apr 2025 16:00:18 +0200 Subject: [PATCH] add deep sleep mode --- esp32_readout/display.cpp | 5 +++ esp32_readout/display.h | 1 + esp32_readout/env.h | 8 +++++ .../{readout_test.ino => esp32_readout.ino} | 33 +++++++++++++++++++ 4 files changed, 47 insertions(+) rename esp32_readout/{readout_test.ino => esp32_readout.ino} (74%) diff --git a/esp32_readout/display.cpp b/esp32_readout/display.cpp index c799e4c..4879c19 100644 --- a/esp32_readout/display.cpp +++ b/esp32_readout/display.cpp @@ -34,4 +34,9 @@ void Display::print_weight(double val) { this->_display->setCursor(12, this->mid_x); this->_display->printf("%.2f g", val); this->_display->refresh(); +} + +void Display::clear() { + this->_display->clearDisplay(); + this->_display->refresh(); } \ No newline at end of file diff --git a/esp32_readout/display.h b/esp32_readout/display.h index cc89794..f7faa0f 100644 --- a/esp32_readout/display.h +++ b/esp32_readout/display.h @@ -20,6 +20,7 @@ void setup(); void print(const char* text); void print_weight(double weight); + void clear(); }; #endif \ No newline at end of file diff --git a/esp32_readout/env.h b/esp32_readout/env.h index df1ff75..4931d74 100644 --- a/esp32_readout/env.h +++ b/esp32_readout/env.h @@ -1,5 +1,8 @@ #ifndef ENV #define ENV + #include + #include "driver/rtc_io.h" + #include #include #include @@ -9,6 +12,11 @@ #include #include + /********************* General ****************************/ + #define BUTTON_PIN_BITMASK(GPIO) (1ULL << GPIO) + #define WAKEUP_GPIO GPIO_NUM_1 + #define SLEEP_GPIO 1 + /********************* NAU7802 ****************************/ #define LDO NAU7802_3V3 diff --git a/esp32_readout/readout_test.ino b/esp32_readout/esp32_readout.ino similarity index 74% rename from esp32_readout/readout_test.ino rename to esp32_readout/esp32_readout.ino index bd19af1..35f900b 100644 --- a/esp32_readout/readout_test.ino +++ b/esp32_readout/esp32_readout.ino @@ -5,6 +5,7 @@ Display display; Adafruit_NAU7802 nau; unsigned long _millis = 0; int32_t val = 0; +bool goToSleep = False; #if BLE BLEServer *pServer = NULL; BLECharacteristic *millisCharacteristic = NULL; @@ -26,10 +27,42 @@ int32_t val = 0; }; #endif +void print_wakeup_reason() { + esp_sleep_wakeup_cause_t wakeup_reason; + + wakeup_reason = esp_sleep_get_wakeup_cause(); + + switch (wakeup_reason) { + case ESP_SLEEP_WAKEUP_EXT0: Serial.println("Wakeup caused by external signal using RTC_IO"); break; + case ESP_SLEEP_WAKEUP_EXT1: Serial.println("Wakeup caused by external signal using RTC_CNTL"); break; + case ESP_SLEEP_WAKEUP_TIMER: Serial.println("Wakeup caused by timer"); break; + case ESP_SLEEP_WAKEUP_TOUCHPAD: Serial.println("Wakeup caused by touchpad"); break; + case ESP_SLEEP_WAKEUP_ULP: Serial.println("Wakeup caused by ULP program"); break; + default: Serial.printf("Wakeup was not caused by deep sleep: %d\n", wakeup_reason); break; + } +} + +void IRAM_ATTR startSleep() { + display.clear(); + + esp_sleep_enable_ext0_wakeup(WAKEUP_GPIO, 1); + + rtc_gpio_pullup_dis(WAKEUP_GPIO); + rtc_gpio_pulldown_en(WAKEUP_GPIO); + + esp_deep_sleep_start(); +} + void setup() { Serial.begin(115200); display.setup(); + print_wakeup_reason(); + + /**** setup sleep interrupt ******/ + pinMode(SLEEP_GPIO, INPUT); + attachInterrupt(digitalPinToInterrupt(SLEEP_GPIO), startSleep, FALLING); + if (! nau.begin()) { Serial.println("Failed to find NAU7802"); while (1) delay(10); // Don't proceed.