-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGPSInterface.cpp
143 lines (116 loc) · 4.05 KB
/
GPSInterface.cpp
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// ###########################################################################
#include <SoftwareSerial.h>
#include "GPSInterface.h"
void addByteToBuffer(int byte);
bool parseDataFromGPS();
GPSInterface gpsInterface;
//SoftwareSerial gpsSerial(GPS_PIN_TX,GPS_PIN_RX);
// ###########################################################################
void initGPSInterface()
{
Serial.begin(4800);
resetGPSInterface();
DEBUG(F("Initializing GPS..."));
}
void resetGPSInterface()
{
// Initialize buffer with empty stuff
gpsInterface.dataFromGPSLength = 0;
for (int i = 0 ; i < GPS_BUFFER_SIZE ; i++)
gpsInterface.dataFromGPS[i] = ' ';
for (int i = 0 ; i < 13 ; i++)
gpsInterface.indices[i] = -1;
}
void addByteToBuffer(int byte)
{
// If buffer is already full, reset the interface
if (gpsInterface.dataFromGPSLength >= GPS_BUFFER_SIZE)
resetGPSInterface();
gpsInterface.dataFromGPS[gpsInterface.dataFromGPSLength] = byte;
gpsInterface.dataFromGPSLength++;
}
bool readGPS()
{
// Read a byte of the serial port
int byteFromGPS = Serial.read();
// If byte is empty, just wait a bit and return
if (byteFromGPS == -1)
{
return false;
}
// Else, we have new stuff to read.
// Put the new byte in the buffer
addByteToBuffer(byteFromGPS);
// If the new byte is not a carriage return, nothing else to do.
if (byteFromGPS != 0x0D) return false;
// Else, this is the end of current transmission
// NB: the actual end of transmission is <CR><LF> (i.e. 0x13 0x10)
else return parseDataFromGPS();
}
bool parseDataFromGPS()
{
// The string we are interested in is $GPRMC, so lets check the data
// we received start with this.
char GPRMC[7] = "$GPRMC";
// The following for loop starts at 1,
// because this code is clowny and the first byte is
// the <LF> (0x10) from the previous transmission.
bool thisIsGPRMC = true;
for (int i = 1 ; i < 7 ; i++)
{
if (gpsInterface.dataFromGPS[i] != GPRMC[i-1]) thisIsGPRMC = false;
}
// If this is not GPRMC, just reset stuff and return
if (!thisIsGPRMC) { resetGPSInterface(); return false; }
// Otherwise, lets look deeper into the string
// We want to identify the different substring delimited by ','
int nextIndex = 0;
for (int i = 0 ; i < GPS_BUFFER_SIZE ; i++)
{
if (gpsInterface.dataFromGPS[i] == ',')
{
// FIXME : possible buffer overflow here !
gpsInterface.indices[nextIndex] = i;
nextIndex++;
}
if (gpsInterface.dataFromGPS[i]=='*')
{ // ... and the "*"
gpsInterface.indices[12] = i;
nextIndex++;
}
}
DEBUG(F("---------------"));
for (int i=0;i<12;i++)
{
if ((i == 3)
|| (i == 5)
|| (i == 6)
|| (i == 7)
|| (i == 9)
|| (i == 10)
|| (i == 11)) continue;
switch(i)
{
case 0 :DEBUG(F("Time in UTC (HhMmSs): ")); break;
case 1 :DEBUG(F("Status (A=OK,V=KO): ")); break;
case 2 :DEBUG(F("Latitude: ")); break;
case 3 :DEBUG(F("Direction (N/S): ")); break;
case 4 :DEBUG(F("Longitude: ")); break;
case 5 :DEBUG(F("Direction (E/W): ")); break;
case 6 :DEBUG(F("Velocity in knots: ")); break;
case 7 :DEBUG(F("Heading in degrees: ")); break;
case 8 :DEBUG(F("Date UTC (DdMmAa): ")); break;
case 9 :DEBUG(F("Magnetic degrees: ")); break;
case 10 :DEBUG(F("(E/W): ")); break;
case 11 :DEBUG(F("Mode: ")); break;
case 12 :DEBUG(F("Checksum: ")); break;
}
for (int j = gpsInterface.indices[i] ;
j < (gpsInterface.indices[i+1]-1) ;
j++
)
DEBUG_NOLN(gpsInterface.dataFromGPS[j+1]);
DEBUG("");
}
DEBUG(F("---------------"));
}