2019-06-13 13:13:03 +00:00
|
|
|
#include "gps.h"
|
|
|
|
#include "settings.h"
|
|
|
|
|
2019-06-26 14:59:49 +00:00
|
|
|
SoftwareSerial gps_begin() {
|
|
|
|
SoftwareSerial serial_gps(PIN_GPS_RX, PIN_GPS_TX);
|
2019-06-13 13:33:08 +00:00
|
|
|
// Initialise the software-based serial connection to the GPS device
|
2019-06-26 14:59:49 +00:00
|
|
|
serial_gps.begin(BAUD_GPS);
|
|
|
|
return serial_gps;
|
2019-06-13 13:13:03 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 14:46:07 +00:00
|
|
|
GPSLocation gps_fetch() {
|
2019-06-26 14:59:49 +00:00
|
|
|
// The serial connection to the GPS module
|
|
|
|
SoftwareSerial serial_gps = gps_begin();
|
2019-06-26 14:46:07 +00:00
|
|
|
// The GPS message decoder
|
|
|
|
TinyGPSPlus gps;
|
2019-06-26 14:59:49 +00:00
|
|
|
// The struct that will hold the result
|
2019-06-26 14:46:07 +00:00
|
|
|
GPSLocation result;
|
|
|
|
|
|
|
|
Serial.print(F("[gps] Working: "));
|
2019-06-13 14:15:23 +00:00
|
|
|
uint32_t ms_last_update = millis();
|
2019-06-27 15:27:33 +00:00
|
|
|
uint8_t ticks = 0;
|
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-26 14:46:07 +00:00
|
|
|
Serial.println(F("\n[error] GPS device init failed"));
|
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
|
2019-06-26 14:59:49 +00:00
|
|
|
if(serial_gps.available() <= 0)
|
2019-06-13 13:33:08 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// If it failed, go around again
|
2019-06-26 14:59:49 +00:00
|
|
|
if(!gps.encode(serial_gps.read()))
|
2019-06-13 13:13:03 +00:00
|
|
|
continue;
|
|
|
|
|
2019-06-13 13:33:08 +00:00
|
|
|
// If we haven't acquired a lock yet, go around again
|
2019-06-26 14:59:49 +00:00
|
|
|
if(!gps.location.isValid()
|
|
|
|
#ifdef SD_DEBUG_ENABLED
|
|
|
|
|| !gps.time.isValid()
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
// NOTE: It can take a rather long time to get a lock. Just wait patiently :-)
|
2019-06-27 15:27:33 +00:00
|
|
|
if(millis() - ms_last_update > 500) {
|
|
|
|
ticks++;
|
2019-06-27 15:28:28 +00:00
|
|
|
#ifdef SD_DEBUG_ENABLED
|
2019-06-27 15:27:33 +00:00
|
|
|
Serial.print(gps.time.isValid() ? '-' : '.');
|
2019-06-27 15:28:28 +00:00
|
|
|
#else
|
|
|
|
Serial.print('.');
|
|
|
|
#endif
|
2019-06-27 15:27:33 +00:00
|
|
|
if(ticks > 80) {
|
|
|
|
Serial.println();
|
|
|
|
ticks = 0;
|
|
|
|
}
|
|
|
|
// Serial.println(gps.location.lat());
|
|
|
|
// Serial.println(gps.location.lng());
|
|
|
|
// Serial.println(gps.date.year());
|
|
|
|
// Serial.println(gps.time.hour());
|
|
|
|
// Serial.println(gps.time.minute());
|
|
|
|
}
|
2019-06-13 14:15:23 +00:00
|
|
|
ms_last_update = millis();
|
2019-06-13 13:13:03 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-13 13:33:08 +00:00
|
|
|
// Hooray!
|
2019-06-24 14:47:00 +00:00
|
|
|
Serial.println(F("ok"));
|
2019-06-26 14:46:07 +00:00
|
|
|
|
|
|
|
result.lat = gps.location.lat();
|
|
|
|
result.lng = gps.location.lng();
|
|
|
|
#ifdef SD_DEBUG_ENABLED
|
|
|
|
snprintf(result.time, 19, "%04d-%02d-%02d %02d:%02d:%02d",
|
2019-06-26 14:59:49 +00:00
|
|
|
gps.date.year(), gps.date.month(), gps.date.day(),
|
|
|
|
gps.time.hour(), gps.time.minute(), gps.time.second()
|
2019-06-26 14:46:07 +00:00
|
|
|
);
|
|
|
|
#endif
|
|
|
|
return result;
|
2019-06-13 13:13:03 +00:00
|
|
|
}
|
2019-06-26 14:59:49 +00:00
|
|
|
|
|
|
|
gps_end(serial_gps);
|
2019-06-13 13:13:03 +00:00
|
|
|
}
|
|
|
|
|
2019-06-26 14:59:49 +00:00
|
|
|
void gps_end(SoftwareSerial serial_gps) {
|
|
|
|
serial_gps.end();
|
2019-06-13 13:13:03 +00:00
|
|
|
}
|