Refactor gps system again to save memory

This commit is contained in:
Starbeamrainbowlabs 2019-06-26 15:59:49 +01:00
parent bdf9d4dd16
commit 35995fda3a
3 changed files with 30 additions and 35 deletions

View File

@ -1,18 +1,19 @@
#include "gps.h"
#include "settings.h"
// The serial connection to the GPS device
SoftwareSerial* serial_gps;
void gps_begin() {
serial_gps = new SoftwareSerial(PIN_GPS_RX, PIN_GPS_TX);
SoftwareSerial gps_begin() {
SoftwareSerial serial_gps(PIN_GPS_RX, PIN_GPS_TX);
// Initialise the software-based serial connection to the GPS device
serial_gps->begin(BAUD_GPS);
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
TinyGPSPlus gps;
// The struct that will hold the result
GPSLocation result;
Serial.print(F("[gps] Working: "));
@ -25,17 +26,20 @@ GPSLocation gps_fetch() {
}
// Make sure there's something to read
if(serial_gps->available() <= 0)
if(serial_gps.available() <= 0)
continue;
// If it failed, go around again
if(!gps.encode(serial_gps->read()))
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(!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 :-)
if(millis() - ms_last_update > 500) Serial.print(".");
ms_last_update = millis();
continue;
@ -48,21 +52,16 @@ GPSLocation gps_fetch() {
result.lng = gps.location.lng();
#ifdef SD_DEBUG_ENABLED
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()
gps.date.year(), gps.date.month(), gps.date.day(),
gps.time.hour(), gps.time.minute(), gps.time.second()
);
#endif
return result;
}
gps_end(serial_gps);
}
void gps_end() {
serial_gps->end();
delete serial_gps;
//delete gps;
// Serial.println(F("[warning] Putting the GPS device to sleep isn't implemented yet."));
void gps_end(SoftwareSerial serial_gps) {
serial_gps.end();
}

View File

@ -19,7 +19,7 @@ struct GPSLocation {
/**
* Initialises the connection to the GPS device.
*/
void gps_begin();
SoftwareSerial gps_begin();
/**
* Fetches new data from the GPS module.
@ -34,5 +34,6 @@ GPSLocation gps_fetch();
* system and so doesn't get turned off after each measurement, as it takes
* ~30s to reacquire a lock when it first starts up - hence why we put it to
* sleep instead.
* TODO: Connect the gps module to the timed power rail instead, as its; got an onboard battery.
*/
void gps_end();
void gps_end(SoftwareSerial gps);

View File

@ -19,19 +19,10 @@ void setup() {
Serial.println(F("[main] Starting"));
random_begin();
Serial.println(freeMemory(), DEC); // 1
peripheral_register(PIN_SPI_CS_RFM95);
peripheral_register(PIN_SPI_CS_SD);
Serial.println(freeMemory(), DEC); // 2
gps_begin();
Serial.println(freeMemory(), DEC); // 3
Serial.println(freeMemory(), DEC);
GPSLocation gps_data = gps_fetch();
Serial.println(freeMemory(), DEC); // 4
gps_end();
Serial.println(freeMemory(), DEC); // 5
Serial.println(freeMemory(), DEC);
Serial.print(F("[main] Location ")); Serial.print(gps_data.lat); Serial.print(F(", ")); Serial.println(gps_data.lng);
@ -41,6 +32,10 @@ void setup() {
Serial.println(id);
// Activate microSD card breakout board on the SPI bus
peripheral_register(PIN_SPI_CS_RFM95);
peripheral_register(PIN_SPI_CS_SD);
peripheral_unsilence(PIN_SPI_CS_SD);
store_init();