mirror of
https://github.com/meshtastic/firmware.git
synced 2025-12-20 17:52:35 +00:00
* Fix protobuf structs handling (#4140) * Fix protobuf structs handling * Log instead of assert --------- Co-authored-by: Ben Meadors <benmmeadors@gmail.com> * BLE based logging (#4146) * WIP log characteristic * Bluetooth logging plumbing * Characteristic * Callback * Check for nullptr * Esp32 bluetooth impl * Formatting * Add thread name and log level * Add settings guard * Remove comments * Field name * Fixes esp32 * Open it up * Whoops * Move va_end past our logic * Use `upload_protocol = esptool` as with the other heltec devices instead of `esp-builtin` (#4151) * Standardize lat/lon position logs (#4156) * Standardize lat/lon position logs * Missed sone and condensed logs * [create-pull-request] automated change (#4157) Co-authored-by: thebentern <9000580+thebentern@users.noreply.github.com> * Pause BLE logging during want_config flow (#4162) * Update NimBLE to 1.4.2 (#4163) * Implement replies for all telemetry types based on variant tag (#4164) * Implement replies for all telemetry types based on variant tag * Remove check for `ignoreRequest`: modules can set this, don't need to check --------- Co-authored-by: Ben Meadors <benmmeadors@gmail.com> * Esptool is better * Explicitly set characteristic * fix INA3221 sensor (#4168) - pass wire to begin() - remove redundant setAddr() (already set in header) * Show compass on waypoint frame; clear when waypoint deleted (#4116) * Clear expired or deleted waypoint frame * Return 0 to CallbackObserver * Add a missing comment * Draw compass for waypoint frame * Display our own waypoints * [create-pull-request] automated change (#4171) Co-authored-by: thebentern <9000580+thebentern@users.noreply.github.com> * Add semihosting support for nrf52 devices (#4137) * Turn off vscode cmake prompt - we don't use cmake on meshtastic * Add rak4631_dap variant for debugging with NanoDAP debug probe device. * The rak device can also run freertos (which is underneath nrf52 arduino) * Add semihosting support for nrf52840 devices Initial platformio.ini file only supports rak4630 Default to non TCP for the semihosting log output for now... Fixes https://github.com/meshtastic/firmware/issues/4135 * fix my botched merge - keep board_level = extra flag for rak3631_dbg --------- Co-authored-by: Ben Meadors <benmmeadors@gmail.com> * [create-pull-request] automated change (#4174) Co-authored-by: thebentern <9000580+thebentern@users.noreply.github.com> * Display alerts (#4170) * Move static functions into Screen.h, show compass during calibration * Move to _fontHeight macro to avoid collision * Move some alert functions to new alert handler * Catch missed reboot code * ESP32 fixes * Bump esp8266-oled-ssd1306 * Fixes for when a device has no screen * Use new startAlert(char*) helper class * Add EINK bits back to alert handling * Add noop class for no-display devices --------- Co-authored-by: Ben Meadors <benmmeadors@gmail.com> * Send file system manifest up on want_config (#4176) * Send file system manifest up on want_config * Platform specific methods * Helps to actually make the change * Clear * tell vscode, if formatting, use whatever our trunk formatter wants (#4186) without this flag if the user has set some other formatter (clang) in their user level settings, it will be looking in the wrong directory for the clang options (we want the options in .trunk/clang) Note: formatOnSave is true in master, which means a bunch of our older files are non compliant and if you edit them it will generate lots of formatting related diffs. I guess I'll start letting that happen with my future commits ;-). * fix the build - would loop forever if there were no files to send (#4188) * Show owner.short_name on boot (and E-Ink sleep screen) (#4134) * Show owner.short_name on boot and sleep screen (on e-ink) * Update Screen.cpp - new line for short_name Boot screen short_name now below the region setting. Looks better on small screens. * Draw short_name on right --------- Co-authored-by: Thomas Göttgens <tgoettgens@gmail.com> Co-authored-by: todd-herbert <herbert.todd@gmail.com> Co-authored-by: Ben Meadors <benmmeadors@gmail.com> * nrf52 soft device will watchdog if you use ICE while BT on... (#4189) so have debugger disable bluetooth. * correct xiao_ble build preventing sx1262 init (#4191) * Force a compile time failur if FromRadio or ToRadio get larger than (#4190) a BLE packet size. We are actually very close to this threshold so important to make sure we don't accidentally pass it. * Clear vector after complete config state (#4194) * Clear after complete config * Don't collect . entries * Log file name and size * [create-pull-request] automated change (#4200) Co-authored-by: thebentern <9000580+thebentern@users.noreply.github.com> * Make the logs Colorful! (#4199) * Squash needlessly static functions (#4183) * Trim extra vprintf and filter for unprintable characters * Deprecate Router Client role (and make it Client) (#4201) * [create-pull-request] automated change (#4205) Co-authored-by: thebentern <9000580+thebentern@users.noreply.github.com> * Move waypoint (#4202) * Move waypoint screen draw into the waypoint module * Get the observer set up for the waypoint screen draw * Static squashing: screen dimensions Macros moved back to Screen.cpp, as a band-aid until we eventually move all those static functions into the Screen class. * Move getCompassDiam into Screen class (supress compiler warnings) At this stage, the method is still static, because it's used by drawNodeInfo, which has no tidy reference to our screen instance. This is probably just another band-aid until these static functions all move. * Use new getCompassDiam function in AccelerometerThread * Properly gate display code in WaypointModule --------- Co-authored-by: Todd Herbert <herbert.todd@gmail.com> * Fix flakey phone api transition from file manifest to complete (#4209) * Try fix flakey phone api transition from file manifest to complete * Skip * enable colors in platformio serial monitor (#4217) * When talking via serial, encapsulate log messages in protobufs if necessary (#4187) * clean up RedirectablePrint::log so it doesn't have three very different implementations inline. * remove NoopPrint - it is no longer needed * when talking to API clients via serial, don't turn off log msgs instead encapsuate them * fix the build - would loop forever if there were no files to send * don't use Segger code if not talking to a Segger debugger * when encapsulating logs, make sure the strings always has nul terminators * nrf52 soft device will watchdog if you use ICE while BT on... so have debugger disable bluetooth. * Important to not print debug messages while writing to the toPhone scratch buffer * don't include newlines if encapsulating log records as protobufs --------- Co-authored-by: Ben Meadors <benmmeadors@gmail.com> * [create-pull-request] automated change (#4218) Co-authored-by: thebentern <9000580+thebentern@users.noreply.github.com> * Fix SHT41 support (#4222) * Add SHT41 Serial to I2c Detection Code On the Seeed Wio-WM1110 Dev Kit board, the SHT41 chip was being incorrectly detected as SHT31. This patch adds the necessary serial number for the SHT41 chip to be correctly detected. fixes meshtastic/firmware#4221 * Add missing sensor read for SHT41 * Typo fix in logs - mhz - MHz (#4225) As reported by karamo, a few different places in our logs had incorrect capitalization of MHz. fixes meshtastic/firmware#4126 * New new BLE logging characteristic with LogRecord protos (#4220) * New UUID * New log radio characteristic with LogRecord protobuf * LogRecord * Merge derp * How did you get there * Trunk * Fix length * Remove assert * minor cleanup proposal (#4169) * MESHTASTIC_EXCLUDE_WIFI and HAS_WIFI cleanup... Our code was checking HAS_WIFI and the new MESHTASTIC_EXCLUDE_WIFI flags in various places (even though EXCLUDE_WIFI forces HAS_WIFI to 0). Instead just check HAS_WIFI, only use EXCLUDE_WIFI inside configuration.h * cleanup: use HAS_NETWORKING instead of HAS_WIFI || HAS_ETHERNET We already had HAS_NETWORKING as flag in MQTT to mean 'we have tcpip'. Generallize that and move it into configuration.h so that we can use it elsewhere. * Use #pragma once, because supported by gcc and all modern compilers instead of #ifdef DOTHFILE_H etc... --------- Co-authored-by: Jonathan Bennett <jbennett@incomsystems.biz> * Add PowerMon support (#4155) * Turn off vscode cmake prompt - we don't use cmake on meshtastic * Add rak4631_dap variant for debugging with NanoDAP debug probe device. * The rak device can also run freertos (which is underneath nrf52 arduino) * Add semihosting support for nrf52840 devices Initial platformio.ini file only supports rak4630 Default to non TCP for the semihosting log output for now... Fixes https://github.com/meshtastic/firmware/issues/4135 * powermon WIP (for https://github.com/meshtastic/firmware/issues/4136 ) * oops - mean't to mark the _dbg variant as an 'extra' board. * powermon wip * Make serial port on wio-sdk-wm1110 board work By disabling the (inaccessible) adafruit USB * Instrument (radiolib only for now) lora for powermon per https://github.com/meshtastic/firmware/issues/4136 * powermon gps support https://github.com/meshtastic/firmware/issues/4136 * Add CPU deep and light sleep powermon states https://github.com/meshtastic/firmware/issues/4136 * Change the board/swversion bootstring so it is a new "structured" log msg. * powermon wip * add example script for getting esp S3 debugging working Not yet used but I didn't want these nasty tricks to get lost yet. * Add PowerMon reporting for screen and bluetooth pwr. * make power.powermon_enables config setting work. * update to latest protobufs * fix bogus shellcheck warning * make powermon optional (but default enabled because tiny and no runtime impact) * tell vscode, if formatting, use whatever our trunk formatter wants without this flag if the user has set some other formatter (clang) in their user level settings, it will be looking in the wrong directory for the clang options (we want the options in .trunk/clang) Note: formatOnSave is true in master, which means a bunch of our older files are non compliant and if you edit them it will generate lots of formatting related diffs. I guess I'll start letting that happen with my future commits ;-). * add PowerStress module * nrf52 arduino is built upon freertos, so let platformio debug it * don't accidentally try to Segger ICE if we are using another ICE * clean up RedirectablePrint::log so it doesn't have three very different implementations inline. * remove NoopPrint - it is no longer needed * when talking to API clients via serial, don't turn off log msgs instead encapsuate them * fix the build - would loop forever if there were no files to send * don't use Segger code if not talking to a Segger debugger * when encapsulating logs, make sure the strings always has nul terminators * nrf52 soft device will watchdog if you use ICE while BT on... so have debugger disable bluetooth. * Important to not print debug messages while writing to the toPhone scratch buffer * don't include newlines if encapsulating log records as protobufs * update to latest protobufs (needed for powermon goo) * PowerStress WIP * fix linter warning * Cleanup buffer * Merge hex for wm1110 target(s) * Only sdk * Sudo * Fix exclude macros (#4233) * fix MESHTASTIC_EXCLUDE_BLUETOOTH * fix HAS_SCREEN=0 * fix MESHTASTIC_EXCLUDE_GPS * fix typo in build-nrf52.sh (#4231) chmod is the command, '+x' is the argument. * Tidy Wireless Paper variant files (#4238) * Quick tidy of pins_arduino.h Matches requests made at https://github.com/meshtastic/firmware/pull/4226#discussion_r1664183480) * Tidy variant.h * Change deprecated ADC attenuation parameter From 11dB to 12dB. Resolves compiler warning. Allegly, no impact on function: `This is deprecated, it behaves the same as `ADC_ATTEN_DB_12` * Updated raspbian CI to update apt repository ahead of libbluetooth. (#4243) * Fix BLE logging on nrf52 (#4244) * allow ble logrecords to be fetched either by NOTIFY or INDICATE ble types This allows 'lossless' log reading. If client has requested INDICATE (rather than NOTIFY) each log record emitted via log() will have to fetched by the client device before the meshtastic node can continue. * Fix serious problem with nrf52 BLE logging. When doing notifies of LogRecords it is important to use the binary write routines - writing using the 'string' write won't work. Because protobufs can contain \0 nuls inside of them which if being parsed as a string will cause only a portion of the protobuf to be sent. I noticed this because some log messages were not getting through. --------- Co-authored-by: Ben Meadors <benmmeadors@gmail.com> * Fix build when HAS_NETWORKING is false on nrf52 (#4237) (tested on a rak4631 by setting HAS_ETHERNET false when shrinking image) * If `toPhoneQueue` is full, still increment `fromNum` to avoid client never getting packets (#4246) * Update to SoftDevice 7.3.0 for wio-sdk-wm1110 and wio-tracker-wm1110 (#4248) * Update variant.h * Update wio-tracker-wm1110.json * Update wio-sdk-wm1110.json * Update platformio.ini * Update platformio.ini * Add files via upload * Add files via upload * Update variant.h --------- Co-authored-by: Mike <mikhael.skvortsov@gmail.com> Co-authored-by: Ben Meadors <benmmeadors@gmail.com> Co-authored-by: Mike G <mkgin@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: thebentern <9000580+thebentern@users.noreply.github.com> Co-authored-by: GUVWAF <78759985+GUVWAF@users.noreply.github.com> Co-authored-by: Warren Guy <5602790+warrenguy@users.noreply.github.com> Co-authored-by: todd-herbert <herbert.todd@gmail.com> Co-authored-by: geeksville <kevinh@geeksville.com> Co-authored-by: Jonathan Bennett <jbennett@incomsystems.biz> Co-authored-by: Alexander <156134901+Dorn8010@users.noreply.github.com> Co-authored-by: Thomas Göttgens <tgoettgens@gmail.com> Co-authored-by: quimnut <github@dopegoat.com> Co-authored-by: Manuel <71137295+mverch67@users.noreply.github.com> Co-authored-by: Agent Blu, 006 <blu006@ucr.edu> Co-authored-by: Mark Trevor Birss <markbirss@gmail.com>
411 lines
18 KiB
C++
411 lines
18 KiB
C++
/**
|
|
* @file PowerFSM.cpp
|
|
* @brief Implements the finite state machine for power management.
|
|
*
|
|
* This file contains the implementation of the finite state machine (FSM) for power management.
|
|
* The FSM controls the power states of the device, including SDS (shallow deep sleep), LS (light sleep),
|
|
* NB (normal mode), and POWER (powered mode). The FSM also handles transitions between states and
|
|
* actions to be taken upon entering or exiting each state.
|
|
*/
|
|
#include "PowerFSM.h"
|
|
#include "Default.h"
|
|
#include "MeshService.h"
|
|
#include "NodeDB.h"
|
|
#include "PowerMon.h"
|
|
#include "configuration.h"
|
|
#include "graphics/Screen.h"
|
|
#include "main.h"
|
|
#include "sleep.h"
|
|
#include "target_specific.h"
|
|
|
|
#ifndef SLEEP_TIME
|
|
#define SLEEP_TIME 30
|
|
#endif
|
|
|
|
/// Should we behave as if we have AC power now?
|
|
static bool isPowered()
|
|
{
|
|
// Circumvent the battery sensing logic and assumes constant power if no battery pin or power mgmt IC
|
|
#if !defined(BATTERY_PIN) && !defined(HAS_AXP192) && !defined(HAS_AXP2101)
|
|
return true;
|
|
#endif
|
|
|
|
bool isRouter = (config.device.role == meshtastic_Config_DeviceConfig_Role_ROUTER ? 1 : 0);
|
|
|
|
// If we are not a router and we already have AC power go to POWER state after init, otherwise go to ON
|
|
// We assume routers might be powered all the time, but from a low current (solar) source
|
|
bool isPowerSavingMode = config.power.is_power_saving || isRouter;
|
|
|
|
/* To determine if we're externally powered, assumptions
|
|
1) If we're powered up and there's no battery, we must be getting power externally. (because we'd be dead otherwise)
|
|
|
|
2) If we detect USB power from the power management chip, we must be getting power externally.
|
|
|
|
3) On some boards we don't have the power management chip (like AXPxxxx) so we use EXT_PWR_DETECT GPIO pin to detect
|
|
external power source (see `isVbusIn()` in `Power.cpp`)
|
|
*/
|
|
return !isPowerSavingMode && powerStatus && (!powerStatus->getHasBattery() || powerStatus->getHasUSB());
|
|
}
|
|
|
|
static void sdsEnter()
|
|
{
|
|
LOG_DEBUG("Enter state: SDS\n");
|
|
powerMon->setState(meshtastic_PowerMon_State_CPU_DeepSleep);
|
|
// FIXME - make sure GPS and LORA radio are off first - because we want close to zero current draw
|
|
doDeepSleep(Default::getConfiguredOrDefaultMs(config.power.sds_secs), false);
|
|
}
|
|
|
|
extern Power *power;
|
|
|
|
static void shutdownEnter()
|
|
{
|
|
LOG_DEBUG("Enter state: SHUTDOWN\n");
|
|
power->shutdown();
|
|
}
|
|
|
|
#include "error.h"
|
|
|
|
static uint32_t secsSlept;
|
|
|
|
static void lsEnter()
|
|
{
|
|
LOG_INFO("lsEnter begin, ls_secs=%u\n", config.power.ls_secs);
|
|
powerMon->clearState(meshtastic_PowerMon_State_Screen_On);
|
|
screen->setOn(false);
|
|
secsSlept = 0; // How long have we been sleeping this time
|
|
|
|
// LOG_INFO("lsEnter end\n");
|
|
}
|
|
|
|
static void lsIdle()
|
|
{
|
|
// LOG_INFO("lsIdle begin ls_secs=%u\n", getPref_ls_secs());
|
|
|
|
#ifdef ARCH_ESP32
|
|
|
|
// Do we have more sleeping to do?
|
|
if (secsSlept < config.power.ls_secs) {
|
|
// If some other service would stall sleep, don't let sleep happen yet
|
|
if (doPreflightSleep()) {
|
|
// Briefly come out of sleep long enough to blink the led once every few seconds
|
|
uint32_t sleepTime = SLEEP_TIME;
|
|
|
|
powerMon->setState(meshtastic_PowerMon_State_CPU_LightSleep);
|
|
setLed(false); // Never leave led on while in light sleep
|
|
esp_sleep_source_t wakeCause2 = doLightSleep(sleepTime * 1000LL);
|
|
powerMon->clearState(meshtastic_PowerMon_State_CPU_LightSleep);
|
|
|
|
switch (wakeCause2) {
|
|
case ESP_SLEEP_WAKEUP_TIMER:
|
|
// Normal case: timer expired, we should just go back to sleep ASAP
|
|
|
|
setLed(true); // briefly turn on led
|
|
wakeCause2 = doLightSleep(100); // leave led on for 1ms
|
|
|
|
secsSlept += sleepTime;
|
|
// LOG_INFO("sleeping, flash led!\n");
|
|
break;
|
|
|
|
case ESP_SLEEP_WAKEUP_UART:
|
|
// Not currently used (because uart triggers in hw have problems)
|
|
powerFSM.trigger(EVENT_SERIAL_CONNECTED);
|
|
break;
|
|
|
|
default:
|
|
// We woke for some other reason (button press, device IRQ interrupt)
|
|
|
|
#ifdef BUTTON_PIN
|
|
bool pressed = !digitalRead(config.device.button_gpio ? config.device.button_gpio : BUTTON_PIN);
|
|
#else
|
|
bool pressed = false;
|
|
#endif
|
|
if (pressed) { // If we woke because of press, instead generate a PRESS event.
|
|
powerFSM.trigger(EVENT_PRESS);
|
|
} else {
|
|
// Otherwise let the NB state handle the IRQ (and that state will handle stuff like IRQs etc)
|
|
// we lie and say "wake timer" because the interrupt will be handled by the regular IRQ code
|
|
powerFSM.trigger(EVENT_WAKE_TIMER);
|
|
}
|
|
break;
|
|
}
|
|
} else {
|
|
// Someone says we can't sleep now, so just save some power by sleeping the CPU for 100ms or so
|
|
delay(100);
|
|
}
|
|
} else {
|
|
// Time to stop sleeping!
|
|
setLed(false);
|
|
LOG_INFO("Reached ls_secs, servicing loop()\n");
|
|
powerFSM.trigger(EVENT_WAKE_TIMER);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
static void lsExit()
|
|
{
|
|
LOG_INFO("Exit state: LS\n");
|
|
}
|
|
|
|
static void nbEnter()
|
|
{
|
|
LOG_DEBUG("Enter state: NB\n");
|
|
powerMon->clearState(meshtastic_PowerMon_State_BT_On);
|
|
screen->setOn(false);
|
|
#ifdef ARCH_ESP32
|
|
// Only ESP32 should turn off bluetooth
|
|
setBluetoothEnable(false);
|
|
#endif
|
|
|
|
// FIXME - check if we already have packets for phone and immediately trigger EVENT_PACKETS_FOR_PHONE
|
|
}
|
|
|
|
static void darkEnter()
|
|
{
|
|
powerMon->clearState(meshtastic_PowerMon_State_BT_On);
|
|
powerMon->clearState(meshtastic_PowerMon_State_Screen_On);
|
|
setBluetoothEnable(true);
|
|
screen->setOn(false);
|
|
}
|
|
|
|
static void serialEnter()
|
|
{
|
|
LOG_DEBUG("Enter state: SERIAL\n");
|
|
powerMon->clearState(meshtastic_PowerMon_State_BT_On);
|
|
powerMon->setState(meshtastic_PowerMon_State_Screen_On);
|
|
setBluetoothEnable(false);
|
|
screen->setOn(true);
|
|
screen->print("Serial connected\n");
|
|
}
|
|
|
|
static void serialExit()
|
|
{
|
|
// Turn bluetooth back on when we leave serial stream API
|
|
powerMon->setState(meshtastic_PowerMon_State_BT_On);
|
|
setBluetoothEnable(true);
|
|
screen->print("Serial disconnected\n");
|
|
}
|
|
|
|
static void powerEnter()
|
|
{
|
|
// LOG_DEBUG("Enter state: POWER\n");
|
|
if (!isPowered()) {
|
|
// If we got here, we are in the wrong state - we should be in powered, let that state ahndle things
|
|
LOG_INFO("Loss of power in Powered\n");
|
|
powerFSM.trigger(EVENT_POWER_DISCONNECTED);
|
|
} else {
|
|
powerMon->setState(meshtastic_PowerMon_State_BT_On);
|
|
powerMon->setState(meshtastic_PowerMon_State_Screen_On);
|
|
screen->setOn(true);
|
|
setBluetoothEnable(true);
|
|
// within enter() the function getState() returns the state we came from
|
|
|
|
// Mothballed: print change of power-state to device screen
|
|
/* if (strcmp(powerFSM.getState()->name, "BOOT") != 0 && strcmp(powerFSM.getState()->name, "POWER") != 0 &&
|
|
strcmp(powerFSM.getState()->name, "DARK") != 0) {
|
|
screen->print("Powered...\n");
|
|
}*/
|
|
}
|
|
}
|
|
|
|
static void powerIdle()
|
|
{
|
|
if (!isPowered()) {
|
|
// If we got here, we are in the wrong state
|
|
LOG_INFO("Loss of power in Powered\n");
|
|
powerFSM.trigger(EVENT_POWER_DISCONNECTED);
|
|
}
|
|
}
|
|
|
|
static void powerExit()
|
|
{
|
|
powerMon->setState(meshtastic_PowerMon_State_BT_On);
|
|
powerMon->setState(meshtastic_PowerMon_State_Screen_On);
|
|
screen->setOn(true);
|
|
setBluetoothEnable(true);
|
|
|
|
// Mothballed: print change of power-state to device screen
|
|
/*if (!isPowered())
|
|
screen->print("Unpowered...\n");*/
|
|
}
|
|
|
|
static void onEnter()
|
|
{
|
|
LOG_DEBUG("Enter state: ON\n");
|
|
powerMon->setState(meshtastic_PowerMon_State_BT_On);
|
|
powerMon->setState(meshtastic_PowerMon_State_Screen_On);
|
|
screen->setOn(true);
|
|
setBluetoothEnable(true);
|
|
}
|
|
|
|
static void onIdle()
|
|
{
|
|
if (isPowered()) {
|
|
// If we got here, we are in the wrong state - we should be in powered, let that state ahndle things
|
|
powerFSM.trigger(EVENT_POWER_CONNECTED);
|
|
}
|
|
}
|
|
|
|
static void screenPress()
|
|
{
|
|
screen->onPress();
|
|
}
|
|
|
|
static void bootEnter()
|
|
{
|
|
LOG_DEBUG("Enter state: BOOT\n");
|
|
}
|
|
|
|
State stateSHUTDOWN(shutdownEnter, NULL, NULL, "SHUTDOWN");
|
|
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 stateSERIAL(serialEnter, NULL, serialExit, "SERIAL");
|
|
State stateBOOT(bootEnter, NULL, NULL, "BOOT");
|
|
State stateON(onEnter, onIdle, NULL, "ON");
|
|
State statePOWER(powerEnter, powerIdle, powerExit, "POWER");
|
|
Fsm powerFSM(&stateBOOT);
|
|
|
|
void PowerFSM_setup()
|
|
{
|
|
bool isRouter = (config.device.role == meshtastic_Config_DeviceConfig_Role_ROUTER ? 1 : 0);
|
|
bool isTrackerOrSensor = config.device.role == meshtastic_Config_DeviceConfig_Role_TRACKER ||
|
|
config.device.role == meshtastic_Config_DeviceConfig_Role_TAK_TRACKER ||
|
|
config.device.role == meshtastic_Config_DeviceConfig_Role_SENSOR;
|
|
bool hasPower = isPowered();
|
|
|
|
LOG_INFO("PowerFSM init, USB power=%d\n", hasPower ? 1 : 0);
|
|
powerFSM.add_timed_transition(&stateBOOT, hasPower ? &statePOWER : &stateON, 3 * 1000, NULL, "boot timeout");
|
|
|
|
// wake timer expired or a packet arrived
|
|
// if we are a router node, we go to NB (no need for bluetooth) otherwise we go to DARK (so we can send message to phone)
|
|
#ifdef ARCH_ESP32
|
|
powerFSM.add_transition(&stateLS, isRouter ? &stateNB : &stateDARK, EVENT_WAKE_TIMER, NULL, "Wake timer");
|
|
#else // Don't go into a no-bluetooth state on low power platforms
|
|
powerFSM.add_transition(&stateLS, &stateDARK, EVENT_WAKE_TIMER, NULL, "Wake timer");
|
|
#endif
|
|
|
|
// We need this transition, because we might not transition if we were waiting to enter light-sleep, because when we wake from
|
|
// light sleep we _always_ transition to NB or dark and
|
|
powerFSM.add_transition(&stateLS, isRouter ? &stateNB : &stateDARK, EVENT_PACKET_FOR_PHONE, NULL,
|
|
"Received packet, exiting light sleep");
|
|
powerFSM.add_transition(&stateNB, &stateNB, EVENT_PACKET_FOR_PHONE, NULL, "Received packet, resetting win wake");
|
|
|
|
// Handle press events - note: we ignore button presses when in API mode
|
|
powerFSM.add_transition(&stateLS, &stateON, EVENT_PRESS, NULL, "Press");
|
|
powerFSM.add_transition(&stateNB, &stateON, EVENT_PRESS, NULL, "Press");
|
|
powerFSM.add_transition(&stateDARK, isPowered() ? &statePOWER : &stateON, EVENT_PRESS, NULL, "Press");
|
|
powerFSM.add_transition(&statePOWER, &statePOWER, EVENT_PRESS, screenPress, "Press");
|
|
powerFSM.add_transition(&stateON, &stateON, EVENT_PRESS, screenPress, "Press"); // reenter On to restart our timers
|
|
powerFSM.add_transition(&stateSERIAL, &stateSERIAL, EVENT_PRESS, screenPress,
|
|
"Press"); // Allow button to work while in serial API
|
|
|
|
// Handle critically low power battery by forcing deep sleep
|
|
powerFSM.add_transition(&stateBOOT, &stateSDS, EVENT_LOW_BATTERY, NULL, "LowBat");
|
|
powerFSM.add_transition(&stateLS, &stateSDS, EVENT_LOW_BATTERY, NULL, "LowBat");
|
|
powerFSM.add_transition(&stateNB, &stateSDS, EVENT_LOW_BATTERY, NULL, "LowBat");
|
|
powerFSM.add_transition(&stateDARK, &stateSDS, EVENT_LOW_BATTERY, NULL, "LowBat");
|
|
powerFSM.add_transition(&stateON, &stateSDS, EVENT_LOW_BATTERY, NULL, "LowBat");
|
|
powerFSM.add_transition(&stateSERIAL, &stateSDS, EVENT_LOW_BATTERY, NULL, "LowBat");
|
|
|
|
// Handle being told to power off
|
|
powerFSM.add_transition(&stateBOOT, &stateSHUTDOWN, EVENT_SHUTDOWN, NULL, "Shutdown");
|
|
powerFSM.add_transition(&stateLS, &stateSHUTDOWN, EVENT_SHUTDOWN, NULL, "Shutdown");
|
|
powerFSM.add_transition(&stateNB, &stateSHUTDOWN, EVENT_SHUTDOWN, NULL, "Shutdown");
|
|
powerFSM.add_transition(&stateDARK, &stateSHUTDOWN, EVENT_SHUTDOWN, NULL, "Shutdown");
|
|
powerFSM.add_transition(&stateON, &stateSHUTDOWN, EVENT_SHUTDOWN, NULL, "Shutdown");
|
|
powerFSM.add_transition(&stateSERIAL, &stateSHUTDOWN, EVENT_SHUTDOWN, NULL, "Shutdown");
|
|
|
|
// Inputbroker
|
|
powerFSM.add_transition(&stateLS, &stateON, EVENT_INPUT, NULL, "Input Device");
|
|
powerFSM.add_transition(&stateNB, &stateON, EVENT_INPUT, NULL, "Input Device");
|
|
powerFSM.add_transition(&stateDARK, &stateON, EVENT_INPUT, NULL, "Input Device");
|
|
powerFSM.add_transition(&stateON, &stateON, EVENT_INPUT, NULL, "Input Device"); // restarts the sleep timer
|
|
powerFSM.add_transition(&statePOWER, &statePOWER, EVENT_INPUT, NULL, "Input Device"); // restarts the sleep timer
|
|
|
|
powerFSM.add_transition(&stateDARK, &stateON, EVENT_BLUETOOTH_PAIR, NULL, "Bluetooth pairing");
|
|
powerFSM.add_transition(&stateON, &stateON, EVENT_BLUETOOTH_PAIR, NULL, "Bluetooth pairing");
|
|
|
|
// if we are a router we don't turn the screen on for these things
|
|
if (!isRouter) {
|
|
// if any packet destined for phone arrives, turn on bluetooth at least
|
|
powerFSM.add_transition(&stateNB, &stateDARK, EVENT_PACKET_FOR_PHONE, NULL, "Packet for phone");
|
|
|
|
// show the latest node when we get a new node db update
|
|
powerFSM.add_transition(&stateNB, &stateON, EVENT_NODEDB_UPDATED, NULL, "NodeDB update");
|
|
powerFSM.add_transition(&stateDARK, &stateON, EVENT_NODEDB_UPDATED, NULL, "NodeDB update");
|
|
powerFSM.add_transition(&stateON, &stateON, EVENT_NODEDB_UPDATED, NULL, "NodeDB update");
|
|
|
|
// Show the received text message
|
|
powerFSM.add_transition(&stateLS, &stateON, EVENT_RECEIVED_MSG, NULL, "Received text");
|
|
powerFSM.add_transition(&stateNB, &stateON, EVENT_RECEIVED_MSG, NULL, "Received text");
|
|
powerFSM.add_transition(&stateDARK, &stateON, EVENT_RECEIVED_MSG, NULL, "Received text");
|
|
powerFSM.add_transition(&stateON, &stateON, EVENT_RECEIVED_MSG, NULL, "Received text"); // restarts the sleep timer
|
|
}
|
|
|
|
// If we are not in statePOWER but get a serial connection, suppress sleep (and keep the screen on) while connected
|
|
powerFSM.add_transition(&stateLS, &stateSERIAL, EVENT_SERIAL_CONNECTED, NULL, "serial API");
|
|
powerFSM.add_transition(&stateNB, &stateSERIAL, EVENT_SERIAL_CONNECTED, NULL, "serial API");
|
|
powerFSM.add_transition(&stateDARK, &stateSERIAL, EVENT_SERIAL_CONNECTED, NULL, "serial API");
|
|
powerFSM.add_transition(&stateON, &stateSERIAL, EVENT_SERIAL_CONNECTED, NULL, "serial API");
|
|
powerFSM.add_transition(&statePOWER, &stateSERIAL, EVENT_SERIAL_CONNECTED, NULL, "serial API");
|
|
|
|
// If we get power connected, go to the power connect state
|
|
powerFSM.add_transition(&stateLS, &statePOWER, EVENT_POWER_CONNECTED, NULL, "power connect");
|
|
powerFSM.add_transition(&stateNB, &statePOWER, EVENT_POWER_CONNECTED, NULL, "power connect");
|
|
powerFSM.add_transition(&stateDARK, &statePOWER, EVENT_POWER_CONNECTED, NULL, "power connect");
|
|
powerFSM.add_transition(&stateON, &statePOWER, EVENT_POWER_CONNECTED, NULL, "power connect");
|
|
|
|
powerFSM.add_transition(&statePOWER, &stateON, EVENT_POWER_DISCONNECTED, NULL, "power disconnected");
|
|
// powerFSM.add_transition(&stateSERIAL, &stateON, EVENT_POWER_DISCONNECTED, NULL, "power disconnected");
|
|
|
|
// the only way to leave state serial is for the client to disconnect (or we timeout and force disconnect them)
|
|
// when we leave, go to ON (which might not be the correct state if we have power connected, we will fix that in onEnter)
|
|
powerFSM.add_transition(&stateSERIAL, &stateON, EVENT_SERIAL_DISCONNECTED, NULL, "serial disconnect");
|
|
|
|
powerFSM.add_transition(&stateDARK, &stateDARK, EVENT_CONTACT_FROM_PHONE, NULL, "Contact from phone");
|
|
|
|
#ifdef USE_EINK
|
|
// Allow E-Ink devices to suppress the screensaver, if screen timeout set to 0
|
|
if (config.display.screen_on_secs > 0)
|
|
#endif
|
|
{
|
|
powerFSM.add_timed_transition(&stateON, &stateDARK,
|
|
Default::getConfiguredOrDefaultMs(config.display.screen_on_secs, default_screen_on_secs),
|
|
NULL, "Screen-on timeout");
|
|
powerFSM.add_timed_transition(&statePOWER, &stateDARK,
|
|
Default::getConfiguredOrDefaultMs(config.display.screen_on_secs, default_screen_on_secs),
|
|
NULL, "Screen-on timeout");
|
|
}
|
|
|
|
// We never enter light-sleep or NB states on NRF52 (because the CPU uses so little power normally)
|
|
#ifdef ARCH_ESP32
|
|
// See: https://github.com/meshtastic/firmware/issues/1071
|
|
// Don't add power saving transitions if we are a power saving tracker or sensor. Sleep will be initiatiated through the
|
|
// modules
|
|
if ((isRouter || config.power.is_power_saving) && !isTrackerOrSensor) {
|
|
powerFSM.add_timed_transition(&stateNB, &stateLS,
|
|
Default::getConfiguredOrDefaultMs(config.power.min_wake_secs, default_min_wake_secs), NULL,
|
|
"Min wake timeout");
|
|
|
|
// If ESP32 and using power-saving, timer mover from DARK to light-sleep
|
|
// Also serves purpose of the old DARK to DARK transition(?) See https://github.com/meshtastic/firmware/issues/3517
|
|
powerFSM.add_timed_transition(
|
|
&stateDARK, &stateLS,
|
|
Default::getConfiguredOrDefaultMs(config.power.wait_bluetooth_secs, default_wait_bluetooth_secs), NULL,
|
|
"Bluetooth timeout");
|
|
} else {
|
|
// If ESP32, but not using power-saving, check periodically if config has drifted out of stateDark
|
|
powerFSM.add_timed_transition(&stateDARK, &stateDARK,
|
|
Default::getConfiguredOrDefaultMs(config.display.screen_on_secs, default_screen_on_secs),
|
|
NULL, "Screen-on timeout");
|
|
}
|
|
#else
|
|
// If not ESP32, light-sleep not used. Check periodically if config has drifted out of stateDark
|
|
powerFSM.add_timed_transition(&stateDARK, &stateDARK,
|
|
Default::getConfiguredOrDefaultMs(config.display.screen_on_secs, default_screen_on_secs), NULL,
|
|
"Screen-on timeout");
|
|
#endif
|
|
|
|
powerFSM.run_machine(); // run one iteration of the state machine, so we run our on enter tasks for the initial DARK state
|
|
} |