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,46 +26,44 @@ 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;
// If some other service would stall sleep, don't let sleep happen yet
if (doPreflightSleep()) {
setLed(false); // Never leave led on while in light sleep setLed(false); // Never leave led on while in light sleep
wakeCause = doLightSleep(sleepTime * 1000LL); wakeCause = doLightSleep(sleepTime * 1000LL);
if (wakeCause != ESP_SLEEP_WAKEUP_TIMER)
break; if (wakeCause == ESP_SLEEP_WAKEUP_TIMER) {
// Normal case: timer expired, we should just go back to sleep ASAP
setLed(true); // briefly turn on led setLed(true); // briefly turn on led
doLightSleep(1); wakeCause = doLightSleep(1); // leave led on for 1ms
if (wakeCause != ESP_SLEEP_WAKEUP_TIMER)
break;
secsSlept += sleepTime; secsSlept += sleepTime;
reached_ls_secs = secsSlept >= radioConfig.preferences.ls_secs; // DEBUG_MSG("sleeping, flash led!\n");
}
setLed(false);
if (reached_ls_secs) {
// stay in LS mode but let loop check whatever it wants
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
@ -81,6 +79,13 @@ static void lsIdle()
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?