88 lines
2.2 KiB
C++
88 lines
2.2 KiB
C++
#include "gps.h"
|
|
#include "settings.h"
|
|
|
|
#include <TinyGPS.h>
|
|
|
|
SoftwareSerial gps_begin() {
|
|
// FUTURE: If this doesn't work as expected, we may want to find a more reliable SoftwareSerial library
|
|
SoftwareSerial serial_gps(PIN_GPS_RX, PIN_GPS_TX);
|
|
// Initialise the software-based serial connection to the GPS device
|
|
serial_gps.begin(BAUD_GPS);
|
|
return serial_gps;
|
|
}
|
|
|
|
GPSLocation gps_fetch() {
|
|
// The serial connection to the GPS module
|
|
SoftwareSerial serial_gps = gps_begin();
|
|
// The GPS message decoder
|
|
TinyGPS gps;
|
|
// The struct that will hold the result
|
|
GPSLocation result;
|
|
|
|
Serial.print(F("[gps] Working "));
|
|
uint32_t start = millis(), ms_last_update = millis();
|
|
uint8_t ticks = 0;
|
|
unsigned long fix_age = TinyGPS::GPS_INVALID_AGE;
|
|
unsigned long chars;
|
|
unsigned short sentences, failed_checksum;
|
|
|
|
|
|
|
|
// We WILL discover our location - if it's the last thing we do!
|
|
while(true) {
|
|
gps.stats(&chars, &sentences, &failed_checksum);
|
|
|
|
if(millis() - start > 5000 && chars < 10) {
|
|
Serial.println(F("\n[error/gps] Init failed"));
|
|
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;
|
|
|
|
gps.f_get_position(&(result.lat), &(result.lng), &fix_age);
|
|
|
|
// If we haven't acquired a lock yet, go around again
|
|
if(fix_age == TinyGPS::GPS_INVALID_AGE) {
|
|
// NOTE: It can take a rather long time to get a lock. Just wait patiently :-)
|
|
if(millis() - ms_last_update > 500) {
|
|
ticks++;
|
|
Serial.print('.');
|
|
if(ticks > 80) {
|
|
Serial.println();
|
|
ticks = 0;
|
|
}
|
|
}
|
|
ms_last_update = millis();
|
|
continue;
|
|
}
|
|
|
|
// Hooray!
|
|
Serial.println(F("ok"));
|
|
|
|
// gps.f_get_position pushes the values into result directly
|
|
|
|
#ifdef SD_DEBUG_ENABLED
|
|
int year;
|
|
byte month, day, hour, minute, second, _hundredths;
|
|
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &_hundredths, &fix_age);
|
|
|
|
snprintf(result.time, 19, "%04d-%02d-%02d %02d:%02d:%02d",
|
|
year, month, day,
|
|
hour, minute, second
|
|
);
|
|
#endif
|
|
return result;
|
|
}
|
|
|
|
gps_end(serial_gps);
|
|
}
|
|
|
|
void gps_end(SoftwareSerial serial_gps) {
|
|
serial_gps.end();
|
|
}
|