From ca37e503623ab9525eef5651436dd5a8589434ad Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 22 Feb 2020 14:45:58 -0800 Subject: [PATCH] WIP light sleep kinda works --- src/PowerFSM.cpp | 20 +++++++++++++++++--- src/sleep.cpp | 9 +++++---- src/sleep.h | 3 ++- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/PowerFSM.cpp b/src/PowerFSM.cpp index 0efab68a..8f4b1bb8 100644 --- a/src/PowerFSM.cpp +++ b/src/PowerFSM.cpp @@ -34,14 +34,21 @@ static void lsEnter() delay(msecstosleep); } */ + while (!service.radio.rf95.canSleep()) + delay(10); // Kinda yucky - wait until radio says say we can shutdown (finished in process sends/receives) + gps.prepareSleep(); // abandon in-process parsing setGPSPower(false); // kill GPS power - doLightSleep(radioConfig.preferences.ls_secs * 1000LL); } static void lsIdle() { + doLightSleep(radioConfig.preferences.ls_secs * 1000LL); + // FIXME - blink led when we occasionally wake from timer, then go back to light sleep + + // Regardless of why we woke (for now) just transition to NB + powerFSM.trigger(EVENT_WAKE_TIMER); } static void lsExit() @@ -53,6 +60,8 @@ static void lsExit() static void nbEnter() { setBluetoothEnable(false); + + // FIXME - check if we already have packets for phone and immediately trigger EVENT_PACKETS_FOR_PHONE } static void darkEnter() @@ -90,10 +99,15 @@ void PowerFSM_setup() { powerFSM.add_transition(&stateDARK, &stateON, EVENT_BOOT, NULL, "Boot"); powerFSM.add_transition(&stateLS, &stateDARK, EVENT_WAKE_TIMER, wakeForPing, "Wake timer"); - powerFSM.add_transition(&stateLS, &stateNB, EVENT_RECEIVED_PACKET, NULL, "Received packet"); + + // Note we don't really use this transition, because when we wake from light sleep we _always_ transition to NB and then it handles things + // powerFSM.add_transition(&stateLS, &stateNB, EVENT_RECEIVED_PACKET, NULL, "Received packet"); + powerFSM.add_transition(&stateNB, &stateNB, EVENT_RECEIVED_PACKET, NULL, "Received packet, resetting win wake"); - powerFSM.add_transition(&stateLS, &stateON, EVENT_PRESS, NULL, "Press"); + // Note we don't really use this transition, because when we wake from light sleep we _always_ transition to NB and then it handles things + // powerFSM.add_transition(&stateLS, &stateON, EVENT_PRESS, NULL, "Press"); + powerFSM.add_transition(&stateNB, &stateON, EVENT_PRESS, NULL, "Press"); powerFSM.add_transition(&stateDARK, &stateON, EVENT_PRESS, NULL, "Press"); powerFSM.add_transition(&stateON, &stateON, EVENT_PRESS, screenPress, "Press"); // reenter On to restart our timers diff --git a/src/sleep.cpp b/src/sleep.cpp index c4e90c44..4769e91c 100644 --- a/src/sleep.cpp +++ b/src/sleep.cpp @@ -207,7 +207,7 @@ void setBluetoothEnable(bool on) * * Returns (after restoring hw state) when the user presses a button or we get a LoRa interrupt */ -void doLightSleep(uint64_t sleepMsec) // FIXME, use a more reasonable default +esp_sleep_wakeup_cause_t doLightSleep(uint64_t sleepMsec) // FIXME, use a more reasonable default { DEBUG_MSG("Enter light sleep\n"); uint64_t sleepUsec = sleepMsec * 1000LL; @@ -225,10 +225,11 @@ void doLightSleep(uint64_t sleepMsec) // FIXME, use a more reasonable default #ifdef PMU_IRQ gpio_wakeup_enable((gpio_num_t)PMU_IRQ, GPIO_INTR_HIGH_LEVEL); // pmu irq #endif - esp_sleep_enable_gpio_wakeup(); - esp_sleep_enable_timer_wakeup(sleepUsec); - esp_light_sleep_start(); + assert(esp_sleep_enable_gpio_wakeup() == ESP_OK); + assert(esp_sleep_enable_timer_wakeup(sleepUsec) == ESP_OK); + assert(esp_light_sleep_start() == ESP_OK); DEBUG_MSG("Exit light sleep\n"); + return esp_sleep_get_wakeup_cause(); } #if 0 diff --git a/src/sleep.h b/src/sleep.h index dd72878b..ea5d0029 100644 --- a/src/sleep.h +++ b/src/sleep.h @@ -1,9 +1,10 @@ #pragma once #include "Arduino.h" +#include "esp_sleep.h" void doDeepSleep(uint64_t msecToWake); -void doLightSleep(uint64_t msecToWake); +esp_sleep_wakeup_cause_t doLightSleep(uint64_t msecToWake); void setBluetoothEnable(bool on); void setGPSPower(bool on);