Allows for a GPS module to be connected to a serial port and exposes an easy to use API to get the GPS data.
Fork of MODGPS by
GPS.cpp@2:8aa059e7d8b1, 2011-04-15 (annotated)
- Committer:
- AjK
- Date:
- Fri Apr 15 12:23:52 2011 +0000
- Revision:
- 2:8aa059e7d8b1
- Parent:
- 0:db98027c0bbb
- Child:
- 3:28a1b60b0f37
1.12 See ChangeLog.c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AjK | 0:db98027c0bbb | 1 | /* |
AjK | 0:db98027c0bbb | 2 | Copyright (c) 2010 Andy Kirkham |
AjK | 0:db98027c0bbb | 3 | |
AjK | 0:db98027c0bbb | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
AjK | 0:db98027c0bbb | 5 | of this software and associated documentation files (the "Software"), to deal |
AjK | 0:db98027c0bbb | 6 | in the Software without restriction, including without limitation the rights |
AjK | 0:db98027c0bbb | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
AjK | 0:db98027c0bbb | 8 | copies of the Software, and to permit persons to whom the Software is |
AjK | 0:db98027c0bbb | 9 | furnished to do so, subject to the following conditions: |
AjK | 0:db98027c0bbb | 10 | |
AjK | 0:db98027c0bbb | 11 | The above copyright notice and this permission notice shall be included in |
AjK | 0:db98027c0bbb | 12 | all copies or substantial portions of the Software. |
AjK | 0:db98027c0bbb | 13 | |
AjK | 0:db98027c0bbb | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
AjK | 0:db98027c0bbb | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
AjK | 0:db98027c0bbb | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
AjK | 0:db98027c0bbb | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
AjK | 0:db98027c0bbb | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
AjK | 0:db98027c0bbb | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
AjK | 0:db98027c0bbb | 20 | THE SOFTWARE. |
AjK | 0:db98027c0bbb | 21 | */ |
AjK | 0:db98027c0bbb | 22 | |
AjK | 0:db98027c0bbb | 23 | #include "GPS.h" |
AjK | 0:db98027c0bbb | 24 | |
AjK | 0:db98027c0bbb | 25 | GPS::GPS(PinName tx, PinName rx, const char *name) : Serial(tx, rx, name) |
AjK | 0:db98027c0bbb | 26 | { |
AjK | 0:db98027c0bbb | 27 | switch(_uidx) { |
AjK | 0:db98027c0bbb | 28 | case 1: _base = LPC_UART1; break; |
AjK | 0:db98027c0bbb | 29 | case 2: _base = LPC_UART2; break; |
AjK | 0:db98027c0bbb | 30 | case 3: _base = LPC_UART3; break; |
AjK | 0:db98027c0bbb | 31 | default : _base = NULL; break; |
AjK | 0:db98027c0bbb | 32 | } |
AjK | 0:db98027c0bbb | 33 | |
AjK | 0:db98027c0bbb | 34 | _pps = NULL; |
AjK | 0:db98027c0bbb | 35 | _ppsInUse = false; |
AjK | 0:db98027c0bbb | 36 | |
AjK | 0:db98027c0bbb | 37 | if (_base != NULL) attach(this, &GPS::rx_irq); |
AjK | 0:db98027c0bbb | 38 | |
AjK | 0:db98027c0bbb | 39 | _second100 = new Ticker; |
AjK | 0:db98027c0bbb | 40 | _second100->attach_us(this, &GPS::ticktock, GPS_TICKTOCK); |
AjK | 0:db98027c0bbb | 41 | } |
AjK | 0:db98027c0bbb | 42 | |
AjK | 0:db98027c0bbb | 43 | void |
AjK | 0:db98027c0bbb | 44 | GPS::ppsAttach(PinName irq, ppsEdgeType type) |
AjK | 0:db98027c0bbb | 45 | { |
AjK | 0:db98027c0bbb | 46 | if (_pps != NULL) delete(_pps); |
AjK | 0:db98027c0bbb | 47 | _pps = new InterruptIn(irq); |
AjK | 0:db98027c0bbb | 48 | if (type == ppsRise) _pps->rise(this, &GPS::pps_irq); |
AjK | 0:db98027c0bbb | 49 | else _pps->fall(this, &GPS::pps_irq); |
AjK | 0:db98027c0bbb | 50 | _ppsInUse = true; |
AjK | 0:db98027c0bbb | 51 | } |
AjK | 0:db98027c0bbb | 52 | |
AjK | 0:db98027c0bbb | 53 | void |
AjK | 0:db98027c0bbb | 54 | GPS::ppsUnattach(void) |
AjK | 0:db98027c0bbb | 55 | { |
AjK | 0:db98027c0bbb | 56 | if (_pps != NULL) delete(_pps); |
AjK | 0:db98027c0bbb | 57 | _ppsInUse = false; |
AjK | 0:db98027c0bbb | 58 | } |
AjK | 0:db98027c0bbb | 59 | |
AjK | 0:db98027c0bbb | 60 | double |
AjK | 0:db98027c0bbb | 61 | GPS::latitude(void) |
AjK | 0:db98027c0bbb | 62 | { |
AjK | 0:db98027c0bbb | 63 | double a, b; |
AjK | 0:db98027c0bbb | 64 | do { a = thePlace.lat; b = thePlace.lat; } while (a != b); |
AjK | 0:db98027c0bbb | 65 | return a; |
AjK | 0:db98027c0bbb | 66 | } |
AjK | 0:db98027c0bbb | 67 | |
AjK | 0:db98027c0bbb | 68 | double |
AjK | 0:db98027c0bbb | 69 | GPS::longitude(void) |
AjK | 0:db98027c0bbb | 70 | { |
AjK | 0:db98027c0bbb | 71 | double a, b; |
AjK | 0:db98027c0bbb | 72 | do { a = thePlace.lon; b = thePlace.lon; } while (a != b); |
AjK | 0:db98027c0bbb | 73 | return a; |
AjK | 0:db98027c0bbb | 74 | } |
AjK | 0:db98027c0bbb | 75 | |
AjK | 0:db98027c0bbb | 76 | double |
AjK | 0:db98027c0bbb | 77 | GPS::altitude(void) |
AjK | 0:db98027c0bbb | 78 | { |
AjK | 0:db98027c0bbb | 79 | double a, b; |
AjK | 0:db98027c0bbb | 80 | do { a = thePlace.alt; b = thePlace.alt; } while (a != b); |
AjK | 0:db98027c0bbb | 81 | return a; |
AjK | 0:db98027c0bbb | 82 | } |
AjK | 0:db98027c0bbb | 83 | |
AjK | 0:db98027c0bbb | 84 | GPS_Geodetic * |
AjK | 0:db98027c0bbb | 85 | GPS::geodetic(GPS_Geodetic *q) |
AjK | 0:db98027c0bbb | 86 | { |
AjK | 0:db98027c0bbb | 87 | GPS_Geodetic a, b; |
AjK | 0:db98027c0bbb | 88 | |
AjK | 0:db98027c0bbb | 89 | if (q == NULL) q = new GPS_Geodetic; |
AjK | 0:db98027c0bbb | 90 | |
AjK | 0:db98027c0bbb | 91 | do { |
AjK | 0:db98027c0bbb | 92 | memcpy(&a, &thePlace, sizeof(GPS_Geodetic)); |
AjK | 0:db98027c0bbb | 93 | memcpy(&b, &thePlace, sizeof(GPS_Geodetic)); |
AjK | 0:db98027c0bbb | 94 | } |
AjK | 0:db98027c0bbb | 95 | while (memcmp(&a, &b, sizeof(GPS_Geodetic)) != 0); |
AjK | 0:db98027c0bbb | 96 | |
AjK | 0:db98027c0bbb | 97 | return q; |
AjK | 0:db98027c0bbb | 98 | } |
AjK | 0:db98027c0bbb | 99 | |
AjK | 2:8aa059e7d8b1 | 100 | GPS_VTG * |
AjK | 2:8aa059e7d8b1 | 101 | GPS::vtg(GPS_VTG *q) |
AjK | 2:8aa059e7d8b1 | 102 | { |
AjK | 2:8aa059e7d8b1 | 103 | GPS_VTG a, b; |
AjK | 2:8aa059e7d8b1 | 104 | |
AjK | 2:8aa059e7d8b1 | 105 | if (q == NULL) q = new GPS_VTG; |
AjK | 2:8aa059e7d8b1 | 106 | |
AjK | 2:8aa059e7d8b1 | 107 | do { |
AjK | 2:8aa059e7d8b1 | 108 | memcpy(&a, &theVTG, sizeof(GPS_VTG)); |
AjK | 2:8aa059e7d8b1 | 109 | memcpy(&b, &theVTG, sizeof(GPS_VTG)); |
AjK | 2:8aa059e7d8b1 | 110 | } |
AjK | 2:8aa059e7d8b1 | 111 | while (memcmp(&a, &b, sizeof(GPS_VTG)) != 0); |
AjK | 2:8aa059e7d8b1 | 112 | |
AjK | 2:8aa059e7d8b1 | 113 | return q; |
AjK | 2:8aa059e7d8b1 | 114 | } |
AjK | 2:8aa059e7d8b1 | 115 | |
AjK | 0:db98027c0bbb | 116 | void |
AjK | 0:db98027c0bbb | 117 | GPS::ticktock(void) |
AjK | 0:db98027c0bbb | 118 | { |
AjK | 0:db98027c0bbb | 119 | // Increment the time structure by 1/100th of a second. |
AjK | 0:db98027c0bbb | 120 | ++theTime; |
AjK | 0:db98027c0bbb | 121 | |
AjK | 0:db98027c0bbb | 122 | // Test the serial queue. |
AjK | 0:db98027c0bbb | 123 | if (process_required) { |
AjK | 0:db98027c0bbb | 124 | char *s = buffer[active_buffer == 0 ? 1 : 0]; |
AjK | 0:db98027c0bbb | 125 | if (!strncmp(s, "$GPRMC", 6)) { |
AjK | 0:db98027c0bbb | 126 | theTime.nmea_rmc(s); |
AjK | 0:db98027c0bbb | 127 | cb_rmc.call(); |
AjK | 0:db98027c0bbb | 128 | if (!_ppsInUse) theTime.fractionalReset(); |
AjK | 0:db98027c0bbb | 129 | } |
AjK | 0:db98027c0bbb | 130 | else if (!strncmp(s, "$GPGGA", 6)) { |
AjK | 0:db98027c0bbb | 131 | thePlace.nmea_gga(s); |
AjK | 0:db98027c0bbb | 132 | cb_gga.call(); |
AjK | 0:db98027c0bbb | 133 | } |
AjK | 2:8aa059e7d8b1 | 134 | else if (!strncmp(s, "$GPVTG", 6)) { |
AjK | 2:8aa059e7d8b1 | 135 | theVTG.nmea_vtg(s); |
AjK | 2:8aa059e7d8b1 | 136 | cb_vtg.call(); |
AjK | 2:8aa059e7d8b1 | 137 | } |
AjK | 0:db98027c0bbb | 138 | process_required = false; |
AjK | 0:db98027c0bbb | 139 | } |
AjK | 0:db98027c0bbb | 140 | } |
AjK | 0:db98027c0bbb | 141 | |
AjK | 0:db98027c0bbb | 142 | void |
AjK | 0:db98027c0bbb | 143 | GPS::pps_irq(void) |
AjK | 0:db98027c0bbb | 144 | { |
AjK | 0:db98027c0bbb | 145 | theTime.fractionalReset(); |
AjK | 0:db98027c0bbb | 146 | theTime++; // Increment the time/date by one second. |
AjK | 0:db98027c0bbb | 147 | cb_pps.call(); |
AjK | 0:db98027c0bbb | 148 | } |
AjK | 2:8aa059e7d8b1 | 149 | |
AjK | 0:db98027c0bbb | 150 | void |
AjK | 0:db98027c0bbb | 151 | GPS::rx_irq(void) |
AjK | 0:db98027c0bbb | 152 | { |
AjK | 0:db98027c0bbb | 153 | uint32_t iir __attribute__((unused)); |
AjK | 0:db98027c0bbb | 154 | char c; |
AjK | 0:db98027c0bbb | 155 | |
AjK | 0:db98027c0bbb | 156 | if (_base) { |
AjK | 0:db98027c0bbb | 157 | iir = (uint32_t)*((char *)_base + GPS_IIR); |
AjK | 0:db98027c0bbb | 158 | while((int)(*((char *)_base + GPS_LSR) & 0x1)) { |
AjK | 0:db98027c0bbb | 159 | c = (char)(*((char *)_base + GPS_RBR) & 0xFF); |
AjK | 2:8aa059e7d8b1 | 160 | // debugging LPC_UART0->RBR = c; |
AjK | 0:db98027c0bbb | 161 | buffer[active_buffer][rx_buffer_in++] = c; |
AjK | 0:db98027c0bbb | 162 | rx_buffer_in &= (GPS_BUFFER_LEN - 1); |
AjK | 0:db98027c0bbb | 163 | if (c == '\n') { |
AjK | 0:db98027c0bbb | 164 | active_buffer = active_buffer == 0 ? 1 : 0; |
AjK | 0:db98027c0bbb | 165 | process_required = true; |
AjK | 0:db98027c0bbb | 166 | rx_buffer_in = 0; |
AjK | 0:db98027c0bbb | 167 | } |
AjK | 0:db98027c0bbb | 168 | } |
AjK | 0:db98027c0bbb | 169 | } |
AjK | 0:db98027c0bbb | 170 | } |