-
Notifications
You must be signed in to change notification settings - Fork 15
/
gps.cpp
343 lines (298 loc) · 8.22 KB
/
gps.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
#include "gps.h"
char GPSGSM::getBattInf(char *str_perc, char *str_vol)
{
char ret_val=0;
char *p_char;
char *p_char1;
gsm.SimpleWriteln("AT+CBC");
gsm.WaitResp(5000, 100, "OK");
if(gsm.IsStringReceived("+CBC"))
ret_val=1;
//BCL
p_char = strchr((char *)(gsm.comm_buf),',');
p_char1 = p_char+1; //we are on the first char of BCS
p_char = strchr((char *)(p_char1), ',');
if (p_char != NULL) {
*p_char = 0;
}
strcpy(str_perc, (char *)(p_char1));
//Voltage
p_char++;
p_char1 = strchr((char *)(p_char), '\r');
if (p_char1 != NULL) {
*p_char1 = 0;
}
strcpy(str_vol, (char *)(p_char));
return ret_val;
}
char GPSGSM::getBattTVol(char *str_vol)
{
char *p_char;
char *p_char1;
char ret_val=0;
gsm.SimpleWriteln("AT+CBTE?");
gsm.WaitResp(5000, 100, "OK");
if(gsm.IsStringReceived("+CBTE"))
ret_val=1;
//BCL
p_char = strchr((char *)(gsm.comm_buf),':');
p_char1 = p_char+2; //we are on the first char of BCS
p_char = strchr((char *)(p_char1), '\r');
if (p_char != NULL) {
*p_char = 0;
}
strcpy(str_vol, (char *)(p_char1));
return ret_val;
}
#ifdef GPS_OLD
char GPSGSM::attachGPS()
{
if(AT_RESP_ERR_DIF_RESP == gsm.SendATCmdWaitResp("AT+CGPSPWR=1", 500, 100, "OK", 5))
return 0;
if(AT_RESP_ERR_DIF_RESP == gsm.SendATCmdWaitResp("AT+CGPSRST=1", 500, 100, "OK", 5))
return 0;
return 1;
}
#endif
#ifdef GPS_GNSS
char GPSGSM::attachGPS()
{
if(AT_RESP_ERR_DIF_RESP == gsm.SendATCmdWaitResp("AT+CGNSPWR=1", 500, 100, "OK", 5))
return 0;
return 1;
}
#endif
#ifdef GPS_OLD
char GPSGSM::deattachGPS()
{
if(AT_RESP_ERR_DIF_RESP == gsm.SendATCmdWaitResp("AT+CGPSPWR=0", 500, 100, "OK", 5))
return 0;
return 1;
}
#endif
#ifdef GPS_GNSS
char GPSGSM::deattachGPS()
{
if(AT_RESP_ERR_DIF_RESP == gsm.SendATCmdWaitResp("AT+CGNSPWR=0", 500, 100, "OK", 5))
return 0;
return 1;
}
#endif
#ifdef GPS_OLD
char GPSGSM::getStat()
{
char ret_val=-1;
gsm.SimpleWriteln("AT+CGPSSTATUS?");
gsm.WaitResp(5000, 100, "OK");
if(gsm.IsStringReceived("Unknown")||gsm.IsStringReceived("unknown"))
ret_val=0;
else if(gsm.IsStringReceived("Not"))
ret_val=1;
else if(gsm.IsStringReceived("2D")||gsm.IsStringReceived("2d"))
ret_val=2;
else if(gsm.IsStringReceived("3D")||gsm.IsStringReceived("3d"))
ret_val=3;
return ret_val;
}
#endif
#ifdef GPS_GNSS
char GPSGSM::getStat()
{
char ret_val=0;
char *p_char;
char *p_char1;
char gns_status;
int ret;
gsm.SimpleWriteln("AT+CGNSINF");
gsm.WaitResp(5000, 100, "OK");
if(gsm.IsStringReceived("OK"))
ret_val=1;
// UTC time
p_char = strchr((char *)(gsm.comm_buf),',');
p_char1 = p_char+1; //Fix status
p_char = strchr((char *)(p_char1), ',');
if (p_char != NULL) {
*p_char = 0;
}
if(strcmp(p_char1,"0") == 0)
{
ret = 0;
} else if (strcmp(p_char1,"1") == 0)
{
ret = 1;
}
return ret; // 0 or 1
}
#endif
#ifdef GPS_OLD
char GPSGSM::getPar(char *str_long, char *str_lat, char *str_alt, char *str_time, char *str_speed)
{
char ret_val=0;
char *p_char;
char *p_char1;
gsm.SimpleWriteln("AT+CGPSINF=0");
gsm.WaitResp(5000, 100, "OK");
if(gsm.IsStringReceived("OK"))
ret_val=1;
//longitude
p_char = strchr((char *)(gsm.comm_buf),',');
p_char1 = p_char+1; //we are on the first char of longitude
p_char = strchr((char *)(p_char1), ',');
if (p_char != NULL) {
*p_char = 0;
}
strcpy(str_long, (char *)(p_char1));
// latitude
p_char++;
p_char1 = strchr((char *)(p_char), ',');
if (p_char1 != NULL) {
*p_char1 = 0;
}
strcpy(str_lat, (char *)(p_char));
// altitude
p_char1++;
p_char = strchr((char *)(p_char1), ',');
if (p_char != NULL) {
*p_char = 0;
}
strcpy(str_alt, (char *)(p_char1));
// UTC time
p_char++;
p_char1 = strchr((char *)(p_char), ',');
if (p_char1 != NULL) {
*p_char1 = 0;
}
strcpy(str_time, (char *)(p_char));
// TTFF
p_char1++;
p_char = strchr((char *)(p_char1), ',');
if (p_char != NULL) {
*p_char = 0;
}
// num
p_char++;
p_char1 = strchr((char *)(p_char), ',');
if (p_char1 != NULL) {
*p_char1 = 0;
}
// speed
p_char1++;
p_char = strchr((char *)(p_char1), ',');
if (p_char != NULL) {
*p_char = 0;
}
strcpy(str_speed, (char *)(p_char1));
return ret_val;
}
#endif
#ifdef GPS_GNSS
char GPSGSM::getPar(char *str_long, char *str_lat, char *str_alt, char *str_time, char *str_speed)
{
char ret_val=0;
char *p_char;
char *p_char1;
gsm.SimpleWriteln("AT+CGNSINF");
gsm.WaitResp(5000, 100, "OK");
if(gsm.IsStringReceived("OK"))
ret_val=1;
// UTC time
p_char = strchr((char *)(gsm.comm_buf),',');
p_char++;
p_char1 = strchr((char *)(p_char), ',');
p_char1++;
p_char = strchr((char *)(p_char1), ',');
if (p_char != NULL) {
*p_char = 0;
}
strcpy(str_time, (char *)(p_char1));
// latitude
p_char++;
p_char1 = strchr((char *)(p_char), ',');
if (p_char1 != NULL) {
*p_char1 = 0;
}
strcpy(str_lat, (char *)(p_char));
// Longitude
p_char1++;
p_char = strchr((char *)(p_char1), ',');
if (p_char != NULL) {
*p_char = 0;
}
strcpy(str_long, (char *)(p_char1));
//MSL Altitude
p_char++;
p_char1 = strchr((char *)(p_char), ',');
if (p_char1 != NULL) {
*p_char1 = 0;
}
strcpy(str_alt, (char *)(p_char));
//Speed Over Ground
p_char1++;
p_char = strchr((char *)(p_char1), ',');
if (p_char != NULL) {
*p_char = 0;
}
strcpy(str_speed, (char *)(p_char1));
// Course Over Ground
p_char++;
p_char1 = strchr((char *)(p_char), ',');
if (p_char1 != NULL) {
*p_char1 = 0;
}
//Fix Mode
p_char1++;
p_char = strchr((char *)(p_char1), ',');
if (p_char != NULL) {
*p_char = 0;
}
return ret_val;
}
#endif
void parseTime(char *field, int *time)
{
////////////////Time////////////
char tmp[4];
tmp[2]=0; // Init tmp and null terminate
tmp[0] = field[8];
tmp[1] = field[9];
time[0] = atoi(tmp); // Hours
tmp[0] = field[10];
tmp[1] = field[11];
time[1] = atoi(tmp); // Minutes
tmp[0] = field[12];
tmp[1] = field[13];
time[2] = atoi(tmp); // Seconds
/////////////Date///////////////
tmp[0] = field[0];
tmp[1] = field[1];
tmp[2] = field[2];
tmp[3] = field[3];
tmp[4]=0; // Init tmp and null terminate
time[3] = atoi(tmp); // year
tmp[0] = field[4];
tmp[1] = field[5];
tmp[2]=0; // Init tmp and null terminate
time[4] = atoi(tmp); // month
tmp[0] = field[6];
tmp[1] = field[7];
tmp[2]=0; // Init tmp and null terminate
time[5] = atoi(tmp); // day
}
// Read the latitude in decimal format from a GGA string
double convertLat(char* latString)
{
double latitude = atof(latString); // convert to a double (precise)
int deg = (int) latitude / 100; // extract the number of degrees
double min = latitude - (100 * deg); // work out the number of minutes
latitude = deg + (double) min/60.0; // convert to decimal format
return latitude;
}
// Read the longitude in decimal format from a GGA string
double convertLong(char* longString)
{
double longitude = atof(longString); // convert to a double
int deg = (int) longitude / 100; // extract the number of degrees
double min = longitude - (100 * deg); // work out the number of minutes
longitude = deg + (double) min/60.00; // convert to decimal format
return longitude;
}