diff --git a/src/gps/GPS.cpp b/src/gps/GPS.cpp index b72d27d74..160031076 100644 --- a/src/gps/GPS.cpp +++ b/src/gps/GPS.cpp @@ -5,6 +5,7 @@ #include #include +// If we have a serial GPS port it will not be null #ifdef GPS_RX_PIN HardwareSerial _serial_gps_real(GPS_SERIAL_NUM); HardwareSerial *GPS::_serial_gps = &_serial_gps_real; @@ -15,6 +16,8 @@ HardwareSerial *GPS::_serial_gps = &Serial1; HardwareSerial *GPS::_serial_gps = NULL; #endif +uint8_t GPS::i2cAddress = 0; + bool timeSetFromGPS; // We try to set our time from GPS each time we wake from sleep GPS *gps; diff --git a/src/gps/GPS.h b/src/gps/GPS.h index cd6629995..46b4a5c05 100644 --- a/src/gps/GPS.h +++ b/src/gps/GPS.h @@ -1,8 +1,8 @@ #pragma once -#include "Observer.h" -#include "GPSStatus.h" #include "../concurrency/PeriodicTask.h" +#include "GPSStatus.h" +#include "Observer.h" #include "sys/time.h" /// If we haven't yet set our RTC this boot, set it from a GPS derived time @@ -30,12 +30,17 @@ class GPS : public Observable protected: bool hasValidLocation = false; // default to false, until we complete our first read + public: + /** If !NULL we will use this serial port to construct our GPS */ static HardwareSerial *_serial_gps; - public: + /** If !0 we will attempt to connect to the GPS over I2C */ + static uint8_t i2cAddress; + int32_t latitude = 0, longitude = 0; // as an int mult by 1e-7 to get value as double int32_t altitude = 0; - uint32_t dop = 0; // Diminution of position; PDOP where possible (UBlox), HDOP otherwise (TinyGPS) in 10^2 units (needs scaling before use) + uint32_t dop = 0; // Diminution of position; PDOP where possible (UBlox), HDOP otherwise (TinyGPS) in 10^2 units (needs + // scaling before use) uint32_t heading = 0; // Heading of motion, in degrees * 10^-5 uint32_t numSatellites = 0; diff --git a/src/gps/UBloxGPS.cpp b/src/gps/UBloxGPS.cpp index 8f52ace35..d648a0e83 100644 --- a/src/gps/UBloxGPS.cpp +++ b/src/gps/UBloxGPS.cpp @@ -7,23 +7,35 @@ UBloxGPS::UBloxGPS() : concurrency::PeriodicTask() notifySleepObserver.observe(¬ifySleep); } +bool UBloxGPS::tryConnect() +{ + isConnected = false; + + if (_serial_gps) + isConnected = ublox.begin(*_serial_gps); + + if (!isConnected && i2cAddress) + isConnected = ublox.begin(Wire, i2cAddress); + + return isConnected; +} + bool UBloxGPS::setup() { + if (_serial_gps) { #ifdef GPS_RX_PIN - _serial_gps->begin(GPS_BAUDRATE, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN); + _serial_gps->begin(GPS_BAUDRATE, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN); #else - _serial_gps->begin(GPS_BAUDRATE); + _serial_gps->begin(GPS_BAUDRATE); #endif - // _serial_gps.setRxBufferSize(1024); // the default is 256 + // _serial_gps.setRxBufferSize(1024); // the default is 256 + } + // ublox.enableDebugging(Serial); - // note: the lib's implementation has the wrong docs for what the return val is - // it is not a bool, it returns zero for success - isConnected = ublox.begin(*_serial_gps); - // try a second time, the ublox lib serial parsing is buggy? - if (!isConnected) - isConnected = ublox.begin(*_serial_gps); + if (!tryConnect()) + tryConnect(); if (isConnected) { DEBUG_MSG("Connected to UBLOX GPS successfully\n"); @@ -35,7 +47,7 @@ bool UBloxGPS::setup() // GPS_TX connected) ublox.factoryReset(); delay(3000); - isConnected = ublox.begin(*_serial_gps); + tryConnect(); DEBUG_MSG("Factory reset success=%d\n", isConnected); ok = ublox.saveConfiguration(3000); assert(ok); @@ -131,7 +143,8 @@ The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of s wantNewLocation = true; // Notify any status instances that are observing us - const meshtastic::GPSStatus status = meshtastic::GPSStatus(hasLock(), isConnected, latitude, longitude, altitude, dop, heading, numSatellites); + const meshtastic::GPSStatus status = + meshtastic::GPSStatus(hasLock(), isConnected, latitude, longitude, altitude, dop, heading, numSatellites); newStatus.notifyObservers(&status); // Once we have sent a location once we only poll the GPS rarely, otherwise check back every 1s until we have something over diff --git a/src/gps/UBloxGPS.h b/src/gps/UBloxGPS.h index 2bbecd61a..e0e3d636b 100644 --- a/src/gps/UBloxGPS.h +++ b/src/gps/UBloxGPS.h @@ -38,4 +38,7 @@ class UBloxGPS : public GPS, public concurrency::PeriodicTask /// Prepare the GPS for the cpu entering deep or light sleep, expect to be gone for at least 100s of msecs /// always returns 0 to indicate okay to sleep int prepareSleep(void *unused); + + /// Attempt to connect to our GPS, returns false if no gps is present + bool tryConnect(); }; diff --git a/src/main.cpp b/src/main.cpp index b6f963dc0..cf254b49f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -269,8 +269,12 @@ void setup() // assume NEMA at 9600 baud. DEBUG_MSG("ERROR: No UBLOX GPS found, hoping that NEMA might work\n"); delete gps; - gps = new NEMAGPS(); - gps->setup(); + + if(GPS::_serial_gps) { + // dumb NEMA access only work for serial GPSes) + gps = new NEMAGPS(); + gps->setup(); + } } #else gps = new NEMAGPS();