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

Fork of GPS by Simon Ford

Committer:
WilliamO7
Date:
Sun Mar 04 18:47:05 2018 +0000
Revision:
2:0d0ce3b0052d
Parent:
1:bc118a161471
Child:
3:145bdf751843
Added time string return support

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WilliamO7 1:bc118a161471 1 /* mbed EM-406 GPS Module Library with time return support
simon 0:15611c7938a3 2 * Copyright (c) 2008-2010, sford
WilliamO7 1:bc118a161471 3 * Copyright (c) 2018 WilliamO7
simon 0:15611c7938a3 4 *
simon 0:15611c7938a3 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
simon 0:15611c7938a3 6 * of this software and associated documentation files (the "Software"), to deal
simon 0:15611c7938a3 7 * in the Software without restriction, including without limitation the rights
simon 0:15611c7938a3 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
simon 0:15611c7938a3 9 * copies of the Software, and to permit persons to whom the Software is
simon 0:15611c7938a3 10 * furnished to do so, subject to the following conditions:
simon 0:15611c7938a3 11 *
simon 0:15611c7938a3 12 * The above copyright notice and this permission notice shall be included in
simon 0:15611c7938a3 13 * all copies or substantial portions of the Software.
simon 0:15611c7938a3 14 *
simon 0:15611c7938a3 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
simon 0:15611c7938a3 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
simon 0:15611c7938a3 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
simon 0:15611c7938a3 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
simon 0:15611c7938a3 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
simon 0:15611c7938a3 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
simon 0:15611c7938a3 21 * THE SOFTWARE.
simon 0:15611c7938a3 22 */
WilliamO7 2:0d0ce3b0052d 23 #include <string>
WilliamO7 2:0d0ce3b0052d 24 #include <sstream>
WilliamO7 2:0d0ce3b0052d 25
simon 0:15611c7938a3 26 #include "GPS.h"
simon 0:15611c7938a3 27
simon 0:15611c7938a3 28 GPS::GPS(PinName tx, PinName rx) : _gps(tx, rx) {
simon 0:15611c7938a3 29 _gps.baud(4800);
simon 0:15611c7938a3 30 longitude = 0.0;
WilliamO7 1:bc118a161471 31 latitude = 0.0;
WilliamO7 2:0d0ce3b0052d 32 time_utc = "";
simon 0:15611c7938a3 33 }
simon 0:15611c7938a3 34
simon 0:15611c7938a3 35 int GPS::sample() {
simon 0:15611c7938a3 36 float time;
simon 0:15611c7938a3 37 char ns, ew;
simon 0:15611c7938a3 38 int lock;
simon 0:15611c7938a3 39
simon 0:15611c7938a3 40 while(1) {
simon 0:15611c7938a3 41 getline();
simon 0:15611c7938a3 42
simon 0:15611c7938a3 43 // Check if it is a GPGGA msg (matches both locked and non-locked msg)
simon 0:15611c7938a3 44 if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d", &time, &latitude, &ns, &longitude, &ew, &lock) >= 1) {
simon 0:15611c7938a3 45 if(!lock) {
simon 0:15611c7938a3 46 longitude = 0.0;
WilliamO7 1:bc118a161471 47 latitude = 0.0;
WilliamO7 2:0d0ce3b0052d 48 time_utc = "";
simon 0:15611c7938a3 49 return 0;
simon 0:15611c7938a3 50 } else {
simon 0:15611c7938a3 51 if(ns == 'S') { latitude *= -1.0; }
simon 0:15611c7938a3 52 if(ew == 'W') { longitude *= -1.0; }
simon 0:15611c7938a3 53 float degrees = trunc(latitude / 100.0f);
simon 0:15611c7938a3 54 float minutes = latitude - (degrees * 100.0f);
simon 0:15611c7938a3 55 latitude = degrees + minutes / 60.0f;
simon 0:15611c7938a3 56 degrees = trunc(longitude / 100.0f * 0.01f);
simon 0:15611c7938a3 57 minutes = longitude - (degrees * 100.0f);
simon 0:15611c7938a3 58 longitude = degrees + minutes / 60.0f;
WilliamO7 2:0d0ce3b0052d 59 time_utc = timef_to_str(time);
simon 0:15611c7938a3 60 return 1;
simon 0:15611c7938a3 61 }
simon 0:15611c7938a3 62 }
simon 0:15611c7938a3 63 }
simon 0:15611c7938a3 64 }
simon 0:15611c7938a3 65
simon 0:15611c7938a3 66 float GPS::trunc(float v) {
simon 0:15611c7938a3 67 if(v < 0.0) {
simon 0:15611c7938a3 68 v*= -1.0;
simon 0:15611c7938a3 69 v = floor(v);
simon 0:15611c7938a3 70 v*=-1.0;
simon 0:15611c7938a3 71 } else {
simon 0:15611c7938a3 72 v = floor(v);
simon 0:15611c7938a3 73 }
simon 0:15611c7938a3 74 return v;
simon 0:15611c7938a3 75 }
simon 0:15611c7938a3 76
simon 0:15611c7938a3 77 void GPS::getline() {
simon 0:15611c7938a3 78 while(_gps.getc() != '$'); // wait for the start of a line
simon 0:15611c7938a3 79 for(int i=0; i<256; i++) {
simon 0:15611c7938a3 80 msg[i] = _gps.getc();
simon 0:15611c7938a3 81 if(msg[i] == '\r') {
simon 0:15611c7938a3 82 msg[i] = 0;
simon 0:15611c7938a3 83 return;
simon 0:15611c7938a3 84 }
simon 0:15611c7938a3 85 }
simon 0:15611c7938a3 86 error("Overflowed message limit");
simon 0:15611c7938a3 87 }
WilliamO7 2:0d0ce3b0052d 88
WilliamO7 2:0d0ce3b0052d 89 std::string GPS::timef_to_str(float time) {
WilliamO7 2:0d0ce3b0052d 90 // Convert time to string
WilliamO7 2:0d0ce3b0052d 91 // 234960.123
WilliamO7 2:0d0ce3b0052d 92 std::stringstream stream;
WilliamO7 2:0d0ce3b0052d 93 stream << time;
WilliamO7 2:0d0ce3b0052d 94 std::string time_str = stream.str();
WilliamO7 2:0d0ce3b0052d 95
WilliamO7 2:0d0ce3b0052d 96 // Put into hh:mm:ss.sss format.
WilliamO7 2:0d0ce3b0052d 97 std::string new_time_str = time_str.substr(0, 2) // hh
WilliamO7 2:0d0ce3b0052d 98 + ":" + time_str.substr(2, 2) // mm
WilliamO7 2:0d0ce3b0052d 99 + ":" + time_str.substr(4, 2); // ss
WilliamO7 2:0d0ce3b0052d 100 return new_time_str;
WilliamO7 2:0d0ce3b0052d 101 }