Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / altosui / AltosGPS.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package altosui;
19
20 import java.lang.*;
21 import java.text.*;
22
23 public class AltosGPS {
24         public class AltosGPSSat {
25                 int     svid;
26                 int     c_n0;
27         }
28
29         int     nsat;
30         boolean locked;
31         boolean connected;
32         boolean date_valid;
33         double  lat;            /* degrees (+N -S) */
34         double  lon;            /* degrees (+E -W) */
35         int     alt;            /* m */
36         int     year;
37         int     month;
38         int     day;
39         int     hour;
40         int     minute;
41         int     second;
42
43         int     gps_extended;   /* has extra data */
44         double  ground_speed;   /* m/s */
45         int     course;         /* degrees */
46         double  climb_rate;     /* m/s */
47         double  hdop;           /* unitless? */
48         int     h_error;        /* m */
49         int     v_error;        /* m */
50
51         AltosGPSSat[] cc_gps_sat;       /* tracking data */
52
53         void ParseGPSDate(String date) throws ParseException {
54                 String[] ymd = date.split("-");
55                 if (ymd.length != 3)
56                         throw new ParseException("error parsing GPS date " + date + " got " + ymd.length, 0);
57                 year = AltosParse.parse_int(ymd[0]);
58                 month = AltosParse.parse_int(ymd[1]);
59                 day = AltosParse.parse_int(ymd[2]);
60         }
61
62         void ParseGPSTime(String time) throws ParseException {
63                 String[] hms = time.split(":");
64                 if (hms.length != 3)
65                         throw new ParseException("Error parsing GPS time " + time + " got " + hms.length, 0);
66                 hour = AltosParse.parse_int(hms[0]);
67                 minute = AltosParse.parse_int(hms[1]);
68                 second = AltosParse.parse_int(hms[2]);
69         }
70
71         void ClearGPSTime() {
72                 year = month = day = 0;
73                 hour = minute = second = 0;
74         }
75
76         public AltosGPS(String[] words, int i, int version) throws ParseException {
77                 AltosParse.word(words[i++], "GPS");
78                 nsat = AltosParse.parse_int(words[i++]);
79                 AltosParse.word(words[i++], "sat");
80
81                 connected = false;
82                 locked = false;
83                 lat = lon = 0;
84                 alt = 0;
85                 ClearGPSTime();
86                 if ((words[i]).equals("unlocked")) {
87                         connected = true;
88                         i++;
89                 } else if ((words[i]).equals("not-connected")) {
90                         i++;
91                 } else if (words.length >= 40) {
92                         locked = true;
93                         connected = true;
94
95                         if (version > 1)
96                                 ParseGPSDate(words[i++]);
97                         else
98                                 year = month = day = 0;
99                         ParseGPSTime(words[i++]);
100                         lat = AltosParse.parse_coord(words[i++]);
101                         lon = AltosParse.parse_coord(words[i++]);
102                         alt = AltosParse.parse_int(words[i++]);
103                         if (version > 1 || (i < words.length && !words[i].equals("SAT"))) {
104                                 ground_speed = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "m/s(H)"));
105                                 course = AltosParse.parse_int(words[i++]);
106                                 climb_rate = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "m/s(V)"));
107                                 hdop = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "(hdop)"));
108                                 h_error = AltosParse.parse_int(words[i++]);
109                                 v_error = AltosParse.parse_int(words[i++]);
110                         }
111                 } else {
112                         i++;
113                 }
114                 if (i < words.length) {
115                         AltosParse.word(words[i++], "SAT");
116                         int tracking_channels = 0;
117                         if (words[i].equals("not-connected"))
118                                 tracking_channels = 0;
119                         else
120                                 tracking_channels = AltosParse.parse_int(words[i]);
121                         i++;
122                         cc_gps_sat = new AltosGPS.AltosGPSSat[tracking_channels];
123                         for (int chan = 0; chan < tracking_channels; chan++) {
124                                 cc_gps_sat[chan] = new AltosGPS.AltosGPSSat();
125                                 cc_gps_sat[chan].svid = AltosParse.parse_int(words[i++]);
126                                 /* Older versions included SiRF status bits */
127                                 if (version < 2)
128                                         i++;
129                                 cc_gps_sat[chan].c_n0 = AltosParse.parse_int(words[i++]);
130                         }
131                 } else
132                         cc_gps_sat = new AltosGPS.AltosGPSSat[0];
133         }
134
135         public void set_latitude(int in_lat) {
136                 lat = in_lat / 10.0e7;
137         }
138
139         public void set_longitude(int in_lon) {
140                 lon = in_lon / 10.0e7;
141         }
142
143         public void set_time(int hour, int minute, int second) {
144                 hour = hour;
145                 minute = minute;
146                 second = second;
147         }
148
149         public void set_date(int year, int month, int day) {
150                 year = year;
151                 month = month;
152                 day = day;
153         }
154
155         public void set_flags(int flags) {
156                 flags = flags;
157         }
158
159         public void set_altitude(int altitude) {
160                 altitude = altitude;
161         }
162
163         public void add_sat(int svid, int c_n0) {
164                 if (cc_gps_sat == null) {
165                         cc_gps_sat = new AltosGPS.AltosGPSSat[1];
166                 } else {
167                         AltosGPSSat[] new_gps_sat = new AltosGPS.AltosGPSSat[cc_gps_sat.length + 1];
168                         for (int i = 0; i < cc_gps_sat.length; i++)
169                                 new_gps_sat[i] = cc_gps_sat[i];
170                         cc_gps_sat = new_gps_sat;
171                 }
172                 AltosGPS.AltosGPSSat    sat = new AltosGPS.AltosGPSSat();
173                 sat.svid = svid;
174                 sat.c_n0 = c_n0;
175                 cc_gps_sat[cc_gps_sat.length - 1] = sat;
176         }
177
178         public AltosGPS() {
179                 ClearGPSTime();
180                 cc_gps_sat = null;
181         }
182
183         public AltosGPS(AltosGPS old) {
184                 nsat = old.nsat;
185                 locked = old.locked;
186                 connected = old.connected;
187                 date_valid = old.date_valid;
188                 lat = old.lat;          /* degrees (+N -S) */
189                 lon = old.lon;          /* degrees (+E -W) */
190                 alt = old.alt;          /* m */
191                 year = old.year;
192                 month = old.month;
193                 day = old.day;
194                 hour = old.hour;
195                 minute = old.minute;
196                 second = old.second;
197
198                 gps_extended = old.gps_extended;        /* has extra data */
199                 ground_speed = old.ground_speed;        /* m/s */
200                 course = old.course;            /* degrees */
201                 climb_rate = old.climb_rate;    /* m/s */
202                 hdop = old.hdop;                /* unitless? */
203                 h_error = old.h_error;  /* m */
204                 v_error = old.v_error;  /* m */
205
206                 if (old.cc_gps_sat != null) {
207                         cc_gps_sat = new AltosGPSSat[old.cc_gps_sat.length];
208                         for (int i = 0; i < old.cc_gps_sat.length; i++) {
209                                 cc_gps_sat[i] = new AltosGPSSat();
210                                 cc_gps_sat[i].svid = old.cc_gps_sat[i].svid;
211                                 cc_gps_sat[i].c_n0 = old.cc_gps_sat[i].c_n0;
212                         }
213                 }
214         }
215 }