firmware/src/GPS.cpp

105 lines
2.5 KiB
C++
Raw Normal View History

2020-02-06 15:39:21 +00:00
#include "GPS.h"
#include "time.h"
#include <sys/time.h>
2020-02-06 15:39:21 +00:00
2020-02-06 16:18:20 +00:00
// stuff that really should be in in the instance instead...
2020-02-06 15:39:21 +00:00
HardwareSerial _serial_gps(GPS_SERIAL_NUM);
2020-02-06 16:18:20 +00:00
uint32_t timeStartMsec; // Once we have a GPS lock, this is where we hold the initial msec clock that corresponds to that time
2020-02-19 18:53:09 +00:00
uint64_t zeroOffsetSecs; // GPS based time in secs since 1970 - only updated once on initial lock
2020-02-19 16:17:28 +00:00
bool timeSetFromGPS; // We only reset our time once per wake
2020-02-06 15:39:21 +00:00
GPS gps;
GPS::GPS() : PeriodicTask(30 * 1000)
{
}
void GPS::setup()
2020-02-19 16:17:28 +00:00
{
readFromRTC();
#ifdef GPS_RX_PIN
_serial_gps.begin(GPS_BAUDRATE, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN);
#endif
}
void GPS::readFromRTC()
2020-02-06 15:39:21 +00:00
{
struct timeval tv; /* btw settimeofday() is helpfull here too*/
if (!gettimeofday(&tv, NULL))
{
uint32_t now = millis();
DEBUG_MSG("Read RTC time as %ld (cur millis %u)\n", tv.tv_sec, now);
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
void GPS::perhapsSetRTC(const struct timeval *tv)
{
if (!timeSetFromGPS)
{
timeSetFromGPS = true;
DEBUG_MSG("Setting RTC %ld secs\n", tv->tv_sec);
settimeofday(tv, NULL);
readFromRTC();
}
2020-02-06 15:39:21 +00:00
}
2020-02-06 16:18:20 +00:00
// for the time being we need to rapidly read from the serial port to prevent overruns
2020-02-06 15:39:21 +00:00
void GPS::loop()
{
PeriodicTask::loop();
2020-02-06 16:18:20 +00:00
#ifdef GPS_RX_PIN
2020-02-06 15:39:21 +00:00
while (_serial_gps.available())
{
2020-02-06 16:18:20 +00:00
encode(_serial_gps.read());
}
if (!timeSetFromGPS && time.isValid() && date.isValid())
2020-02-06 16:18:20 +00:00
{
2020-02-19 16:17:28 +00:00
struct timeval tv;
2020-02-06 16:18:20 +00:00
// FIXME, this is a shit not right version of the standard def of unix time!!!
2020-02-19 16:17:28 +00:00
tv.tv_sec = time.second() + time.minute() * 60 + time.hour() * 60 * 60 +
24 * 60 * 60 * (date.month() * 31 + date.day() + 365 * (date.year() - 1970));
tv.tv_usec = time.centisecond() * (10 / 1000);
2020-02-06 16:18:20 +00:00
2020-02-19 18:53:09 +00:00
perhapsSetRTC(&tv);
2020-02-06 15:39:21 +00:00
}
#endif
}
2020-02-19 18:53:09 +00:00
uint32_t GPS::getTime()
{
return ((millis() - timeStartMsec) / 1000) + zeroOffsetSecs;
}
uint32_t GPS::getValidTime()
{
2020-02-19 18:53:09 +00:00
return timeSetFromGPS ? getTime() : 0;
2020-02-06 16:18:20 +00:00
}
2020-02-06 15:39:21 +00:00
void GPS::doTask()
{
2020-02-06 16:18:20 +00:00
if (location.isValid() && location.isUpdated())
{ // we only notify if position has changed
// DEBUG_MSG("new gps pos\n");
notifyObservers();
}
2020-02-06 15:39:21 +00:00
}
2020-02-06 16:18:20 +00:00
String GPS::getTimeStr()
2020-02-06 15:39:21 +00:00
{
static char t[12]; // used to sprintf for Serial output
snprintf(t, sizeof(t), "%02d:%02d:%02d", time.hour(), time.minute(), time.second());
return t;
}