-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGpsneo.cpp
352 lines (303 loc) · 8.29 KB
/
Gpsneo.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "Arduino.h"
#include "Gpsneo.h"
#ifdef ESP8266
#include <pgmspace.h>
#else //ESP8266
#include <avr/pgmspace.h>
#endif //ESP8266
// --------------------SUBSTRING
void substring(char *dest, char *string, int start, int end) {
if (start < 0 || end < 0) {
return;
}
int offset = end - start;
strncpy(dest, ((string) + start), offset);
dest[offset] = '\0'; /* null character manually added */
}
//----------------------------------------------------
// ------------ SEARCH FOR A STRING
int indexOf(char *string, const __FlashStringHelper *search) {
int pos = int(strstr_P(string, (const char *)search) - string);
if (pos == NULL || pos < 0)
return -1;
return pos;
}
int indexOf(char *string, char *search, int offset) {
//Serial.println("IndexOf offset->");
//if (strlen(search) > offset)
// return -1; //if the offset is greater than the string length,return -1
int ind = int(strstr(&string[offset], search));
int pos = int(ind - int(string));
if (pos != NULL && pos > 0)
return pos;
return -1;
}
int indexOf(char *string, char *search) {
int pos = int(strstr(string, search) - string);
if (pos == NULL || pos < 0)
return -1;
return pos;
}
//-----------------------------------
//------------------------Split String
void split(char *string, char *separator, unsigned int position, char *dest) {
int last;
int end = 0;
for (int i = 0; i <= position; ++i) {
last = end;
end = indexOf(string, separator, last + 1);
}
int offset = end - last;
//char dest[offset];
strncpy(dest, ((string) + last + 1), offset - 1);
dest[offset] = '\0';
return;
}
//------------------------------------------
//--------------------------------------------Constructors
Gpsneo::Gpsneo(void)
: HardwareSerial(RX_PIN_DEFAULT, TX_PIN_DEFAULT) {
init(BAUDRATE_DEFAULT);
}
Gpsneo::Gpsneo(uint8_t rx, uint8_t tx)
: HardwareSerial(rx, tx) {
init(BAUDRATE_DEFAULT);
}
Gpsneo::Gpsneo(uint8_t rx, uint8_t tx, long baudrate)
: HardwareSerial(rx, tx) {
init(baudrate);
}
//---------------------------------------------------------------
Gpsneo::~Gpsneo(void) {
Serial.println("destructor");
}
void Gpsneo::init(int baudrate) {
this->begin(baudrate);
/*
for (int i = 0; i < BUFFER_SIZE ; ++i)
{
string[i]='\0';
}
*/
}
bool Gpsneo::checksum(char *string) {
int end = indexOf(string, F("*"));
if (end < 0)
return false;
int checksum = 0x00;
int i;
for (i = 0; i < end; ++i) {
checksum = checksum ^ string[i];
}
char check[4] = "";
i = 0;
end++;
// copy the checksum of the string
for (end; end < strlen(string) - 1; ++end) {
check[i] = string[end];
i++;
if (i > 4) {
return false;
}
}
check[i] = '\0';
//convert integer 'checksum' to String hex text
String cc = String(checksum, HEX);
cc.toUpperCase();
if (cc = check) {
return true;
} else {
return false;
}
}
bool Gpsneo::readSerial(char *buffer) {
int max_lenght = BUFFER_2 - 1; // limit the size for reading.
int length = 0;
long unsigned int timeout;
timeout = millis() + 2000;
while (!this->available() && millis() < timeout) {}
if (millis() >= timeout)
return false; // if not receive a response from the gps, return false
timeout = millis() + 600; // 600ms for read data from serial port.
while (millis() < timeout && length < max_lenght) {
if (this->available()) {
buffer[length] = char(this->read());
length++;
}
}
buffer[length - 1] = '\0';
//buffer[BUFFER_2 -1]='\0';
return true;
}
char *Gpsneo::getDataRaw(const __FlashStringHelper *look, char *buffer) {
/*parameter look can be =
GPBOD - Bearing, origin to destination
GPBWC - Bearing and distance to waypoint, great circle
GPGGA - Global Positioning System Fix Data
GPGLL - Geographic position, latitude / longitude
GPGSA - GPS DOP and active satellites
GPGSV - GPS Satellites in view
GPHDT - Heading, True
GPR00 - List of waypoints in currently active route
GPRMA - Recommended minimum specific Loran-C data
GPRMB - Recommended minimum navigation info
GPRMC - Recommended minimum specific GPS/Transit data
GPRTE - Routes
GPTRF - Transit Fix Data
GPSTN - Multiple Data ID
GPVBW - Dual Ground / Water Speed
GPVTG - Track made good and ground speed
GPWPL - Waypoint location
GPXTE - Cross-track error, Measured
GPZDA - Date & Time
*/
int end = -1;
int start = -1;
bool fail = true;
uint8_t attemps = 0;
bool error;
while (fail == true && attemps < 2) {
if (readSerial(buffer) == false) {
break;
}
attemps++;
start = indexOf(buffer, look);
if (start > -1) {
end = indexOf(buffer, "\n", start + 1);
if (end > 0) {
substring(&buffer[BUFFER_2], &buffer[BUFFER_1], start, end);
return &buffer[BUFFER_2];
} else {
fail = true;
}
} else {
fail = true;
}
}
return NULL;
}
void Gpsneo::getDataGPRMC(char *latitude, char *longitude) {
char x[2];
x[0] = 'z';
x[1] = '\0';
getDataGPRMC(&x[0], &x[0], latitude, &x[0], longitude, &x[0], &x[0], &x[0], &x[0], &x[0], &x[0]);
return;
}
void Gpsneo::getDataGPRMC(char *time, char *status, char *latitude, char *latitudHemisphere, char *longitude, char *longitudeMeridian, char *speedKnots, char *trackAngle, char *date, char *magneticVaration, char *magneticVarationOrientation) {
#ifdef DEBUG
Serial.println(F("getDataGPRMC"));
#endif
char buffer[BUFFER_SIZE];
char *string;
string = &buffer[BUFFER_2];
string = getDataRaw(F("GPRMC"), buffer);
#ifdef DEBUG
Serial.println(string);
#endif
if (string != NULL) {
if (checksum(string)) {
if (time[0] != 'z')
split(string, ",", 1, time);
if (status[0] != 'z')
split(string, ",", 2, status);
if (latitude[0] != 'z')
split(string, ",", 3, latitude);
if (latitudHemisphere[0] != 'z')
split(string, ",", 4, latitudHemisphere);
if (longitude[0] != 'z')
split(string, ",", 5, longitude);
if (longitudeMeridian[0] != 'z')
split(string, ",", 6, longitudeMeridian);
if (speedKnots[0] != 'z')
split(string, ",", 7, speedKnots);
if (trackAngle[0] != 'z')
split(string, ",", 8, trackAngle);
if (date[0] != 'z')
split(string, ",", 9, date);
if (magneticVaration[0] != 'z')
split(string, ",", 10, magneticVaration);
if (magneticVarationOrientation[0] != 'z')
split(string, ",", 11, magneticVarationOrientation);
}
} else {
//string es null, entonces el contenido de los punteros tiene que ser NULL
time[0] = NULL;
status[0] = NULL;
latitude[0] = NULL;
latitudHemisphere[0] = NULL;
longitude[0] = NULL;
longitudeMeridian[0] = NULL;
speedKnots[0] = NULL;
}
#ifdef DEBUG
Serial.println(F("EndgetDataGPRMC"));
#endif
return;
}
float Gpsneo::convertLatitude(char *latitude) {
/*
El formato de la latitud es de ddmm.mmmm
hay que convertirlo en dd.dddd
*/
#ifdef DEBUG
Serial.println(F("convLat"));
#endif
int dot = indexOf(latitude, F(".")); //busco la posicion del punto
if (dot != -1) {
char div[10] = "";
substring(div, latitude, dot - 2, strlen(latitude));
float mm = atof(div);
mm = mm / 60;
substring(div, latitude, dot - 4, 2);
float dd = atof(div);
#ifdef DEBUG
Serial.println(F("EndconvLat"));
#endif
return dd + mm;
}
#ifdef DEBUG
Serial.println(F("EndconvLat"));
#endif
return -1;
}
float Gpsneo::convertLongitude(char *longitude) {
/*
El formato de la latitud es de dddmm.mmmm
hay que convertirlo en dd.dddd
*/
#ifdef DEBUG
Serial.println(F("convLon"));
#endif
int dot = indexOf(longitude, F(".")); //busco la posicion del punto
if (dot != -1) {
char div[10] = "";
substring(div, longitude, dot - 2, strlen(longitude));
float mm = atof(div);
mm = mm / 60;
substring(div, longitude, dot - 5, 3);
float dd = atof(div);
#ifdef DEBUG
Serial.println(F("EndconvLon"));
#endif
return dd + mm;
}
#ifdef DEBUG
Serial.println(F("EndconvLon"));
#endif
return -1;
}
bool Gpsneo::getDataGPGSA() {
//TODO
#ifdef GPGSA
char buffer[BUFFER_SIZE];
char *string;
string = &buffer[BUFFER_2];
string = getDataRaw(F("GPGSA"), buffer);
if (checksum(string)) {
Serial.println(F("checksum OK---> "));
return true;
// Serial.println(string);
}
#endif
return false;
}