Merge remote-tracking branch 'rfa/main'

This commit is contained in:
tth committed 2026-11-14 10:42:44 +01:00
commit 2e8aea5c44
19 files changed
+973 -22

No files matched your search

+8
View File
@@ -64,6 +64,7 @@ public:
uint32_t gps_interval = 0; // in seconds
uint32_t gps_period = 0; // periodic acquisition interval in seconds
int32_t gps_fence = -1; // GPS_GUARD fence in metres, 0 = off, -1 = build default
int32_t gps_first_fix = -1; // first-fix timeout in seconds, 0 = no limit, -1 = build default
uint8_t gps_guard = 1; // GPS_GUARD on/off
uint8_t advert_loc_policy = 0;
uint32_t discovery_mod_timestamp = 0;
@@ -204,6 +205,7 @@ private:
def("int", _parent->gps_interval); // interval in seconds
def("period", _parent->gps_period); // periodic acquisition interval in seconds
def("adv_loc", _parent->advert_loc_policy);
def("ffix", _parent->gps_first_fix);
#ifdef GPS_GUARD
def("fence", _parent->gps_fence);
def("guard", _parent->gps_guard);
@@ -215,6 +217,12 @@ private:
void setGpsEnabled(bool enabled) override { _parent->gps_enabled = enabled; markDirty(); }
uint32_t getGpsPeriod() const override { return _parent->gps_period; }
void setGpsPeriod(uint32_t seconds) override { _parent->gps_period = seconds; markDirty(); }
bool getFirstFixTimeout(uint32_t& seconds) const override {
if (_parent->gps_first_fix < 0) return false;
seconds = (uint32_t)_parent->gps_first_fix;
return true;
}
void setFirstFixTimeout(uint32_t seconds) override { _parent->gps_first_fix = (int32_t)seconds; markDirty(); }
bool getGuardFence(uint32_t& metres) const override {
if (_parent->gps_fence < 0) return false;
metres = (uint32_t)_parent->gps_fence;
+44 -10
View File
@@ -50,6 +50,8 @@ bool CommonGpsPrefs::apply(SensorManager& sensors) const {
LocationProvider* location = sensors.getLocationProvider();
if (location == nullptr) return false;
applyGuardReference(sensors);
uint32_t first_fix;
if (getFirstFixTimeout(first_fix)) location->setFirstFixTimeout(first_fix);
location->setGpsPeriod(getGpsPeriod());
location->setGpsOn(isGpsEnabled());
return true;
@@ -66,6 +68,20 @@ static void formatDuration(char* buf, size_t size, uint32_t seconds) {
}
}
// "<n>[s|m|h|d]", seconds by default
static bool parseDuration(const char* text, uint32_t& seconds) {
char* end;
unsigned long value = strtoul(text, &end, 10);
unsigned long mult = 1;
if (*end == 'm') mult = 60;
else if (*end == 'h') mult = 3600;
else if (*end == 'd') mult = 86400;
if (mult > 1 || *end == 's') end++; // skip the suffix char
if (text[0] == '\0' || *end != '\0') return false;
seconds = (uint32_t)(value * mult);
return true;
}
bool CommonGpsPrefs::handleCommand(const char* command, SensorManager& sensors, char* reply) {
if (strcmp(command, "gps") == 0) {
LocationProvider* location = sensors.getLocationProvider();
@@ -184,17 +200,10 @@ bool CommonGpsPrefs::handleCommand(const char* command, SensorManager& sensors,
return true;
}
if (memcmp(command, "gps period ", 11) == 0) {
char* end;
unsigned long period = strtoul(command + 11, &end, 10);
unsigned long mult = 1;
if (*end == 'm') mult = 60;
else if (*end == 'h') mult = 3600;
else if (*end == 'd') mult = 86400;
if (mult > 1 || *end == 's') end++; // skip the suffix char
if (command[11] == '\0' || *end != '\0') {
uint32_t period;
if (!parseDuration(command + 11, period)) {
strcpy(reply, "Error, invalid gps period");
} else if (!setGpsPeriod(sensors, (uint32_t)(period * mult)) ||
} else if (!setGpsPeriod(sensors, period) ||
!setGpsEnabled(sensors, true)) {
strcpy(reply, "Error, gps provider not found");
} else {
@@ -202,6 +211,31 @@ bool CommonGpsPrefs::handleCommand(const char* command, SensorManager& sensors,
}
return true;
}
if (strcmp(command, "gps firstfix") == 0) {
LocationProvider* location = sensors.getLocationProvider();
if (location == nullptr) {
strcpy(reply, "Error, gps provider not found");
} else if (location->getFirstFixTimeout() == 0) {
strcpy(reply, "> 0 (no limit)");
} else {
sprintf(reply, "> %lu", (unsigned long)location->getFirstFixTimeout());
}
return true;
}
if (memcmp(command, "gps firstfix ", 13) == 0) {
LocationProvider* location = sensors.getLocationProvider();
uint32_t seconds;
if (location == nullptr) {
strcpy(reply, "Error, gps provider not found");
} else if (!parseDuration(command + 13, seconds) || seconds > 86400UL) { // at most a day
strcpy(reply, "Error, invalid first fix timeout");
} else {
location->setFirstFixTimeout(seconds);
setFirstFixTimeout(seconds); // saved by the caller (isDirty)
strcpy(reply, "OK");
}
return true;
}
if (memcmp(command, "gps sync", 8) == 0) {
LocationProvider* location = sensors.getLocationProvider();
if (location == nullptr) {
+3
View File
@@ -18,6 +18,9 @@ public:
virtual void setGpsEnabled(bool enabled) = 0;
virtual uint32_t getGpsPeriod() const = 0;
virtual void setGpsPeriod(uint32_t seconds) = 0;
// Saved first-fix timeout in seconds; false = not set, use the build default.
virtual bool getFirstFixTimeout(uint32_t& seconds) const { return false; }
virtual void setFirstFixTimeout(uint32_t seconds) { }
// Reference position for the anti-spoofing guard (GPS_GUARD), e.g. the configured node position.
virtual bool getGuardReference(double& lat, double& lon) const { return false; }
+4
View File
@@ -18,6 +18,7 @@ public:
long satellites = 0;
long timestamp = 0;
unsigned long acquiredAt = 0; // millis() when the fix was stored
uint8_t fixMode = 0; // from GSA: 2 = 2D, 3 = 3D, 0 = unknown
};
struct FixStats {
@@ -81,6 +82,9 @@ public:
}
virtual void setGpsPeriod(uint32_t seconds) { }
virtual uint32_t getGpsPeriod() const { return 0; }
// periodic mode: limit of an acquisition before the first fix since boot, 0 = no limit
virtual void setFirstFixTimeout(uint32_t seconds) { }
virtual uint32_t getFirstFixTimeout() const { return 0; }
virtual uint32_t getLastFixAge() const { // in seconds, UINT32_MAX if never
const LastFix f = getLastFix();
return f.valid ? (uint32_t)(millis() - f.acquiredAt) / 1000UL : UINT32_MAX;
@@ -12,6 +12,12 @@
#ifndef GPS_PERIODIC_ACQUISITION_TIMEOUT_MS
#define GPS_PERIODIC_ACQUISITION_TIMEOUT_MS 300000UL
#endif
// default limit for the periodic window before the first fix since boot (a cold start
// can take up to ~30 min with a weak sky view); 0 = keep searching until a fix.
// Can be changed at runtime with `gps firstfix`.
#ifndef GPS_FIRST_FIX_TIMEOUT_MS
#define GPS_FIRST_FIX_TIMEOUT_MS 3600000UL
#endif
// in periodic mode, keep reading after the first valid fix until enough satellites
// are used or the settle time has elapsed
@@ -22,6 +28,11 @@
#define GPS_FIX_SETTLE_MS 10000UL
#endif
// a 2D fix does not replace a 3D fix younger than this (it is less precise)
#ifndef GPS_3D_FIX_PREFERRED_MS
#define GPS_3D_FIX_PREFERRED_MS 300000UL
#endif
class MicroNMEALocationProvider : public LocationProvider {
// MicroNMEA never writes past the length it is given, but its fixed-width field
// skips can read a few bytes past the end of a malformed sentence. The zeroed tail
@@ -36,9 +47,11 @@ class MicroNMEALocationProvider : public LocationProvider {
long time_valid = 0;
unsigned long _last_time_sync = 0;
uint32_t _gpsPeriodSeconds = 0;
uint32_t _firstFixTimeoutSeconds = GPS_FIRST_FIX_TIMEOUT_MS / 1000UL;
unsigned long _nextAcquisition = 0;
unsigned long _acquisitionStarted = 0;
unsigned long _firstFixInWindow = 0;
bool _goodFixInWindow = false; // a fix that lets the GPS sleep (3D, or fix mode unknown)
bool _acquiringPeriodically = false;
unsigned long _onceDeadline = 0;
static const unsigned long TIME_SYNC_INTERVAL = 1800000;
@@ -52,6 +65,26 @@ class MicroNMEALocationProvider : public LocationProvider {
unsigned long _last_nmea_ms = 0;
bool _uart_seen = false;
bool _nmea_seen = false;
uint8_t _gsa_fix_mode = 0; // fix mode from the last GSA: 1 = none, 2 = 2D, 3 = 3D
unsigned long _gsa_ms = 0;
static bool isGsa(const char* id) { // GPGSA, GNGSA, BDGSA, ...
const size_t n = strlen(id);
return n >= 3 && strcmp(id + n - 3, "GSA") == 0;
}
// $--GSA,<mode A/M>,<fix 1/2/3>,...
void parseGsa(const char* s) {
s = strchr(s, ',');
if (s != NULL) s = strchr(s + 1, ',');
if (s != NULL && s[1] >= '1' && s[1] <= '3') {
_gsa_fix_mode = s[1] - '0';
_gsa_ms = millis();
}
}
// 0 (unknown) if the receiver does not send GSA, or sent none recently
uint8_t currentFixMode() const {
return (_gsa_fix_mode != 0 && (uint32_t)(millis() - _gsa_ms) <= 2000) ? _gsa_fix_mode : 0;
}
#ifdef GPS_GUARD
GpsGuard _guard;
#endif
@@ -76,6 +109,7 @@ class MicroNMEALocationProvider : public LocationProvider {
_acquiringPeriodically = true;
_acquisitionStarted = millis();
_firstFixInWindow = 0;
_goodFixInWindow = false;
}
void finishPeriodicAcquisition() {
@@ -89,13 +123,17 @@ class MicroNMEALocationProvider : public LocationProvider {
bool saveFix() {
// only store a fix when the snapshot is coherent (sats > 0).
// Returns true if the fix was stored.
// Returns true if the fix was accepted (stored, or kept out by a recent 3D fix).
if (nmea.getNumSatellites() == 0) return false;
#ifdef GPS_GUARD
// only plausible, stable fixes become the last fix (position, telemetry, time sync)
if (!_guard.acceptFix(nmea.getLatitude(), nmea.getLongitude(), nmea.getNumSatellites(),
nmea.getHDOP(), getTimestamp())) return false;
#endif
const uint8_t mode = currentFixMode();
// a 2D fix is valid but less precise: keep a recent 3D fix instead
if (mode == 2 && _lastFix.valid && _lastFix.fixMode == 3 &&
(uint32_t)(millis() - _lastFix.acquiredAt) < GPS_3D_FIX_PREFERRED_MS) return true;
LastFix fix;
fix.latitude = nmea.getLatitude();
@@ -107,6 +145,7 @@ class MicroNMEALocationProvider : public LocationProvider {
fix.satellites = nmea.getNumSatellites();
fix.timestamp = getTimestamp();
fix.acquiredAt = millis();
fix.fixMode = mode;
fix.valid = true;
_lastFix = fix;
return true;
@@ -150,6 +189,7 @@ public:
if (!wasActive) {
_acquisitionStarted = millis();
_firstFixInWindow = 0;
_goodFixInWindow = false;
}
if (_gpsPeriodSeconds > 0 && _driver->isActive()) {
nmea.clear();
@@ -157,6 +197,7 @@ public:
_acquiringPeriodically = true;
_acquisitionStarted = millis();
_firstFixInWindow = 0;
_goodFixInWindow = false;
}
}
@@ -199,6 +240,14 @@ public:
return _gpsPeriodSeconds;
}
void setFirstFixTimeout(uint32_t seconds) override {
_firstFixTimeoutSeconds = seconds;
}
uint32_t getFirstFixTimeout() const override {
return _firstFixTimeoutSeconds;
}
uint32_t getAcquisitionAge() const override {
return (uint32_t)(millis() - _acquisitionStarted) / 1000UL;
}
@@ -299,7 +348,7 @@ public:
if (_driver->pollGpsSwitch()) _gpsOn = true;
if (_onceDeadline != 0) {
if (_firstFixInWindow != 0 && (long)(_onceDeadline - (millis() + GPS_FIX_SETTLE_MS)) > 0) {
if (_goodFixInWindow && (long)(_onceDeadline - (millis() + GPS_FIX_SETTLE_MS)) > 0) {
_onceDeadline = millis() + GPS_FIX_SETTLE_MS;
}
if ((long)(millis() - _onceDeadline) >= 0) {
@@ -335,6 +384,7 @@ public:
_nmea_seen = true;
const char* messageId = nmea.getMessageID();
if (isGsa(messageId)) parseGsa(nmea.getSentence());
#ifdef GPS_GUARD
if (parsed && !nmea.isValid() &&
(strcmp(messageId, "GGA") == 0 || strcmp(messageId, "RMC") == 0)) {
@@ -348,7 +398,11 @@ public:
_firstFixInWindow = millis();
fixStatsFix((uint32_t)(_firstFixInWindow - _acquisitionStarted));
}
if (_acquiringPeriodically) {
// after a 2D fix the GPS stays on (up to the acquisition timeout)
// to give the receiver a chance to get a 3D fix
const uint8_t mode = currentFixMode();
if (mode == 0 || mode == 3) _goodFixInWindow = true;
if (_acquiringPeriodically && _goodFixInWindow) {
// stay on until a pending time sync is done (or the settle time is over)
const bool synced = _clock == nullptr || !_time_sync_needed;
if ((nmea.getNumSatellites() >= GPS_MIN_SATS_FOR_FIX && synced) ||
@@ -363,10 +417,13 @@ public:
}
}
// no timeout until a first fix has been obtained (cold start can be long)
if (_acquiringPeriodically && _lastFix.valid &&
(uint32_t)(millis() - _acquisitionStarted) >= GPS_PERIODIC_ACQUISITION_TIMEOUT_MS) {
finishPeriodicAcquisition();
// a longer limit until a first fix has been obtained (cold start can be long)
if (_acquiringPeriodically) {
const uint32_t limit = _lastFix.valid ? GPS_PERIODIC_ACQUISITION_TIMEOUT_MS
: _firstFixTimeoutSeconds * 1000UL;
if (limit > 0 && (uint32_t)(millis() - _acquisitionStarted) >= limit) {
finishPeriodicAcquisition();
}
}
updateTime();
@@ -395,5 +452,8 @@ public:
fix_age,
(unsigned long)_begin_calls,
(unsigned long)_stop_calls);
// fix mode from GSA (1 none, 2 2D, 3 3D, 0 no recent GSA)
const size_t len = strlen(out);
if (len < out_size) snprintf(out + len, out_size - len, " fm:%u", currentFixMode());
}
};
+6
View File
@@ -425,7 +425,13 @@ bool NV3001BDisplay::begin() {
digitalWrite(PIN_TFT_DC, HIGH);
delay(20);
#if defined(NRF52_PLATFORM)
spi.begin();
#elif defined(ESP_PLATFORM) || defined(ESP32_PLATFORM)
spi.begin(PIN_TFT_SCL, PIN_TFT_MISO, PIN_TFT_SDA, PIN_TFT_CS);
#else
spi.begin();
#endif
if (PIN_TFT_RST >= 0) {
pinMode(PIN_TFT_RST, OUTPUT);
digitalWrite(PIN_TFT_RST, HIGH);
+16 -4
View File
@@ -20,12 +20,14 @@
#define NV3001B_PANEL_HEIGHT 220
#endif
class NV3001BDisplay : public DisplayDriver {
#if defined(ESP_PLATFORM) || defined(ESP32_PLATFORM)
#ifndef NV3001B_SPI_HOST
#define NV3001B_SPI_HOST HSPI
#endif
class NV3001BDisplay : public DisplayDriver {
SPIClass spi;
SPIClass owned_spi;
#endif
SPIClass& spi;
RefCountedDigitalPin* periph_power;
bool is_on = false;
uint16_t color = 0xffff;
@@ -43,8 +45,18 @@ class NV3001BDisplay : public DisplayDriver {
void drawChar(int x, int y, char ch);
public:
#if defined(ESP_PLATFORM) || defined(ESP32_PLATFORM)
NV3001BDisplay(RefCountedDigitalPin* power = nullptr) :
DisplayDriver(NV3001B_LOGICAL_WIDTH, NV3001B_LOGICAL_HEIGHT), spi(NV3001B_SPI_HOST), periph_power(power) { }
DisplayDriver(NV3001B_LOGICAL_WIDTH, NV3001B_LOGICAL_HEIGHT),
owned_spi(NV3001B_SPI_HOST), spi(owned_spi), periph_power(power) { }
NV3001BDisplay(SPIClass* displaySpi, RefCountedDigitalPin* power = nullptr) :
DisplayDriver(NV3001B_LOGICAL_WIDTH, NV3001B_LOGICAL_HEIGHT),
owned_spi(NV3001B_SPI_HOST), spi(displaySpi ? *displaySpi : owned_spi), periph_power(power) { }
#else
NV3001BDisplay(SPIClass* displaySpi = &SPI, RefCountedDigitalPin* power = nullptr) :
DisplayDriver(NV3001B_LOGICAL_WIDTH, NV3001B_LOGICAL_HEIGHT), spi(displaySpi ? *displaySpi : SPI), periph_power(power) { }
#endif
bool begin();
static const char* driverName() { return "NV3001B"; }