2024-03-17 08:18:30 -05:00
|
|
|
#include "Default.h"
|
2022-05-07 20:31:21 +10:00
|
|
|
#include "NodeDB.h"
|
|
|
|
|
#include "PowerFSM.h"
|
2022-03-01 18:40:21 +00:00
|
|
|
#include "concurrency/OSThread.h"
|
2022-05-07 20:31:21 +10:00
|
|
|
#include "configuration.h"
|
2022-03-01 18:40:21 +00:00
|
|
|
#include "main.h"
|
|
|
|
|
#include "power.h"
|
|
|
|
|
|
|
|
|
|
namespace concurrency
|
|
|
|
|
{
|
|
|
|
|
/// Wrapper to convert our powerFSM stuff into a 'thread'
|
|
|
|
|
class PowerFSMThread : public OSThread
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
// callback returns the period for the next callback invocation (or 0 if we should no longer be called)
|
|
|
|
|
PowerFSMThread() : OSThread("PowerFSM") {}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
int32_t runOnce() override
|
|
|
|
|
{
|
2024-08-06 18:48:55 -05:00
|
|
|
#if !EXCLUDE_POWER_FSM
|
2022-03-01 18:40:21 +00:00
|
|
|
powerFSM.run_machine();
|
|
|
|
|
|
|
|
|
|
/// If we are in power state we force the CPU to wake every 10ms to check for serial characters (we don't yet wake
|
|
|
|
|
/// cpu for serial rx - FIXME)
|
2023-12-14 07:35:46 -06:00
|
|
|
const State *state = powerFSM.getState();
|
2022-03-01 18:40:21 +00:00
|
|
|
canSleep = (state != &statePOWER) && (state != &stateSERIAL);
|
|
|
|
|
|
|
|
|
|
if (powerStatus->getHasUSB()) {
|
|
|
|
|
timeLastPowered = millis();
|
2023-01-21 14:34:29 +01:00
|
|
|
} else if (config.power.on_battery_shutdown_after_secs > 0 && config.power.on_battery_shutdown_after_secs != UINT32_MAX &&
|
|
|
|
|
millis() > (timeLastPowered +
|
2024-03-17 08:18:30 -05:00
|
|
|
Default::getConfiguredOrDefaultMs(
|
2023-01-21 14:34:29 +01:00
|
|
|
config.power.on_battery_shutdown_after_secs))) { // shutdown after 30 minutes unpowered
|
2022-03-01 18:40:21 +00:00
|
|
|
powerFSM.trigger(EVENT_SHUTDOWN);
|
|
|
|
|
}
|
2022-05-07 20:31:21 +10:00
|
|
|
|
2023-12-08 11:13:15 -06:00
|
|
|
return 100;
|
2024-08-06 18:48:55 -05:00
|
|
|
#else
|
|
|
|
|
return INT32_MAX;
|
|
|
|
|
#endif
|
2022-03-01 18:40:21 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-05-07 20:31:21 +10:00
|
|
|
} // namespace concurrency
|