diff --git a/TODO.md b/TODO.md index 4c1f56843..c6d773a1a 100644 --- a/TODO.md +++ b/TODO.md @@ -59,6 +59,8 @@ until the phone pulls those packets. Ever so often power on bluetooth just so w # Low priority +* fix GPS.zeroOffset calculation it is wrong +* handle millis() rollover in GPS.getTime - otherwise we will break after 50 days * reneable the bluetooth battely level service on the T-BEAM, because we can read battery level there * report esp32 device code bugs back to the mothership via android diff --git a/src/GPS.cpp b/src/GPS.cpp index 31e26ff7d..79b6eca58 100644 --- a/src/GPS.cpp +++ b/src/GPS.cpp @@ -1,7 +1,10 @@ #include "GPS.h" +// stuff that really should be in in the instance instead... HardwareSerial _serial_gps(GPS_SERIAL_NUM); +uint32_t timeStartMsec; // Once we have a GPS lock, this is where we hold the initial msec clock that corresponds to that time +uint64_t zeroOffset; // GPS based time in millis since 1970 - only updated once on initial lock GPS gps; @@ -16,27 +19,49 @@ void GPS::setup() #endif } +// for the time being we need to rapidly read from the serial port to prevent overruns void GPS::loop() { PeriodicTask::loop(); -#ifdef GPX_RX_PIN +#ifdef GPS_RX_PIN while (_serial_gps.available()) { - _gps.encode(_serial_gps.read()); + encode(_serial_gps.read()); + } + + if (!timeStartMsec && time.isValid() && date.isValid()) + { + timeStartMsec = millis(); + + // FIXME, this is a shit not right version of the standard def of unix time!!! + zeroOffset = 1000LL * (time.second() + time.minute() * 60 + time.hour() * 60 * 60 + + 24 * 60 * 60 * (date.month() * 31 + date.day() + 365 * (date.year() - 1970))) + + time.centisecond() * 10; + + DEBUG_MSG("Setting time zero %lld", zeroOffset); } #endif } -void GPS::doTask() -{ +uint64_t GPS::getTime() { + return (millis() - timeStartMsec) * 1000LL + zeroOffset; } -String GPS::getTime() +void GPS::doTask() +{ + if (location.isValid() && location.isUpdated()) + { // we only notify if position has changed + // DEBUG_MSG("new gps pos\n"); + notifyObservers(); + } +} + + +String GPS::getTimeStr() { 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; } - diff --git a/src/GPS.h b/src/GPS.h index b82cc714e..48a81ed88 100644 --- a/src/GPS.h +++ b/src/GPS.h @@ -14,7 +14,10 @@ class GPS : public PeriodicTask, public Observable, public TinyGPSPlus public: GPS(); - String getTime(); + /// Return time since 1970 in msecs. Until we have a GPS lock we will be returning time based at zero + uint64_t getTime(); + + String getTimeStr(); void setup(); diff --git a/src/NodeDB.cpp b/src/NodeDB.cpp index 9debc47cf..bc9ea0620 100644 --- a/src/NodeDB.cpp +++ b/src/NodeDB.cpp @@ -7,6 +7,7 @@ #include "configuration.h" #include "mesh-pb-constants.h" #include "NodeDB.h" +#include "GPS.h" MyNodeInfo myNodeInfo = MyNodeInfo_init_zero; NodeDB nodeDB; @@ -34,11 +35,6 @@ static NodeNum getDesiredNodeNum() return r; } -/// return number msecs since 1970 -uint64_t getCurrentTime() -{ - return 4403; // FIXME -} NodeDB::NodeDB() @@ -60,7 +56,7 @@ void NodeDB::init() { NodeInfo *info = getOrCreateNode(getNodeNum()); info->user = owner; info->has_user = true; - info->last_seen = getCurrentTime(); + info->last_seen = 0; // haven't heard a real message yet } const NodeInfo *NodeDB::readNextInfo() @@ -87,7 +83,7 @@ void NodeDB::updateFrom(const MeshPacket &mp) if (oldNumNodes != numNodes) updateGUI = true; // we just created a nodeinfo - info->last_seen = getCurrentTime(); + info->last_seen = gps.getTime(); switch (p.which_variant) { diff --git a/src/PeriodicTask.h b/src/PeriodicTask.h index 5c92f7c7c..fa68e8059 100644 --- a/src/PeriodicTask.h +++ b/src/PeriodicTask.h @@ -27,7 +27,7 @@ public: // FIXME, this lets period slightly drift based on scheduling - not sure if that is always good prevMsec = now; - DEBUG_MSG("Calling periodic task\n"); + // DEBUG_MSG("Calling periodic task\n"); doTask(); } } diff --git a/src/configuration.h b/src/configuration.h index 5ac76bf51..523de668c 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -91,7 +91,7 @@ along with this program. If not, see . #if defined(T_BEAM_V10) #define GPS_RX_PIN 34 #ifdef USE_JTAG -#define GPS_TX_PIN -1 // We can't send bytes to the GPS while using JTAG (not a big deal) +#define GPS_TX_PIN -1 #else #define GPS_TX_PIN 12 #endif diff --git a/src/screen.ino b/src/screen.ino index 4081dc3c9..755149320 100644 --- a/src/screen.ino +++ b/src/screen.ino @@ -42,7 +42,7 @@ void _screen_header() { // Datetime display->setTextAlignment(TEXT_ALIGN_CENTER); - display->drawString(display->getWidth()/2, 2, gps.getTime()); + display->drawString(display->getWidth()/2, 2, gps.getTimeStr()); // Satellite count display->setTextAlignment(TEXT_ALIGN_RIGHT); @@ -135,7 +135,6 @@ void screen_loop() { if (axp.isVbusRemoveIRQ()) { baChStatus = "No Charging"; } - DEBUG_MSG("%s\n", baChStatus); //Prints charging status to screen // This is not a GPIO actually connected on the tbeam board // digitalWrite(2, !digitalRead(2)); axp.clearIRQ();