diff --git a/usermods/deep_sleep/readme.md b/usermods/deep_sleep/readme.md index 0bddcceefa..006aa31fd9 100644 --- a/usermods/deep_sleep/readme.md +++ b/usermods/deep_sleep/readme.md @@ -5,8 +5,8 @@ During deep sleep the ESP is shut down completely: no WiFi, no CPU, no outputs. # A word of warning -When you disable the WLED option 'Turn LEDs on after power up/reset' the ESP will go into deep sleep directly after power up and only start WLED after it has been woken up. -If the ESP can not be awoken from deep sleep due to a wrong configuration it has to be flashed again through USB. There is no other way to wake it up. +When you disable the WLED option 'Turn LEDs on after power up/reset' and 'DelaySleep' is set to zero the ESP will go into deep sleep directly after power-up and only start WLED after it has been woken up. +If the ESP can not be awoken from deep sleep due to a wrong configuration it has to be factory reset, disabling sleep at power-up. There is no other way to wake it up. # Power Consumption in deep sleep diff --git a/usermods/deep_sleep/usermod_deep_sleep.h b/usermods/deep_sleep/usermod_deep_sleep.h index 3f2842ddd8..7f4efd5caf 100644 --- a/usermods/deep_sleep/usermod_deep_sleep.h +++ b/usermods/deep_sleep/usermod_deep_sleep.h @@ -20,10 +20,10 @@ #define DEEPSLEEP_WAKEUPINTERVAL 0 #endif #ifndef DEEPSLEEP_DELAY -#define DEEPSLEEP_DELAY 0 +#define DEEPSLEEP_DELAY 1 #endif -RTC_DATA_ATTR bool bootup = true; // variable in RTC data persists on a reboot +RTC_DATA_ATTR bool powerup = true; // variable in RTC data persists on a reboot class DeepSleepUsermod : public Usermod { @@ -36,7 +36,7 @@ class DeepSleepUsermod : public Usermod { bool noPull = true; // use pullup/pulldown resistor int wakeupAfter = DEEPSLEEP_WAKEUPINTERVAL; // in seconds, <=0: button only int sleepDelay = DEEPSLEEP_DELAY; // in seconds, 0 = immediate - int delaycounter = -1; // negative means timer (yet) not set + int delaycounter = 5; // delay deep sleep at bootup until preset settings are applied uint32_t lastLoopTime = 0; // string that are used multiple time (this will save some flash memory) static const char _name[]; @@ -71,11 +71,6 @@ class DeepSleepUsermod : public Usermod { void setup() { //TODO: if the de-init of RTC pins is required to do it could be done here //rtc_gpio_deinit(wakeupPin); - if(bootup == false && !turnOnAtBoot) { // not first bootup, turn LEDs on (override turnOnAtBoot) or loop() will go back to deep sleep - if (briS == 0) bri = 5; // turn on at low brightness - else bri = briS; - offMode = false; - } initDone = true; } @@ -85,15 +80,30 @@ class DeepSleepUsermod : public Usermod { return; } - if (sleepDelay > 0 && !bootup) { + if (sleepDelay > 0) { if(lastLoopTime == 0) lastLoopTime = millis(); // initialize if (millis() - lastLoopTime < sleepDelay * 1000) { return; // wait until delay is over } } + if(powerup == false && delaycounter) { // delay sleep in case a preset is being loaded and turnOnAtBoot is disabled (handleIO() does enable offMode temporarily in this case) + delaycounter--; + if(delaycounter == 2 && offMode) { // force turn on, no matter the settings (device is bricked if user set sleepDelay=0, no bootup preset and turnOnAtBoot=false) + if (briS == 0) bri = 10; // turn on at low brightness + else bri = briS; + strip.setBrightness(bri); // needed to make handleIO() not turn off LEDs (really? does not help in bootup preset) + offMode = false; + applyPresetWithFallback(0, CALL_MODE_INIT, FX_MODE_STATIC, 0); // try to apply preset 0, fallback to static + if (rlyPin >= 0) { + digitalWrite(rlyPin, (rlyMde ? HIGH : LOW)); // turn relay on TODO: this should be done by wled, what function to call? + } + } + return; + } + DEBUG_PRINTLN(F("DeepSleep UM: entering deep sleep...")); - bootup = false; // turn leds on in all subsequent bootups (overrides Turn LEDs on after power up/reset' at reboot) + powerup = false; // turn leds on in all subsequent bootups (overrides Turn LEDs on after power up/reset' at reboot) if(!pin_is_valid(wakeupPin)) return; esp_err_t halerror = ESP_OK; pinMode(wakeupPin, INPUT); // make sure GPIO is input with pullup/pulldown disabled @@ -102,7 +112,7 @@ class DeepSleepUsermod : public Usermod { if(wakeupAfter) esp_sleep_enable_timer_wakeup((uint64_t)wakeupAfter * (uint64_t)1e6); //sleep for x seconds - #if defined(CONFIG_IDF_TARGET_ESP32C3) // ESP32 C3 + #if defined(CONFIG_IDF_TARGET_ESP32C3) // ESP32 C3 if(noPull) gpio_sleep_set_pull_mode((gpio_num_t)wakeupPin, GPIO_FLOATING); else { // enable pullup/pulldown resistor @@ -115,7 +125,7 @@ class DeepSleepUsermod : public Usermod { halerror = esp_deep_sleep_enable_gpio_wakeup(1<(0 = never)');")); + oappend(SET_F("addInfo('DeepSleep:delaySleep',1,'seconds (0 = sleep at powerup)');")); // first string is suffix, second string is prefix } /*