Improved, thread compatible. Adds new features

Dependents:   GroveGPS-Example

Fork of GroveGPS by Michael Ray

Files at this revision

API Documentation at this revision

Comitter:
JimCarver
Date:
Thu May 31 17:21:40 2018 +0000
Parent:
3:cc5c9faa1cc6
Commit message:
Thread based example

Changed in this revision

GroveGPS.cpp Show annotated file Show diff for this revision Revisions of this file
GroveGPS.h Show annotated file Show diff for this revision Revisions of this file
--- a/GroveGPS.cpp	Sun Apr 22 00:06:53 2018 +0000
+++ b/GroveGPS.cpp	Thu May 31 17:21:40 2018 +0000
@@ -20,15 +20,45 @@
 
 #include "GroveGPS.h"
 
-Serial gps_serial(PE_8, PE_7, 9600);
+Serial gps_serial(D1, D0, 9600);
+
+
+
+//Thread gpsThread;
+GroveGPS gps;
+    
+Semaphore parse;
+Thread parsethread(osPriorityBelowNormal);
 
 
-Thread gpsThread;
-GroveGPS gps;
+    static void parseLine() {
+        while(1) {
+            parse.wait();
+            if (gps._last_line.find("GPGGA") != std::string::npos) {
+                gps.parseGGA();
+            }
+            if (gps._last_line.find("GPZDA") != std::string::npos) {
+                gps.parseZDA();
+            }
+            if (gps._last_line.find("GPVTG") != std::string::npos) {
+                gps.parseVTG();
+            }
+            gps._last_line = "";
+        }
+    }
 
 
+    void readCharacter(char newCharacter) {
+        if (newCharacter == '\n') {
+            parse.release();
+        } else {
+            gps._last_line += newCharacter;
+        }
+    }
+        
+
 void service_serial(void) {
-    gps.readCharacter(gps_serial.getc());
+    readCharacter(gps_serial.getc());
     }
  
 
@@ -44,7 +74,7 @@
     
 void nema_send( void ) {
     
-    char nema_mode[] = "$PMTK314,5,5,5,5,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0*";
+    char nema_mode[] = "$PMTK314,0,0,5,5,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0*";
     char nema_cmd[64];
    int x = 0;
     sprintf( nema_cmd, "%s%x\r\n", nema_mode, calc_cs(nema_mode));
@@ -54,11 +84,13 @@
     }
 
 int GPS_init() {
-
+    gps._last_line = "";
    gps_serial.attach( &service_serial, Serial::RxIrq );
    gps.gps_gga.new_flag = 0;
    gps.gps_zda.new_flag = 0;
    gps.gps_vtg.new_flag = 0;
    nema_send();
-    return 0;
+   printf("\r\nGPS Init\r\n");
+   parsethread.start(parseLine);
+   return 0;
 }
\ No newline at end of file
--- a/GroveGPS.h	Sun Apr 22 00:06:53 2018 +0000
+++ b/GroveGPS.h	Thu May 31 17:21:40 2018 +0000
@@ -5,21 +5,17 @@
 #include <stdlib.h>
 #include <string>
 
+
 class GroveGPS {
 
 public:
 
-	GroveGPS() : _last_line("") {}
+	GroveGPS() : _last_line("") {
 
-	void readCharacter(char newCharacter) {
-		if (newCharacter == '\n') {
-			parseLine();
-			_last_line = "";
-		} else {
-			_last_line += newCharacter;
-		}
 	}
 
+	std::string _last_line;
+	
 	struct GGA {
 		double  utc_time; 		// Format: hhmmss.sss
 		double  latitude; 		// Format: ddmm.mmmm
@@ -102,7 +98,8 @@
 		if (gps_gga.position_fix==0) {
 			sprintf(buffer, "N/A");
 		} else {	
-		sprintf(buffer, "%c%f", (gps_gga.ns_indicator == 'N') ? '0' : '-', coordinate);
+		if(gps_gga.ns_indicator == 'S') coordinate *= -1.000;
+		sprintf(buffer, "%f", coordinate);
 		}
 	}
 
@@ -110,13 +107,12 @@
 		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);
+		else {
+		if(gps_gga.ew_indicator == 'W') coordinate *= -1.0000;
+		sprintf(buffer, "%f", coordinate);
+		}
 	}
 
-private:
-	std::string _last_line;
-
 
 	double convertGPSToDecimal(double coordinate) {
 		int degrees = coordinate/100.0;
@@ -126,18 +122,6 @@
 
 	}
 
-	void parseLine() {
-		if (_last_line.find("GPGGA") != std::string::npos) {
-			parseGGA();
-		}
-		if (_last_line.find("GPZDA") != std::string::npos) {
-			parseZDA();
-		}
-		if (_last_line.find("GPVTG") != std::string::npos) {
-			parseVTG();
-		}
-	}
-	
 void parseVTG() {
   		char* pEnd;
 		for (int i=0; i<5; i++) {