Implement GPS & microSD card functionality, but it's not finished or tested yet.

This commit is contained in:
Starbeamrainbowlabs 2019-06-13 14:13:03 +01:00
parent ba8068be19
commit 0cdd8f5539
8 changed files with 80 additions and 26 deletions

View File

@ -1,6 +0,0 @@
#include "gps.h"
#include "settings.h"
void gps_begin() {
serial_gps.begin(BAUD_GPS);
}

27
iot/main/gps.cpp Normal file
View File

@ -0,0 +1,27 @@
#include "gps.h"
#include "settings.h"
void gps_begin() {
serial_gps.begin(BAUD_GPS);
}
TinyGPSLocation gps_location() {
Serial.print("[gps] Getting location: ");
while(serial_gps.available() > 0) {
if(!gps.encode(serial_gps.read()))
continue;
if(!gps.location.isValid() || gis.location.isUpdated()) {
Serial.print(".");
delay(100);
continue;
}
Serial.println("ok");
return gps.location;
}
}
void gps_end() {
Serial.println(F("[warning] Putting the GPS device to sleep isn't implemented yet."));
}

View File

@ -22,4 +22,14 @@ void gps_begin();
* May take a moment, as the GPS device needs time to acquire a satellite fix.
* @return TinyGPSLocation The current location.
*/
TinyGPSLocation get_location();
TinyGPSLocation gps_location();
/**
* Ends the connection to the GPS device and puts it to sleep in order to save
* power.
* Note that the GPS device is connected directly to the power management
* 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.
*/
void gps_end();

View File

@ -1,4 +1,5 @@
#include <Arduino.h>
#include <TinyGPS++.h>
#include "settings.h"
#include "gps.h"
@ -7,10 +8,13 @@ void setup() {
Serial.begin(BAUD_PC);
gps_begin();
TinyGPSLocation loc = get_location();
gps_end();
Serial.print("[main] Location: ("); Serial.print(loc.longitude()); Serial.print(", "); Serial.print(loc.latitude()); Serial.println();
get_location();
}
void loop() {

1
iot/main/random.h Normal file
View File

@ -0,0 +1 @@
#pragma once

View File

@ -29,5 +29,7 @@
////////// microSD Card //////////
//////////////////////////////////
//
// The chip select pin that activates the connection to the microSD card over SPI.
#define PIN_SD_SPI_CHIP_SELECT 3
#define SD_FILENAME "data.tsv"

View File

@ -1,17 +0,0 @@
#include <Arduino.h>
#include <SD.h>
#include "settings.h"
void store_init() {
pinMode(PIN_SPI_CHIP_SELECT, OUTPUT);
if(!SD.begin(PIN_SPI_DATA)) {
Serial.println("Error: Failed to initialise connection to microSD card");
while(true) { delay(1); } // Pseudo-halt, but uses delay() to ensure we keep passing back control to allow easy re-programming
}
}
void store_reading(uint32_t id, TinyGPSLocation location) {
File handle;
}

33
iot/main/storage.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <Arduino.h>
#include <SD.h>
#include "settings.h"
void store_init() {
// NOTE: If this doesn't work, then we need to use pinMode() & specify the data pin here instead.
if(!SD.begin(PIN_SD_SPI_CHIP_SELECT)) {
Serial.println(F("Error: Failed to initialise connection to microSD card"));
while(true) { delay(1); } // Pseudo-halt, but uses delay() to ensure we keep passing back control to allow easy re-programming
}
}
void store_reading(uint32_t id, TinyGPSLocation location) {
// Open the file to write the data to. If it doesn't exist it will be created.
// Unlike elsewhere, FILE_WRITE opens the file with the cursor starting at the end, so it's basically actually equivalent to FILE_APPEND that we use in other environments. Confusing, I know.
File handle = SD.open(SD_FILENAME, FILE_WRITE);
handle.print(id);
handle.print("\t");
handle.print(location.longitude());
handle.print("\t");
handle.print(location.latitude());
handle.println();
handle.close();
}
void store_close() {
SD.end();
}