From 45542edf1767d0a40a6c4c9104644dc73df878f7 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Wed, 12 Jun 2019 20:18:37 +0100
Subject: [PATCH] Start weaving the libraries together, but there's quite a
 ways to go.....

---
 compile_flags.txt   |  5 +++++
 iot/main/gps.c      |  6 ++++++
 iot/main/gps.h      | 25 +++++++++++++++++++++++++
 iot/main/main.ino   | 18 ++++++++++++++++++
 iot/main/settings.h | 33 +++++++++++++++++++++++++++++++++
 iot/main/storage.c  | 17 +++++++++++++++++
 iot/main/storage.h  | 12 ++++++++++++
 7 files changed, 116 insertions(+)
 create mode 100644 compile_flags.txt
 create mode 100644 iot/main/gps.c
 create mode 100644 iot/main/gps.h
 create mode 100644 iot/main/main.ino
 create mode 100644 iot/main/settings.h
 create mode 100644 iot/main/storage.c
 create mode 100644 iot/main/storage.h

diff --git a/compile_flags.txt b/compile_flags.txt
new file mode 100644
index 0000000..d7ff18d
--- /dev/null
+++ b/compile_flags.txt
@@ -0,0 +1,5 @@
+-include/home/sbrl/Downloads/arduino/hardware/arduino/avr/cores/arduino/Arduino.h
+-include/home/sbrl/Arduino/libraries/TinyGPSPlus/src/TinyGPS++.h
+-include/home/sbrl/Downloads/arduino/libraries/SD/src/SD.h
+-I/home/sbrl/Downloads/arduino/libraries/
+-I
diff --git a/iot/main/gps.c b/iot/main/gps.c
new file mode 100644
index 0000000..d13e9c1
--- /dev/null
+++ b/iot/main/gps.c
@@ -0,0 +1,6 @@
+#include "gps.h"
+#include "settings.h"
+
+void gps_begin() {
+	serial_gps.begin(BAUD_GPS);
+}
diff --git a/iot/main/gps.h b/iot/main/gps.h
new file mode 100644
index 0000000..95d8c39
--- /dev/null
+++ b/iot/main/gps.h
@@ -0,0 +1,25 @@
+#pragma once
+
+#include <SoftwareSerial.h>
+
+#include <TinyGPS++.h>
+
+#include "settings.h"
+
+
+// The GPS message decoder
+TinyGPSPlus	gps;
+// The serial connection to the GPS device
+SoftwareSerial serial_gps(PIN_GPS_RX, PIN_GPS_TX);
+
+/**
+ * Initialises the connection to the GPS device.
+ */
+void gps_begin();
+
+/**
+ * Fetches the current location from the GPS device.
+ * May take a moment, as the GPS device needs time to acquire a satellite fix.
+ * @return TinyGPSLocation	The current location.
+ */
+TinyGPSLocation get_location();
diff --git a/iot/main/main.ino b/iot/main/main.ino
new file mode 100644
index 0000000..584c802
--- /dev/null
+++ b/iot/main/main.ino
@@ -0,0 +1,18 @@
+#include <Arduino.h>
+
+#include "settings.h"
+#include "gps.h"
+
+void setup() {
+	Serial.begin(BAUD_PC);
+	
+	gps_begin();
+	
+	
+	
+	get_location();
+}
+
+void loop() {
+	
+}
diff --git a/iot/main/settings.h b/iot/main/settings.h
new file mode 100644
index 0000000..982d2c2
--- /dev/null
+++ b/iot/main/settings.h
@@ -0,0 +1,33 @@
+#pragma once
+
+//////////////////////////////////
+////////////// Main //////////////
+//////////////////////////////////
+
+// The speed at which we should talk over our main hardware serial connection.
+#define BAUD_PC 115200
+
+// Multiple devices can use the same SPI data pin AFAIKT, but some libraries *cough* SD *cough* are too stupid to figure out which pin it is on their own.
+#define PIN_SPI_DATA			9
+
+/////////////////////////////////
+////////////// GPS //////////////
+/////////////////////////////////
+
+// The *TX* gin of the GPS device.
+// This is swapped because we receive the GPS device's message on our side on the RX pin, and the GPS device transmits messages on the TX.
+#define PIN_GPS_RX	4
+// The *RX* pin on the GPS device.
+// This is swapped because where the GPs device is receiving, we aresending and vice versa.
+// The TX / RX here are according to *our* side, not the GPS device's side.
+#define PIN_GPS_TX	3
+// The speed at which we should talk to the GPS device. Some GPS devices require a certain speed in order to use certain commands, so it's important that you check the datasheets for the device you're using.
+// 9600 is the correct speed for a NEO-6M.
+#define BAUD_GPS	9600
+
+//////////////////////////////////
+////////// microSD Card //////////
+//////////////////////////////////
+
+// 
+#define	PIN_SD_SPI_CHIP_SELECT	3
diff --git a/iot/main/storage.c b/iot/main/storage.c
new file mode 100644
index 0000000..75d1196
--- /dev/null
+++ b/iot/main/storage.c
@@ -0,0 +1,17 @@
+#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;
+}
diff --git a/iot/main/storage.h b/iot/main/storage.h
new file mode 100644
index 0000000..6642718
--- /dev/null
+++ b/iot/main/storage.h
@@ -0,0 +1,12 @@
+#pragma once
+
+#include <SPI.h>
+#include <SD.h>
+
+#include <TinyGPS++.h>
+
+void store_init();
+
+void store_reading(uint32_t id, TinyGPSLocation location);
+
+void store_close();
