2019-06-13 13:13:03 +00:00
|
|
|
#include "gps.h"
|
|
|
|
#include "settings.h"
|
|
|
|
|
2019-06-13 13:33:08 +00:00
|
|
|
// The GPS message decoder
|
|
|
|
TinyGPSPlus gps;
|
|
|
|
// The serial connection to the GPS device
|
|
|
|
SoftwareSerial serial_gps(PIN_GPS_RX, PIN_GPS_TX);
|
|
|
|
|
2019-06-13 13:13:03 +00:00
|
|
|
void gps_begin() {
|
2019-06-13 13:33:08 +00:00
|
|
|
// Initialise the software-based serial connection to the GPS device
|
2019-06-13 13:13:03 +00:00
|
|
|
serial_gps.begin(BAUD_GPS);
|
|
|
|
}
|
|
|
|
|
|
|
|
TinyGPSLocation gps_location() {
|
|
|
|
Serial.print("[gps] Getting location: ");
|
2019-06-13 13:33:08 +00:00
|
|
|
// We WILL discover our location - if it's the last thing we do!
|
|
|
|
while(true) {
|
2019-06-13 13:53:59 +00:00
|
|
|
if(millis() > 5000 && gps.charsProcessed() < 10) {
|
2019-06-13 14:10:12 +00:00
|
|
|
Serial.println(F("\n[error] Failed to initialise GPS device."));
|
2019-06-13 13:53:59 +00:00
|
|
|
while(true) { delay(1); }
|
|
|
|
}
|
|
|
|
|
2019-06-13 13:33:08 +00:00
|
|
|
// Make sure there's something to read
|
|
|
|
if(serial_gps.available() <= 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it failed, go around again
|
2019-06-13 13:13:03 +00:00
|
|
|
if(!gps.encode(serial_gps.read()))
|
|
|
|
continue;
|
|
|
|
|
2019-06-13 13:33:08 +00:00
|
|
|
// If we haven't acquired a lock yet, go around again
|
|
|
|
// Also go around again if the location is the same hasn't yet been updated (GPS devices are strange)
|
2019-06-13 14:10:12 +00:00
|
|
|
if(!gps.location.isValid()) {
|
2019-06-13 13:13:03 +00:00
|
|
|
Serial.print(".");
|
|
|
|
delay(100);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-13 13:33:08 +00:00
|
|
|
// Hooray!
|
2019-06-13 13:13:03 +00:00
|
|
|
Serial.println("ok");
|
|
|
|
return gps.location;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void gps_end() {
|
|
|
|
Serial.println(F("[warning] Putting the GPS device to sleep isn't implemented yet."));
|
|
|
|
}
|