diff --git a/examples/companion_radio/main.cpp b/examples/companion_radio/main.cpp index 67aa6537c..f9d9acbac 100644 --- a/examples/companion_radio/main.cpp +++ b/examples/companion_radio/main.cpp @@ -470,6 +470,14 @@ void setup() { sensors.begin(); +#if ENV_INCLUDE_GPS == 1 && defined(BLE_PIN_CODE) + // BLE companions keep GPS duty cycling enabled by default: at most 10 + // minutes awake, followed by 5 minutes asleep. + if (sensors.getLocationProvider() != NULL) { + sensors.getLocationProvider()->setPowerSavingProfile(600, 300); + sensors.setPowerSavingEnabled(true); + } +#endif #if ENV_INCLUDE_GPS == 1 the_mesh.applyGpsPrefs(); #endif diff --git a/examples/simple_repeater/main.cpp b/examples/simple_repeater/main.cpp index 982e22212..06b5eba8e 100644 --- a/examples/simple_repeater/main.cpp +++ b/examples/simple_repeater/main.cpp @@ -127,6 +127,13 @@ void setup() { ethernet_command[0] = 0; #endif +#if ENV_INCLUDE_GPS == 1 + if (sensors.getLocationProvider() != NULL) { + // Keep GPS awake for at most 10 minutes, then asleep for one day. + sensors.getLocationProvider()->setPowerSavingProfile(600, 86400); + } +#endif + sensors.begin(); the_mesh.begin(fs); diff --git a/src/MeshCore.h b/src/MeshCore.h index 46dd9f195..f599acf24 100644 --- a/src/MeshCore.h +++ b/src/MeshCore.h @@ -37,6 +37,15 @@ #define BRIDGE_DEBUG_PRINTLN(...) {} #endif +#if POWERSAVING_DEBUG && ARDUINO + #include + #define POWERSAVING_DEBUG_PRINT(F, ...) Serial.printf("POWERSAVING: " F, ##__VA_ARGS__) + #define POWERSAVING_DEBUG_PRINTLN(F, ...) Serial.printf("POWERSAVING: " F "\n", ##__VA_ARGS__) +#else + #define POWERSAVING_DEBUG_PRINT(...) {} + #define POWERSAVING_DEBUG_PRINTLN(...) {} +#endif + namespace mesh { #define BD_STARTUP_NORMAL 0 // getStartupReason() codes diff --git a/src/helpers/sensors/EnvironmentSensorManager.cpp b/src/helpers/sensors/EnvironmentSensorManager.cpp index f60966783..a2261f0b6 100644 --- a/src/helpers/sensors/EnvironmentSensorManager.cpp +++ b/src/helpers/sensors/EnvironmentSensorManager.cpp @@ -196,6 +196,7 @@ class RAK12500LocationProvider : public LocationProvider { unsigned long _last_time_sync = 0; uint8_t _valid_time_samples = 0; static const unsigned long TIME_SYNC_INTERVAL = 1800000; + int _pin_en = -1; public: void setRTCClock(mesh::RTCClock* clock) { _clock = clock; } mesh::RTCClock* getRTCClock() override { return _clock; } @@ -250,7 +251,11 @@ public: } } } - bool isEnabled() override { return true; } + bool isEnabled() override { + return _pin_en < 0 || digitalRead(_pin_en) == HIGH; + } + void setPinEn(int pin_en) override { _pin_en = pin_en; } + int getPinEn() override { return _pin_en; } }; static RAK12500LocationProvider RAK12500_provider; @@ -663,18 +668,6 @@ static const SensorDef SENSOR_TABLE[] = { static const size_t SENSOR_TABLE_SIZE = (sizeof(SENSOR_TABLE) / sizeof(SENSOR_TABLE[0])) - 1; -// ============================================================ -// Power Saving GPS -// ============================================================ -#if ENV_INCLUDE_GPS -#ifndef GPS_POWERSAVING_ON_DURATION_SECS -#define GPS_POWERSAVING_ON_DURATION_SECS (10UL * 60UL) -#endif -#ifndef GPS_POWERSAVING_OFF_DURATION_SECS -#define GPS_POWERSAVING_OFF_DURATION_SECS (24UL * 60UL * 60UL) -#endif -#endif - // ============================================================ // begin() - scan the I2C bus, then initialize only what was // found. A sensor whose address does not ACK during the scan @@ -870,24 +863,23 @@ void EnvironmentSensorManager::initBasicGPS() { // gps code for rak might be moved to MicroNMEALoactionProvider // or make a new location provider ... #ifdef RAK_WISBLOCK_GPS -void EnvironmentSensorManager::rakGPSInit(){ - +void EnvironmentSensorManager::rakGPSInit() { Serial1.setPins(PIN_GPS_TX, PIN_GPS_RX); - #ifdef GPS_BAUD_RATE +#ifdef GPS_BAUD_RATE Serial1.begin(GPS_BAUD_RATE); - #else +#else Serial1.begin(9600); - #endif +#endif - //search for the correct IO standby pin depending on socket used - if(gpsIsAwake(WB_IO2)){ - } - else if(gpsIsAwake(WB_IO4)){ - } - else if(gpsIsAwake(WB_IO5)){ - } - else{ + // search for the correct IO standby pin depending on socket used + if (gpsIsAwake(WB_IO2)) { + _location->setPinEn(WB_IO2); + } else if (gpsIsAwake(WB_IO4)) { + _location->setPinEn(WB_IO4); + } else if (gpsIsAwake(WB_IO5)) { + _location->setPinEn(WB_IO5); + } else { MESH_DEBUG_PRINTLN("No GPS found"); gps_active = false; gps_detected = false; @@ -895,31 +887,30 @@ void EnvironmentSensorManager::rakGPSInit(){ return; } - #ifndef FORCE_GPS_ALIVE // for use with repeaters, until GPS toggle is implimented - //Now that GPS is found and set up, set to sleep for initial state +#ifndef FORCE_GPS_ALIVE // for use with repeaters, until GPS toggle is implimented + // Now that GPS is found and set up, set to sleep for initial state stop_gps(); - #endif +#endif } -bool EnvironmentSensorManager::gpsIsAwake(uint8_t ioPin){ +bool EnvironmentSensorManager::gpsIsAwake(uint8_t ioPin) { +#if defined(ETHERNET_ENABLED) && defined(RAK_BOARD) + if (ioPin == WB_IO2) { + // WB_IO2 powers the Ethernet module on RAK baseboards. + return false; + } +#endif - #if defined(ETHERNET_ENABLED) && defined(RAK_BOARD) - if (ioPin == WB_IO2) { - // WB_IO2 powers the Ethernet module on RAK baseboards. - return false; - } - #endif - - //set initial waking state - pinMode(ioPin,OUTPUT); - digitalWrite(ioPin,LOW); + // set initial waking state + pinMode(ioPin, OUTPUT); + digitalWrite(ioPin, LOW); delay(500); - digitalWrite(ioPin,HIGH); + digitalWrite(ioPin, HIGH); delay(500); - //Try to init RAK12500 on I2C - if (ublox_GNSS.begin(Wire) == true){ - MESH_DEBUG_PRINTLN("RAK12500 GPS init correctly with pin %i",ioPin); + // Try to init RAK12500 on I2C + if (ublox_GNSS.begin(Wire) == true) { + MESH_DEBUG_PRINTLN("RAK12500 GPS init correctly with pin %i", ioPin); ublox_GNSS.setI2COutput(COM_TYPE_UBX); ublox_GNSS.enableGNSS(true, SFE_UBLOX_GNSS_ID_GPS); ublox_GNSS.enableGNSS(true, SFE_UBLOX_GNSS_ID_GALILEO); @@ -938,10 +929,10 @@ bool EnvironmentSensorManager::gpsIsAwake(uint8_t ioPin){ RAK12500_provider.setRTCClock(_location->getRTCClock()); _location = &RAK12500_provider; return true; - } else if (Serial1.available()) { + } else if (Serial1.available()) { // RAK12501 (L76K) on UART MESH_DEBUG_PRINTLN("Serial GPS init correctly and is turned on"); #ifdef PIN_GPS_EN - if(PIN_GPS_EN){ + if (PIN_GPS_EN) { gpsResetPin = PIN_GPS_EN; } #endif @@ -961,23 +952,29 @@ void EnvironmentSensorManager::armGpsPowerSavingCycle() { if (!powersaving_enabled || !_location->getGPSPowerSaving()) return; _location->syncTime(); _location->setNextGPSOn(0); - _location->setNextGPSOff(millis() + GPS_POWERSAVING_ON_DURATION_SECS * 1000UL); + _location->setNextSleep(); } void EnvironmentSensorManager::start_gps() { if (gps_active) return; gps_active = true; - #ifdef RAK_WISBLOCK_GPS +#ifdef RAK_WISBLOCK_GPS pinMode(gpsResetPin, OUTPUT); digitalWrite(gpsResetPin, HIGH); - #else + int pin_en = _location->getPinEn(); + if (pin_en >= 0 && !gpsIsAwake(pin_en)) { + gps_active = false; + MESH_DEBUG_PRINTLN("GPS wake failed"); + return; + } +#else _location->begin(); _location->reset(); +#endif #ifndef PIN_GPS_EN MESH_DEBUG_PRINTLN("Start GPS is N/A on this board. Actual GPS state unchanged"); #endif - #endif armGpsPowerSavingCycle(); } @@ -989,20 +986,21 @@ void EnvironmentSensorManager::stop_gps() { if (powersaving_enabled && _location->getGPSPowerSaving()) { _location->stopTimeSync(); _location->setNextGPSOff(0); - _location->setNextGPSOn(millis() + GPS_POWERSAVING_OFF_DURATION_SECS * 1000UL); + _location->setNextWake(); } #ifdef RAK_WISBLOCK_GPS - pinMode(gpsResetPin, OUTPUT); - digitalWrite(gpsResetPin, LOW); - return; - #endif - + pinMode(gpsResetPin, OUTPUT); + digitalWrite(gpsResetPin, LOW); + int pin_en = _location->getPinEn(); + if (pin_en >= 0) digitalWrite(pin_en, LOW); +#else _location->stop(); +#endif - #ifndef PIN_GPS_EN +#ifndef PIN_GPS_EN MESH_DEBUG_PRINTLN("Stop GPS is N/A on this board. Actual GPS state unchanged"); - #endif +#endif } #endif // ENV_INCLUDE_GPS @@ -1019,18 +1017,19 @@ void EnvironmentSensorManager::loop() { unsigned long next_on = _location->getNextGPSOn(); if (gps_active && ((next_off != 0 && (long)(now - next_off) >= 0) || !_location->waitingTimeSync())) { + POWERSAVING_DEBUG_PRINTLN("GPS entering sleep"); stop_gps(); } else if (!gps_active && ((next_on != 0 && (long)(now - next_on) >= 0) || _location->waitingTimeSync())) { + POWERSAVING_DEBUG_PRINTLN("GPS waking"); start_gps(); } } - if (gps_active) { - _location->loop(); - } + if (gps_active) _location->loop(); + if ((long)(now - next_gps_update) >= 0) { - if(gps_active){ + if (gps_active) { #ifdef RAK_WISBLOCK_GPS if ((i2cGPSFlag || serialGPSFlag) && _location->isValid()) { float gps_lat = ((float)_location->getLatitude()) / 1000000.0f; diff --git a/src/helpers/sensors/LocationProvider.h b/src/helpers/sensors/LocationProvider.h index 78e19d431..2628445c4 100644 --- a/src/helpers/sensors/LocationProvider.h +++ b/src/helpers/sensors/LocationProvider.h @@ -2,6 +2,13 @@ #include "Mesh.h" +#ifndef GPS_POWERSAVING_ON_DURATION_SECS +#define GPS_POWERSAVING_ON_DURATION_SECS (10UL * 60UL) +#endif + +#ifndef GPS_POWERSAVING_OFF_DURATION_SECS +#define GPS_POWERSAVING_OFF_DURATION_SECS (24UL * 60UL * 60UL) +#endif class LocationProvider { protected: @@ -10,6 +17,8 @@ protected: bool _gps_powersaving_enabled = false; unsigned long _next_gps_off = 0; unsigned long _next_gps_on = 0; + unsigned long _gps_on_duration_secs = GPS_POWERSAVING_ON_DURATION_SECS; + unsigned long _gps_off_duration_secs = GPS_POWERSAVING_OFF_DURATION_SECS; unsigned long _last_valid_time_sync = 0; void markTimeSyncApplied() { _time_sync_applied = true; } @@ -25,12 +34,34 @@ public: return applied; } virtual void stopTimeSync() { _time_sync_needed = false; } - virtual void setGPSPowerSaving(bool enabled) { _gps_powersaving_enabled = enabled; _next_gps_off = 0; _next_gps_on = 0; } + virtual void setGPSPowerSaving(bool enabled) { + _gps_powersaving_enabled = enabled; + _next_gps_off = 0; + _next_gps_on = 0; + } virtual bool getGPSPowerSaving() { return _gps_powersaving_enabled; } virtual void setNextGPSOff(unsigned long _millis) { _next_gps_off = _millis; } virtual unsigned long getNextGPSOff() { return _next_gps_off; } virtual void setNextGPSOn(unsigned long _millis) { _next_gps_on = _millis; } virtual unsigned long getNextGPSOn() { return _next_gps_on; } + + virtual void setPowerSavingProfile(unsigned long wake_duration_secs, unsigned long sleep_duration_secs) { + _gps_on_duration_secs = wake_duration_secs; + _gps_off_duration_secs = sleep_duration_secs; + } + + // Compatibility names used by the PowerSaving branch. The existing GPS + // names remain canonical so older targets and stored behavior stay intact. + virtual void enablePowerSaving(bool enabled) { setGPSPowerSaving(enabled); } + virtual bool isPowerSavingEnabled() { return getGPSPowerSaving(); } + virtual void setNextWake() { + setNextGPSOn(millis() + _gps_off_duration_secs * 1000UL); + } + virtual unsigned long getNextWake() { return getNextGPSOn(); } + virtual void setNextSleep() { + setNextGPSOff(millis() + _gps_on_duration_secs * 1000UL); + } + virtual unsigned long getNextSleep() { return getNextGPSOff(); } virtual unsigned long getLastValidTimeSync() { return _last_valid_time_sync; } virtual mesh::RTCClock* getRTCClock() { return NULL; } virtual long getLatitude() = 0; @@ -45,4 +76,6 @@ public: virtual void stop() = 0; virtual void loop() = 0; virtual bool isEnabled() = 0; + virtual void setPinEn(int pin_en) { (void)pin_en; } + virtual int getPinEn() { return -1; } }; diff --git a/src/helpers/sensors/MicroNMEALocationProvider.h b/src/helpers/sensors/MicroNMEALocationProvider.h index 964ff22c9..69e4ce909 100644 --- a/src/helpers/sensors/MicroNMEALocationProvider.h +++ b/src/helpers/sensors/MicroNMEALocationProvider.h @@ -113,6 +113,14 @@ public : } } + void setPinEn(int pin_en) override { + _pin_en = pin_en; + } + + int getPinEn() override { + return _pin_en; + } + void syncTime() override { nmea.clear(); LocationProvider::syncTime(); } mesh::RTCClock* getRTCClock() override { return _clock; } long getLatitude() override { return nmea.getLatitude(); } diff --git a/variants/heltec_mesh_solar/target.cpp b/variants/heltec_mesh_solar/target.cpp index 0b67d0192..94e6146e5 100644 --- a/variants/heltec_mesh_solar/target.cpp +++ b/variants/heltec_mesh_solar/target.cpp @@ -29,18 +29,29 @@ mesh::LocalIdentity radio_new_identity() { return mesh::LocalIdentity(&rng); // create new random identity } +void SolarSensorManager::armGpsPowerSavingCycle() { + if (!powersaving_enabled || !_location->getGPSPowerSaving()) return; + _location->syncTime(); + _location->setNextGPSOn(0); + _location->setNextSleep(); +} + void SolarSensorManager::start_gps() { - if (!gps_active) { - gps_active = true; - _location->begin(); - } + if (gps_active) return; + gps_active = true; + _location->begin(); + armGpsPowerSavingCycle(); } void SolarSensorManager::stop_gps() { - if (gps_active) { - gps_active = false; - _location->stop(); + if (!gps_active) return; + gps_active = false; + if (powersaving_enabled && _location->getGPSPowerSaving()) { + _location->stopTimeSync(); + _location->setNextGPSOff(0); + _location->setNextWake(); } + _location->stop(); } bool SolarSensorManager::begin() { @@ -68,6 +79,20 @@ void SolarSensorManager::loop() { unsigned long now = millis(); loopGpsTelemetry(now); + if (powersaving_enabled && gps_detected && _location->getGPSPowerSaving()) { + unsigned long next_off = _location->getNextGPSOff(); + unsigned long next_on = _location->getNextGPSOn(); + if (gps_active && ((next_off != 0 && (long)(now - next_off) >= 0) + || !_location->waitingTimeSync())) { + POWERSAVING_DEBUG_PRINTLN("GPS entering sleep"); + stop_gps(); + } else if (!gps_active && ((next_on != 0 && (long)(now - next_on) >= 0) + || _location->waitingTimeSync())) { + POWERSAVING_DEBUG_PRINTLN("GPS waking"); + start_gps(); + } + } + if (gps_active) _location->loop(); if ((long)(now - next_gps_update) >= 0) { @@ -99,12 +124,37 @@ const char* SolarSensorManager::getSettingValue(int i) const { bool SolarSensorManager::setSettingValue(const char* name, const char* value) { if (gps_detected && strcmp(name, "gps") == 0) { - setGpsTelemetryUserEnabled(strcmp(value, "0") != 0); + bool enabled = strcmp(value, "0") != 0; + bool was_active = gps_active; + _location->setGPSPowerSaving(enabled && powersaving_enabled); + setGpsTelemetryUserEnabled(enabled); + if (enabled && powersaving_enabled && was_active) { + armGpsPowerSavingCycle(); + } return true; } return false; // not supported } +void SolarSensorManager::setPowerSavingEnabled(bool enabled) { + if (powersaving_enabled == enabled) return; + powersaving_enabled = enabled; + + bool gps_user_enabled = isGpsTelemetryUserEnabled(); + _location->setGPSPowerSaving(enabled && gps_user_enabled); + if (!gps_user_enabled) return; + + if (enabled) { + if (gps_active) { + armGpsPowerSavingCycle(); + } else { + start_gps(); + } + } else if (!gps_active) { + start_gps(); + } +} + bool SolarExternalWatchdog::begin() { last_feed_watchdog = 0; pinMode(EXTERNAL_WATCHDOG_WAKE_PIN, INPUT); diff --git a/variants/heltec_mesh_solar/target.h b/variants/heltec_mesh_solar/target.h index d9b1770f1..1db5fcce4 100644 --- a/variants/heltec_mesh_solar/target.h +++ b/variants/heltec_mesh_solar/target.h @@ -18,6 +18,7 @@ class SolarSensorManager : public SensorManager { bool gps_detected = false; LocationProvider* _location; + void armGpsPowerSavingCycle(); void start_gps(); void stop_gps(); bool telemetryGpsDetected() const override { return gps_detected; } @@ -33,6 +34,8 @@ public: const char* getSettingName(int i) const override; const char* getSettingValue(int i) const override; bool setSettingValue(const char* name, const char* value) override; + void setPowerSavingEnabled(bool enabled) override; + LocationProvider* getLocationProvider() override { return _location; } }; class SolarExternalWatchdog : public ExternalWatchdogManager { diff --git a/variants/heltec_t096/platformio.ini b/variants/heltec_t096/platformio.ini index 822b5b71f..7cc29826f 100644 --- a/variants/heltec_t096/platformio.ini +++ b/variants/heltec_t096/platformio.ini @@ -76,6 +76,7 @@ build_flags = -D MAX_NEIGHBOURS=50 ; -D MESH_PACKET_LOGGING=1 ; -D MESH_DEBUG=1 +; -D POWERSAVING_DEBUG=1 ; Explicit lean alternative to the full-sensor, non-OTA repeater above. [env:Heltec_t096_repeater_lora_ota_no_external_sensors] @@ -186,6 +187,7 @@ build_flags = -D OFFLINE_QUEUE_SIZE=256 ; -D MESH_PACKET_LOGGING=1 ; -D MESH_DEBUG=1 +; -D POWERSAVING_DEBUG=1 build_src_filter = ${Heltec_t096.build_src_filter} + +<../examples/companion_radio/*.cpp> diff --git a/variants/heltec_t114/platformio.ini b/variants/heltec_t114/platformio.ini index 108aec6b3..2c118bb6d 100644 --- a/variants/heltec_t114/platformio.ini +++ b/variants/heltec_t114/platformio.ini @@ -173,6 +173,7 @@ build_flags = -D MAX_NEIGHBOURS=50 ; -D MESH_PACKET_LOGGING=1 ; -D MESH_DEBUG=1 +; -D POWERSAVING_DEBUG=1 ; Explicit lean alternative to the full-sensor, non-OTA repeater above. [env:Heltec_t114_repeater_lora_ota_no_external_sensors] @@ -241,6 +242,7 @@ build_flags = -D OFFLINE_QUEUE_SIZE=256 ; -D MESH_PACKET_LOGGING=1 ; -D MESH_DEBUG=1 +; -D POWERSAVING_DEBUG=1 build_src_filter = ${Heltec_t114_with_display.build_src_filter} + +<../examples/companion_radio/*.cpp> diff --git a/variants/heltec_tracker/target.cpp b/variants/heltec_tracker/target.cpp index 46d3dc06d..e77c02d2c 100644 --- a/variants/heltec_tracker/target.cpp +++ b/variants/heltec_tracker/target.cpp @@ -42,19 +42,30 @@ mesh::LocalIdentity radio_new_identity() { return mesh::LocalIdentity(&rng); // create new random identity } +void HWTSensorManager::armGpsPowerSavingCycle() { + if (!powersaving_enabled || !_location->getGPSPowerSaving()) return; + _location->syncTime(); + _location->setNextGPSOn(0); + _location->setNextSleep(); +} + void HWTSensorManager::start_gps() { - if (!gps_active) { - _location->begin(); // Claims periph_power via RefCountedDigitalPin - gps_active = true; - Serial1.println("$CFGSYS,h35155*68"); // Configure GPS for all constellations - } + if (gps_active) return; + _location->begin(); // Claims periph_power via RefCountedDigitalPin + gps_active = true; + armGpsPowerSavingCycle(); + Serial1.println("$CFGSYS,h35155*68"); // Configure GPS for all constellations } void HWTSensorManager::stop_gps() { - if (gps_active) { - gps_active = false; - _location->stop(); // Releases periph_power via RefCountedDigitalPin + if (!gps_active) return; + gps_active = false; + if (powersaving_enabled && _location->getGPSPowerSaving()) { + _location->stopTimeSync(); + _location->setNextGPSOff(0); + _location->setNextWake(); } + _location->stop(); // Releases periph_power via RefCountedDigitalPin } bool HWTSensorManager::begin() { @@ -73,6 +84,20 @@ void HWTSensorManager::loop() { unsigned long now = millis(); loopGpsTelemetry(now); + if (powersaving_enabled && _location->getGPSPowerSaving()) { + unsigned long next_off = _location->getNextGPSOff(); + unsigned long next_on = _location->getNextGPSOn(); + if (gps_active && ((next_off != 0 && (long)(now - next_off) >= 0) + || !_location->waitingTimeSync())) { + POWERSAVING_DEBUG_PRINTLN("GPS entering sleep"); + stop_gps(); + } else if (!gps_active && ((next_on != 0 && (long)(now - next_on) >= 0) + || _location->waitingTimeSync())) { + POWERSAVING_DEBUG_PRINTLN("GPS waking"); + start_gps(); + } + } + if (gps_active) _location->loop(); if ((long)(now - next_gps_update) >= 0) { @@ -100,8 +125,33 @@ const char* HWTSensorManager::getSettingValue(int i) const { } bool HWTSensorManager::setSettingValue(const char* name, const char* value) { if (strcmp(name, "gps") == 0) { - setGpsTelemetryUserEnabled(strcmp(value, "0") != 0); + bool enabled = strcmp(value, "0") != 0; + bool was_active = gps_active; + _location->setGPSPowerSaving(enabled && powersaving_enabled); + setGpsTelemetryUserEnabled(enabled); + if (enabled && powersaving_enabled && was_active) { + armGpsPowerSavingCycle(); + } return true; } return false; // not supported } + +void HWTSensorManager::setPowerSavingEnabled(bool enabled) { + if (powersaving_enabled == enabled) return; + powersaving_enabled = enabled; + + bool gps_user_enabled = isGpsTelemetryUserEnabled(); + _location->setGPSPowerSaving(enabled && gps_user_enabled); + if (!gps_user_enabled) return; + + if (enabled) { + if (gps_active) { + armGpsPowerSavingCycle(); + } else { + start_gps(); + } + } else if (!gps_active) { + start_gps(); + } +} diff --git a/variants/heltec_tracker/target.h b/variants/heltec_tracker/target.h index 823186c1a..95765082b 100644 --- a/variants/heltec_tracker/target.h +++ b/variants/heltec_tracker/target.h @@ -17,6 +17,7 @@ class HWTSensorManager : public SensorManager { bool gps_active = false; LocationProvider * _location; + void armGpsPowerSavingCycle(); void start_gps(); void stop_gps(); bool telemetryGpsDetected() const override { return true; } @@ -32,6 +33,7 @@ public: const char* getSettingName(int i) const override; const char* getSettingValue(int i) const override; bool setSettingValue(const char* name, const char* value) override; + void setPowerSavingEnabled(bool enabled) override; LocationProvider* getLocationProvider() override { return _location; } }; diff --git a/variants/rak4631/platformio.ini b/variants/rak4631/platformio.ini index 5534f33d7..db31fa5b6 100644 --- a/variants/rak4631/platformio.ini +++ b/variants/rak4631/platformio.ini @@ -59,6 +59,7 @@ build_flags = ; -D OTA_DEBUG=1 ; bring-up: trace OTA fetch (REQ / block / page-flush) over Serial ; -D MESH_PACKET_LOGGING=1 ; -D MESH_DEBUG=1 +; -D POWERSAVING_DEBUG=1 build_src_filter = ${rak4631.build_src_filter} + ; Shared sources remain inherited, but ENABLE_OTA is explicitly undefined for this oversized role. @@ -311,10 +312,11 @@ build_flags = -D MAX_CONTACTS=350 -D MAX_GROUP_CHANNELS=40 -D BLE_PIN_CODE=123456 - -D BLE_DEBUG_LOGGING=1 +; -D BLE_DEBUG_LOGGING=1 -D OFFLINE_QUEUE_SIZE=256 ; -D MESH_PACKET_LOGGING=1 ; -D MESH_DEBUG=1 +; -D POWERSAVING_DEBUG=1 build_src_filter = ${rak4631.build_src_filter} + +<../examples/companion_radio/*.cpp> diff --git a/variants/t1000-e/target.cpp b/variants/t1000-e/target.cpp index 8b1d67dec..b325c26a5 100644 --- a/variants/t1000-e/target.cpp +++ b/variants/t1000-e/target.cpp @@ -79,6 +79,13 @@ mesh::LocalIdentity radio_new_identity() { return mesh::LocalIdentity(&rng); // create new random identity } +void T1000SensorManager::armGpsPowerSavingCycle() { + if (!powersaving_enabled || !_nmea->getGPSPowerSaving()) return; + _nmea->syncTime(); + _nmea->setNextGPSOn(0); + _nmea->setNextSleep(); +} + void T1000SensorManager::start_gps() { if (gps_active) return; gps_active = true; @@ -102,10 +109,18 @@ void T1000SensorManager::start_gps() { pinMode(GPS_RTC_INT, OUTPUT); digitalWrite(GPS_RTC_INT, LOW); pinMode(GPS_RESETB, INPUT_PULLUP); + armGpsPowerSavingCycle(); } void T1000SensorManager::sleep_gps() { + if (!gps_active) return; gps_active = false; + if (powersaving_enabled && _nmea->getGPSPowerSaving()) { + _nmea->stopTimeSync(); + _nmea->setNextGPSOff(0); + _nmea->setNextWake(); + } + digitalWrite(GPS_VRTC_EN, HIGH); digitalWrite(GPS_EN, LOW); digitalWrite(GPS_RESET, HIGH); @@ -117,7 +132,14 @@ void T1000SensorManager::sleep_gps() { } void T1000SensorManager::stop_gps() { + if (!gps_active) return; gps_active = false; + if (powersaving_enabled && _nmea->getGPSPowerSaving()) { + _nmea->stopTimeSync(); + _nmea->setNextGPSOff(0); + _nmea->setNextWake(); + } + digitalWrite(GPS_VRTC_EN, LOW); digitalWrite(GPS_EN, LOW); digitalWrite(GPS_RESET, HIGH); @@ -150,6 +172,20 @@ void T1000SensorManager::loop() { unsigned long now = millis(); loopGpsTelemetry(now); + if (powersaving_enabled && _nmea->getGPSPowerSaving()) { + unsigned long next_off = _nmea->getNextGPSOff(); + unsigned long next_on = _nmea->getNextGPSOn(); + if (gps_active && ((next_off != 0 && (long)(now - next_off) >= 0) + || !_nmea->waitingTimeSync())) { + POWERSAVING_DEBUG_PRINTLN("GPS entering sleep"); + stop_gps(); + } else if (!gps_active && ((next_on != 0 && (long)(now - next_on) >= 0) + || _nmea->waitingTimeSync())) { + POWERSAVING_DEBUG_PRINTLN("GPS waking"); + start_gps(); + } + } + if (gps_active) _nmea->loop(); if ((long)(now - next_gps_update) >= 0) { @@ -177,8 +213,33 @@ const char* T1000SensorManager::getSettingValue(int i) const { } bool T1000SensorManager::setSettingValue(const char* name, const char* value) { if (strcmp(name, "gps") == 0) { - setGpsTelemetryUserEnabled(strcmp(value, "0") != 0); + bool enabled = strcmp(value, "0") != 0; + bool was_active = gps_active; + _nmea->setGPSPowerSaving(enabled && powersaving_enabled); + setGpsTelemetryUserEnabled(enabled); + if (enabled && powersaving_enabled && was_active) { + armGpsPowerSavingCycle(); + } return true; } return false; // not supported } + +void T1000SensorManager::setPowerSavingEnabled(bool enabled) { + if (powersaving_enabled == enabled) return; + powersaving_enabled = enabled; + + bool gps_user_enabled = isGpsTelemetryUserEnabled(); + _nmea->setGPSPowerSaving(enabled && gps_user_enabled); + if (!gps_user_enabled) return; + + if (enabled) { + if (gps_active) { + armGpsPowerSavingCycle(); + } else { + start_gps(); + } + } else if (!gps_active) { + start_gps(); + } +} diff --git a/variants/t1000-e/target.h b/variants/t1000-e/target.h index 27e79d429..ba6cf0cae 100644 --- a/variants/t1000-e/target.h +++ b/variants/t1000-e/target.h @@ -16,6 +16,7 @@ class T1000SensorManager: public SensorManager { bool gps_active = false; LocationProvider * _nmea; + void armGpsPowerSavingCycle(); void start_gps(); void sleep_gps(); void stop_gps(); @@ -32,7 +33,8 @@ public: const char* getSettingName(int i) const override; const char* getSettingValue(int i) const override; bool setSettingValue(const char* name, const char* value) override; - LocationProvider* getLocationProvider() { return _nmea; } + void setPowerSavingEnabled(bool enabled) override; + LocationProvider* getLocationProvider() override { return _nmea; } }; #ifdef DISPLAY_CLASS