Browse Source

gps: code cleanup

master
Franz Levin 5 years ago
parent
commit
01ec23bb14
3 changed files with 44 additions and 35 deletions
  1. +11
    -8
      projects/bikecomputer/gps/gps.c
  2. +18
    -24
      projects/bikecomputer/gps/nmea_parser.c
  3. +15
    -3
      projects/bikecomputer/gps/nmea_parser.h

+ 11
- 8
projects/bikecomputer/gps/gps.c View File

@ -9,17 +9,20 @@
static void handle_gps_data(const uint8_t* data, uint8_t len)
{
gpsdata_t gpsdata;
nmea_parser_parse((const char*)data, len, &gpsdata);
{
nmeadata_t nmea;
nmea_parser_parse((const char*)data, len, &nmea);
if (nmea.type == nmeatype_fix) {
static int counter = 0;
char buf[14];
snprintf(buf, sizeof(buf), "%d-%d:%d %d %d %d", gpsdata.fix, gpsdata.sats, gpsdata.latitude, gpsdata.longitude, gpsdata.time, counter++);
snprintf(buf, sizeof(buf), "%d-%d:%d %d %d %d",
nmea.data.fix.fix,
nmea.data.fix.satelites,
nmea.data.fix.latitude,
nmea.data.fix.longitude,
nmea.data.fix.time,
counter++);
screen_draw_text(buf,0);
}
}


+ 18
- 24
projects/bikecomputer/gps/nmea_parser.c View File

@ -1,7 +1,6 @@
#include <stdio.h>
#include <string.h>
#include "nmea_parser.h"
static char get_checksum(const char* data, int len)
@ -45,7 +44,7 @@ static const char* get_pos(const char* c, int* pos)
return c;
}
static void get_loc(const char* data, int* time, int* lat, int* lon, int* fix, int* sats)
static void parse_GGA(const char* data, nmeadata_t* nmea)
{
const char* c;
@ -53,24 +52,25 @@ static void get_loc(const char* data, int* time, int* lat, int* lon, int* fix, i
c = strchr(c, ',');
c++;
sscanf(c, "%d", time);
sscanf(c, "%d", &nmea->data.fix.time);
c = strchr(c, ',');
c++;
c = get_pos(c, lat);
c = get_pos(c, lon);
c = get_pos(c, &nmea->data.fix.latitude);
c = get_pos(c, &nmea->data.fix.longitude);
sscanf(c, "%d", fix);
sscanf(c, "%d", &nmea->data.fix.fix);
c = strchr(c, ',');
c++;
sscanf(c, "%d", sats);
sscanf(c, "%d", &nmea->data.fix.satelites);
c = strchr(c, ',');
c++;
nmea->type = nmeatype_fix;
}
void nmea_parser_parse(const char* data, int len, gpsdata_t* gpsdata)
void nmea_parser_parse(const char* data, int len, nmeadata_t* nmea)
{
char checksum;
char* checksum_pos;
@ -78,11 +78,11 @@ void nmea_parser_parse(const char* data, int len, gpsdata_t* gpsdata)
int res;
int checksum_file;
int lon;
int lat;
int time;
int fix;
int sats;
if (nmea == NULL) {
return;
}
nmea->type = nmeatype_unknown;
if (data[0] != '$') {
return;
@ -94,6 +94,7 @@ void nmea_parser_parse(const char* data, int len, gpsdata_t* gpsdata)
if (checksum_pos == NULL) {
return;
}
checksum_pos++;
checksum_line[0] = *checksum_pos;
checksum_pos++;
@ -107,18 +108,11 @@ void nmea_parser_parse(const char* data, int len, gpsdata_t* gpsdata)
return;
}
if (strncmp(&data[1], "GPGGA", 5) != 0) {
if (strncmp(&data[1], "GP", 2) != 0) {
return;
}
get_loc(data, &time, &lat, &lon, &fix, &sats);
gpsdata->latitude = lat;
gpsdata->longitude = lon;
gpsdata->sats = sats;
gpsdata->fix = fix;
gpsdata->time = time;
if (strncmp(&data[3], "GGA", 3) != 0) {
parse_GGA(data, nmea);
}
}

+ 15
- 3
projects/bikecomputer/gps/nmea_parser.h View File

@ -1,15 +1,27 @@
#ifndef NMEA_PARSER_H
#define NMEA_PARSER_H
typedef enum {
nmeatype_unknown,
nmeatype_fix, // GGA
} nmeatype_t;
// GGA
typedef struct {
int latitude;
int longitude;
int fix;
int sats;
int satelites;
int time;
} gpsdata_t;
} nmeadata_fix;
void nmea_parser_parse(const char* data, int len, gpsdata_t* gpsdata);
typedef struct {
nmeatype_t type;
union {
nmeadata_fix fix;
} data;
} nmeadata_t;
void nmea_parser_parse(const char* data, int len, nmeadata_t* nmea);
#endif // NMEA_PARSER_H

Loading…
Cancel
Save