2021 mbed Grove GPS

Dependents:   er4_2022

Revision:
1:0607ba3aa02d
Parent:
0:56d6407653a7
Child:
2:82fce70de15f
--- a/GroveGPS.h	Tue Jan 30 13:04:53 2018 -0600
+++ b/GroveGPS.h	Thu May 23 16:10:05 2019 +0000
@@ -9,7 +9,7 @@
 
 public:
 
-	GroveGPS() : _last_line("") {}
+	GroveGPS(PinName tx=D1, PinName rx=D0) : _last_line(""), gps_serial(tx, rx) {}
 
 	void readCharacter(char newCharacter) {
 		if (newCharacter == '\n') {
@@ -39,27 +39,48 @@
 	} gps_gga;
 
 	void getTimestamp(char* buffer) {
+        m.lock();
 		sprintf(buffer, "%f", gps_gga.utc_time);
+        m.unlock();
 	}
 
 	void getLatitude(char* buffer) {
+        m.lock();
 		double coordinate = convertGPSToDecimal(gps_gga.latitude);
 		if (gps_gga.position_fix==0)
 			sprintf(buffer, "N/A");
 		else
 			sprintf(buffer, "%c%f", (gps_gga.ns_indicator == 'N') ? '0' : '-', coordinate);
+        m.unlock();
 	}
 
 	void getLongitude(char* buffer) {
+        m.lock();
 		double coordinate = convertGPSToDecimal(gps_gga.longitude);
 		if (gps_gga.position_fix==0)
 			sprintf(buffer, "N/A");
 		else
 		sprintf(buffer, "%c%f", (gps_gga.ew_indicator == 'E') ? '0' : '-', coordinate);
+        m.unlock();
 	}
+    
+    void start() {
+        serial_thread.start(callback(this, &GroveGPS::read_serial));
+    }
+        
 
 private:
 	std::string _last_line;
+    Thread serial_thread;
+    Serial gps_serial;
+    Mutex m;
+    void read_serial() {
+        while (true) {
+            if (gps_serial.readable()) {
+                 readCharacter(gps_serial.getc());
+            }
+        }
+    }
 
 	double convertGPSToDecimal(double coordinate) {
 		int degrees = coordinate/100.0;
@@ -71,7 +92,9 @@
 
 	void parseLine() {
 		if (_last_line.find("GPGGA") != std::string::npos) {
+            m.lock();
 			parseGGA();
+            m.unlock();
 		}
 	}
 
@@ -98,4 +121,4 @@
 	}
 };
 
-#endif
\ No newline at end of file
+#endif