Rewrite to use TinyGPS instead of TinyGPS++, but it's still not small enough!

This commit is contained in:
Starbeamrainbowlabs 2019-06-28 13:57:39 +01:00
parent 04cc89ea50
commit 40f4e9130b
4 changed files with 26 additions and 20 deletions

View File

@ -1,7 +1,10 @@
#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);
@ -12,16 +15,24 @@ GPSLocation gps_fetch() {
// The serial connection to the GPS module
SoftwareSerial serial_gps = gps_begin();
// The GPS message decoder
TinyGPSPlus gps;
TinyGPS gps;
// The struct that will hold the result
GPSLocation result;
Serial.print(F("[gps] Working: "));
uint32_t ms_last_update = millis();
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) {
if(millis() > 5000 && gps.charsProcessed() < 10) {
gps.stats(&chars, &sentences, &failed_checksum);
if(millis() - start > 5000 && chars < 10) {
Serial.println(F("\n[error] GPS device init failed"));
while(true) { delay(1); }
}
@ -34,20 +45,14 @@ GPSLocation gps_fetch() {
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(!gps.location.isValid()
#ifdef SD_DEBUG_ENABLED
|| !gps.time.isValid()
#endif
) {
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++;
#ifdef SD_DEBUG_ENABLED
Serial.print(gps.time.isValid() ? '-' : '.');
#else
Serial.print('.');
#endif
if(ticks > 80) {
Serial.println();
ticks = 0;
@ -65,12 +70,16 @@ GPSLocation gps_fetch() {
// Hooray!
Serial.println(F("ok"));
result.lat = gps.location.lat();
result.lng = gps.location.lng();
// 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",
gps.date.year(), gps.date.month(), gps.date.day(),
gps.time.hour(), gps.time.minute(), gps.time.second()
year, month, day,
hour, minute, second
);
#endif
return result;

View File

@ -2,8 +2,6 @@
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
#include "settings.h"
// A lightweight struct to hold location information.

View File

@ -1,6 +1,5 @@
#include "settings.h"
#include <Arduino.h>
#include <TinyGPS++.h>
#include <MemoryFree.h>
#include "random.h"

View File

@ -3,7 +3,7 @@
#include <SPI.h>
#include <SdFat.h>
#include <TinyGPS++.h>
#include "gps.h"
void store_init();