Fix #167 while in light sleep, allow loop() to still run occasionally

This commit is contained in:
geeksville 2020-06-10 14:11:43 -07:00
parent 21a90a42e5
commit ddfdae64bf
4 changed files with 46 additions and 34 deletions

View File

@ -26,60 +26,65 @@ static void sdsEnter()
#include "error.h" #include "error.h"
static uint32_t secsSlept;
static void lsEnter() static void lsEnter()
{ {
DEBUG_MSG("lsEnter begin, ls_secs=%u\n", radioConfig.preferences.ls_secs); DEBUG_MSG("lsEnter begin, ls_secs=%u\n", radioConfig.preferences.ls_secs);
screen.setOn(false); screen.setOn(false);
secsSlept = 0; // How long have we been sleeping this time
DEBUG_MSG("lsEnter end\n"); DEBUG_MSG("lsEnter end\n");
} }
static void lsIdle() static void lsIdle()
{ {
DEBUG_MSG("lsIdle begin ls_secs=%u\n", radioConfig.preferences.ls_secs); // DEBUG_MSG("lsIdle begin ls_secs=%u\n", radioConfig.preferences.ls_secs);
#ifndef NO_ESP32 #ifndef NO_ESP32
uint32_t secsSlept = 0;
esp_sleep_source_t wakeCause = ESP_SLEEP_WAKEUP_UNDEFINED; esp_sleep_source_t wakeCause = ESP_SLEEP_WAKEUP_UNDEFINED;
bool reached_ls_secs = false;
while (!reached_ls_secs) { // Do we have more sleeping to do?
if (secsSlept < radioConfig.preferences.ls_secs) {
// Briefly come out of sleep long enough to blink the led once every few seconds // Briefly come out of sleep long enough to blink the led once every few seconds
uint32_t sleepTime = 5; uint32_t sleepTime = 30;
setLed(false); // Never leave led on while in light sleep // If some other service would stall sleep, don't let sleep happen yet
wakeCause = doLightSleep(sleepTime * 1000LL); if (doPreflightSleep()) {
if (wakeCause != ESP_SLEEP_WAKEUP_TIMER) setLed(false); // Never leave led on while in light sleep
break; wakeCause = doLightSleep(sleepTime * 1000LL);
setLed(true); // briefly turn on led if (wakeCause == ESP_SLEEP_WAKEUP_TIMER) {
doLightSleep(1); // Normal case: timer expired, we should just go back to sleep ASAP
if (wakeCause != ESP_SLEEP_WAKEUP_TIMER)
break;
secsSlept += sleepTime; setLed(true); // briefly turn on led
reached_ls_secs = secsSlept >= radioConfig.preferences.ls_secs; wakeCause = doLightSleep(1); // leave led on for 1ms
}
setLed(false);
if (reached_ls_secs) { secsSlept += sleepTime;
// stay in LS mode but let loop check whatever it wants // DEBUG_MSG("sleeping, flash led!\n");
DEBUG_MSG("reached ls_secs, servicing loop()\n"); } else {
} else { // We woke for some other reason (button press, uart, device interrupt)
DEBUG_MSG("wakeCause %d\n", wakeCause); DEBUG_MSG("wakeCause %d\n", wakeCause);
#ifdef BUTTON_PIN #ifdef BUTTON_PIN
bool pressed = !digitalRead(BUTTON_PIN); bool pressed = !digitalRead(BUTTON_PIN);
#else #else
bool pressed = false; bool pressed = false;
#endif #endif
if (pressed) // If we woke because of press, instead generate a PRESS event. if (pressed) // If we woke because of press, instead generate a PRESS event.
{ {
powerFSM.trigger(EVENT_PRESS); powerFSM.trigger(EVENT_PRESS);
} else { } else {
// Otherwise let the NB state handle the IRQ (and that state will handle stuff like IRQs etc) // Otherwise let the NB state handle the IRQ (and that state will handle stuff like IRQs etc)
powerFSM.trigger(EVENT_WAKE_TIMER); powerFSM.trigger(EVENT_WAKE_TIMER);
}
}
} }
} else {
// Time to stop sleeping!
setLed(false);
DEBUG_MSG("reached ls_secs, servicing loop()\n");
powerFSM.trigger(EVENT_WAKE_TIMER);
} }
#endif #endif
} }

View File

@ -134,7 +134,7 @@ bool RadioLibInterface::canSleep()
{ {
bool res = txQueue.isEmpty(); bool res = txQueue.isEmpty();
if (!res) // only print debug messages if we are vetoing sleep if (!res) // only print debug messages if we are vetoing sleep
DEBUG_MSG("radio wait to sleep, txEmpty=%d\n", txQueue.isEmpty()); DEBUG_MSG("radio wait to sleep, txEmpty=%d\n", res);
return res; return res;
} }
@ -173,11 +173,13 @@ void RadioLibInterface::loop()
case ISR_TX: case ISR_TX:
handleTransmitInterrupt(); handleTransmitInterrupt();
startReceive(); startReceive();
// DEBUG_MSG("tx complete - starting timer\n");
startTransmitTimer(); startTransmitTimer();
break; break;
case ISR_RX: case ISR_RX:
handleReceiveInterrupt(); handleReceiveInterrupt();
startReceive(); startReceive();
// DEBUG_MSG("rx complete - starting timer\n");
startTransmitTimer(); startTransmitTimer();
break; break;
case TRANSMIT_DELAY_COMPLETED: case TRANSMIT_DELAY_COMPLETED:
@ -192,6 +194,8 @@ void RadioLibInterface::loop()
assert(txp); assert(txp);
startSend(txp); startSend(txp);
} }
} else {
// DEBUG_MSG("done with txqueue\n");
} }
break; break;
default: default:
@ -216,7 +220,7 @@ void RadioLibInterface::startTransmitTimer(bool withDelay)
uint32_t delay = uint32_t delay =
!withDelay ? 1 : random(MIN_TX_WAIT_MSEC, MAX_TX_WAIT_MSEC); // See documentation for loop() wrt these values !withDelay ? 1 : random(MIN_TX_WAIT_MSEC, MAX_TX_WAIT_MSEC); // See documentation for loop() wrt these values
// DEBUG_MSG("xmit timer %d\n", delay); // DEBUG_MSG("xmit timer %d\n", delay);
// DEBUG_MSG("delaying %u\n", delay);
setPeriod(delay); setPeriod(delay);
} }
} }

View File

@ -111,8 +111,8 @@ void initDeepSleep()
#endif #endif
} }
/// return true if sleep is allowed
static bool doPreflightSleep() bool doPreflightSleep()
{ {
if (preflightSleep.notifyObservers(NULL) != 0) if (preflightSleep.notifyObservers(NULL) != 0)
return false; // vetoed return false; // vetoed

View File

@ -19,6 +19,9 @@ void initDeepSleep();
void setCPUFast(bool on); void setCPUFast(bool on);
void setLed(bool ledOn); void setLed(bool ledOn);
/** return true if sleep is allowed right now */
bool doPreflightSleep();
extern int bootCount; extern int bootCount;
// is bluetooth sw currently running? // is bluetooth sw currently running?