GPS精度実験
Fork of MBed_Adafruit-GPS-Library by
MBed_Adafruit_GPS.cpp@6:7b7e9dc49edd, 2016-09-14 (annotated)
- Committer:
- kityann
- Date:
- Wed Sep 14 11:01:52 2016 +0000
- Revision:
- 6:7b7e9dc49edd
- Parent:
- 5:ce26d53dc358
- Child:
- 7:3f389529228e
forGPS??
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mlee350 | 0:a23e3099bb0a | 1 | /*********************************** |
mlee350 | 0:a23e3099bb0a | 2 | This is our GPS library |
mlee350 | 0:a23e3099bb0a | 3 | |
mlee350 | 0:a23e3099bb0a | 4 | Adafruit invests time and resources providing this open source code, |
mlee350 | 0:a23e3099bb0a | 5 | please support Adafruit and open-source hardware by purchasing |
mlee350 | 0:a23e3099bb0a | 6 | products from Adafruit! |
mlee350 | 0:a23e3099bb0a | 7 | |
mlee350 | 0:a23e3099bb0a | 8 | Written by Limor Fried/Ladyada for Adafruit Industries. |
mlee350 | 0:a23e3099bb0a | 9 | BSD license, check license.txt for more information |
mlee350 | 0:a23e3099bb0a | 10 | All text above must be included in any redistribution |
mlee350 | 0:a23e3099bb0a | 11 | ****************************************/ |
mlee350 | 0:a23e3099bb0a | 12 | |
mlee350 | 0:a23e3099bb0a | 13 | #include "MBed_Adafruit_GPS.h" |
mlee350 | 0:a23e3099bb0a | 14 | |
mlee350 | 0:a23e3099bb0a | 15 | // how long are max NMEA lines to parse? |
kityann | 5:ce26d53dc358 | 16 | #define MAXLINELENGTH 200 |
mlee350 | 0:a23e3099bb0a | 17 | |
mlee350 | 0:a23e3099bb0a | 18 | // we double buffer: read one line in and leave one for the main program |
mlee350 | 0:a23e3099bb0a | 19 | volatile char line1[MAXLINELENGTH]; |
mlee350 | 0:a23e3099bb0a | 20 | volatile char line2[MAXLINELENGTH]; |
mlee350 | 0:a23e3099bb0a | 21 | // our index into filling the current line |
mlee350 | 0:a23e3099bb0a | 22 | volatile uint16_t lineidx=0; |
mlee350 | 0:a23e3099bb0a | 23 | // pointers to the double buffers |
mlee350 | 0:a23e3099bb0a | 24 | volatile char *currentline; |
mlee350 | 0:a23e3099bb0a | 25 | volatile char *lastline; |
mlee350 | 0:a23e3099bb0a | 26 | volatile bool recvdflag; |
mlee350 | 0:a23e3099bb0a | 27 | volatile bool inStandbyMode; |
mlee350 | 0:a23e3099bb0a | 28 | |
mlee350 | 0:a23e3099bb0a | 29 | |
mlee350 | 0:a23e3099bb0a | 30 | bool Adafruit_GPS::parse(char *nmea) { |
mlee350 | 0:a23e3099bb0a | 31 | // do checksum check |
mlee350 | 0:a23e3099bb0a | 32 | |
mlee350 | 0:a23e3099bb0a | 33 | // first look if we even have one |
mlee350 | 0:a23e3099bb0a | 34 | if (nmea[strlen(nmea)-4] == '*') { |
mlee350 | 0:a23e3099bb0a | 35 | uint16_t sum = parseHex(nmea[strlen(nmea)-3]) * 16; |
mlee350 | 0:a23e3099bb0a | 36 | sum += parseHex(nmea[strlen(nmea)-2]); |
mlee350 | 0:a23e3099bb0a | 37 | |
mlee350 | 0:a23e3099bb0a | 38 | // check checksum |
mlee350 | 0:a23e3099bb0a | 39 | for (uint8_t i=1; i < (strlen(nmea)-4); i++) { |
mlee350 | 0:a23e3099bb0a | 40 | sum ^= nmea[i]; |
mlee350 | 0:a23e3099bb0a | 41 | } |
mlee350 | 0:a23e3099bb0a | 42 | if (sum != 0) { |
mlee350 | 0:a23e3099bb0a | 43 | // bad checksum :( |
mlee350 | 0:a23e3099bb0a | 44 | //return false; |
mlee350 | 0:a23e3099bb0a | 45 | } |
mlee350 | 0:a23e3099bb0a | 46 | } |
mlee350 | 0:a23e3099bb0a | 47 | |
mlee350 | 0:a23e3099bb0a | 48 | // look for a few common sentences |
kityann | 5:ce26d53dc358 | 49 | if (strstr(nmea, "$GPGSA")) { |
kityann | 5:ce26d53dc358 | 50 | // found GSA |
kityann | 5:ce26d53dc358 | 51 | char *p = nmea; |
kityann | 5:ce26d53dc358 | 52 | GPGSAdata = nmea; |
kityann | 6:7b7e9dc49edd | 53 | printf("%s",nmea); |
kityann | 5:ce26d53dc358 | 54 | |
kityann | 5:ce26d53dc358 | 55 | return true; |
kityann | 5:ce26d53dc358 | 56 | } |
kityann | 5:ce26d53dc358 | 57 | |
kityann | 5:ce26d53dc358 | 58 | // look for a few common sentences |
mlee350 | 0:a23e3099bb0a | 59 | if (strstr(nmea, "$GPGGA")) { |
mlee350 | 0:a23e3099bb0a | 60 | // found GGA |
mlee350 | 0:a23e3099bb0a | 61 | char *p = nmea; |
kityann | 5:ce26d53dc358 | 62 | GPGGAdata = nmea; |
kityann | 6:7b7e9dc49edd | 63 | printf("%s",nmea); |
mlee350 | 0:a23e3099bb0a | 64 | // get time |
mlee350 | 0:a23e3099bb0a | 65 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 66 | float timef = atof(p); |
mlee350 | 0:a23e3099bb0a | 67 | uint32_t time = timef; |
mlee350 | 0:a23e3099bb0a | 68 | hour = time / 10000; |
mlee350 | 0:a23e3099bb0a | 69 | minute = (time % 10000) / 100; |
mlee350 | 0:a23e3099bb0a | 70 | seconds = (time % 100); |
mlee350 | 0:a23e3099bb0a | 71 | |
mlee350 | 0:a23e3099bb0a | 72 | milliseconds = fmod((double) timef, 1.0) * 1000; |
mlee350 | 0:a23e3099bb0a | 73 | |
mlee350 | 0:a23e3099bb0a | 74 | // parse out latitude |
m5171135 | 2:8203e954d8e1 | 75 | /* |
mlee350 | 0:a23e3099bb0a | 76 | p = strchr(p, ',')+1; |
m5171135 | 1:ff72e93bcb0e | 77 | latitudeH = atol(p); |
m5171135 | 1:ff72e93bcb0e | 78 | p = strchr(p, '.')+1; |
m5171135 | 1:ff72e93bcb0e | 79 | latitudeL = atol(p); |
m5171135 | 2:8203e954d8e1 | 80 | */ |
m5171135 | 2:8203e954d8e1 | 81 | |
m5171135 | 2:8203e954d8e1 | 82 | p = strchr(p, ',')+1; |
m5171135 | 2:8203e954d8e1 | 83 | long sub_latitudeH = atol(p); |
m5171135 | 2:8203e954d8e1 | 84 | p = strchr(p, '.')+1; |
m5171135 | 2:8203e954d8e1 | 85 | long sub_latitudeL = atol(p); |
m5171135 | 2:8203e954d8e1 | 86 | |
m5171135 | 2:8203e954d8e1 | 87 | latitudeH = sub_latitudeH / 100; |
m5171135 | 2:8203e954d8e1 | 88 | latitudeL = ((sub_latitudeH % 100) *10000) + sub_latitudeL; |
m5171135 | 2:8203e954d8e1 | 89 | |
mlee350 | 0:a23e3099bb0a | 90 | |
mlee350 | 0:a23e3099bb0a | 91 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 92 | if (p[0] == 'N') lat = 'N'; |
mlee350 | 0:a23e3099bb0a | 93 | else if (p[0] == 'S') lat = 'S'; |
mlee350 | 0:a23e3099bb0a | 94 | else if (p[0] == ',') lat = 0; |
mlee350 | 0:a23e3099bb0a | 95 | else return false; |
mlee350 | 0:a23e3099bb0a | 96 | |
mlee350 | 0:a23e3099bb0a | 97 | // parse out longitude |
m5171135 | 2:8203e954d8e1 | 98 | /*p = strchr(p, ',')+1; |
m5171135 | 1:ff72e93bcb0e | 99 | longitudeH = atol(p); |
m5171135 | 1:ff72e93bcb0e | 100 | p = strchr(p, '.')+1; |
m5171135 | 1:ff72e93bcb0e | 101 | longitudeL = atol(p); |
m5171135 | 2:8203e954d8e1 | 102 | */ |
m5171135 | 2:8203e954d8e1 | 103 | |
m5171135 | 2:8203e954d8e1 | 104 | p = strchr(p, ',')+1; |
m5171135 | 2:8203e954d8e1 | 105 | long sub_longitudeH = atol(p); |
m5171135 | 2:8203e954d8e1 | 106 | p = strchr(p, '.')+1; |
m5171135 | 2:8203e954d8e1 | 107 | long sub_longitudeL = atol(p); |
m5171135 | 2:8203e954d8e1 | 108 | |
m5171135 | 2:8203e954d8e1 | 109 | longitudeH = sub_longitudeH / 100; |
m5171135 | 2:8203e954d8e1 | 110 | longitudeL = ((sub_longitudeH % 100) *10000) + sub_longitudeL; |
m5171135 | 2:8203e954d8e1 | 111 | |
mlee350 | 0:a23e3099bb0a | 112 | |
mlee350 | 0:a23e3099bb0a | 113 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 114 | if (p[0] == 'W') lon = 'W'; |
mlee350 | 0:a23e3099bb0a | 115 | else if (p[0] == 'E') lon = 'E'; |
mlee350 | 0:a23e3099bb0a | 116 | else if (p[0] == ',') lon = 0; |
mlee350 | 0:a23e3099bb0a | 117 | else return false; |
mlee350 | 0:a23e3099bb0a | 118 | |
mlee350 | 0:a23e3099bb0a | 119 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 120 | fixquality = atoi(p); |
mlee350 | 0:a23e3099bb0a | 121 | |
mlee350 | 0:a23e3099bb0a | 122 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 123 | satellites = atoi(p); |
mlee350 | 0:a23e3099bb0a | 124 | |
mlee350 | 0:a23e3099bb0a | 125 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 126 | HDOP = atof(p); |
mlee350 | 0:a23e3099bb0a | 127 | |
mlee350 | 0:a23e3099bb0a | 128 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 129 | altitude = atof(p); |
mlee350 | 0:a23e3099bb0a | 130 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 131 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 132 | geoidheight = atof(p); |
mlee350 | 0:a23e3099bb0a | 133 | return true; |
mlee350 | 0:a23e3099bb0a | 134 | } |
mlee350 | 0:a23e3099bb0a | 135 | if (strstr(nmea, "$GPRMC")) { |
mlee350 | 0:a23e3099bb0a | 136 | // found RMC |
mlee350 | 0:a23e3099bb0a | 137 | char *p = nmea; |
kityann | 5:ce26d53dc358 | 138 | this->GPRMCdata = nmea; |
kityann | 6:7b7e9dc49edd | 139 | printf("%s",nmea); |
mlee350 | 0:a23e3099bb0a | 140 | // get time |
mlee350 | 0:a23e3099bb0a | 141 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 142 | float timef = atof(p); |
mlee350 | 0:a23e3099bb0a | 143 | uint32_t time = timef; |
mlee350 | 0:a23e3099bb0a | 144 | hour = time / 10000; |
mlee350 | 0:a23e3099bb0a | 145 | minute = (time % 10000) / 100; |
mlee350 | 0:a23e3099bb0a | 146 | seconds = (time % 100); |
mlee350 | 0:a23e3099bb0a | 147 | |
mlee350 | 0:a23e3099bb0a | 148 | milliseconds = fmod((double) timef, 1.0) * 1000; |
mlee350 | 0:a23e3099bb0a | 149 | |
mlee350 | 0:a23e3099bb0a | 150 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 151 | // Serial.println(p); |
mlee350 | 0:a23e3099bb0a | 152 | if (p[0] == 'A') |
mlee350 | 0:a23e3099bb0a | 153 | fix = true; |
mlee350 | 0:a23e3099bb0a | 154 | else if (p[0] == 'V') |
mlee350 | 0:a23e3099bb0a | 155 | fix = false; |
mlee350 | 0:a23e3099bb0a | 156 | else |
mlee350 | 0:a23e3099bb0a | 157 | return false; |
mlee350 | 0:a23e3099bb0a | 158 | |
mlee350 | 0:a23e3099bb0a | 159 | // parse out latitude |
m5171135 | 2:8203e954d8e1 | 160 | /* |
mlee350 | 0:a23e3099bb0a | 161 | p = strchr(p, ',')+1; |
m5171135 | 1:ff72e93bcb0e | 162 | latitudeH = atol(p); |
m5171135 | 1:ff72e93bcb0e | 163 | p = strchr(p, '.')+1; |
m5171135 | 1:ff72e93bcb0e | 164 | latitudeL = atol(p); |
m5171135 | 2:8203e954d8e1 | 165 | */ |
m5171135 | 2:8203e954d8e1 | 166 | |
m5171135 | 2:8203e954d8e1 | 167 | p = strchr(p, ',')+1; |
m5171135 | 2:8203e954d8e1 | 168 | long sub_latitudeH = atol(p); |
m5171135 | 2:8203e954d8e1 | 169 | p = strchr(p, '.')+1; |
m5171135 | 2:8203e954d8e1 | 170 | long sub_latitudeL = atol(p); |
m5171135 | 2:8203e954d8e1 | 171 | |
m5171135 | 2:8203e954d8e1 | 172 | latitudeH = sub_latitudeH / 100; |
m5171135 | 2:8203e954d8e1 | 173 | latitudeL = ((sub_latitudeH % 100) *10000) + sub_latitudeL; |
m5171135 | 2:8203e954d8e1 | 174 | |
mlee350 | 0:a23e3099bb0a | 175 | |
mlee350 | 0:a23e3099bb0a | 176 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 177 | if (p[0] == 'N') lat = 'N'; |
mlee350 | 0:a23e3099bb0a | 178 | else if (p[0] == 'S') lat = 'S'; |
mlee350 | 0:a23e3099bb0a | 179 | else if (p[0] == ',') lat = 0; |
mlee350 | 0:a23e3099bb0a | 180 | else return false; |
mlee350 | 0:a23e3099bb0a | 181 | |
mlee350 | 0:a23e3099bb0a | 182 | // parse out longitude |
m5171135 | 2:8203e954d8e1 | 183 | /* |
mlee350 | 0:a23e3099bb0a | 184 | p = strchr(p, ',')+1; |
m5171135 | 1:ff72e93bcb0e | 185 | longitudeH = atol(p); |
m5171135 | 1:ff72e93bcb0e | 186 | p = strchr(p, '.')+1; |
m5171135 | 1:ff72e93bcb0e | 187 | longitudeL = atol(p); |
m5171135 | 2:8203e954d8e1 | 188 | */ |
m5171135 | 2:8203e954d8e1 | 189 | |
m5171135 | 2:8203e954d8e1 | 190 | p = strchr(p, ',')+1; |
m5171135 | 2:8203e954d8e1 | 191 | long sub_longitudeH = atol(p); |
m5171135 | 2:8203e954d8e1 | 192 | p = strchr(p, '.')+1; |
m5171135 | 2:8203e954d8e1 | 193 | long sub_longitudeL = atol(p); |
m5171135 | 2:8203e954d8e1 | 194 | |
m5171135 | 2:8203e954d8e1 | 195 | longitudeH = sub_longitudeH / 100; |
m5171135 | 2:8203e954d8e1 | 196 | longitudeL = ((sub_longitudeH % 100) *10000) + sub_longitudeL; |
m5171135 | 2:8203e954d8e1 | 197 | |
m5171135 | 2:8203e954d8e1 | 198 | |
m5171135 | 1:ff72e93bcb0e | 199 | |
mlee350 | 0:a23e3099bb0a | 200 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 201 | if (p[0] == 'W') lon = 'W'; |
mlee350 | 0:a23e3099bb0a | 202 | else if (p[0] == 'E') lon = 'E'; |
mlee350 | 0:a23e3099bb0a | 203 | else if (p[0] == ',') lon = 0; |
mlee350 | 0:a23e3099bb0a | 204 | else return false; |
mlee350 | 0:a23e3099bb0a | 205 | |
mlee350 | 0:a23e3099bb0a | 206 | // speed |
mlee350 | 0:a23e3099bb0a | 207 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 208 | speed = atof(p); |
mlee350 | 0:a23e3099bb0a | 209 | |
mlee350 | 0:a23e3099bb0a | 210 | // angle |
mlee350 | 0:a23e3099bb0a | 211 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 212 | angle = atof(p); |
mlee350 | 0:a23e3099bb0a | 213 | |
mlee350 | 0:a23e3099bb0a | 214 | p = strchr(p, ',')+1; |
mlee350 | 0:a23e3099bb0a | 215 | uint32_t fulldate = atof(p); |
mlee350 | 0:a23e3099bb0a | 216 | day = fulldate / 10000; |
mlee350 | 0:a23e3099bb0a | 217 | month = (fulldate % 10000) / 100; |
mlee350 | 0:a23e3099bb0a | 218 | year = (fulldate % 100); |
mlee350 | 0:a23e3099bb0a | 219 | |
mlee350 | 0:a23e3099bb0a | 220 | // we dont parse the remaining, yet! |
mlee350 | 0:a23e3099bb0a | 221 | return true; |
mlee350 | 0:a23e3099bb0a | 222 | } |
mlee350 | 0:a23e3099bb0a | 223 | |
mlee350 | 0:a23e3099bb0a | 224 | return false; |
mlee350 | 0:a23e3099bb0a | 225 | } |
mlee350 | 0:a23e3099bb0a | 226 | |
mlee350 | 0:a23e3099bb0a | 227 | char Adafruit_GPS::read(void) { |
mlee350 | 0:a23e3099bb0a | 228 | char c = 0; |
mlee350 | 0:a23e3099bb0a | 229 | |
mlee350 | 0:a23e3099bb0a | 230 | if (paused) return c; |
mlee350 | 0:a23e3099bb0a | 231 | |
mlee350 | 0:a23e3099bb0a | 232 | if(!gpsSerial->readable()) return c; |
mlee350 | 0:a23e3099bb0a | 233 | c = gpsSerial->getc(); |
mlee350 | 0:a23e3099bb0a | 234 | |
mlee350 | 0:a23e3099bb0a | 235 | //Serial.print(c); |
mlee350 | 0:a23e3099bb0a | 236 | |
mlee350 | 0:a23e3099bb0a | 237 | if (c == '$') { |
mlee350 | 0:a23e3099bb0a | 238 | currentline[lineidx] = 0; |
mlee350 | 0:a23e3099bb0a | 239 | lineidx = 0; |
mlee350 | 0:a23e3099bb0a | 240 | } |
mlee350 | 0:a23e3099bb0a | 241 | if (c == '\n') { |
mlee350 | 0:a23e3099bb0a | 242 | currentline[lineidx] = 0; |
mlee350 | 0:a23e3099bb0a | 243 | |
mlee350 | 0:a23e3099bb0a | 244 | if (currentline == line1) { |
mlee350 | 0:a23e3099bb0a | 245 | currentline = line2; |
mlee350 | 0:a23e3099bb0a | 246 | lastline = line1; |
mlee350 | 0:a23e3099bb0a | 247 | } else { |
mlee350 | 0:a23e3099bb0a | 248 | currentline = line1; |
mlee350 | 0:a23e3099bb0a | 249 | lastline = line2; |
mlee350 | 0:a23e3099bb0a | 250 | } |
mlee350 | 0:a23e3099bb0a | 251 | |
mlee350 | 0:a23e3099bb0a | 252 | lineidx = 0; |
mlee350 | 0:a23e3099bb0a | 253 | recvdflag = true; |
mlee350 | 0:a23e3099bb0a | 254 | } |
mlee350 | 0:a23e3099bb0a | 255 | |
mlee350 | 0:a23e3099bb0a | 256 | currentline[lineidx++] = c; |
mlee350 | 0:a23e3099bb0a | 257 | if (lineidx >= MAXLINELENGTH) |
mlee350 | 0:a23e3099bb0a | 258 | lineidx = MAXLINELENGTH-1; |
mlee350 | 0:a23e3099bb0a | 259 | |
mlee350 | 0:a23e3099bb0a | 260 | return c; |
mlee350 | 0:a23e3099bb0a | 261 | } |
mlee350 | 0:a23e3099bb0a | 262 | |
mlee350 | 0:a23e3099bb0a | 263 | Adafruit_GPS::Adafruit_GPS (Serial *ser) |
mlee350 | 0:a23e3099bb0a | 264 | { |
mlee350 | 0:a23e3099bb0a | 265 | common_init(); // Set everything to common state, then... |
mlee350 | 0:a23e3099bb0a | 266 | gpsSerial = ser; // ...override gpsSwSerial with value passed. |
kityann | 4:7ef0dd12940c | 267 | latitudeH=0;latitudeL=0;longitudeH=0;longitudeL=0; |
kityann | 4:7ef0dd12940c | 268 | latitudeKH=0;latitudeKL=0;longitudeKH=0;longitudeKL=0; |
mlee350 | 0:a23e3099bb0a | 269 | } |
mlee350 | 0:a23e3099bb0a | 270 | |
mlee350 | 0:a23e3099bb0a | 271 | // Initialization code used by all constructor types |
mlee350 | 0:a23e3099bb0a | 272 | void Adafruit_GPS::common_init(void) { |
mlee350 | 0:a23e3099bb0a | 273 | gpsSerial = NULL; |
mlee350 | 0:a23e3099bb0a | 274 | recvdflag = false; |
mlee350 | 0:a23e3099bb0a | 275 | paused = false; |
mlee350 | 0:a23e3099bb0a | 276 | lineidx = 0; |
mlee350 | 0:a23e3099bb0a | 277 | currentline = line1; |
mlee350 | 0:a23e3099bb0a | 278 | lastline = line2; |
mlee350 | 0:a23e3099bb0a | 279 | |
mlee350 | 0:a23e3099bb0a | 280 | hour = minute = seconds = year = month = day = |
mlee350 | 0:a23e3099bb0a | 281 | fixquality = satellites = 0; // uint8_t |
mlee350 | 0:a23e3099bb0a | 282 | lat = lon = mag = 0; // char |
mlee350 | 0:a23e3099bb0a | 283 | fix = false; // bool |
mlee350 | 0:a23e3099bb0a | 284 | milliseconds = 0; // uint16_t |
m5171135 | 1:ff72e93bcb0e | 285 | geoidheight = altitude = speed = angle = magvariation = HDOP = 0.0; // float |
m5171135 | 1:ff72e93bcb0e | 286 | latitudeH = latitudeL = longitudeH = longitudeL = 0; |
m5171135 | 1:ff72e93bcb0e | 287 | |
mlee350 | 0:a23e3099bb0a | 288 | } |
mlee350 | 0:a23e3099bb0a | 289 | |
mlee350 | 0:a23e3099bb0a | 290 | void Adafruit_GPS::begin(int baud) |
mlee350 | 0:a23e3099bb0a | 291 | { |
mlee350 | 0:a23e3099bb0a | 292 | gpsSerial->baud(baud); |
mlee350 | 0:a23e3099bb0a | 293 | wait_ms(10); |
mlee350 | 0:a23e3099bb0a | 294 | } |
mlee350 | 0:a23e3099bb0a | 295 | |
mlee350 | 0:a23e3099bb0a | 296 | void Adafruit_GPS::sendCommand(char *str) { |
mlee350 | 0:a23e3099bb0a | 297 | gpsSerial->printf("%s",str); |
mlee350 | 0:a23e3099bb0a | 298 | } |
mlee350 | 0:a23e3099bb0a | 299 | |
mlee350 | 0:a23e3099bb0a | 300 | bool Adafruit_GPS::newNMEAreceived(void) { |
mlee350 | 0:a23e3099bb0a | 301 | return recvdflag; |
mlee350 | 0:a23e3099bb0a | 302 | } |
mlee350 | 0:a23e3099bb0a | 303 | |
mlee350 | 0:a23e3099bb0a | 304 | void Adafruit_GPS::pause(bool p) { |
mlee350 | 0:a23e3099bb0a | 305 | paused = p; |
mlee350 | 0:a23e3099bb0a | 306 | } |
mlee350 | 0:a23e3099bb0a | 307 | |
mlee350 | 0:a23e3099bb0a | 308 | char *Adafruit_GPS::lastNMEA(void) { |
mlee350 | 0:a23e3099bb0a | 309 | recvdflag = false; |
mlee350 | 0:a23e3099bb0a | 310 | return (char *)lastline; |
mlee350 | 0:a23e3099bb0a | 311 | } |
mlee350 | 0:a23e3099bb0a | 312 | |
mlee350 | 0:a23e3099bb0a | 313 | // read a Hex value and return the decimal equivalent |
mlee350 | 0:a23e3099bb0a | 314 | uint8_t Adafruit_GPS::parseHex(char c) { |
mlee350 | 0:a23e3099bb0a | 315 | if (c < '0') |
mlee350 | 0:a23e3099bb0a | 316 | return 0; |
mlee350 | 0:a23e3099bb0a | 317 | if (c <= '9') |
mlee350 | 0:a23e3099bb0a | 318 | return c - '0'; |
mlee350 | 0:a23e3099bb0a | 319 | if (c < 'A') |
mlee350 | 0:a23e3099bb0a | 320 | return 0; |
mlee350 | 0:a23e3099bb0a | 321 | if (c <= 'F') |
mlee350 | 0:a23e3099bb0a | 322 | return (c - 'A')+10; |
mlee350 | 0:a23e3099bb0a | 323 | } |
mlee350 | 0:a23e3099bb0a | 324 | |
mlee350 | 0:a23e3099bb0a | 325 | bool Adafruit_GPS::waitForSentence(char *wait4me, uint8_t max) { |
mlee350 | 0:a23e3099bb0a | 326 | char str[20]; |
mlee350 | 0:a23e3099bb0a | 327 | |
mlee350 | 0:a23e3099bb0a | 328 | uint8_t i=0; |
mlee350 | 0:a23e3099bb0a | 329 | while (i < max) { |
mlee350 | 0:a23e3099bb0a | 330 | if (newNMEAreceived()) { |
mlee350 | 0:a23e3099bb0a | 331 | char *nmea = lastNMEA(); |
mlee350 | 0:a23e3099bb0a | 332 | strncpy(str, nmea, 20); |
mlee350 | 0:a23e3099bb0a | 333 | str[19] = 0; |
mlee350 | 0:a23e3099bb0a | 334 | i++; |
mlee350 | 0:a23e3099bb0a | 335 | |
mlee350 | 0:a23e3099bb0a | 336 | if (strstr(str, wait4me)) |
mlee350 | 0:a23e3099bb0a | 337 | return true; |
mlee350 | 0:a23e3099bb0a | 338 | } |
mlee350 | 0:a23e3099bb0a | 339 | } |
mlee350 | 0:a23e3099bb0a | 340 | |
mlee350 | 0:a23e3099bb0a | 341 | return false; |
mlee350 | 0:a23e3099bb0a | 342 | } |
mlee350 | 0:a23e3099bb0a | 343 | |
mlee350 | 0:a23e3099bb0a | 344 | bool Adafruit_GPS::LOCUS_StartLogger(void) { |
mlee350 | 0:a23e3099bb0a | 345 | sendCommand(PMTK_LOCUS_STARTLOG); |
mlee350 | 0:a23e3099bb0a | 346 | recvdflag = false; |
mlee350 | 0:a23e3099bb0a | 347 | return waitForSentence(PMTK_LOCUS_LOGSTARTED); |
mlee350 | 0:a23e3099bb0a | 348 | } |
mlee350 | 0:a23e3099bb0a | 349 | |
mlee350 | 0:a23e3099bb0a | 350 | bool Adafruit_GPS::LOCUS_ReadStatus(void) { |
mlee350 | 0:a23e3099bb0a | 351 | sendCommand(PMTK_LOCUS_QUERY_STATUS); |
mlee350 | 0:a23e3099bb0a | 352 | |
mlee350 | 0:a23e3099bb0a | 353 | if (! waitForSentence("$PMTKLOG")) |
mlee350 | 0:a23e3099bb0a | 354 | return false; |
mlee350 | 0:a23e3099bb0a | 355 | |
mlee350 | 0:a23e3099bb0a | 356 | char *response = lastNMEA(); |
mlee350 | 0:a23e3099bb0a | 357 | uint16_t parsed[10]; |
mlee350 | 0:a23e3099bb0a | 358 | uint8_t i; |
mlee350 | 0:a23e3099bb0a | 359 | |
mlee350 | 0:a23e3099bb0a | 360 | for (i=0; i<10; i++) parsed[i] = -1; |
mlee350 | 0:a23e3099bb0a | 361 | |
mlee350 | 0:a23e3099bb0a | 362 | response = strchr(response, ','); |
mlee350 | 0:a23e3099bb0a | 363 | for (i=0; i<10; i++) { |
mlee350 | 0:a23e3099bb0a | 364 | if (!response || (response[0] == 0) || (response[0] == '*')) |
mlee350 | 0:a23e3099bb0a | 365 | break; |
mlee350 | 0:a23e3099bb0a | 366 | response++; |
mlee350 | 0:a23e3099bb0a | 367 | parsed[i]=0; |
mlee350 | 0:a23e3099bb0a | 368 | while ((response[0] != ',') && |
mlee350 | 0:a23e3099bb0a | 369 | (response[0] != '*') && (response[0] != 0)) { |
mlee350 | 0:a23e3099bb0a | 370 | parsed[i] *= 10; |
mlee350 | 0:a23e3099bb0a | 371 | char c = response[0]; |
mlee350 | 0:a23e3099bb0a | 372 | if (isdigit(c)) |
mlee350 | 0:a23e3099bb0a | 373 | parsed[i] += c - '0'; |
mlee350 | 0:a23e3099bb0a | 374 | else |
mlee350 | 0:a23e3099bb0a | 375 | parsed[i] = c; |
mlee350 | 0:a23e3099bb0a | 376 | response++; |
mlee350 | 0:a23e3099bb0a | 377 | } |
mlee350 | 0:a23e3099bb0a | 378 | } |
mlee350 | 0:a23e3099bb0a | 379 | LOCUS_serial = parsed[0]; |
mlee350 | 0:a23e3099bb0a | 380 | LOCUS_type = parsed[1]; |
mlee350 | 0:a23e3099bb0a | 381 | if (isalpha(parsed[2])) { |
mlee350 | 0:a23e3099bb0a | 382 | parsed[2] = parsed[2] - 'a' + 10; |
mlee350 | 0:a23e3099bb0a | 383 | } |
mlee350 | 0:a23e3099bb0a | 384 | LOCUS_mode = parsed[2]; |
mlee350 | 0:a23e3099bb0a | 385 | LOCUS_config = parsed[3]; |
mlee350 | 0:a23e3099bb0a | 386 | LOCUS_interval = parsed[4]; |
mlee350 | 0:a23e3099bb0a | 387 | LOCUS_distance = parsed[5]; |
mlee350 | 0:a23e3099bb0a | 388 | LOCUS_speed = parsed[6]; |
mlee350 | 0:a23e3099bb0a | 389 | LOCUS_status = !parsed[7]; |
mlee350 | 0:a23e3099bb0a | 390 | LOCUS_records = parsed[8]; |
mlee350 | 0:a23e3099bb0a | 391 | LOCUS_percent = parsed[9]; |
mlee350 | 0:a23e3099bb0a | 392 | |
mlee350 | 0:a23e3099bb0a | 393 | return true; |
mlee350 | 0:a23e3099bb0a | 394 | } |
mlee350 | 0:a23e3099bb0a | 395 | |
mlee350 | 0:a23e3099bb0a | 396 | // Standby Mode Switches |
mlee350 | 0:a23e3099bb0a | 397 | bool Adafruit_GPS::standby(void) { |
mlee350 | 0:a23e3099bb0a | 398 | if (inStandbyMode) { |
mlee350 | 0:a23e3099bb0a | 399 | return false; // Returns false if already in standby mode, so that you do not wake it up by sending commands to GPS |
mlee350 | 0:a23e3099bb0a | 400 | } |
mlee350 | 0:a23e3099bb0a | 401 | else { |
mlee350 | 0:a23e3099bb0a | 402 | inStandbyMode = true; |
mlee350 | 0:a23e3099bb0a | 403 | sendCommand(PMTK_STANDBY); |
mlee350 | 0:a23e3099bb0a | 404 | //return waitForSentence(PMTK_STANDBY_SUCCESS); // don't seem to be fast enough to catch the message, or something else just is not working |
mlee350 | 0:a23e3099bb0a | 405 | return true; |
mlee350 | 0:a23e3099bb0a | 406 | } |
mlee350 | 0:a23e3099bb0a | 407 | } |
mlee350 | 0:a23e3099bb0a | 408 | |
mlee350 | 0:a23e3099bb0a | 409 | bool Adafruit_GPS::wakeup(void) { |
mlee350 | 0:a23e3099bb0a | 410 | if (inStandbyMode) { |
mlee350 | 0:a23e3099bb0a | 411 | inStandbyMode = false; |
mlee350 | 0:a23e3099bb0a | 412 | sendCommand(""); // send byte to wake it up |
mlee350 | 0:a23e3099bb0a | 413 | return waitForSentence(PMTK_AWAKE); |
mlee350 | 0:a23e3099bb0a | 414 | } |
mlee350 | 0:a23e3099bb0a | 415 | else { |
mlee350 | 0:a23e3099bb0a | 416 | return false; // Returns false if not in standby mode, nothing to wakeup |
mlee350 | 0:a23e3099bb0a | 417 | } |
mlee350 | 0:a23e3099bb0a | 418 | } |