From c06def2f4120deede4b9d01ce6e1abdfc2e8d82b Mon Sep 17 00:00:00 2001 From: Florent Date: Sat, 7 Nov 2026 09:05:11 -0400 Subject: [PATCH] gps: once command --- src/helpers/CommonGpsPrefs.cpp | 27 ++++++++++++++++++ src/helpers/sensors/LocationProvider.h | 1 + .../sensors/MicroNMEALocationProvider.h | 28 +++++++++++++++++++ src/helpers/sensors/RakLocationProvider.h | 8 ++++++ 4 files changed, 64 insertions(+) diff --git a/src/helpers/CommonGpsPrefs.cpp b/src/helpers/CommonGpsPrefs.cpp index c4936d4f6..926efe71e 100644 --- a/src/helpers/CommonGpsPrefs.cpp +++ b/src/helpers/CommonGpsPrefs.cpp @@ -97,6 +97,33 @@ bool CommonGpsPrefs::handleCommand(const char* command, SensorManager& sensors, } return true; } + if (memcmp(command, "gps once", 8) == 0) { // before "gps on" + LocationProvider* location = sensors.getLocationProvider(); + + if (location == nullptr) { + strcpy(reply, "gps provider not found"); + return true; + } + + if (location->hasHardwareEnableState() && !location->isHardwareEnabled()) { + strcpy(reply, "gps hw switch off"); + return true; + } + + if (strstr(command, " -s") != nullptr) location->syncTime(); + + const char* state = !location->isGpsOn() ? "turning on and acquiring" + : (location->getGpsPeriod() > 0 && !location->isActive()) ? "waking gps" + : "gps already on"; + + if (location->startOnce()) { + snprintf(reply, 160, "ok (%s)", state); + } else { + strcpy(reply, "gps once not supported"); + } + + return true; + } if (memcmp(command, "gps on", 6) == 0) { if (!setGpsEnabled(sensors, true)) { strcpy(reply, "gps toggle not found"); diff --git a/src/helpers/sensors/LocationProvider.h b/src/helpers/sensors/LocationProvider.h index ad2bba1d3..b15c0ed2a 100644 --- a/src/helpers/sensors/LocationProvider.h +++ b/src/helpers/sensors/LocationProvider.h @@ -86,6 +86,7 @@ public: virtual bool hasHardwareEnableState() const { return false; } virtual bool isHardwareEnabled() const { return true; } virtual bool isGpsOn() const { return _gpsOn; } + virtual bool startOnce() { return false; } virtual void setGpsOn(bool on) { _gpsOn = on; if (on) { diff --git a/src/helpers/sensors/MicroNMEALocationProvider.h b/src/helpers/sensors/MicroNMEALocationProvider.h index ea87ad193..9a6d80060 100644 --- a/src/helpers/sensors/MicroNMEALocationProvider.h +++ b/src/helpers/sensors/MicroNMEALocationProvider.h @@ -32,6 +32,7 @@ class MicroNMEALocationProvider : public LocationProvider { unsigned long _acquisitionStarted = 0; unsigned long _firstFixInWindow = 0; bool _acquiringPeriodically = false; + unsigned long _onceDeadline = 0; static const unsigned long TIME_SYNC_INTERVAL = 1800000; uint32_t _uart_bytes = 0; @@ -119,6 +120,7 @@ public: void begin() override { _begin_calls++; + _onceDeadline = 0; const bool wasActive = _driver->isActive(); _driver->begin(); if (!wasActive) { @@ -140,6 +142,7 @@ public: void stop() override { _stop_calls++; + _onceDeadline = 0; _acquiringPeriodically = false; _driver->stop(); nmea.clear(); @@ -172,6 +175,16 @@ public: return _gpsPeriodSeconds; } + bool startOnce() override { + if (_gpsOn) { // already on, acquire now + _nextAcquisition = millis(); + return true; + } + setGpsOn(true); + _onceDeadline = millis() + GPS_PERIODIC_ACQUISITION_TIMEOUT_MS; + return true; + } + void syncTime() override { nmea.clear(); LocationProvider::syncTime(); @@ -232,6 +245,21 @@ public: void loop() override { if (_driver->pollGpsSwitch()) _gpsOn = true; + if (_onceDeadline != 0) { + if (_firstFixInWindow != 0 && (long)(_onceDeadline - (millis() + GPS_FIX_SETTLE_MS)) > 0) { + _onceDeadline = millis() + GPS_FIX_SETTLE_MS; + } + if ((long)(millis() - _onceDeadline) >= 0) { + if (_driver->isActive()) { + setGpsOn(false); + } else { // do not stop twice + _gpsOn = false; + _onceDeadline = 0; + _acquiringPeriodically = false; + } + } + } + if (_gpsOn && _gpsPeriodSeconds > 0 && !_acquiringPeriodically && (long)(millis() - _nextAcquisition) >= 0) { startPeriodicAcquisition(); diff --git a/src/helpers/sensors/RakLocationProvider.h b/src/helpers/sensors/RakLocationProvider.h index 094e70ece..6ad051255 100644 --- a/src/helpers/sensors/RakLocationProvider.h +++ b/src/helpers/sensors/RakLocationProvider.h @@ -190,6 +190,14 @@ public: return _gpsPeriodSeconds; } + bool isGpsOn() const override { + return _useI2c ? _gpsOn : _nmea.isGpsOn(); + } + + bool startOnce() override { + return _useI2c ? false : _nmea.startOnce(); // not supported on the RAK12500 + } + void setGpsOn(bool on) override { if (_useI2c) LocationProvider::setGpsOn(on); else { _gpsOn = on; _nmea.setGpsOn(on); }