Slightly modified version of the SerialGPS library (https://os.mbed.com/teams/components/code/SerialGPS/) to support the BufferedSerial class
SerialGPS.cpp@1:214714f54773, 2021-02-15 (annotated)
- Committer:
- runesla
- Date:
- Mon Feb 15 20:32:48 2021 +0100
- Revision:
- 1:214714f54773
- Parent:
- 0:1a8299deb917
- Child:
- 2:e7f24389167b
switched to unbufferedserial, fixed getline
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
runesla |
0:1a8299deb917 | 1 | /* mbed GPS Module Library |
runesla |
0:1a8299deb917 | 2 | * Copyright (c) 2008-2010, sford |
runesla |
0:1a8299deb917 | 3 | * Copyright (c) 2013, B.Adryan |
runesla |
0:1a8299deb917 | 4 | * |
runesla |
0:1a8299deb917 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
runesla |
0:1a8299deb917 | 6 | * of this software and associated documentation files (the "Software"), to deal |
runesla |
0:1a8299deb917 | 7 | * in the Software without restriction, including without limitation the rights |
runesla |
0:1a8299deb917 | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
runesla |
0:1a8299deb917 | 9 | * copies of the Software, and to permit persons to whom the Software is |
runesla |
0:1a8299deb917 | 10 | * furnished to do so, subject to the following conditions: |
runesla |
0:1a8299deb917 | 11 | * |
runesla |
0:1a8299deb917 | 12 | * The above copyright notice and this permission notice shall be included in |
runesla |
0:1a8299deb917 | 13 | * all copies or substantial portions of the Software. |
runesla |
0:1a8299deb917 | 14 | * |
runesla |
0:1a8299deb917 | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
runesla |
0:1a8299deb917 | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
runesla |
0:1a8299deb917 | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
runesla |
0:1a8299deb917 | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
runesla |
0:1a8299deb917 | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
runesla |
0:1a8299deb917 | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
runesla |
0:1a8299deb917 | 21 | * THE SOFTWARE. |
runesla |
0:1a8299deb917 | 22 | */ |
runesla |
0:1a8299deb917 | 23 | |
runesla |
0:1a8299deb917 | 24 | #include "SerialGPS.h" |
runesla |
1:214714f54773 | 25 | #include <cstdio> |
runesla |
0:1a8299deb917 | 26 | |
runesla |
1:214714f54773 | 27 | SerialGPS::SerialGPS(PinName tx, PinName rx, int baud) |
runesla |
1:214714f54773 | 28 | : |
runesla |
1:214714f54773 | 29 | _gps_p(new UnbufferedSerial(tx, rx)), |
runesla |
1:214714f54773 | 30 | _gps(*_gps_p) |
runesla |
1:214714f54773 | 31 | { |
runesla |
1:214714f54773 | 32 | printf("SerialGPS"); |
runesla |
1:214714f54773 | 33 | _gps.baud(baud); |
runesla |
0:1a8299deb917 | 34 | longitude = 0.0; |
runesla |
0:1a8299deb917 | 35 | latitude = 0.0; |
runesla |
0:1a8299deb917 | 36 | } |
runesla |
0:1a8299deb917 | 37 | |
runesla |
1:214714f54773 | 38 | int SerialGPS::sample() |
runesla |
1:214714f54773 | 39 | { |
runesla |
0:1a8299deb917 | 40 | char ns, ew, unit; |
runesla |
0:1a8299deb917 | 41 | int lock; |
runesla |
0:1a8299deb917 | 42 | |
runesla |
1:214714f54773 | 43 | while(1) |
runesla |
1:214714f54773 | 44 | { |
runesla |
0:1a8299deb917 | 45 | getline(); |
runesla |
0:1a8299deb917 | 46 | |
runesla |
0:1a8299deb917 | 47 | // Check if it is a GPGGA msg (matches both locked and non-locked msg) |
runesla |
0:1a8299deb917 | 48 | if(sscanf(msg, "GPGGA,%f,%f,%c,%f,%c,%d,%d,%f,%f,%c,%f", &time, &latitude, &ns, &longitude, &ew, &lock, &sats, &hdop, &alt, &unit, &geoid) >= 1) |
runesla |
0:1a8299deb917 | 49 | { |
runesla |
0:1a8299deb917 | 50 | if(!lock) |
runesla |
0:1a8299deb917 | 51 | { |
runesla |
0:1a8299deb917 | 52 | time = 0.0; |
runesla |
0:1a8299deb917 | 53 | longitude = 0.0; |
runesla |
0:1a8299deb917 | 54 | latitude = 0.0; |
runesla |
0:1a8299deb917 | 55 | sats = 0; |
runesla |
0:1a8299deb917 | 56 | hdop = 0.0; |
runesla |
0:1a8299deb917 | 57 | alt = 0.0; |
runesla |
0:1a8299deb917 | 58 | geoid = 0.0; |
runesla |
0:1a8299deb917 | 59 | |
runesla |
0:1a8299deb917 | 60 | return 0; |
runesla |
0:1a8299deb917 | 61 | } |
runesla |
0:1a8299deb917 | 62 | else |
runesla |
0:1a8299deb917 | 63 | { |
runesla |
0:1a8299deb917 | 64 | //GPGGA format according http://aprs.gids.nl/nmea/#gga |
runesla |
0:1a8299deb917 | 65 | // time (float), lat (f), (N/S) (c), long (f), (E/W) (c), fix (d), sats (d), |
runesla |
0:1a8299deb917 | 66 | // hdop (float), altitude (float), M, geoid (float), M, , , |
runesla |
0:1a8299deb917 | 67 | //GPGGA,092010.000,5210.9546,N,00008.8913,E,1,07,1.3,9.7,M,47.0,M,,0000*5D |
runesla |
0:1a8299deb917 | 68 | |
runesla |
0:1a8299deb917 | 69 | if(ns == 'S') { latitude *= -1.0; } |
runesla |
0:1a8299deb917 | 70 | if(ew == 'W') { longitude *= -1.0; } |
runesla |
0:1a8299deb917 | 71 | |
runesla |
0:1a8299deb917 | 72 | float degrees = trunc(latitude / 100.0f); |
runesla |
0:1a8299deb917 | 73 | float minutes = latitude - (degrees * 100.0f); |
runesla |
0:1a8299deb917 | 74 | |
runesla |
0:1a8299deb917 | 75 | latitude = degrees + minutes / 60.0f; |
runesla |
0:1a8299deb917 | 76 | degrees = trunc(longitude / 100.0f * 0.01f); |
runesla |
0:1a8299deb917 | 77 | minutes = longitude - (degrees * 100.0f); |
runesla |
0:1a8299deb917 | 78 | longitude = degrees + minutes / 60.0f; |
runesla |
0:1a8299deb917 | 79 | |
runesla |
0:1a8299deb917 | 80 | return 1; |
runesla |
0:1a8299deb917 | 81 | } |
runesla |
0:1a8299deb917 | 82 | } |
runesla |
0:1a8299deb917 | 83 | } |
runesla |
0:1a8299deb917 | 84 | } |
runesla |
0:1a8299deb917 | 85 | |
runesla |
0:1a8299deb917 | 86 | float SerialGPS::trunc(float v) |
runesla |
0:1a8299deb917 | 87 | { |
runesla |
0:1a8299deb917 | 88 | if(v < 0.0) |
runesla |
0:1a8299deb917 | 89 | { |
runesla |
0:1a8299deb917 | 90 | v*= -1.0; |
runesla |
0:1a8299deb917 | 91 | v = floor(v); |
runesla |
0:1a8299deb917 | 92 | v*=-1.0; |
runesla |
0:1a8299deb917 | 93 | } |
runesla |
0:1a8299deb917 | 94 | else |
runesla |
0:1a8299deb917 | 95 | { |
runesla |
0:1a8299deb917 | 96 | v = floor(v); |
runesla |
0:1a8299deb917 | 97 | } |
runesla |
0:1a8299deb917 | 98 | return v; |
runesla |
0:1a8299deb917 | 99 | } |
runesla |
0:1a8299deb917 | 100 | |
runesla |
0:1a8299deb917 | 101 | void SerialGPS::getline() |
runesla |
0:1a8299deb917 | 102 | { |
runesla |
1:214714f54773 | 103 | char ch; |
runesla |
0:1a8299deb917 | 104 | |
runesla |
1:214714f54773 | 105 | while(_gps.read(&ch, 1) != '$') |
runesla |
0:1a8299deb917 | 106 | { |
runesla |
1:214714f54773 | 107 | for(int i = 0; i < 256; i++) |
runesla |
0:1a8299deb917 | 108 | { |
runesla |
1:214714f54773 | 109 | msg[i] = _gps.read(&ch, 1); |
runesla |
1:214714f54773 | 110 | if(msg[i] == '\r') |
runesla |
1:214714f54773 | 111 | { |
runesla |
1:214714f54773 | 112 | msg[i] = 0; |
runesla |
1:214714f54773 | 113 | return; |
runesla |
1:214714f54773 | 114 | } |
runesla |
0:1a8299deb917 | 115 | } |
runesla |
0:1a8299deb917 | 116 | } |
runesla |
0:1a8299deb917 | 117 | |
runesla |
0:1a8299deb917 | 118 | error("Overflowed message limit"); |
runesla |
0:1a8299deb917 | 119 | } |