Revised Embedded Artists' MTK3339 library to save all raw GPS messages and enable optional decoding of RMC message.

Fork of MTK3339 by EmbeddedArtists AB

Committer:
tbronez
Date:
Sun Aug 23 21:48:48 2015 +0000
Revision:
3:124e25586234
Parent:
2:2391d165df47
Child:
4:c9861b0d3219
Removed LED2 flashing used for debugging. See DEBUG comments.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tbronez 1:3057ad6a5d4b 1 // Class to represent serial interface to MTK3339 GPS chip
tbronez 1:3057ad6a5d4b 2 // Original author: Embedded Artists
tbronez 3:124e25586234 3 // Revised by T. Bronez, 2015-08-23
embeddedartists 0:bd0fe2412980 4
embeddedartists 0:bd0fe2412980 5 #include "mbed.h"
embeddedartists 0:bd0fe2412980 6 #include "MTK3339.h"
tbronez 1:3057ad6a5d4b 7 #define min(a,b) a>b?a:b
embeddedartists 0:bd0fe2412980 8
tbronez 3:124e25586234 9 /*
tbronez 3:124e25586234 10 // For DEBUG, can flash LED2 on arrival of RMC message
tbronez 1:3057ad6a5d4b 11 DigitalOut led2(LED2);
tbronez 1:3057ad6a5d4b 12 Timeout timeout2;
tbronez 1:3057ad6a5d4b 13 void led2Off() {
tbronez 1:3057ad6a5d4b 14 led2 = 1;
tbronez 1:3057ad6a5d4b 15 }
tbronez 1:3057ad6a5d4b 16 void flashLED2() {
tbronez 1:3057ad6a5d4b 17 led2 = 0;
tbronez 1:3057ad6a5d4b 18 timeout2.attach(&led2Off, 0.1);
tbronez 1:3057ad6a5d4b 19 }
tbronez 3:124e25586234 20 */
embeddedartists 0:bd0fe2412980 21
embeddedartists 0:bd0fe2412980 22 MTK3339::MTK3339(PinName tx, PinName rx) : _serial(tx, rx) {
embeddedartists 0:bd0fe2412980 23 _serial.baud(9600);
embeddedartists 0:bd0fe2412980 24 _state = StateStart;
embeddedartists 0:bd0fe2412980 25 _sentenceMask = 0;
tbronez 1:3057ad6a5d4b 26 _availDataType = NMEA_NONE;
tbronez 1:3057ad6a5d4b 27 memset(&rmc, 0, sizeof(RmcType));
tbronez 1:3057ad6a5d4b 28
tbronez 2:2391d165df47 29 memset(&ggaMsg, 0, MSG_BUF_SZ+1);
tbronez 2:2391d165df47 30 memset(&gsaMsg, 0, MSG_BUF_SZ+1);
tbronez 2:2391d165df47 31 memset(&gsvMsg, 0, MSG_BUF_SZ+1);
tbronez 2:2391d165df47 32 memset(&rmcMsg, 0, MSG_BUF_SZ+1);
tbronez 2:2391d165df47 33 memset(&vtgMsg, 0, MSG_BUF_SZ+1);
embeddedartists 0:bd0fe2412980 34 }
embeddedartists 0:bd0fe2412980 35
embeddedartists 0:bd0fe2412980 36 void MTK3339::start(void (*fptr)(void), int mask) {
embeddedartists 0:bd0fe2412980 37 if (fptr && mask) {
embeddedartists 0:bd0fe2412980 38 _dataCallback.attach(fptr);
embeddedartists 0:bd0fe2412980 39 _sentenceMask = mask;
embeddedartists 0:bd0fe2412980 40 _serial.attach(this, &MTK3339::uartIrq, Serial::RxIrq);
tbronez 1:3057ad6a5d4b 41 }
embeddedartists 0:bd0fe2412980 42 }
embeddedartists 0:bd0fe2412980 43
embeddedartists 0:bd0fe2412980 44 void MTK3339::stop() {
embeddedartists 0:bd0fe2412980 45 _dataCallback.attach(NULL);
embeddedartists 0:bd0fe2412980 46 _sentenceMask = 0;
embeddedartists 0:bd0fe2412980 47 _serial.attach(NULL);
embeddedartists 0:bd0fe2412980 48 }
embeddedartists 0:bd0fe2412980 49
embeddedartists 0:bd0fe2412980 50 MTK3339::NmeaSentence MTK3339::getAvailableDataType() {
embeddedartists 0:bd0fe2412980 51 return _availDataType;
embeddedartists 0:bd0fe2412980 52 }
embeddedartists 0:bd0fe2412980 53
tbronez 1:3057ad6a5d4b 54 void MTK3339::decodeRMC() {
tbronez 1:3057ad6a5d4b 55 // See http://aprs.gids.nl/nmea/#rmc for RMC sentence definition
tbronez 1:3057ad6a5d4b 56 const double sixtieth = 1.0/60.0;
tbronez 1:3057ad6a5d4b 57 int n = 0; // scratch int for conversions
tbronez 1:3057ad6a5d4b 58 double d = 0; // scratch double for conversions
tbronez 1:3057ad6a5d4b 59 char* q = NULL; // scratch pointer for conversions
tbronez 1:3057ad6a5d4b 60
tbronez 1:3057ad6a5d4b 61 memset(&rmc, 0, sizeof(RmcType));
tbronez 1:3057ad6a5d4b 62 char* p = rmcMsg;
tbronez 1:3057ad6a5d4b 63
tbronez 1:3057ad6a5d4b 64 /**
tbronez 1:3057ad6a5d4b 65 * Find and decode field 0, UTC time, as hhmmss.sss
tbronez 1:3057ad6a5d4b 66 */
tbronez 1:3057ad6a5d4b 67 p = strchr(p, ','); // First comma
tbronez 1:3057ad6a5d4b 68 if (p == NULL || *p == 0) return; // Unexpected
tbronez 1:3057ad6a5d4b 69 p++; // Start of field
tbronez 1:3057ad6a5d4b 70
tbronez 1:3057ad6a5d4b 71 q = p;
tbronez 1:3057ad6a5d4b 72 // Decode time hours
tbronez 1:3057ad6a5d4b 73 n = ((*q++) - 48)*10; // tens
tbronez 1:3057ad6a5d4b 74 n += ((*q++) - 48); // ones
tbronez 1:3057ad6a5d4b 75 rmc.gps_tm.tm_hour = n;
tbronez 1:3057ad6a5d4b 76 // Decode time minutes
tbronez 1:3057ad6a5d4b 77 n = ((*q++) - 48)*10; // tens
tbronez 1:3057ad6a5d4b 78 n += ((*q++) - 48); // ones
tbronez 1:3057ad6a5d4b 79 rmc.gps_tm.tm_min = n;
tbronez 1:3057ad6a5d4b 80 // Decode time seconds
tbronez 1:3057ad6a5d4b 81 n = ((*q++) - 48)*10; // tens
tbronez 1:3057ad6a5d4b 82 n += ((*q++) - 48); // ones
tbronez 1:3057ad6a5d4b 83 rmc.gps_tm.tm_sec = n;
tbronez 1:3057ad6a5d4b 84 // Decode time milliseconds
tbronez 1:3057ad6a5d4b 85 q++; // decimal point
tbronez 1:3057ad6a5d4b 86 n = ((*q++) - 48)*100; // hundreds of msec = tenths of second
tbronez 1:3057ad6a5d4b 87 n += ((*q++) - 48)*10; // tens of msec = hundredths of second
tbronez 1:3057ad6a5d4b 88 n += ((*q++) - 48); // ones of msec = thousandths of second
tbronez 1:3057ad6a5d4b 89 rmc.msec = n;
tbronez 1:3057ad6a5d4b 90
tbronez 1:3057ad6a5d4b 91 /**
tbronez 1:3057ad6a5d4b 92 * Find and decode field 1, GPS status, as single
tbronez 1:3057ad6a5d4b 93 * character 'A' for valid or 'V' for invalid
tbronez 1:3057ad6a5d4b 94 */
tbronez 1:3057ad6a5d4b 95 p = strchr(p, ','); // Next comma
tbronez 1:3057ad6a5d4b 96 if (p == NULL || *p == 0) return; // Unexpected
tbronez 1:3057ad6a5d4b 97 p++; // Start of field
tbronez 1:3057ad6a5d4b 98
tbronez 1:3057ad6a5d4b 99 if (*p == 'A' || *p == 'V')
tbronez 1:3057ad6a5d4b 100 rmc.status = *p;
tbronez 1:3057ad6a5d4b 101 else
tbronez 1:3057ad6a5d4b 102 rmc.status = ' ';
tbronez 1:3057ad6a5d4b 103
tbronez 1:3057ad6a5d4b 104 /**
tbronez 1:3057ad6a5d4b 105 * Find and decode field 2, latitude, formatted as ddmm.mmmm
tbronez 1:3057ad6a5d4b 106 */
tbronez 1:3057ad6a5d4b 107 p = strchr(p, ','); // Next comma
tbronez 1:3057ad6a5d4b 108 if (p == NULL || *p == 0) return; // Unexpected
tbronez 1:3057ad6a5d4b 109 p++; // Start of field
tbronez 1:3057ad6a5d4b 110
tbronez 1:3057ad6a5d4b 111 q = p;
tbronez 1:3057ad6a5d4b 112 d = ((*q++) - 48)*10; // tens
tbronez 1:3057ad6a5d4b 113 d += ((*q++) - 48); // ones
tbronez 1:3057ad6a5d4b 114 rmc.lat_dd = d; // whole degrees
embeddedartists 0:bd0fe2412980 115
tbronez 1:3057ad6a5d4b 116 d = ((*q++) - 48)*10; // tens
tbronez 1:3057ad6a5d4b 117 d += ((*q++) - 48); // ones
tbronez 1:3057ad6a5d4b 118 q++; // decimal point
tbronez 1:3057ad6a5d4b 119 d += ((*q++) - 48)*0.1000; // tenths
tbronez 1:3057ad6a5d4b 120 d += ((*q++) - 48)*0.0100; // hundredths
tbronez 1:3057ad6a5d4b 121 d += ((*q++) - 48)*0.0010; // thousandths
tbronez 1:3057ad6a5d4b 122 d += ((*q++) - 48)*0.0001; // ten-thousandths
tbronez 1:3057ad6a5d4b 123 if (*q == ',')
tbronez 1:3057ad6a5d4b 124 rmc.lat_dd += sixtieth*d; // decimal degrees
tbronez 1:3057ad6a5d4b 125 else
tbronez 1:3057ad6a5d4b 126 rmc.lat_dd = NAN; // Unexpected
tbronez 1:3057ad6a5d4b 127
tbronez 1:3057ad6a5d4b 128 /**
tbronez 1:3057ad6a5d4b 129 * Find and decode field 3, latitude sense, as single
tbronez 1:3057ad6a5d4b 130 * character 'N' for north or 'S' for south
tbronez 1:3057ad6a5d4b 131 */
tbronez 1:3057ad6a5d4b 132 p = strchr(p, ','); // Next comma
tbronez 1:3057ad6a5d4b 133 if (p == NULL || *p == 0) return; // Unexpected
tbronez 1:3057ad6a5d4b 134 p++; // Start of field
embeddedartists 0:bd0fe2412980 135
tbronez 1:3057ad6a5d4b 136 if (*p == 'S') rmc.lat_dd = -rmc.lat_dd;
tbronez 1:3057ad6a5d4b 137
tbronez 1:3057ad6a5d4b 138 /**
tbronez 1:3057ad6a5d4b 139 * Find and decode field 4, longitude, formatted as dddmm.mmmm
tbronez 1:3057ad6a5d4b 140 */
tbronez 1:3057ad6a5d4b 141 p = strchr(p, ','); // Next comma
tbronez 1:3057ad6a5d4b 142 if (p == NULL || *p == 0) return; // Unexpected
tbronez 1:3057ad6a5d4b 143 p++; // Start of field
tbronez 1:3057ad6a5d4b 144
tbronez 1:3057ad6a5d4b 145 q = p;
tbronez 1:3057ad6a5d4b 146 d = ((*q++) - 48)*100; // hundreds
tbronez 1:3057ad6a5d4b 147 d += ((*q++) - 48)*10; // tens
tbronez 1:3057ad6a5d4b 148 d += ((*q++) - 48); // ones
tbronez 1:3057ad6a5d4b 149 rmc.lon_dd = d; // whole degrees
embeddedartists 0:bd0fe2412980 150
tbronez 1:3057ad6a5d4b 151 d = ((*q++) - 48)*10; // tens
tbronez 1:3057ad6a5d4b 152 d += ((*q++) - 48); // ones
tbronez 1:3057ad6a5d4b 153 q++; // decimal point
tbronez 1:3057ad6a5d4b 154 d += ((*q++) - 48)*0.1000; // tenths
tbronez 1:3057ad6a5d4b 155 d += ((*q++) - 48)*0.0100; // hundredths
tbronez 1:3057ad6a5d4b 156 d += ((*q++) - 48)*0.0010; // thousandths
tbronez 1:3057ad6a5d4b 157 d += ((*q++) - 48)*0.0001; // ten-thousandths
tbronez 1:3057ad6a5d4b 158 if (*q == ',')
tbronez 1:3057ad6a5d4b 159 rmc.lon_dd += sixtieth*d; // decimal degrees
tbronez 1:3057ad6a5d4b 160 else
tbronez 1:3057ad6a5d4b 161 rmc.lon_dd = NAN; // Unexpected
tbronez 1:3057ad6a5d4b 162
tbronez 1:3057ad6a5d4b 163 /**
tbronez 1:3057ad6a5d4b 164 * Find and decode field 5, longitude sense, as single
tbronez 1:3057ad6a5d4b 165 * character 'E' for east or 'W' for west
tbronez 1:3057ad6a5d4b 166 */
tbronez 1:3057ad6a5d4b 167 p = strchr(p, ','); // Next comma
tbronez 1:3057ad6a5d4b 168 if (p == NULL || *p == 0) return; // Unexpected
tbronez 1:3057ad6a5d4b 169 p++; // Start of field
tbronez 1:3057ad6a5d4b 170
tbronez 1:3057ad6a5d4b 171 if (*p == 'W') rmc.lon_dd = -rmc.lon_dd;
embeddedartists 0:bd0fe2412980 172
tbronez 1:3057ad6a5d4b 173 /**
tbronez 1:3057ad6a5d4b 174 * Find and skip field 6, speed in knots
tbronez 1:3057ad6a5d4b 175 */
tbronez 1:3057ad6a5d4b 176 p = strchr(p, ','); // Next comma
tbronez 1:3057ad6a5d4b 177 if (p == NULL || *p == 0) return; // Unexpected
tbronez 1:3057ad6a5d4b 178 p++; // Start of field
tbronez 1:3057ad6a5d4b 179
tbronez 1:3057ad6a5d4b 180 /**
tbronez 1:3057ad6a5d4b 181 * Find and skip field 7, course in degrees
tbronez 1:3057ad6a5d4b 182 */
tbronez 1:3057ad6a5d4b 183 p = strchr(p, ','); // Next comma
tbronez 1:3057ad6a5d4b 184 if (p == NULL || *p == 0) return; // Unexpected
tbronez 1:3057ad6a5d4b 185 p++; // Start of field
tbronez 1:3057ad6a5d4b 186
tbronez 1:3057ad6a5d4b 187 /**
tbronez 1:3057ad6a5d4b 188 * Find and decode field 8, UTC date, formatted as ddmmyy
tbronez 1:3057ad6a5d4b 189 * where 1<=dd<=31, 1<=mm<=12, and yy is offset from 2000
tbronez 1:3057ad6a5d4b 190 */
tbronez 1:3057ad6a5d4b 191 p = strchr(p, ','); // Next comma
tbronez 1:3057ad6a5d4b 192 if (p == NULL || *p == 0) return; // Unexpected
tbronez 1:3057ad6a5d4b 193 p++; // Start of field
tbronez 1:3057ad6a5d4b 194
tbronez 1:3057ad6a5d4b 195 q = p;
tbronez 1:3057ad6a5d4b 196 // Decode date day
tbronez 1:3057ad6a5d4b 197 n = ((*q++) - 48)*10; // tens
tbronez 1:3057ad6a5d4b 198 n += ((*q++) - 48); // ones
tbronez 1:3057ad6a5d4b 199 rmc.gps_tm.tm_mday = n;
tbronez 1:3057ad6a5d4b 200 // Decode date month
tbronez 1:3057ad6a5d4b 201 n = ((*q++) - 48)*10; // tens
tbronez 1:3057ad6a5d4b 202 n += ((*q++) - 48); // ones
tbronez 1:3057ad6a5d4b 203 rmc.gps_tm.tm_mon = n-1; // tm_mon runs 0-11, not 1-12
tbronez 1:3057ad6a5d4b 204 // Decode date year
tbronez 1:3057ad6a5d4b 205 n = ((*q++) - 48)*10; // tens
tbronez 1:3057ad6a5d4b 206 n += ((*q++) - 48); // ones
tbronez 1:3057ad6a5d4b 207 rmc.gps_tm.tm_year = n+100; // tm_year is offset from year 1900, not 2000
tbronez 1:3057ad6a5d4b 208
tbronez 1:3057ad6a5d4b 209 /**
tbronez 1:3057ad6a5d4b 210 * Find and skip field 9, magnetic variation
tbronez 1:3057ad6a5d4b 211 * (not implemented in GPS firmware)
tbronez 1:3057ad6a5d4b 212 */
tbronez 1:3057ad6a5d4b 213 p = strchr(p, ','); // Next comma
tbronez 1:3057ad6a5d4b 214 if (p == NULL || *p == 0) return; // Unexpected
tbronez 1:3057ad6a5d4b 215 p++; // Start of field
tbronez 1:3057ad6a5d4b 216
tbronez 1:3057ad6a5d4b 217 /**
tbronez 1:3057ad6a5d4b 218 * Find and skip field 10, magnetic variation direction
tbronez 1:3057ad6a5d4b 219 * (not implemented in GPS firmware)
tbronez 1:3057ad6a5d4b 220 */
tbronez 1:3057ad6a5d4b 221 p = strchr(p, ','); // Next comma
tbronez 1:3057ad6a5d4b 222 if (p == NULL || *p == 0) return; // Unexpected
tbronez 1:3057ad6a5d4b 223 p++; // Start of field
embeddedartists 0:bd0fe2412980 224 }
embeddedartists 0:bd0fe2412980 225
tbronez 1:3057ad6a5d4b 226 void MTK3339::saveGGA(char* data, int dataLen) {
tbronez 1:3057ad6a5d4b 227 // See http://aprs.gids.nl/nmea/#gga for GGA sentence definition
tbronez 1:3057ad6a5d4b 228 memcpy (ggaMsg, data, min(dataLen, MSG_BUF_SZ));
tbronez 1:3057ad6a5d4b 229 }
tbronez 1:3057ad6a5d4b 230
tbronez 1:3057ad6a5d4b 231 void MTK3339::saveGSA(char* data, int dataLen) {
tbronez 1:3057ad6a5d4b 232 // See http://aprs.gids.nl/nmea/#gsa for GSA sentence definition
tbronez 1:3057ad6a5d4b 233 memcpy (gsaMsg, data, min(dataLen, MSG_BUF_SZ));
embeddedartists 0:bd0fe2412980 234 }
embeddedartists 0:bd0fe2412980 235
tbronez 1:3057ad6a5d4b 236 void MTK3339::saveGSV(char* data, int dataLen) {
tbronez 1:3057ad6a5d4b 237 // See http://aprs.gids.nl/nmea/#gsv for GSV sentence definition
tbronez 1:3057ad6a5d4b 238 memcpy (gsvMsg, data, min(dataLen, MSG_BUF_SZ));
embeddedartists 0:bd0fe2412980 239 }
embeddedartists 0:bd0fe2412980 240
tbronez 1:3057ad6a5d4b 241 void MTK3339::saveRMC(char* data, int dataLen) {
tbronez 1:3057ad6a5d4b 242 // See http://aprs.gids.nl/nmea/#rmc for RMC sentence definition
tbronez 1:3057ad6a5d4b 243 memcpy (rmcMsg, data, min(dataLen, MSG_BUF_SZ));
tbronez 1:3057ad6a5d4b 244 }
embeddedartists 0:bd0fe2412980 245
tbronez 1:3057ad6a5d4b 246 void MTK3339::saveVTG(char* data, int dataLen) {
tbronez 1:3057ad6a5d4b 247 // See http://aprs.gids.nl/nmea/#vtg for VTG sentence definition
tbronez 1:3057ad6a5d4b 248 memcpy (vtgMsg, data, min(dataLen, MSG_BUF_SZ));
tbronez 1:3057ad6a5d4b 249 }
tbronez 1:3057ad6a5d4b 250
tbronez 1:3057ad6a5d4b 251 void MTK3339::saveData(char* data, int len) {
embeddedartists 0:bd0fe2412980 252 do {
tbronez 1:3057ad6a5d4b 253 // Verify checksum
embeddedartists 0:bd0fe2412980 254 if (len < 3 || (len > 3 && data[len-3] != '*')) {
embeddedartists 0:bd0fe2412980 255 // invalid data
embeddedartists 0:bd0fe2412980 256 break;
embeddedartists 0:bd0fe2412980 257 }
embeddedartists 0:bd0fe2412980 258 int sum = strtol(&data[len-2], NULL, 16);
embeddedartists 0:bd0fe2412980 259 for(int i = 1; i < len-3; i++) {
embeddedartists 0:bd0fe2412980 260 sum ^= data[i];
embeddedartists 0:bd0fe2412980 261 }
embeddedartists 0:bd0fe2412980 262 if (sum != 0) {
embeddedartists 0:bd0fe2412980 263 // invalid checksum
embeddedartists 0:bd0fe2412980 264 break;
tbronez 1:3057ad6a5d4b 265 }
tbronez 1:3057ad6a5d4b 266
tbronez 1:3057ad6a5d4b 267 // Save appropriate sentence for later decoding
tbronez 1:3057ad6a5d4b 268 if (strncmp("$GPRMC", data, 6) == 0 && (_sentenceMask & NMEA_RMC) != 0) {
tbronez 3:124e25586234 269 //flashLED2(); // for DEBUG
tbronez 1:3057ad6a5d4b 270 saveRMC(data, len);
tbronez 1:3057ad6a5d4b 271 _availDataType = NMEA_RMC;
embeddedartists 0:bd0fe2412980 272 _dataCallback.call();
tbronez 1:3057ad6a5d4b 273 _availDataType = NMEA_NONE;
tbronez 1:3057ad6a5d4b 274 }
tbronez 1:3057ad6a5d4b 275 else if (strncmp("$GPGGA", data, 6) == 0 && (_sentenceMask & NMEA_GGA) != 0) {
tbronez 1:3057ad6a5d4b 276 saveGGA(data, len);
tbronez 1:3057ad6a5d4b 277 _availDataType = NMEA_GGA;
tbronez 1:3057ad6a5d4b 278 _dataCallback.call();
tbronez 1:3057ad6a5d4b 279 _availDataType = NMEA_NONE;
embeddedartists 0:bd0fe2412980 280 }
tbronez 1:3057ad6a5d4b 281 else if (strncmp("$GPGSA", data, 6) == 0 && (_sentenceMask & NMEA_GSA) != 0) {
tbronez 1:3057ad6a5d4b 282 saveGSA(data, len);
tbronez 1:3057ad6a5d4b 283 _availDataType = NMEA_GSA;
embeddedartists 0:bd0fe2412980 284 _dataCallback.call();
tbronez 1:3057ad6a5d4b 285 _availDataType = NMEA_NONE;
tbronez 1:3057ad6a5d4b 286 }
tbronez 1:3057ad6a5d4b 287 else if (strncmp("$GPGSV", data, 6) == 0 && (_sentenceMask & NMEA_GSV) != 0) {
tbronez 1:3057ad6a5d4b 288 saveGSV(data, len);
tbronez 1:3057ad6a5d4b 289 _availDataType = NMEA_GSV;
tbronez 1:3057ad6a5d4b 290 _dataCallback.call();
tbronez 1:3057ad6a5d4b 291 _availDataType = NMEA_NONE;
tbronez 1:3057ad6a5d4b 292 }
tbronez 1:3057ad6a5d4b 293 else if (strncmp("$GPVTG", data, 6) == 0 && (_sentenceMask & NMEA_VTG) != 0) {
tbronez 1:3057ad6a5d4b 294 saveVTG(data, len);
tbronez 1:3057ad6a5d4b 295 _availDataType = NMEA_VTG;
tbronez 1:3057ad6a5d4b 296 _dataCallback.call();
tbronez 1:3057ad6a5d4b 297 _availDataType = NMEA_NONE;
tbronez 1:3057ad6a5d4b 298 }
embeddedartists 0:bd0fe2412980 299 } while(0);
embeddedartists 0:bd0fe2412980 300 }
embeddedartists 0:bd0fe2412980 301
embeddedartists 0:bd0fe2412980 302 void MTK3339::uartIrq() {
embeddedartists 0:bd0fe2412980 303 char d = 0;
embeddedartists 0:bd0fe2412980 304 while(_serial.readable()) {
embeddedartists 0:bd0fe2412980 305 d = _serial.getc();
embeddedartists 0:bd0fe2412980 306 switch(_state) {
embeddedartists 0:bd0fe2412980 307 case StateStart:
embeddedartists 0:bd0fe2412980 308 if (d == '$') {
embeddedartists 0:bd0fe2412980 309 _buf[0] = '$';
embeddedartists 0:bd0fe2412980 310 _bufPos = 1;
embeddedartists 0:bd0fe2412980 311 _state = StateData;
embeddedartists 0:bd0fe2412980 312 }
embeddedartists 0:bd0fe2412980 313 break;
embeddedartists 0:bd0fe2412980 314 case StateData:
embeddedartists 0:bd0fe2412980 315 if (_bufPos >= MTK3339_BUF_SZ) {
embeddedartists 0:bd0fe2412980 316 // error
embeddedartists 0:bd0fe2412980 317 _state = StateStart;
embeddedartists 0:bd0fe2412980 318 }
embeddedartists 0:bd0fe2412980 319 else if (d == '\r') {
embeddedartists 0:bd0fe2412980 320 _buf[_bufPos] = 0;
tbronez 1:3057ad6a5d4b 321 saveData(_buf, _bufPos);
embeddedartists 0:bd0fe2412980 322 _state = StateStart;
embeddedartists 0:bd0fe2412980 323 }
embeddedartists 0:bd0fe2412980 324 else {
embeddedartists 0:bd0fe2412980 325 _buf[_bufPos++] = d;
embeddedartists 0:bd0fe2412980 326 }
embeddedartists 0:bd0fe2412980 327 break;
embeddedartists 0:bd0fe2412980 328 }
embeddedartists 0:bd0fe2412980 329 }
embeddedartists 0:bd0fe2412980 330 }
tbronez 1:3057ad6a5d4b 331
tbronez 2:2391d165df47 332