2020-02-06 15:39:21 +00:00
|
|
|
|
|
|
|
#include "GPS.h"
|
2020-03-18 16:16:44 +00:00
|
|
|
#include "configuration.h"
|
2020-10-01 16:11:54 +00:00
|
|
|
#include "sleep.h"
|
2020-04-15 03:22:27 +00:00
|
|
|
#include <assert.h>
|
2020-07-06 08:45:55 +00:00
|
|
|
#include <time.h>
|
2020-02-06 15:39:21 +00:00
|
|
|
|
2020-07-10 18:43:14 +00:00
|
|
|
// If we have a serial GPS port it will not be null
|
2020-04-15 03:22:27 +00:00
|
|
|
#ifdef GPS_RX_PIN
|
2020-05-04 18:15:05 +00:00
|
|
|
HardwareSerial _serial_gps_real(GPS_SERIAL_NUM);
|
2020-07-10 04:27:34 +00:00
|
|
|
HardwareSerial *GPS::_serial_gps = &_serial_gps_real;
|
|
|
|
#elif defined(NRF52840_XXAA)
|
|
|
|
// Assume NRF52840
|
|
|
|
HardwareSerial *GPS::_serial_gps = &Serial1;
|
2020-04-15 03:22:27 +00:00
|
|
|
#else
|
2020-07-10 04:27:34 +00:00
|
|
|
HardwareSerial *GPS::_serial_gps = NULL;
|
2020-04-15 03:22:27 +00:00
|
|
|
#endif
|
2020-02-20 04:02:57 +00:00
|
|
|
|
2020-07-10 21:57:33 +00:00
|
|
|
#ifdef GPS_I2C_ADDRESS
|
2020-07-11 03:17:20 +00:00
|
|
|
uint8_t GPS::i2cAddress = GPS_I2C_ADDRESS;
|
2020-07-10 21:54:32 +00:00
|
|
|
#else
|
2020-07-10 18:43:14 +00:00
|
|
|
uint8_t GPS::i2cAddress = 0;
|
2020-07-10 21:54:32 +00:00
|
|
|
#endif
|
2020-07-10 18:43:14 +00:00
|
|
|
|
2020-04-05 02:16:30 +00:00
|
|
|
bool timeSetFromGPS; // We try to set our time from GPS each time we wake from sleep
|
2020-02-06 15:39:21 +00:00
|
|
|
|
2020-05-04 18:15:05 +00:00
|
|
|
GPS *gps;
|
2020-03-14 01:44:14 +00:00
|
|
|
|
|
|
|
// stuff that really should be in in the instance instead...
|
2020-03-18 16:16:44 +00:00
|
|
|
static uint32_t
|
|
|
|
timeStartMsec; // Once we have a GPS lock, this is where we hold the initial msec clock that corresponds to that time
|
2020-03-14 01:44:14 +00:00
|
|
|
static uint64_t zeroOffsetSecs; // GPS based time in secs since 1970 - only updated once on initial lock
|
|
|
|
|
2020-05-04 18:15:05 +00:00
|
|
|
void readFromRTC()
|
2020-02-06 15:39:21 +00:00
|
|
|
{
|
2020-02-19 15:58:51 +00:00
|
|
|
struct timeval tv; /* btw settimeofday() is helpfull here too*/
|
|
|
|
|
2020-03-18 16:16:44 +00:00
|
|
|
if (!gettimeofday(&tv, NULL)) {
|
2020-09-05 19:34:48 +00:00
|
|
|
uint32_t now = millis();
|
2020-02-19 15:58:51 +00:00
|
|
|
|
2020-02-20 04:02:57 +00:00
|
|
|
DEBUG_MSG("Read RTC time as %ld (cur millis %u) valid=%d\n", tv.tv_sec, now, timeSetFromGPS);
|
2020-02-19 15:58:51 +00:00
|
|
|
timeStartMsec = now;
|
2020-02-19 18:53:09 +00:00
|
|
|
zeroOffsetSecs = tv.tv_sec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If we haven't yet set our RTC this boot, set it from a GPS derived time
|
2020-05-04 18:15:05 +00:00
|
|
|
void perhapsSetRTC(const struct timeval *tv)
|
2020-02-19 18:53:09 +00:00
|
|
|
{
|
2020-03-18 16:16:44 +00:00
|
|
|
if (!timeSetFromGPS) {
|
2020-02-19 18:53:09 +00:00
|
|
|
timeSetFromGPS = true;
|
|
|
|
DEBUG_MSG("Setting RTC %ld secs\n", tv->tv_sec);
|
2020-04-15 03:22:27 +00:00
|
|
|
#ifndef NO_ESP32
|
2020-02-19 18:53:09 +00:00
|
|
|
settimeofday(tv, NULL);
|
2020-04-15 03:22:27 +00:00
|
|
|
#else
|
2020-04-30 20:50:40 +00:00
|
|
|
DEBUG_MSG("ERROR TIME SETTING NOT IMPLEMENTED!\n");
|
2020-04-15 03:22:27 +00:00
|
|
|
#endif
|
2020-02-19 18:53:09 +00:00
|
|
|
readFromRTC();
|
2020-02-19 15:58:51 +00:00
|
|
|
}
|
2020-02-06 15:39:21 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 00:39:57 +00:00
|
|
|
void perhapsSetRTC(struct tm &t)
|
|
|
|
{
|
|
|
|
/* Convert to unix time
|
|
|
|
The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970
|
|
|
|
(midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z).
|
|
|
|
*/
|
|
|
|
time_t res = mktime(&t);
|
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = res;
|
|
|
|
tv.tv_usec = 0; // time.centisecond() * (10 / 1000);
|
|
|
|
|
2020-05-05 03:02:43 +00:00
|
|
|
// DEBUG_MSG("Got time from GPS month=%d, year=%d, unixtime=%ld\n", t.tm_mon, t.tm_year, tv.tv_sec);
|
2020-05-05 00:39:57 +00:00
|
|
|
if (t.tm_year < 0 || t.tm_year >= 300)
|
2020-07-11 03:17:20 +00:00
|
|
|
DEBUG_MSG("Ignoring invalid GPS month=%d, year=%d, unixtime=%ld\n", t.tm_mon, t.tm_year, tv.tv_sec);
|
2020-05-05 00:39:57 +00:00
|
|
|
else
|
|
|
|
perhapsSetRTC(&tv);
|
|
|
|
}
|
|
|
|
|
2020-05-04 18:15:05 +00:00
|
|
|
uint32_t getTime()
|
2020-02-21 16:41:36 +00:00
|
|
|
{
|
2020-09-05 19:34:48 +00:00
|
|
|
return ((millis() - timeStartMsec) / 1000) + zeroOffsetSecs;
|
2020-02-21 16:41:36 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 18:15:05 +00:00
|
|
|
uint32_t getValidTime()
|
2020-02-21 16:41:36 +00:00
|
|
|
{
|
|
|
|
return timeSetFromGPS ? getTime() : 0;
|
|
|
|
}
|
2020-09-29 00:04:19 +00:00
|
|
|
|
2020-10-01 16:11:54 +00:00
|
|
|
bool GPS::setup()
|
|
|
|
{
|
|
|
|
notifySleepObserver.observe(¬ifySleep);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-29 00:04:19 +00:00
|
|
|
/**
|
|
|
|
* Switch the GPS into a mode where we are actively looking for a lock, or alternatively switch GPS into a low power mode
|
|
|
|
*
|
|
|
|
* calls sleep/wake
|
|
|
|
*/
|
2020-10-01 16:11:54 +00:00
|
|
|
void GPS::setAwake(bool on)
|
2020-09-29 00:04:19 +00:00
|
|
|
{
|
2020-10-01 16:11:54 +00:00
|
|
|
if(!wakeAllowed && on) {
|
|
|
|
DEBUG_MSG("Inhibiting because !wakeAllowed\n");
|
|
|
|
on = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isAwake != on) {
|
2020-09-29 00:04:19 +00:00
|
|
|
DEBUG_MSG("WANT GPS=%d\n", on);
|
|
|
|
if (on)
|
|
|
|
wake();
|
|
|
|
else
|
|
|
|
sleep();
|
2020-10-01 16:11:54 +00:00
|
|
|
|
|
|
|
isAwake = on;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPS::loop()
|
|
|
|
{
|
|
|
|
if (whileIdle()) {
|
|
|
|
// if we have received valid NMEA claim we are connected
|
|
|
|
isConnected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are overdue for an update, turn on the GPS and at least publish the current status
|
|
|
|
uint32_t now = millis();
|
|
|
|
bool mustPublishUpdate = false;
|
|
|
|
|
|
|
|
if ((now - lastUpdateMsec) > 30 * 1000 && !isAwake) {
|
|
|
|
// We now want to be awake - so wake up the GPS
|
|
|
|
setAwake(true);
|
|
|
|
|
|
|
|
mustPublishUpdate =
|
|
|
|
true; // Even if we don't have an update this time, we at least want to occasionally publish the current state
|
|
|
|
}
|
|
|
|
|
|
|
|
// While we are awake
|
|
|
|
if (isAwake) {
|
|
|
|
DEBUG_MSG("looking for location\n");
|
|
|
|
bool gotTime = lookForTime();
|
|
|
|
bool gotLoc = lookForLocation();
|
|
|
|
|
|
|
|
if (gotLoc)
|
|
|
|
hasValidLocation = true;
|
|
|
|
|
|
|
|
mustPublishUpdate |= gotLoc;
|
|
|
|
|
|
|
|
// Once we get a location we no longer desperately want an update
|
|
|
|
if (gotLoc) {
|
|
|
|
lastUpdateMsec = now;
|
|
|
|
setAwake(false);
|
2020-09-29 00:04:19 +00:00
|
|
|
}
|
2020-10-01 16:11:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mustPublishUpdate) {
|
|
|
|
DEBUG_MSG("publishing GPS lock=%d\n", hasLock());
|
|
|
|
|
|
|
|
// Notify any status instances that are observing us
|
|
|
|
const meshtastic::GPSStatus status =
|
|
|
|
meshtastic::GPSStatus(hasLock(), isConnected, latitude, longitude, altitude, dop, heading, numSatellites);
|
|
|
|
newStatus.notifyObservers(&status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPS::forceWake(bool on)
|
|
|
|
{
|
|
|
|
if (on) {
|
|
|
|
DEBUG_MSG("Looking for GPS lock\n");
|
|
|
|
lastUpdateMsec = 0; // Force an update ASAP
|
|
|
|
wakeAllowed = true;
|
|
|
|
} else {
|
|
|
|
wakeAllowed = false;
|
|
|
|
setAwake(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Prepare the GPS for the cpu entering deep or light sleep, expect to be gone for at least 100s of msecs
|
|
|
|
int GPS::prepareSleep(void *unused)
|
|
|
|
{
|
|
|
|
forceWake(false);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|