Permalink
Browse files

extract altitude data from GGA message

  • Loading branch information...
1 parent 076fcb9 commit 4163c6f1dc12d5a1926608d7c2fc1b0933e7bb9e @wertarbyte committed Feb 25, 2012
Showing with 59 additions and 3 deletions.
  1. +51 −2 nmea.c
  2. +8 −1 nmea.h
View
53 nmea.c
@@ -10,8 +10,8 @@
* been received completely, we transfer the useful
* data to the output struct
*/
-static struct nmea_rmc_t nmea_rmc = {0};
-static struct nmea_gga_t nmea_gga = {0};
+static struct nmea_rmc_t nmea_rmc;
+static struct nmea_gga_t nmea_gga;
/* this is the data we will be offering */
struct nmea_data_t nmea_data = {0};
@@ -38,6 +38,20 @@ static uint8_t token_nr = 0;
static char token_buffer[TOKEN_BUFFER_SIZE] = "";
+
+static void parse_to_bcd(char *s, uint8_t *b, uint8_t max) {
+ uint8_t i = 0;
+ while (i<max && s[i]) {
+ uint8_t n = (s[i]-'0');
+ if (i%2 == 0) {
+ b[i/2] |= (n << 4);
+ } else {
+ b[i/2] |= (0x0F & n);
+ }
+ i++;
+ }
+}
+
static void parse_coord(struct coord *co) {
/* clear the struct */
memset(co, 0, sizeof(struct coord));
@@ -76,6 +90,35 @@ static void parse_coord(struct coord *co) {
}
}
+static void parse_altitude(struct altitude_t *a) {
+ /* clear the struct */
+ memset(a, 0, sizeof(struct altitude_t));
+ /* find the decimal point */
+ char *p = token_buffer;
+ while (*p && *p != '.') p++;
+ if (*p == '.') {
+ /* we found the point */
+ *p = '\0';
+ p++;
+ }
+ /* parse the integer part */
+ a->m = atoi(token_buffer);
+
+ /* now we BCD encode as many fractions
+ * as we can
+ */
+ uint8_t i = 0;
+ while (i<NMEA_ALTITUDE_FRACTS && p[i]) {
+ uint8_t n = (p[i]-'0');
+ if (i%2 == 0) {
+ a->frac[i/2] |= (n << 4);
+ } else {
+ a->frac[i/2] |= (0x0F & n);
+ }
+ i++;
+ }
+}
+
static void parse_clock(struct clock_t *cl) {
memset(cl, 0, sizeof(struct clock_t));
token_buffer[6] = '\0';
@@ -200,6 +243,10 @@ static void process_gpgga_token(void) {
/* number of used satellites */
nmea_gga.sats = atoi(token_buffer);
break;
+ case 9:
+ /* altitude */
+ parse_altitude(&nmea_gga.alt);
+ break;
default:
/* nothing to do */
break;
@@ -235,6 +282,8 @@ static void sentence_finished(void) {
/* copy quality and number of satellites */
nmea_data.quality = nmea_gga.quality;
nmea_data.sats = nmea_gga.sats;
+ /* copy altitude */
+ memcpy(&nmea_data.alt, &nmea_gga.alt, sizeof(nmea_gga.alt));
break;
default:
break;
View
9 nmea.h
@@ -1,4 +1,5 @@
#define NMEA_MINUTE_FRACTS 4
+#define NMEA_ALTITUDE_FRACTS 4
#define NMEA_RMC_FLAGS_STATUS_OK 0
#define NMEA_RMC_FLAGS_LAT_NORTH 1
@@ -13,6 +14,11 @@ struct coord {
uint8_t frac[(NMEA_MINUTE_FRACTS+1)/2];
};
+struct altitude_t {
+ int16_t m;
+ uint8_t frac[(NMEA_ALTITUDE_FRACTS+1)/2];
+};
+
struct clock_t {
uint8_t hour;
uint8_t minute;
@@ -39,6 +45,7 @@ struct nmea_rmc_t {
};
struct nmea_gga_t {
+ struct altitude_t alt;
uint8_t quality;
uint8_t sats;
};
@@ -49,7 +56,7 @@ struct nmea_data_t {
struct clock_t clock;
struct coord lat;
struct coord lon;
- uint16_t alt;
+ struct altitude_t alt;
uint8_t quality;
uint8_t sats;
};

0 comments on commit 4163c6f

Please sign in to comment.