2020-05-05 00:39:57 +00:00
|
|
|
#include "NEMAGPS.h"
|
|
|
|
#include "configuration.h"
|
2020-07-06 08:45:55 +00:00
|
|
|
#include "timing.h"
|
2020-05-05 00:39:57 +00:00
|
|
|
|
|
|
|
static int32_t toDegInt(RawDegrees d)
|
|
|
|
{
|
|
|
|
int32_t degMult = 10000000; // 1e7
|
|
|
|
int32_t r = d.deg * degMult + d.billionths / 100;
|
|
|
|
if (d.negative)
|
|
|
|
r *= -1;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NEMAGPS::loop()
|
|
|
|
{
|
2020-05-05 03:02:43 +00:00
|
|
|
|
2020-05-05 00:39:57 +00:00
|
|
|
while (_serial_gps.available() > 0) {
|
|
|
|
int c = _serial_gps.read();
|
2020-05-05 03:02:43 +00:00
|
|
|
// Serial.write(c);
|
2020-05-05 00:39:57 +00:00
|
|
|
reader.encode(c);
|
|
|
|
}
|
|
|
|
|
2020-07-06 08:45:55 +00:00
|
|
|
uint32_t now = timing::millis();
|
2020-05-05 03:02:43 +00:00
|
|
|
if ((now - lastUpdateMsec) > 20 * 1000) { // Ugly hack for now - limit update checks to once every 20 secs (but still consume
|
|
|
|
// serial chars at whatever rate)
|
|
|
|
lastUpdateMsec = now;
|
2020-05-05 00:39:57 +00:00
|
|
|
|
2020-05-05 03:02:43 +00:00
|
|
|
auto ti = reader.time;
|
|
|
|
auto d = reader.date;
|
|
|
|
if (ti.isUpdated() && ti.isValid() && d.isValid()) {
|
|
|
|
/* 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).
|
|
|
|
*/
|
|
|
|
struct tm t;
|
|
|
|
t.tm_sec = ti.second();
|
|
|
|
t.tm_min = ti.minute();
|
|
|
|
t.tm_hour = ti.hour();
|
|
|
|
t.tm_mday = d.day();
|
|
|
|
t.tm_mon = d.month() - 1;
|
|
|
|
t.tm_year = d.year() - 1900;
|
|
|
|
t.tm_isdst = false;
|
|
|
|
perhapsSetRTC(t);
|
2020-05-05 00:39:57 +00:00
|
|
|
|
2020-05-05 03:02:43 +00:00
|
|
|
isConnected = true; // we seem to have a real GPS (but not necessarily a lock)
|
2020-05-05 00:39:57 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 03:02:43 +00:00
|
|
|
if (reader.location.isUpdated()) {
|
|
|
|
if (reader.altitude.isValid())
|
|
|
|
altitude = reader.altitude.meters();
|
|
|
|
|
|
|
|
if (reader.location.isValid()) {
|
|
|
|
auto loc = reader.location.value();
|
|
|
|
latitude = toDegInt(loc.lat);
|
|
|
|
longitude = toDegInt(loc.lng);
|
|
|
|
}
|
2020-06-21 01:59:41 +00:00
|
|
|
// Diminution of precision (an accuracy metric) is reported in 10^2 units, so we need to scale down when we use it
|
2020-07-03 09:53:56 +00:00
|
|
|
if (reader.hdop.isValid()) {
|
2020-06-21 01:59:41 +00:00
|
|
|
dop = reader.hdop.value();
|
|
|
|
}
|
2020-07-03 09:53:56 +00:00
|
|
|
if (reader.course.isValid()) {
|
|
|
|
heading = reader.course.value() * 1e3; //Scale the heading (in degrees * 10^-2) to match the expected degrees * 10^-5
|
|
|
|
}
|
|
|
|
if (reader.satellites.isValid()) {
|
|
|
|
numSatellites = reader.satellites.value();
|
|
|
|
}
|
2020-05-05 00:39:57 +00:00
|
|
|
|
2020-05-05 03:02:43 +00:00
|
|
|
// expect gps pos lat=37.520825, lon=-122.309162, alt=158
|
2020-07-03 09:53:56 +00:00
|
|
|
DEBUG_MSG("new NEMA GPS pos lat=%f, lon=%f, alt=%d, hdop=%f, heading=%f\n", latitude * 1e-7, longitude * 1e-7, altitude, dop * 1e-2, heading * 1e-5);
|
2020-05-05 03:02:43 +00:00
|
|
|
|
|
|
|
hasValidLocation = (latitude != 0) || (longitude != 0); // bogus lat lon is reported as 0,0
|
|
|
|
if (hasValidLocation)
|
|
|
|
notifyObservers(NULL);
|
|
|
|
}
|
2020-06-29 01:17:52 +00:00
|
|
|
|
|
|
|
// Notify any status instances that are observing us
|
2020-07-03 09:53:56 +00:00
|
|
|
const meshtastic::GPSStatus status = meshtastic::GPSStatus(hasLock(), isConnected, latitude, longitude, altitude, dop, heading, numSatellites);
|
2020-06-29 01:17:52 +00:00
|
|
|
newStatus.notifyObservers(&status);
|
2020-05-05 00:39:57 +00:00
|
|
|
}
|
|
|
|
}
|