-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgpsPosition.cxx
195 lines (178 loc) · 4.03 KB
/
gpsPosition.cxx
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
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <syslog.h>
#include <pthread.h>
#include <cstring>
#include <cstdlib>
#include <time.h>
#include "gpsPosition.h"
using namespace std;
GPSPosition::GPSPosition(){
clearAll();
}
GPSPosition::~GPSPosition(){
}
void GPSPosition::clearAll(){
latitude=0;
longitude=0;
altitude=0;
sats=0;
gotfix=false;
}
void GPSPosition::setTime(char *time){
// Check the string looks like a time we can parse
hour=(time[0]-'0') * 10;
hour=(time[1]-'0') * 1;
minute=(time[2]-'0') * 10;
minute=(time[3]-'0') * 1;
second=(time[4]-'0') * 10;
second=(time[5]-'0') * 1;
}
void GPSPosition::setLatitude(float lat){
latitude=lat;
//syslog(LOG_INFO,"GPSPosition: Latitude %f",lat);
}
void GPSPosition::setLatitude(char *lat, char *ind){
float result=0;
// TODO Check position of . to see if lat is valid
if (strlen(ind)==1) {
// lat is in form DDMM.MMMMM
result+=(lat[0]-'0')*10;
result+=(lat[1]-'0');
result+=(strtof(&lat[2],NULL) /60);
// ind will be N or S, multiply be -1 if S
if (strcmp(ind,"S")==0){
result*=-1;
}
setLatitude(result);
//printf("Latitude %s %s -> %f\n",lat,ind,result);
} else {
syslog(LOG_ERR,"GPSPosition: N/S Indicator is too long (%s)",ind);
}
}
void GPSPosition::setLongitude(float lon){
longitude=lon;
//syslog(LOG_INFO,"GPSPosition: Longitude %f",lon);
}
void GPSPosition::setLongitude(char *lon, char *ind){
float result=0;
// TODO Check position of . to see if lon is valid
if (strlen(ind)==1) {
// lon is in form DDDMM.MMMMM
result+=(lon[0]-'0')*100;
result+=(lon[1]-'0')*10;
result+=(lon[2]-'0');
result+=(strtof(&lon[3],NULL) /60);
// ind will be E or W, multiply be -1 if W
if (strcmp(ind,"W")==0){
result*=-1;
}
//printf("Longitude %s %s -> %f\n",lon,ind,result);
setLongitude(result);
} else {
syslog(LOG_ERR,"GPSPosition: E/W Indicator is too long (%s)",ind);
}
}
void GPSPosition::setAltitude(float alt){
altitude=alt;
//syslog(LOG_INFO,"GPSPosition: Altitude %f",alt);
}
void GPSPosition::setAltitude(char *alt, char *unit){
float result=0;
if (strcmp(unit,"M")==0){
result=strtof(alt,NULL);
} else {
syslog(LOG_ERR,"GPSPosition: Altitude Unit not recognised (%s)",unit);
}
setAltitude(result);
}
void GPSPosition::setSats(int s){
sats=s;
}
void GPSPosition::setSats(char* s){
int i=0;
i=atoi(s);
setSats(i);
}
void GPSPosition::setFix(bool f){
gotfix=f;
}
void GPSPosition::setFix(char* f){
if (f[0]=='0'){
setFix(false);
} else if ((f[0]=='1') || (f[0]='2')){
setFix(true);
} else {
syslog(LOG_ERR,"GPS: GPGGA Invalid fix value %s",f);
setFix(false);
}
}
void GPSPosition::setUnixTime(char *date, char *time){
//Date DDMMYY
//Time HHMMSS
struct tm t;
t.tm_sec=(time[4]-'0') * 10;
t.tm_sec+=(time[5]-'0') * 1;
t.tm_min=(time[2]-'0') * 10;
t.tm_min+=(time[3]-'0') * 1;
t.tm_hour=(time[0]-'0') * 10;
t.tm_hour+=(time[1]-'0') * 1;
t.tm_mday=(date[0]-'0') * 10;
t.tm_mday+=(date[1]-'0') * 1;
t.tm_mon=(date[2]-'0') * 10;
t.tm_mon+=(date[3]-'0') * 1;
t.tm_mon-=1;
t.tm_year=100;
t.tm_year+=(date[4]-'0') * 10;
t.tm_year+=(date[5]-'0') * 1;
t.tm_wday=0; // Unused
t.tm_yday=0; // Unused
t.tm_isdst=0; // Time is UTC
unixtime=mktime(&t);
}
void GPSPosition::setSpeed(char *speed){
}
void GPSPosition::setCourse(char *course){
}
float GPSPosition::getLatitude(){
return latitude;
}
float GPSPosition::getLongitude(){
return longitude;
}
float GPSPosition::getAltitude(){
return altitude;
}
int GPSPosition::getSats(){
return sats;
}
bool GPSPosition::getFix(){
return gotfix;
}
time_t GPSPosition::getUnixTime(){
return unixtime;
}
time_t GPSPosition::getTime(){
struct tm time;
time.tm_mday=0;
time.tm_mon=0;
time.tm_year=0;
time.tm_hour=hour;
time.tm_min=minute;
time.tm_sec=second;
time.tm_isdst=0;
return mktime(&time);
// TODO Doesn't return a valid value :S
}
struct tm *GPSPosition::getTime(struct tm *time){
time->tm_mday=0;
time->tm_mon=0;
time->tm_year=0;
time->tm_hour=hour;
time->tm_min=minute;
time->tm_sec=second;
time->tm_isdst=0;
return time;
}