-
Notifications
You must be signed in to change notification settings - Fork 9
/
GpsDevice.cpp
249 lines (206 loc) · 5.69 KB
/
GpsDevice.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
// Copyright (c) 2014 David Fannin. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE.txt file.
//
#include <Arduino.h>
#include "GpsDevice.h"
/*
* GpsDevice
* Class for managing GPS device input and output
* provides NMEA message translation to required Yaesu output.
* version 1.0
*
*/
GpsDevice::GpsDevice(AltSoftSerial &gdserial)
{
_gdserial = gdserial;
_init();
}
char GpsDevice::read(){
char c;
if ( isAvailable() || msg.length() >= GD_MAXLEN ) {
_init();
}
if ( _gdserial.available() ) {
c = _gdserial.read();
switch(c) {
case '$':
sot = true ;
ready = false ;
origmsg = String(c);
msg = String("");
break;
case '\n':
case '\r':
if(sot) {
sot = false;
ready = true;
filter();
} else {
_init();
}
break ;
default:
if( sot ) {
origmsg += c ;
}
break ;
}
} else {
c = NULL ;
}
return c;
}
bool GpsDevice::isAvailable(){
return ready;
}
void GpsDevice::_init(){
sot = false ;
ready = false ;
msg = String("");
origmsg = String("");
}
void GpsDevice::begin(int speed){
_gdserial.begin(speed);
}
byte GpsDevice::_atoi(char a) {
if( int(a) >= 65 )
return int(a) - 55;
else
return int(a) - 48;
}
void GpsDevice::write(char c){
_gdserial.print(c);
}
bool GpsDevice::valid(){
if( ! ready) return false ;
int plen = origmsg.length() - 3 ;
pchksum=0;
if ( plen < 1 ) return false ;
psum[0]= origmsg[plen+1];
psum[1]= origmsg[plen+2];
pchksum = ( _atoi(psum[0]) << 4 ) + _atoi(psum[1]) ;
String pstr("");
for(int i=1;i<plen;i++) {
pstr += origmsg[i];
}
chksum = checksum(pstr);
if ( chksum == pchksum)
return true ;
else
return false ;
}
byte GpsDevice::checksum(String str){
char c ;
byte cs = 0 ;
int slen = str.length() ;
for(int i=0;i<slen;i++) {
c = (unsigned char) str[i];
cs ^= c;
}
return cs ;
}
String GpsDevice::schecksum(String str){
String cs = String(checksum(str),HEX);
cs.toUpperCase();
if( cs.length() == 1) cs = "0" + cs ;
return cs ;
}
String GpsDevice::_padzero(String str,int width){
String nstr("");
if( str[0] == '-') {
nstr += '-';
for(int j=0;j< (width-1)-str.length();j++){
nstr += '0' ;
}
nstr += str.substring(1,str.length());
} else {
for(int j=0;j<width-str.length();j++){
nstr += '0' ;
}
nstr += str.substring(0,str.length());
}
return nstr;
}
void GpsDevice::filter(){
int pos;
int pi=-1;
// removes the checksum
String parsemsg = origmsg.substring(0,origmsg.lastIndexOf('*')) ;
// GPGAA messages
if( origmsg.startsWith("$GPGGA,")){
do {
pos = parsemsg.indexOf(',');
pi++;
if(pos != -1) {
switch(pi) {
case 0:
msg = parsemsg.substring(0,pos) + ',' ;
break;
case 8:
msg += _padzero(parsemsg.substring(0,pos),4) + ',';
break;
case 9:
msg += _padzero(parsemsg.substring(0,pos),7) + ',';
break;
case 11:
msg += _padzero(parsemsg.substring(0,pos),6) + ',';
break;
case 13:
msg += "000.0,";
break;
case 14:
msg += "0000" ;
break;
default:
msg += parsemsg.substring(0,pos) + ',' ;
break;
}
parsemsg = parsemsg.substring(pos+1,parsemsg.length());
} else {
// handles case 14
msg += "0000" ;
}
} while(pos >=0) ; // end do while
// add checksum
msg += "*" + schecksum(msg.substring(1,msg.length())) + "\r" ;
return;
} // end GPGGA
// GPRMC messages
if( origmsg.startsWith("$GPRMC,")){
do {
pos = parsemsg.indexOf(',');
pi++;
if(pos != -1) {
switch(pi) {
case 0:
msg = parsemsg.substring(0,pos) + ',' ;
break;
case 7:
msg += _padzero(parsemsg.substring(0,pos),7) + ',';
break;
case 8:
case 9:
msg += _padzero(parsemsg.substring(0,pos),6) + ',';
break;
default:
msg += parsemsg.substring(0,pos) + ',' ;
break;
}
parsemsg = parsemsg.substring(pos+1,parsemsg.length());
} else {
// handles case 14
msg += "," ;
}
} while(pos >=0) ; // end do while
// add checksum
msg += "*" + schecksum(msg.substring(1,msg.length())) + "\r" ;
return;
} // end GPRMC
// GPTXT messages
if( origmsg.startsWith("$GPTXT,")){
msg = "";
return;
} // end GPRMC
msg = origmsg + "\r" ;
}