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

Fork of GPS by Simon Ford

Revision:
2:0d0ce3b0052d
Parent:
1:bc118a161471
Child:
3:145bdf751843
--- 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