From 9b76570be8bd0623fd8a74025f8f1a9ea8b0beb9 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Thu, 13 Jun 2019 14:33:08 +0100
Subject: [PATCH] Fix a bunch of bugs, but ti's still not linking

---
 iot/EntropyTest/EntropyTest.ino |  2 +-
 iot/main/gps.cpp                | 22 ++++++++++++++++++++--
 iot/main/gps.h                  |  6 ------
 iot/main/main.ino               | 12 ++++++++++--
 iot/main/random.cpp             | 10 ++++++++++
 iot/main/random.h               | 15 +++++++++++++++
 iot/main/storage.cpp            |  5 +++--
 7 files changed, 59 insertions(+), 13 deletions(-)
 create mode 100644 iot/main/random.cpp

diff --git a/iot/EntropyTest/EntropyTest.ino b/iot/EntropyTest/EntropyTest.ino
index 4c4f219..e08ed9b 100644
--- a/iot/EntropyTest/EntropyTest.ino
+++ b/iot/EntropyTest/EntropyTest.ino
@@ -24,7 +24,7 @@
 
 void setup() {
 	Serial.begin(9600);
-
+	
 	// This routine sets up the watch dog timer with interrupt handler to maintain a
 	// pool of real entropy for use in sketches. This mechanism is relatively slow
 	// since it will only produce a little less than two 32-bit random values per
diff --git a/iot/main/gps.cpp b/iot/main/gps.cpp
index 52cf6a6..8b58f08 100644
--- a/iot/main/gps.cpp
+++ b/iot/main/gps.cpp
@@ -1,22 +1,40 @@
 #include "gps.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);
+
 void gps_begin() {
+	// Initialise the software-based serial connection to the GPS device
 	serial_gps.begin(BAUD_GPS);
 }
 
 TinyGPSLocation gps_location() {
 	Serial.print("[gps] Getting location: ");
-	while(serial_gps.available() > 0) {
+	// We WILL discover our location - if it's the last thing we do!
+	while(true) {
+		// Make sure there's something to read
+		if(serial_gps.available() <= 0) {
+			Serial.print("!");
+			delay(100);
+			continue;
+		}
+		
+		// If it failed, go around again
 		if(!gps.encode(serial_gps.read()))
 			continue;
 		
-		if(!gps.location.isValid() || gis.location.isUpdated()) {
+		// 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() || gps.location.isUpdated()) {
 			Serial.print(".");
 			delay(100);
 			continue;
 		}
 		
+		// Hooray!
 		Serial.println("ok");
 		return gps.location;
 	}
diff --git a/iot/main/gps.h b/iot/main/gps.h
index b1b4e4d..b03ef4e 100644
--- a/iot/main/gps.h
+++ b/iot/main/gps.h
@@ -6,12 +6,6 @@
 
 #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.
  */
diff --git a/iot/main/main.ino b/iot/main/main.ino
index e4cd59e..f5f9ae6 100644
--- a/iot/main/main.ino
+++ b/iot/main/main.ino
@@ -2,18 +2,26 @@
 #include <TinyGPS++.h>
 
 #include "settings.h"
+#include "random.h"
 #include "gps.h"
 
+
 void setup() {
 	Serial.begin(BAUD_PC);
 	
+	random_begin();
+	
+	
 	gps_begin();
-	TinyGPSLocation loc = get_location();
+	TinyGPSLocation loc = gps_location();
 	gps_end();
 	
-	Serial.print("[main] Location: ("); Serial.print(loc.longitude()); Serial.print(", "); Serial.print(loc.latitude()); Serial.println();
+	Serial.print("[main] Location: ("); Serial.print(loc.lat()); Serial.print(", "); Serial.print(loc.lng()); Serial.println();
 	
+	uint32_t id = random_get();
 	
+	Serial.print("[main] id: ");
+	Serial.println(id);
 	
 }
 
diff --git a/iot/main/random.cpp b/iot/main/random.cpp
new file mode 100644
index 0000000..7d85ca7
--- /dev/null
+++ b/iot/main/random.cpp
@@ -0,0 +1,10 @@
+#include <Entropy.h>
+
+inline void random_begin() {
+	// Initialise the system to start gathering entropy ready to generate random numbers later
+	Entropy.initialize();
+}
+
+inline uint32_t random_get() {
+	return Entropy.random();
+}
diff --git a/iot/main/random.h b/iot/main/random.h
index 6f70f09..cb708e7 100644
--- a/iot/main/random.h
+++ b/iot/main/random.h
@@ -1 +1,16 @@
 #pragma once
+
+/**
+ * Initialises the random number generation system.
+ * It's important to do this as early as possible, as it takes a while to 
+ * gather the necessary entropy in order to actually generate a random number.
+ */
+void random_begin();
+
+// FUTURE: Swap this out for LoRa untuned wideband radio static?
+
+/**
+ * Gets an unsigned 32-bit random number.
+ * @return	{uint32_t}	A random number.
+ */
+uint32_t random_get();
diff --git a/iot/main/storage.cpp b/iot/main/storage.cpp
index d6b498a..8f5be90 100644
--- a/iot/main/storage.cpp
+++ b/iot/main/storage.cpp
@@ -1,5 +1,6 @@
 #include <Arduino.h>
 #include <SD.h>
+#include <TinyGPS++.h>
 
 #include "settings.h"
 
@@ -19,10 +20,10 @@ void store_reading(uint32_t id, TinyGPSLocation location) {
 	handle.print(id);
 	handle.print("\t");
 	
-	handle.print(location.longitude());
+	handle.print(location.lat());
 	handle.print("\t");
 	
-	handle.print(location.latitude());
+	handle.print(location.lng());
 	handle.println();
 	
 	handle.close();
