fsm WIP might work

This commit is contained in:
geeksville 2020-02-22 13:14:10 -08:00
parent 509f9b6e2b
commit c7894f3bc5
8 changed files with 45 additions and 31 deletions

View File

@ -15,7 +15,7 @@ From lower to higher power consumption.
* light-sleep (LS) - CPU is suspended (RAM stays alive), radio is on, bluetooth is off, GPS is off. Note: currently GPS is not turned
off during light sleep, but there is a TODO item to fix this.
onEntry: setBluetoothOn(false), setGPSPower(false) - happens inside doLightSleep()
onEntry: setBluetoothOn(false), setGPSPower(false), doLightSleep()
onIdle: (if we wake because our led blink timer has expired) blink the led then go back to sleep until we sleep for ls_secs
onExit: setGPSPower(true), start trying to get gps lock: gps.startLock(), once lock arrives service.sendPosition(BROADCAST)

View File

@ -137,6 +137,12 @@ void GPS::doTask()
setPeriod(hasValidLocation && !wantNewLocation ? 30 * 1000 : 100);
}
void GPS::startLock()
{
wantNewLocation = true;
setPeriod(1);
}
String GPS::getTimeStr()
{
static char t[12]; // used to sprintf for Serial output

View File

@ -38,6 +38,9 @@ public:
/// Prepare the GPS for the cpu entering deep or light sleep, expect to be gone for at least 100s of msecs
void prepareSleep();
/// Restart our lock attempt - try to get and broadcast a GPS reading ASAP
void startLock();
private:
void readFromRTC();
};

View File

@ -5,6 +5,7 @@
#include "configuration.h"
#include "screen.h"
#include "PowerFSM.h"
#include "GPS.h"
static void sdsEnter()
{
@ -23,7 +24,7 @@ static void sdsEnter()
static void lsEnter()
{
/*
/*
// while we have bluetooth on, we can't do light sleep, but once off stay in light_sleep all the time
// we will wake from light sleep on button press or interrupt from the RF95 radio
if (!bluetoothOn && !is_screen_on() && service.radio.rf95.canSleep() && gps.canSleep())
@ -33,6 +34,8 @@ static void lsEnter()
delay(msecstosleep);
} */
gps.prepareSleep(); // abandon in-process parsing
setGPSPower(false); // kill GPS power
doLightSleep(radioConfig.preferences.ls_secs * 1000LL);
}
@ -43,7 +46,8 @@ static void lsIdle()
static void lsExit()
{
// nothing
setGPSPower(true); // restore GPS power
gps.startLock();
}
static void nbEnter()
@ -54,7 +58,7 @@ static void nbEnter()
static void darkEnter()
{
DEBUG_MSG("screen timeout, turn it off for now...\n");
screen.setOn(true);
screen.setOn(false);
}
static void onEnter()
@ -76,31 +80,33 @@ static void screenPress()
screen.onPress();
}
State stateSDS(sdsEnter, NULL, NULL);
State stateLS(lsEnter, lsIdle, lsExit);
State stateNB(nbEnter, NULL, NULL);
State stateDARK(darkEnter, NULL, NULL);
State stateON(onEnter, NULL, onExit);
State stateSDS(sdsEnter, NULL, NULL, "SDS");
State stateLS(lsEnter, lsIdle, lsExit, "LS");
State stateNB(nbEnter, NULL, NULL, "NB");
State stateDARK(darkEnter, NULL, NULL, "DARK");
State stateON(onEnter, NULL, onExit, "ON");
Fsm powerFSM(&stateDARK);
void PowerFSM_setup()
{
powerFSM.add_transition(&stateDARK, &stateON, EVENT_BOOT, NULL);
powerFSM.add_transition(&stateLS, &stateDARK, EVENT_WAKE_TIMER, wakeForPing);
powerFSM.add_transition(&stateLS, &stateNB, EVENT_RECEIVED_PACKET, NULL);
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");
powerFSM.add_transition(&stateLS, &stateON, EVENT_PRESS, NULL);
powerFSM.add_transition(&stateNB, &stateON, EVENT_PRESS, NULL);
powerFSM.add_transition(&stateDARK, &stateON, EVENT_PRESS, NULL);
powerFSM.add_transition(&stateON, &stateON, EVENT_PRESS, screenPress); // reenter On to restart our timers
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
powerFSM.add_transition(&stateLS, &stateON, EVENT_RECEIVED_TEXT_MSG, NULL);
powerFSM.add_transition(&stateNB, &stateON, EVENT_RECEIVED_TEXT_MSG, NULL);
powerFSM.add_transition(&stateDARK, &stateON, EVENT_RECEIVED_TEXT_MSG, NULL);
powerFSM.add_transition(&stateLS, &stateON, EVENT_RECEIVED_TEXT_MSG, NULL, "Received text");
powerFSM.add_transition(&stateNB, &stateON, EVENT_RECEIVED_TEXT_MSG, NULL, "Received text");
powerFSM.add_transition(&stateDARK, &stateON, EVENT_RECEIVED_TEXT_MSG, NULL, "Received text");
powerFSM.add_transition(&stateNB, &stateDARK, EVENT_PACKET_FOR_PHONE, NULL);
powerFSM.add_transition(&stateNB, &stateDARK, EVENT_PACKET_FOR_PHONE, NULL, "Packet for phone");
powerFSM.add_timed_transition(&stateON, &stateDARK, radioConfig.preferences.screen_on_secs, NULL);
powerFSM.add_timed_transition(&stateON, &stateDARK, radioConfig.preferences.screen_on_secs, NULL, "Screen-on timeout");
powerFSM.add_timed_transition(&stateDARK, &stateNB, radioConfig.preferences.phone_timeout_secs, NULL);
powerFSM.add_timed_transition(&stateDARK, &stateNB, radioConfig.preferences.phone_timeout_secs, NULL, "Phone timeout");
powerFSM.run_machine(); // run one interation of the state machine, so we run our on enter tasks for the initial DARK state
}

View File

@ -11,4 +11,6 @@
#define EVENT_RECEIVED_TEXT_MSG 5
#define EVENT_BOOT 6
extern Fsm powerFSM;
extern Fsm powerFSM;
void PowerFSM_setup();

View File

@ -50,7 +50,6 @@ bool isUSBPowered = false;
bool ssd1306_found = false;
bool axp192_found = false;
#define xstr(s) str(s)
#define str(s) #s
@ -58,7 +57,6 @@ bool axp192_found = false;
// Application
// -----------------------------------------------------------------------------
void scanI2Cdevice(void)
{
byte err, addr;
@ -188,7 +186,6 @@ void axp192Init()
#endif
}
const char *getDeviceName()
{
uint8_t dmac[6];
@ -208,7 +205,6 @@ void setup()
#endif
initDeepSleep();
// delay(1000); FIXME - remove
#ifdef VEXT_ENABLE
pinMode(VEXT_ENABLE, OUTPUT);
@ -267,6 +263,9 @@ void setup()
setBluetoothEnable(false);
setCPUFast(false); // 80MHz is fine for our slow peripherals
PowerFSM_setup();
powerFSM.trigger(EVENT_BOOT); // transition to ON, FIXME, only do this for cold boots, not waking from SDS
}
uint32_t ledBlinker()
@ -303,6 +302,7 @@ void loop()
{
uint32_t msecstosleep = 1000 * 30; // How long can we sleep before we again need to service the main loop?
powerFSM.run_machine();
gps.loop();
screen.loop();
service.loop();

View File

@ -213,8 +213,6 @@ void doLightSleep(uint64_t sleepMsec) // FIXME, use a more reasonable default
uint64_t sleepUsec = sleepMsec * 1000LL;
setBluetoothEnable(false); // has to be off before calling light sleep
gps.prepareSleep(); // abandon in-process parsing
setGPSPower(false); // kill GPS power
setLed(false); // Never leave led on while in light sleep
// NOTE! ESP docs say we must disable bluetooth and wifi before light sleep
@ -231,8 +229,6 @@ void doLightSleep(uint64_t sleepMsec) // FIXME, use a more reasonable default
esp_sleep_enable_timer_wakeup(sleepUsec);
esp_light_sleep_start();
DEBUG_MSG("Exit light sleep\n");
setGPSPower(true); // restore GPS power
}
#if 0

View File

@ -5,6 +5,7 @@
void doDeepSleep(uint64_t msecToWake);
void doLightSleep(uint64_t msecToWake);
void setBluetoothEnable(bool on);
void setGPSPower(bool on);
// Perform power on init that we do on each wake from deep sleep
void initDeepSleep();