Merge upstream dev and reconcile NV3001B SPI support
This commit is contained in:
commit
a72a75cd08
51 files changed
+1226
-818
No files matched your search
@@ -7,6 +7,10 @@
|
||||
#ifdef USE_CC310_HW_CRYPTO
|
||||
#include <Adafruit_nRFCrypto.h>
|
||||
#include "nrf_cc310/include/crys_ec_edw_api.h"
|
||||
#include "nrf_cc310/include/ssi_pal_mutex.h"
|
||||
extern "C" {
|
||||
extern SaSi_PalMutex sasiAsymCryptoMutex;
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace mesh {
|
||||
@@ -30,6 +34,13 @@ bool Identity::verify(const uint8_t* sig, const uint8_t* message, int msg_len) c
|
||||
CRYSError_t rc = CRYS_ECEDW_Verify((uint8_t*)sig, CRYS_ECEDW_SIGNATURE_BYTES,
|
||||
(uint8_t*)pub_key, CRYS_ECEDW_MOD_SIZE_IN_BYTES,
|
||||
(uint8_t*)message, (size_t)msg_len, &cc310_tmp);
|
||||
|
||||
// workaround for CC310 v0.9.13 where mutex can still be locked after a failed verification
|
||||
if (rc != CRYS_OK && sasiAsymCryptoMutex != 0) {
|
||||
MESH_DEBUG_PRINTLN("CRYS_ECEDW_Verify mutex still locked after error result, unlocking");
|
||||
SaSi_PalMutexUnlock(&sasiAsymCryptoMutex);
|
||||
}
|
||||
|
||||
return rc == CRYS_OK;
|
||||
#elif 0
|
||||
// NOTE: memory corruption bug was found in this function!!
|
||||
|
||||
@@ -266,7 +266,8 @@ void BaseChatMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender
|
||||
onCommandDataRecv(from, packet, sender_timestamp, text); // let UI know
|
||||
// NOTE: no ack expected for CLI_DATA replies
|
||||
} else if (flags == TXT_TYPE_CLI_COMMAND) {
|
||||
uint8_t temp[166];
|
||||
// 5-byte datagram header + 3-byte reflected CLI prefix + 160-byte reply text + NUL
|
||||
uint8_t temp[169];
|
||||
char *command = (char *)&data[5];
|
||||
char *reply = (char *)&temp[5];
|
||||
*reply = 0;
|
||||
|
||||
@@ -467,6 +467,22 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
}
|
||||
} else if (memcmp(config, "advert.loc ", 11) == 0) {
|
||||
if (memcmp(config+11, "none", 4) == 0) {
|
||||
_prefs->advert_loc_policy = ADVERT_LOC_NONE;
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else if (memcmp(config+11, "share", 5) == 0) {
|
||||
_prefs->advert_loc_policy = ADVERT_LOC_SHARE;
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else if (memcmp(config+11, "prefs", 5) == 0) {
|
||||
_prefs->advert_loc_policy = ADVERT_LOC_PREFS;
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else {
|
||||
strcpy(reply, "Error, invalid policy");
|
||||
}
|
||||
} else if (memcmp(config, "guest.password ", 15) == 0) {
|
||||
StrHelper::strncpy(_prefs->guest_password, &config[15], sizeof(_prefs->guest_password));
|
||||
savePrefs();
|
||||
@@ -665,6 +681,20 @@ void CommonCLI::handleGetCmd(uint32_t sender_timestamp, char* command, char* rep
|
||||
sprintf(reply, "> %d", ((uint32_t) _prefs->flood_advert_interval));
|
||||
} else if (memcmp(config, "advert.interval", 15) == 0) {
|
||||
sprintf(reply, "> %d", ((uint32_t) _prefs->advert_interval) * 2);
|
||||
} else if (memcmp(config, "advert.loc", 10) == 0) {
|
||||
switch (_prefs->advert_loc_policy) {
|
||||
case ADVERT_LOC_NONE:
|
||||
strcpy(reply, "> none");
|
||||
break;
|
||||
case ADVERT_LOC_PREFS:
|
||||
strcpy(reply, "> prefs");
|
||||
break;
|
||||
case ADVERT_LOC_SHARE:
|
||||
strcpy(reply, "> share");
|
||||
break;
|
||||
default:
|
||||
strcpy(reply, "Error, invalid policy");
|
||||
}
|
||||
} else if (memcmp(config, "guest.password", 14) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->guest_password);
|
||||
} else if (sender_timestamp == 0 && memcmp(config, "prv.key", 7) == 0) { // from serial command line only
|
||||
|
||||
@@ -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;
|
||||
@@ -164,6 +165,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);
|
||||
@@ -175,6 +177,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;
|
||||
|
||||
@@ -46,6 +46,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;
|
||||
@@ -62,6 +64,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();
|
||||
@@ -180,17 +196,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 {
|
||||
@@ -198,6 +207,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) {
|
||||
|
||||
@@ -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; }
|
||||
|
||||
@@ -1,91 +0,0 @@
|
||||
#include "TimeSeriesData.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <math.h>
|
||||
|
||||
TimeSeriesData::TimeSeriesData(float *array, int num, uint32_t secs)
|
||||
: data(array), num_slots(num > 0 ? num : 0), next(0), count(0), last_timestamp(0), interval_secs(secs),
|
||||
owns_data(array == nullptr) {
|
||||
if (num_slots > 0) {
|
||||
if (owns_data) data = new float[num_slots];
|
||||
}
|
||||
}
|
||||
|
||||
TimeSeriesData::~TimeSeriesData() {
|
||||
if (owns_data) delete[] data;
|
||||
}
|
||||
|
||||
void TimeSeriesData::recordData(uint32_t now_secs, float value) {
|
||||
if (num_slots == 0 || isnan(value)) return;
|
||||
uint32_t steps = 1;
|
||||
if (count > 0 && interval_secs > 0) {
|
||||
uint32_t elapsed = now_secs - last_timestamp;
|
||||
if (elapsed < interval_secs) return;
|
||||
steps = elapsed / interval_secs;
|
||||
last_timestamp += steps * interval_secs; // keep a fixed sampling grid
|
||||
} else {
|
||||
last_timestamp = now_secs;
|
||||
}
|
||||
|
||||
// A long pause expires the whole ring in O(capacity), never O(elapsed time).
|
||||
if (steps >= uint32_t(num_slots)) {
|
||||
next = count = 0;
|
||||
} else {
|
||||
for (uint32_t i = 1; i < steps; i++) {
|
||||
data[next] = NAN;
|
||||
next = (next + 1) % num_slots;
|
||||
if (count < num_slots) count++;
|
||||
}
|
||||
}
|
||||
|
||||
data[next] = value;
|
||||
next = (next + 1) % num_slots;
|
||||
if (count < num_slots) count++;
|
||||
}
|
||||
|
||||
void TimeSeriesData::calcMinMaxAvg(uint32_t now_secs, uint32_t start_secs_ago, uint32_t end_secs_ago,
|
||||
MinMaxAvg *dest, uint8_t channel, uint8_t lpp_type) const {
|
||||
dest->_channel = channel;
|
||||
dest->_lpp_type = lpp_type;
|
||||
dest->_min = dest->_max = dest->_avg = NAN;
|
||||
int num_values = 0;
|
||||
float total = 0.0f;
|
||||
int i = next;
|
||||
uint64_t ago = uint32_t(now_secs - last_timestamp);
|
||||
for (int n = 0; n < count; n++) {
|
||||
i = (i + num_slots - 1) % num_slots;
|
||||
if (ago >= end_secs_ago && ago < start_secs_ago && !isnan(data[i])) {
|
||||
float v = data[i];
|
||||
if (num_values == 0) {
|
||||
dest->_min = dest->_max = v;
|
||||
} else {
|
||||
if (v < dest->_min) dest->_min = v;
|
||||
if (v > dest->_max) dest->_max = v;
|
||||
}
|
||||
total += v;
|
||||
num_values++;
|
||||
}
|
||||
ago += interval_secs;
|
||||
}
|
||||
if (num_values > 0) dest->_avg = total / num_values;
|
||||
}
|
||||
|
||||
// Shared only by legacy callers; never consult the RTC, even after a time sync.
|
||||
// At least one legacy call is required per millis() wrap (about 49 days).
|
||||
static uint32_t legacyMonotonicSeconds() {
|
||||
static uint32_t last_millis = 0;
|
||||
static uint64_t uptime_millis = 0;
|
||||
uint32_t now = millis();
|
||||
uptime_millis += uint32_t(now - last_millis);
|
||||
last_millis = now;
|
||||
return uptime_millis / 1000;
|
||||
}
|
||||
|
||||
void TimeSeriesData::recordData(mesh::RTCClock *, float value) {
|
||||
recordData(legacyMonotonicSeconds(), value);
|
||||
}
|
||||
|
||||
void TimeSeriesData::calcMinMaxAvg(mesh::RTCClock *, uint32_t start_secs_ago, uint32_t end_secs_ago,
|
||||
MinMaxAvg *dest, uint8_t channel, uint8_t lpp_type) const {
|
||||
calcMinMaxAvg(legacyMonotonicSeconds(), start_secs_ago, end_secs_ago, dest, channel, lpp_type);
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace mesh {
|
||||
class RTCClock;
|
||||
}
|
||||
|
||||
struct MinMaxAvg {
|
||||
float _min, _max, _avg;
|
||||
uint8_t _lpp_type, _channel;
|
||||
};
|
||||
|
||||
class TimeSeriesData {
|
||||
float *data;
|
||||
int num_slots, next, count;
|
||||
uint32_t last_timestamp, interval_secs;
|
||||
bool owns_data;
|
||||
|
||||
public:
|
||||
TimeSeriesData(float *array, int num, uint32_t secs);
|
||||
TimeSeriesData(int num, uint32_t secs) : TimeSeriesData(nullptr, num, secs) {}
|
||||
~TimeSeriesData();
|
||||
TimeSeriesData(const TimeSeriesData &) = delete;
|
||||
TimeSeriesData &operator=(const TimeSeriesData &) = delete;
|
||||
|
||||
// Caller supplies monotonic seconds, accumulated across millisecond clock wraps.
|
||||
void recordData(uint32_t now_secs, float value);
|
||||
void calcMinMaxAvg(uint32_t now_secs, uint32_t start_secs_ago, uint32_t end_secs_ago, MinMaxAvg *dest,
|
||||
uint8_t channel, uint8_t lpp_type) const;
|
||||
|
||||
// Compatibility for custom sensor sketches. The RTC argument is ignored.
|
||||
[[deprecated("Pass monotonic seconds instead of RTCClock*")]]
|
||||
void recordData(mesh::RTCClock *clock, float value);
|
||||
[[deprecated("Pass monotonic seconds instead of RTCClock*")]]
|
||||
void calcMinMaxAvg(mesh::RTCClock *clock, uint32_t start_secs_ago, uint32_t end_secs_ago, MinMaxAvg *dest,
|
||||
uint8_t channel, uint8_t lpp_type) const;
|
||||
};
|
||||
@@ -1,9 +1,6 @@
|
||||
#include "EnvironmentSensorManager.h"
|
||||
|
||||
#include <Wire.h>
|
||||
#if ENV_INCLUDE_GPS && defined(GPS_GUARD)
|
||||
#include <helpers/sensors/GpsGuard.h>
|
||||
#endif
|
||||
|
||||
#if ENV_PIN_SDA && ENV_PIN_SCL
|
||||
#define TELEM_WIRE &Wire1 // Use Wire1 as the I2C bus for Environment Sensors
|
||||
@@ -650,13 +647,6 @@ bool EnvironmentSensorManager::querySensors(uint8_t requester_permissions, Cayen
|
||||
_active_sensors[i].query(next_available_channel, _active_sensors[i].sub_channel, telemetry);
|
||||
next_available_channel++;
|
||||
}
|
||||
#if ENV_INCLUDE_GPS && defined(GPS_GUARD)
|
||||
// GPS-vs-clock offset in hours on its own channel while the guard's time alert is active
|
||||
GpsGuard* guard = _location->getGuard();
|
||||
if (guard != nullptr && guard->isTimeAlert()) {
|
||||
telemetry.addAnalogInput(next_available_channel++, guard->getAlertOffset() / 3600.0f);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -133,7 +133,6 @@ public:
|
||||
strcpy(_time_status, "-");
|
||||
}
|
||||
bool isTimeAlert() const { return _time_alert; }
|
||||
long getAlertOffset() const { return _time_disagree_offset; }
|
||||
|
||||
// Fix as reported by the receiver: lat/lon in micro-degrees, HDOP in tenths (255 = unknown),
|
||||
// gps_time = UTC epoch of the fix. Returns true if the fix can be stored as trusted.
|
||||
@@ -280,16 +279,17 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
// e.g. "ok 7sat time ok +1s", "rej region time rej nofix",
|
||||
// e.g. "ok 7sat time ok +1s", "ok 7sat time wait", "rej region time rej nofix",
|
||||
// "ok 9sat time rej step 3600s ALERT gps-clk +3600s 75m"
|
||||
void formatStatus(char* out, size_t out_size) const {
|
||||
if (!_enabled) {
|
||||
snprintf(out, out_size, "off");
|
||||
return;
|
||||
}
|
||||
// The time status is only refreshed when a sync is due: without a current trusted fix,
|
||||
// do not keep showing an old "ok" as if GPS time were usable now.
|
||||
const char* time_status = (_time_status[0] == 'o' && !trustedFixIsCurrent()) ? "rej nofix" : _time_status;
|
||||
// The time status is only refreshed when a sync is due. Without a current trusted fix
|
||||
// (e.g. GPS asleep between periodic fixes) an old "ok" is not shown as current: the last
|
||||
// check passed and the next one waits for a trusted fix.
|
||||
const char* time_status = (_time_status[0] == 'o' && !trustedFixIsCurrent()) ? "wait" : _time_status;
|
||||
if (_time_alert) {
|
||||
snprintf(out, out_size, "%s time %s ALERT gps-clk %+lds %lum", _fix_status, time_status,
|
||||
_time_disagree_offset, (unsigned long)((uint32_t)(millis() - _time_disagree_since_ms) / 60000));
|
||||
|
||||
@@ -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,8 +28,18 @@
|
||||
#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 {
|
||||
char _nmeaBuffer[100];
|
||||
// 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
|
||||
// lets those reads stop on NUL bytes instead of reading the members that follow.
|
||||
static constexpr size_t NMEA_BUFFER_LEN = 100;
|
||||
static constexpr size_t NMEA_BUFFER_TAIL = 16;
|
||||
char _nmeaBuffer[NMEA_BUFFER_LEN + NMEA_BUFFER_TAIL] = {};
|
||||
MicroNMEA nmea;
|
||||
mesh::RTCClock* _clock;
|
||||
NmeaGpsDriver* _driver;
|
||||
@@ -31,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;
|
||||
@@ -47,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
|
||||
@@ -71,6 +109,7 @@ class MicroNMEALocationProvider : public LocationProvider {
|
||||
_acquiringPeriodically = true;
|
||||
_acquisitionStarted = millis();
|
||||
_firstFixInWindow = 0;
|
||||
_goodFixInWindow = false;
|
||||
}
|
||||
|
||||
void finishPeriodicAcquisition() {
|
||||
@@ -84,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();
|
||||
@@ -102,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;
|
||||
@@ -125,6 +169,12 @@ class MicroNMEALocationProvider : public LocationProvider {
|
||||
if (isValid()) time_valid++;
|
||||
}
|
||||
|
||||
// the time sync has had its chance: done, not needed, or refused by GPS_GUARD.
|
||||
// updateTime() tries it on the tick where time_valid goes from 3 to 4.
|
||||
bool timeSyncSettled() const {
|
||||
return _clock == nullptr || !_time_sync_needed || time_valid > 3;
|
||||
}
|
||||
|
||||
bool timeSyncAllowed() {
|
||||
#ifdef GPS_GUARD
|
||||
return _guard.acceptTime(getTimestamp(), _clock->getCurrentTime());
|
||||
@@ -135,7 +185,7 @@ class MicroNMEALocationProvider : public LocationProvider {
|
||||
|
||||
public:
|
||||
MicroNMEALocationProvider(NmeaGpsDriver& driver, mesh::RTCClock* clock = nullptr)
|
||||
: nmea(_nmeaBuffer, sizeof(_nmeaBuffer)), _clock(clock), _driver(&driver) {}
|
||||
: nmea(_nmeaBuffer, NMEA_BUFFER_LEN), _clock(clock), _driver(&driver) {}
|
||||
|
||||
void begin() override {
|
||||
_begin_calls++;
|
||||
@@ -145,6 +195,7 @@ public:
|
||||
if (!wasActive) {
|
||||
_acquisitionStarted = millis();
|
||||
_firstFixInWindow = 0;
|
||||
_goodFixInWindow = false;
|
||||
}
|
||||
if (_gpsPeriodSeconds > 0 && _driver->isActive()) {
|
||||
nmea.clear();
|
||||
@@ -152,6 +203,7 @@ public:
|
||||
_acquiringPeriodically = true;
|
||||
_acquisitionStarted = millis();
|
||||
_firstFixInWindow = 0;
|
||||
_goodFixInWindow = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,6 +246,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;
|
||||
}
|
||||
@@ -294,7 +354,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) {
|
||||
@@ -330,6 +390,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)) {
|
||||
@@ -343,11 +404,18 @@ public:
|
||||
_firstFixInWindow = millis();
|
||||
fixStatsFix((uint32_t)(_firstFixInWindow - _acquisitionStarted));
|
||||
}
|
||||
if (_acquiringPeriodically) {
|
||||
// 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) ||
|
||||
(uint32_t)(millis() - _firstFixInWindow) >= GPS_FIX_SETTLE_MS) {
|
||||
// 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 has been tried, even past the settle
|
||||
// time: after a hot start the fix can come before the time is steady, and
|
||||
// the window would close unsynced until the next period. The acquisition
|
||||
// timeout below still bounds it.
|
||||
if (timeSyncSettled() &&
|
||||
(nmea.getNumSatellites() >= GPS_MIN_SATS_FOR_FIX ||
|
||||
(uint32_t)(millis() - _firstFixInWindow) >= GPS_FIX_SETTLE_MS)) {
|
||||
finishPeriodicAcquisition();
|
||||
}
|
||||
}
|
||||
@@ -358,10 +426,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();
|
||||
@@ -390,5 +461,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());
|
||||
}
|
||||
};
|
||||
@@ -41,7 +41,15 @@ class RakI2cGpsDriver : public GpsDriver {
|
||||
}
|
||||
|
||||
#ifndef RAK_3401
|
||||
pinMode(powerPin, INPUT);
|
||||
if (powerPin == WB_IO2) {
|
||||
// WB_IO2 switches the 3V3_S slot rail, which also powers a serial GPS and the other
|
||||
// slot modules. Left as an input, the rail follows whatever the base board pulls it
|
||||
// to: some keep it on, others switch it off, and the serial GPS loses power (#3830).
|
||||
// Keep it driven HIGH, as before the probe; a GPS power pin re-claims it later.
|
||||
digitalWrite(powerPin, HIGH);
|
||||
} else {
|
||||
pinMode(powerPin, INPUT);
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -211,6 +211,20 @@ public:
|
||||
return _gpsPeriodSeconds;
|
||||
}
|
||||
|
||||
// the first-fix limit is only implemented on the serial NMEA path
|
||||
void setFirstFixTimeout(uint32_t seconds) override {
|
||||
_nmea.setFirstFixTimeout(seconds);
|
||||
}
|
||||
|
||||
uint32_t getFirstFixTimeout() const override {
|
||||
return _useI2c ? 0 : _nmea.getFirstFixTimeout();
|
||||
}
|
||||
|
||||
void formatDiagnostics(char* out, size_t out_size) override {
|
||||
if (_useI2c) LocationProvider::formatDiagnostics(out, out_size);
|
||||
else _nmea.formatDiagnostics(out, out_size);
|
||||
}
|
||||
|
||||
bool isGpsOn() const override {
|
||||
return _useI2c ? _gpsOn : _nmea.isGpsOn();
|
||||
}
|
||||
|
||||
@@ -448,10 +448,12 @@ bool NV3001BDisplay::begin() {
|
||||
digitalWrite(PIN_TFT_DC, HIGH);
|
||||
delay(20);
|
||||
|
||||
#ifdef NRF52_PLATFORM
|
||||
spi.begin(); // Pins are set by the nRF52 variant.
|
||||
#else
|
||||
#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);
|
||||
|
||||
@@ -20,16 +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 {
|
||||
#ifdef NRF52_PLATFORM
|
||||
SPIClass& spi;
|
||||
#else
|
||||
SPIClass spi;
|
||||
SPIClass owned_spi;
|
||||
#endif
|
||||
SPIClass& spi;
|
||||
RefCountedDigitalPin* periph_power;
|
||||
bool is_on = false;
|
||||
uint16_t color = 0xffff;
|
||||
@@ -54,12 +52,17 @@ class NV3001BDisplay : public DisplayDriver {
|
||||
void drawChar(int x, int y, char ch);
|
||||
|
||||
public:
|
||||
#ifdef NRF52_PLATFORM
|
||||
explicit NV3001BDisplay(SPIClass& bus) :
|
||||
DisplayDriver(NV3001B_LOGICAL_WIDTH, NV3001B_LOGICAL_HEIGHT), spi(bus), periph_power(nullptr) { }
|
||||
#else
|
||||
#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();
|
||||
|
||||
Reference in new issue
Block a user