LoRaWAN-Signal-Mapping/iot/main/gps.cpp

55 lines
1.4 KiB
C++

#include "gps.h"
#include "settings.h"
// The GPS message decoder
TinyGPSPlus gps;
// The serial connection to the GPS device
SoftwareSerial serial_gps(PIN_GPS_RX, PIN_GPS_TX);
void gps_begin() {
// Initialise the software-based serial connection to the GPS device
serial_gps.begin(BAUD_GPS);
}
void gps_fetch() {
Serial.print("[gps] Getting location: ");
uint32_t ms_last_update = millis();
// We WILL discover our location - if it's the last thing we do!
while(true) {
if(millis() > 5000 && gps.charsProcessed() < 10) {
Serial.println(F("\n[error] Failed to initialise GPS device."));
while(true) { delay(1); }
}
// Make sure there's something to read
if(serial_gps.available() <= 0) {
continue;
}
// If it failed, go around again
if(!gps.encode(serial_gps.read()))
continue;
// 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)
if(!gps.location.isValid()) {
// NOTE: It can really take a rather long time to get a lock. Just wait patiently :-)
if(millis() - ms_last_update > 500) Serial.print(".");
ms_last_update = millis();
continue;
}
// Hooray!
Serial.println("ok");
return;
}
}
TinyGPSPlus gps_info() {
return gps;
}
void gps_end() {
Serial.println(F("[warning] Putting the GPS device to sleep isn't implemented yet."));
}