GPSDevice.h
2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef __GPS_DEVICE_H__
#define __GPS_DEVICE_H__
#include "mbed.h"
#define GPGGA "GPGGA"
class GPSDevice {
public:
const static int length = 80;
const static int baud = 9600;
GPSDevice(Serial *uart) : uart(uart) {}
/*
* Permet de savoir si on lit le début de la trame associée au GPS.
*/
bool canBeRead() {
return uart->readable() && uart->getc() == '$';
}
/*
* Permet de déterminer le type de trame du GPS.
*/
void readType(char type[6]){
uart->scanf("%5s", &type);
}
/*
* Permet de lire la trame du GPS de type GGA.
*/
void readGPGGA() {
char msg[100];
while (uart->getc() != ','); //waits for first comma then copies info
uart->scanf("%s", &msg);
char *tok = strtok(msg, ","); //begins isolating fields of information delimited by a comma
int comma = 0;
while(tok != NULL){
comma++;
switch (comma) {
case 1: UTC = tok; break; //GMT time in seconds, saves pointer to string
case 2: lat = tok; break; //latitude
case 3: latl = tok; break; //lat letter, (N or S)
case 4: lon = tok; break; //longitude
case 5: lonl = tok; break; //lon letter (W or E)
case 6: valid = tok; break; //valid
}
tok = strtok(NULL, ",");
}
}
/*
* Permet de vérifier que la trame lue est valide.
*/
bool isReadValid() {
return atoi(valid) == 1 || atoi(valid) == 2 || atoi(valid) == 3;
}
char* getUTC() {
return UTC;
}
char* getLatitude() {
if(strcmp(latl, "N")){
return lat;
} else {
char *newLat = "";
strcpy(newLat, "-");
strncat(lat, newLat, sizeof(lat)/sizeof(lat[0]));
return newLat;
}
}
char* getLongitude() {
if(strcmp(lonl, "E")){
return lon;
} else {
char *newLon = "";
strcpy(newLon, "-");
strncat(lon, newLon, sizeof(lon)/sizeof(lon[0]));
return newLon;
}
}
private:
Serial *uart;
char *UTC, *lat, *latl, *lon, *lonl, *valid;
};
#endif /* #ifndef __GPS_DEVICE_H__*/