added spectrometer timeout

Dependencies:   SDFileSystem mbed

Fork of All_Combined_Real2 by Colin Maxfield

Committer:
jphbergeson
Date:
Wed Apr 13 23:53:47 2016 +0000
Revision:
13:346b43aa06c0
Parent:
0:26713d1db198
Added some logging, plus a variable to keep track of the value of the LED

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jphbergeson 0:26713d1db198 1 #include "nmea.h"
jphbergeson 0:26713d1db198 2
jphbergeson 0:26713d1db198 3 void get_line(Serial *s, char *buffer, int buflen)
jphbergeson 0:26713d1db198 4 {
jphbergeson 0:26713d1db198 5 char *end = buffer + buflen - 1; /* Allow space for null terminator */
jphbergeson 0:26713d1db198 6 char *dst = buffer;
jphbergeson 0:26713d1db198 7 char c = s->getc();
jphbergeson 0:26713d1db198 8 do {
jphbergeson 0:26713d1db198 9 *dst++ = c;
jphbergeson 0:26713d1db198 10 } while ((c = s->getc()) != '\n' && c && dst < end);
jphbergeson 0:26713d1db198 11 *dst = '\0';
jphbergeson 0:26713d1db198 12 // pc.printf("Read %d characters: %s\r\n", (dst - buffer), buffer);
jphbergeson 0:26713d1db198 13 }
jphbergeson 0:26713d1db198 14
jphbergeson 0:26713d1db198 15 bool StartsWith(const char *a, const char *b)
jphbergeson 0:26713d1db198 16 {
jphbergeson 0:26713d1db198 17 if(strncmp(a, b, strlen(b)) == 0) return 1;
jphbergeson 0:26713d1db198 18 return 0;
jphbergeson 0:26713d1db198 19 }
jphbergeson 0:26713d1db198 20
jphbergeson 0:26713d1db198 21 void get_nmea(Serial *s, char *buffer, int buflen)
jphbergeson 0:26713d1db198 22 {
jphbergeson 0:26713d1db198 23 do {
jphbergeson 0:26713d1db198 24 get_line(s, buffer, buflen);
jphbergeson 0:26713d1db198 25 } while (!StartsWith(buffer, "$GPRMC"));
jphbergeson 0:26713d1db198 26 }
jphbergeson 0:26713d1db198 27
jphbergeson 0:26713d1db198 28 struct NMEA_data empty_nmea()
jphbergeson 0:26713d1db198 29 {
jphbergeson 0:26713d1db198 30 struct NMEA_data ret = { };
jphbergeson 0:26713d1db198 31 return ret;
jphbergeson 0:26713d1db198 32 }
jphbergeson 0:26713d1db198 33
jphbergeson 0:26713d1db198 34 struct NMEA_data parse_line(char *str)
jphbergeson 0:26713d1db198 35 {
jphbergeson 0:26713d1db198 36 char line_begin[] = "$GPRMC,";
jphbergeson 0:26713d1db198 37 if (!StartsWith(str, line_begin))
jphbergeson 0:26713d1db198 38 return empty_nmea();
jphbergeson 0:26713d1db198 39 // Else, continue to parse the line
jphbergeson 0:26713d1db198 40 char* str_ptr = line_begin + strlen(line_begin);
jphbergeson 0:26713d1db198 41 struct NMEA_data nmea;
jphbergeson 0:26713d1db198 42
jphbergeson 0:26713d1db198 43 // Now parse the rest of the line
jphbergeson 0:26713d1db198 44 // pc.printf("%s\r\n", str);
jphbergeson 0:26713d1db198 45 sscanf(str, "$GPRMC,%2d%2d%2d.000,%c,%2d%lf,%c,%3d%lf,%c,%lf,%lf,%2d%2d%2d",
jphbergeson 0:26713d1db198 46 &nmea.hours, &nmea.minutes, &nmea.seconds, &nmea.lock_flag,
jphbergeson 0:26713d1db198 47 // latitude
jphbergeson 0:26713d1db198 48 &nmea.latitude, &nmea.latitude_minutes, &nmea.latitude_direction,
jphbergeson 0:26713d1db198 49 // longitude
jphbergeson 0:26713d1db198 50 &nmea.longitude, &nmea.longitude_minutes, &nmea.longitude_direction,
jphbergeson 0:26713d1db198 51 // bearing
jphbergeson 0:26713d1db198 52 &nmea.speed, &nmea.tracking_angle,
jphbergeson 0:26713d1db198 53 // date
jphbergeson 0:26713d1db198 54 &nmea.day, &nmea.month, &nmea.year );
jphbergeson 0:26713d1db198 55
jphbergeson 0:26713d1db198 56 if (nmea.lock_flag != 'A') {
jphbergeson 0:26713d1db198 57 // No lock -- get rid of garbage data (everything except time)
jphbergeson 0:26713d1db198 58 nmea.latitude = 0;
jphbergeson 0:26713d1db198 59 nmea.latitude_minutes = 0.00;
jphbergeson 0:26713d1db198 60 nmea.latitude_direction = 'N';
jphbergeson 0:26713d1db198 61
jphbergeson 0:26713d1db198 62 nmea.longitude = 0;
jphbergeson 0:26713d1db198 63 nmea.longitude_minutes = 0.00;
jphbergeson 0:26713d1db198 64 nmea.longitude_direction = 'E';
jphbergeson 0:26713d1db198 65
jphbergeson 0:26713d1db198 66 nmea.speed = 0.00;
jphbergeson 0:26713d1db198 67 nmea.tracking_angle = 0.00;
jphbergeson 0:26713d1db198 68
jphbergeson 0:26713d1db198 69 nmea.day = 0;
jphbergeson 0:26713d1db198 70 nmea.month = 0;
jphbergeson 0:26713d1db198 71 nmea.year = 0;
jphbergeson 0:26713d1db198 72 }
jphbergeson 0:26713d1db198 73
jphbergeson 0:26713d1db198 74 return nmea;
jphbergeson 0:26713d1db198 75 }