GPS PowerSaving v2: Added GPS wake up upon telemtry requests for companions. Bug fixes for "gps" CLI.

This commit is contained in:
Kevin Le committed 2026-09-25 22:37:04 +07:00
1 parent d9c42964eb
commit 1aa8ae90fb
16 files changed
+177 -201

No files matched your search

+27 -27
View File
@@ -397,38 +397,41 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
int sats = l->satellitesCount();
bool active = !strcmp(_sensors->getSettingByKey("gps") ?: "0", "1"); // Use "0" if GPS setting is not found
if (_prefs->powersaving_enabled && l->isPowerSavingEnabled()) { // GPS Power Saving
if (enabled) {
unsigned long mins = (l->getNextSleep() - millis()) / 60000UL;
sprintf(reply, "on (powersaving, sleep in %luh %lum), %s, %s, %d sats",
mins / 60UL,
mins % 60UL,
active ? "active" : "deactivated",
fix ? "fix" : "no fix",
sats);
} else {
unsigned long mins = (l->getNextWake() - millis()) / 60000UL;
sprintf(reply, "off (powersaving, wake in %luh %lum)",
mins / 60UL,
mins % 60UL);
}
if (l->isPowerSavingEnabled()) { // GPS Power Saving
if(active) {
if (enabled) {
unsigned long mins = (l->getNextSleep() - millis()) / 60000UL;
sprintf(reply, "on, powered (powersaving, sleep in %luh %lum), %s, %d sats",
mins / 60UL,
mins % 60UL,
fix ? "fix" : "no fix",
sats);
} else {
unsigned long mins = (l->getNextWake() - millis()) / 60000UL;
sprintf(reply, "on, unpowered (powersaving, wake in %luh %lum)",
mins / 60UL,
mins % 60UL);
}
// "last sync" from GPS
DateTime dt = DateTime(l->getLastValidTimeSync());
if (dt.unixtime() == 0) {
sprintf(reply + strlen(reply), ", last sync: none");
// "last sync" from GPS
DateTime dt = DateTime(l->getLastValidTimeSync());
if (dt.unixtime() == 0) {
sprintf(reply + strlen(reply), ", last sync: none");
} else {
sprintf(reply + strlen(reply), ", last sync: %02d:%02d - %d/%d/%d UTC", dt.hour(), dt.minute(),
dt.day(), dt.month(), dt.year());
}
} else {
sprintf(reply + strlen(reply), ", last sync: %02d:%02d - %d/%d/%d UTC", dt.hour(), dt.minute(),
dt.day(), dt.month(), dt.year());
sprintf(reply, "off, %s", enabled ? "powered" : "unpowered");
}
} else { // Normal mode
if (enabled) {
if(active) {
sprintf(reply, "on, %s, %s, %d sats",
active?"active":"deactivated",
enabled?"powered":"unpowered",
fix?"fix":"no fix",
sats);
} else {
strcpy(reply, "off");
sprintf(reply, "off, %s", enabled ? "powered" : "unpowered");
}
}
} else {
@@ -438,12 +441,10 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
} else if (memcmp(command, "powersaving on", 14) == 0) {
#if defined(NRF52_PLATFORM)
_prefs->powersaving_enabled = 1;
_sensors->powersaving_enabled = 1;
savePrefs();
strcpy(reply, "on - Immediate effect");
#elif defined(ESP32) && !defined(WITH_BRIDGE)
_prefs->powersaving_enabled = 1;
_sensors->powersaving_enabled = 1;
savePrefs();
strcpy(reply, "on - After 2 minutes");
#elif defined(WITH_BRIDGE)
@@ -453,7 +454,6 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
#endif
} else if (memcmp(command, "powersaving off", 15) == 0) {
_prefs->powersaving_enabled = 0;
_sensors->powersaving_enabled = 0;
savePrefs();
strcpy(reply, "off");
} else if (memcmp(command, "powersaving", 11) == 0) {
+1 -2
View File
@@ -13,9 +13,8 @@ class SensorManager {
public:
double node_lat, node_lon; // modify these, if you want to affect Advert location
double node_altitude; // altitude in meters
bool powersaving_enabled; // powersaving mode
SensorManager() { node_lat = 0; node_lon = 0; node_altitude = 0; powersaving_enabled = false; }
SensorManager() { node_lat = 0; node_lon = 0; node_altitude = 0; }
virtual bool begin() { return false; }
virtual bool querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) { return false; }
virtual void loop() { }
@@ -740,16 +740,10 @@ bool EnvironmentSensorManager::setSettingValue(const char* name, const char* val
#if ENV_INCLUDE_GPS
if (gps_detected && strcmp(name, "gps") == 0) {
if (strcmp(value, "0") == 0) {
if (powersaving_enabled) {
_location->enablePowerSaving(false);
}
gps_active = false; // Disabled by CLI or App
stop_gps();
} else {
if (powersaving_enabled) {
_location->enablePowerSaving(true);
}
gps_active = true; // Enabled by CLI or App
start_gps();
}
return true;
@@ -889,10 +883,12 @@ bool EnvironmentSensorManager::gpsIsAwake(uint8_t ioPin) {
void EnvironmentSensorManager::start_gps() {
gps_active = true;
if (powersaving_enabled && _location->isPowerSavingEnabled()) {
if (_location->isPowerSavingEnabled()) {
gps_wake = true; // gps_active is true
_location->syncTime(); // Clear GPS data and force sync time
_location->setNextSleep(); // Next time to off
} else {
gps_wake = true;
}
#ifdef RAK_WISBLOCK_GPS
@@ -915,13 +911,13 @@ void EnvironmentSensorManager::start_gps() {
}
void EnvironmentSensorManager::stop_gps() {
if (powersaving_enabled && _location->isPowerSavingEnabled()) {
gps_wake = false; // gps_active is unchanged (true) even the GPS sleep (e.g: off)
if (_location->isPowerSavingEnabled()) {
gps_wake = false; // gps_active is unchanged (true for GPS sleep, false for GPS off)
_location->stopTimeSync(); // Stop time sync
_location->setNextWake(); // Next time to on
} else {
gps_active = false;
gps_wake = false; // When GPS is off, wake is false to be sure
gps_wake = false;
}
#ifdef RAK_WISBLOCK_GPS
@@ -950,36 +946,35 @@ void EnvironmentSensorManager::loop() {
static long next_gps_update = 0;
// PowerSaving
if (powersaving_enabled) {
if (gps_detected && _location->isPowerSavingEnabled()) {
if (gps_wake && ((int32_t)(millis() - _location->getNextSleep()) >= 0 ||
!_location->waitingTimeSync())) { // Time to off or GPS set
if ((int32_t)(millis() - _location->getNextSleep()) >= 0) {
POWERSAVING_DEBUG_PRINTLN("GPS wake timeout. Enter sleep");
}
else if (!_location->waitingTimeSync()) {
POWERSAVING_DEBUG_PRINTLN("GPS set. Enter sleep early");
}
_location->updatePowerSavingSettings(gps_wake); // Handle change in PowerSaving mode
if (_location->isPowerSavingEnabled() && gps_detected) {
if (gps_wake) {
// GPS is awake: check whether it should sleep
if ((int32_t)(millis() - _location->getNextSleep()) >= 0) {
POWERSAVING_DEBUG_PRINTLN("GPS wake timeout. Enter sleep");
stop_gps();
} else if (!gps_wake && ((int32_t)(millis() - _location->getNextWake()) >= 0)) { // Time to on
} else if (!_location->waitingTimeSync()) {
POWERSAVING_DEBUG_PRINTLN("GPS set. Enter sleep early");
stop_gps();
}
} else {
// GPS is asleep: check whether it should wake
if ((int32_t)(millis() - _location->getNextWake()) >= 0) {
POWERSAVING_DEBUG_PRINTLN("GPS sleep timeout. Wakeup.");
start_gps();
} else if (!gps_wake && _location->waitingTimeSync()) { // On for "gps sync"
} else if (_location->waitingTimeSync()) {
POWERSAVING_DEBUG_PRINTLN("CLI gps sync. Wakeup");
start_gps();
}
}
}
if ((!powersaving_enabled && gps_active) || (powersaving_enabled && gps_wake)) {
if ((!_location->isPowerSavingEnabled() && gps_active) || (_location->isPowerSavingEnabled() && gps_wake)) {
_location->loop();
}
if ((int32_t)(millis() - next_gps_update) >= 0) {
if((!powersaving_enabled && gps_active) || (powersaving_enabled && gps_wake)){
if((!_location->isPowerSavingEnabled() && gps_active) || (_location->isPowerSavingEnabled() && gps_wake)){
#ifdef RAK_WISBLOCK_GPS
if ((i2cGPSFlag || serialGPSFlag) && _location->isValid()) {
node_lat = ((double)_location->getLatitude())/1000000.;
@@ -999,10 +994,10 @@ void EnvironmentSensorManager::loop() {
#endif
// In powersaving mode, GPS is on and off. Only update data when GPS is on
if(powersaving_enabled) next_gps_update = millis() + (gps_update_interval_sec * 1000);
if(_location->isPowerSavingEnabled()) next_gps_update = millis() + (gps_update_interval_sec * 1000);
}
if(!powersaving_enabled) next_gps_update = millis() + (gps_update_interval_sec * 1000);
if(!_location->isPowerSavingEnabled()) next_gps_update = millis() + (gps_update_interval_sec * 1000);
}
#endif
#if ENV_INCLUDE_BME680_BSEC
+25 -4
View File
@@ -7,7 +7,8 @@ class LocationProvider {
protected:
bool _time_sync_needed = true;
bool powersaving_enabled = false;
uint8_t* powersaving_enabled = nullptr;
bool _powersaving_enabled_prev = false; // Track previous PowerSaving state
unsigned long _wake_duration_secs = 86400; // Full day
unsigned long _sleep_duration_secs = 0; // No off
unsigned long _next_wake = 0;
@@ -19,13 +20,33 @@ public:
virtual bool waitingTimeSync() { return _time_sync_needed; }
virtual void stopTimeSync() { _time_sync_needed = false; }
virtual void setPowerSavingProfile(unsigned long wake_duration_secs, unsigned long sleep_duration_secs) {
virtual void setPowerSavingProfile(uint8_t &enabled, unsigned long wake_duration_secs,
unsigned long sleep_duration_secs) {
powersaving_enabled = &enabled;
_wake_duration_secs = wake_duration_secs;
_sleep_duration_secs = sleep_duration_secs;
}
virtual void enablePowerSaving(bool enabled) { powersaving_enabled = enabled; _next_wake = 0; _next_sleep = 0; }
virtual bool isPowerSavingEnabled() { return powersaving_enabled; }
virtual bool isPowerSavingEnabled() const {
return powersaving_enabled != nullptr && *powersaving_enabled != 0;
}
virtual void updatePowerSavingSettings(bool gps_wake) {
bool powersaving_enabled = isPowerSavingEnabled();
// PowerSaving changed from OFF to ON
if (!_powersaving_enabled_prev && powersaving_enabled) {
if (gps_wake) {
syncTime(); // Wake when powersaving on
setNextSleep(); // On, set next sleep
} else {
setNextWake(); // Off, set next wake
}
}
_powersaving_enabled_prev = powersaving_enabled;
}
virtual void setNextWake() { _next_wake = millis() + _sleep_duration_secs * 1000UL; }
virtual unsigned long getNextWake() { return _next_wake; }
virtual void setNextSleep() { _next_sleep = millis() + _wake_duration_secs * 1000UL; }