-
Notifications
You must be signed in to change notification settings - Fork 0
/
Neo6GPS.cpp
146 lines (113 loc) · 3.59 KB
/
Neo6GPS.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
#include "Neo6GPS.h"
////////////////////////////////////////////////////PRIVATE DEFINITION////////////////////////////////////////////////////
String Neo6GPS::_readSerial()
{
_timeout=0;
while(!_serial->available() && _timeout<(TIMEOUT*100))
{
delay(10);
yield();
_timeout++;
}
if(_serial->available())
{
return _serial->readStringUntil('\r');
}
return "";
}
void Neo6GPS::_clearSerial()
{
while(_serial->available())
{
_serial->read();
}
}
void Neo6GPS::_parsGPRMC(struct gpsData *ldata)
{
_serialBuffer = _serialBuffer.substring(_serialBuffer.indexOf("$GPRMC"),_serialBuffer.indexOf('*'));
_commaIndex=_serialBuffer.indexOf(',');
ldata->hour = _serialBuffer.substring(_commaIndex+1,_commaIndex+3).toInt();
_commaIndex=_commaIndex+3;
ldata->min = _serialBuffer.substring(_commaIndex,_commaIndex+2).toInt();
_commaIndex=_commaIndex+2;
ldata->sec = _serialBuffer.substring(_commaIndex,_commaIndex+2).toInt();
_commaIndex=_serialBuffer.indexOf(',',_commaIndex);
if(_serialBuffer.substring(_commaIndex+1,_commaIndex+2) == "A")
{
ldata->status = 1;
}
else
{
ldata->status = 0;
}
_commaIndex=_serialBuffer.indexOf(',',_commaIndex+1);
tempValue = _serialBuffer.substring(_commaIndex+1,_commaIndex+3).toInt();
commaIndex=_commaIndex+3;
_commaIndex=_serialBuffer.indexOf(',',commaIndex);
ldata->latitude = tempValue + (((_serialBuffer.substring(commaIndex,_commaIndex)).toFloat())/60.0);
commaIndex=_commaIndex+1;
_commaIndex=_serialBuffer.indexOf(',',commaIndex);
if(_serialBuffer.substring(commaIndex,_commaIndex)=="S")
{
ldata->latitude = 0-ldata->latitude;
}
commaIndex=_commaIndex+1;
tempValue = _serialBuffer.substring(commaIndex,commaIndex+3).toInt();
commaIndex=commaIndex+3;
_commaIndex=_serialBuffer.indexOf(',',commaIndex);
ldata->longitude = tempValue + (((_serialBuffer.substring(commaIndex,_commaIndex)).toFloat())/60.0);
commaIndex=_commaIndex+1;
_commaIndex=_serialBuffer.indexOf(',',commaIndex);
if(_serialBuffer.substring(commaIndex,_commaIndex)=="W")
{
ldata->longitude = 0-ldata->longitude;
}
commaIndex=_commaIndex+1;
_commaIndex=_serialBuffer.indexOf(',',commaIndex);
#ifdef ALL_DATA
ldata->speedOG = _serialBuffer.substring(commaIndex,_commaIndex).toFloat();
#endif
commaIndex=_commaIndex+1;
_commaIndex=_serialBuffer.indexOf(',',commaIndex);
#ifdef ALL_DATA
ldata->courseOG = _serialBuffer.substring(commaIndex,_commaIndex).toFloat();
#endif
ldata->date = _serialBuffer.substring(_commaIndex+1,_commaIndex+3).toInt();
_commaIndex=_commaIndex+3;
ldata->month = _serialBuffer.substring(_commaIndex,_commaIndex+2).toInt();
_commaIndex=_commaIndex+2;
ldata->year = _serialBuffer.substring(_commaIndex,_commaIndex+2).toInt();
}
/////////////////////////////////////////////////INSTANT/INIT DEFINITION/////////////////////////////////////////////////
Neo6GPS::Neo6GPS(void)
{
_serialBuffer.reserve(255); //reserve memory to prevent fragmention
}
void Neo6GPS::begin(Stream &serial) // begin Definition with Serial port assignment
{
_serial = &serial;
yield();
}
////////////////////////////////////////////////////PUBLIC DEFINITION////////////////////////////////////////////////////
bool Neo6GPS::readData(struct gpsData *ldata)
{
_clearSerial();
delay(500);
for(reTry=0;reTry<11;reTry++)
{
_serialBuffer=_readSerial();
if((_serialBuffer.indexOf("$GPRMC")) !=-1 )
{
_parsGPRMC(ldata);
tempValue = (ldata->hour*60)+ ldata->min + timeZone;
ldata->hour = tempValue/60;
ldata->min = tempValue-(ldata->hour*60);
return true;
}
}
return false;
}
void Neo6GPS::setTimeZone(int8_t hrs,int8_t mins)
{
timeZone=(hrs*60)+mins;
}