Library for the EM-406 GPS module with time export support added

Fork of GPS by Simon Ford

Files at this revision

API Documentation at this revision

Comitter:
WilliamO7
Date:
Sun Mar 04 18:47:05 2018 +0000
Parent:
1:bc118a161471
Child:
3:145bdf751843
Commit message:
Added time string return support

Changed in this revision

GPS.cpp Show annotated file Show diff for this revision Revisions of this file
GPS.h Show annotated file Show diff for this revision Revisions of this file
--- a/GPS.cpp	Wed Feb 21 21:05:14 2018 +0000
+++ b/GPS.cpp	Sun Mar 04 18:47:05 2018 +0000
@@ -20,14 +20,16 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
- 
+#include <string>
+#include <sstream>
+
 #include "GPS.h"
 
 GPS::GPS(PinName tx, PinName rx) : _gps(tx, rx) {
     _gps.baud(4800);    
     longitude = 0.0;
     latitude = 0.0;  
-    time_utc = 0.0;      
+    time_utc = "";      
 }
 
 int GPS::sample() {
@@ -43,7 +45,7 @@
             if(!lock) {
                 longitude = 0.0;
                 latitude = 0.0;   
-                time_utc = 0.0;     
+                time_utc = "";     
                 return 0;
             } else {
                 if(ns == 'S') {    latitude  *= -1.0; }
@@ -54,7 +56,7 @@
                 degrees = trunc(longitude / 100.0f * 0.01f);
                 minutes = longitude - (degrees * 100.0f);
                 longitude = degrees + minutes / 60.0f;
-                time_utc = time;
+                time_utc = timef_to_str(time);
                 return 1;
             }
         }
@@ -83,3 +85,17 @@
     }
     error("Overflowed message limit");
 }
+
+std::string GPS::timef_to_str(float time) {
+    // Convert time to string
+    // 234960.123
+    std::stringstream stream;
+    stream << time;
+    std::string time_str = stream.str();
+    
+    // Put into hh:mm:ss.sss format.
+    std::string new_time_str = time_str.substr(0, 2) // hh 
+                               + ":" + time_str.substr(2, 2) // mm 
+                               + ":" + time_str.substr(4, 2); // ss 
+    return new_time_str;
+}
\ No newline at end of file
--- a/GPS.h	Wed Feb 21 21:05:14 2018 +0000
+++ b/GPS.h	Sun Mar 04 18:47:05 2018 +0000
@@ -22,6 +22,7 @@
  */
 
 #include "mbed.h"
+#include <string>
 
 #ifndef MBED_GPS_H
 #define MBED_GPS_H
@@ -47,11 +48,12 @@
     float latitude;
     
     /** The time in UTC float format (call sample() to set) */
-    float time_utc;
+    std::string time_utc;
     
 private:
     float trunc(float v);
     void getline();
+    std::string timef_to_str(float time);
     
     Serial _gps;
     char msg[256];