From 9cef69a9d35a4cfa9b1cbfeaf48eba3a84fd3bdb Mon Sep 17 00:00:00 2001 From: webgeek1234 Date: Sun, 28 Jun 2026 18:22:09 -0500 Subject: [PATCH] feat(gps): Do not start gps if lora region is unset (#10386) * Disable gps thread on startup if lora region is unset There is little reason to waste battery on the gps if the data cannot yet be used. * fix goobered merge Refactor GPS enabling logic and remove duplicate code. * trunk --------- Co-authored-by: Jonathan Bennett --- src/gps/GPS.cpp | 5 +++++ src/gps/GPS.h | 3 +++ src/graphics/draw/MenuHandler.cpp | 5 +++++ src/main.cpp | 6 ++++++ src/modules/AdminModule.cpp | 8 ++++++++ 5 files changed, 27 insertions(+) diff --git a/src/gps/GPS.cpp b/src/gps/GPS.cpp index 5078a6a271..0b9d4b214a 100644 --- a/src/gps/GPS.cpp +++ b/src/gps/GPS.cpp @@ -2257,6 +2257,11 @@ int32_t GPS::disable() return INT32_MAX; } +bool GPS::isEnabled() +{ + return enabled; +} + void GPS::toggleGpsMode() { if (config.position.gps_mode == meshtastic_Config_PositionConfig_GpsMode_ENABLED) { diff --git a/src/gps/GPS.h b/src/gps/GPS.h index 4028e351a3..8709cca904 100644 --- a/src/gps/GPS.h +++ b/src/gps/GPS.h @@ -98,6 +98,9 @@ class GPS : private concurrency::OSThread // Disable the thread int32_t disable() override; + // Returns if the thread is enabled + bool isEnabled(); + // toggle between enabled/disabled void toggleGpsMode(); diff --git a/src/graphics/draw/MenuHandler.cpp b/src/graphics/draw/MenuHandler.cpp index 0f4d8d713f..fd3baff0f0 100644 --- a/src/graphics/draw/MenuHandler.cpp +++ b/src/graphics/draw/MenuHandler.cpp @@ -214,6 +214,11 @@ static void applyLoraRegion(meshtastic_Config_LoRaConfig_RegionCode region, bool snprintf(moduleConfig.mqtt.root, sizeof(moduleConfig.mqtt.root), "%s/%s", default_mqtt_root, myRegion->name); changes |= SEGMENT_MODULECONFIG; } +#if !MESHTASTIC_EXCLUDE_GPS + // Enable gps if it was previously disabled due to region not being set + if (gps != nullptr && !gps->isEnabled() && config.position.gps_mode == meshtastic_Config_PositionConfig_GpsMode_ENABLED) + gps->enable(); +#endif service->reloadConfig(changes); } diff --git a/src/main.cpp b/src/main.cpp index e76e071a11..5071ebea33 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -971,6 +971,12 @@ void setup() gps = GPS::createGps(); if (gps) { gpsStatus->observe(&gps->newStatus); + + // If lora region is unset, disable the gps thread + if (config.lora.region == meshtastic_Config_LoRaConfig_RegionCode_UNSET && + config.position.gps_mode == meshtastic_Config_PositionConfig_GpsMode_ENABLED) { + gps->disable(); + } } else { LOG_DEBUG("Run without GPS"); } diff --git a/src/modules/AdminModule.cpp b/src/modules/AdminModule.cpp index e19ad3dbbc..e2a662db02 100644 --- a/src/modules/AdminModule.cpp +++ b/src/modules/AdminModule.cpp @@ -1020,6 +1020,14 @@ void AdminModule::handleSetConfig(const meshtastic_Config &c, bool fromOthers) } #endif +#if !MESHTASTIC_EXCLUDE_GPS + // Enable gps if it was previously disabled due to region not being set + if (!requiresReboot && config.lora.region != meshtastic_Config_LoRaConfig_RegionCode_UNSET && gps != nullptr && + !gps->isEnabled() && config.position.gps_mode == meshtastic_Config_PositionConfig_GpsMode_ENABLED) { + gps->enable(); + } +#endif + config.lora = validatedLora; // Finally, return the validated config back to the main config break;