2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
22 import altosui.AltosParse;
25 public class AltosGPS {
26 public class AltosGPSSat {
35 double lat; /* degrees (+N -S) */
36 double lon; /* degrees (+E -W) */
45 int gps_extended; /* has extra data */
46 double ground_speed; /* m/s */
47 int course; /* degrees */
48 double climb_rate; /* m/s */
49 double hdop; /* unitless? */
53 AltosGPSSat[] cc_gps_sat; /* tracking data */
55 void ParseGPSDate(String date) throws ParseException {
56 String[] ymd = date.split("-");
58 throw new ParseException("error parsing GPS date " + date + " got " + ymd.length, 0);
59 year = AltosParse.parse_int(ymd[0]);
60 month = AltosParse.parse_int(ymd[1]);
61 day = AltosParse.parse_int(ymd[2]);
64 void ParseGPSTime(String time) throws ParseException {
65 String[] hms = time.split(":");
67 throw new ParseException("Error parsing GPS time " + time + " got " + hms.length, 0);
68 hour = AltosParse.parse_int(hms[0]);
69 minute = AltosParse.parse_int(hms[1]);
70 second = AltosParse.parse_int(hms[2]);
74 year = month = day = 0;
75 hour = minute = second = 0;
78 public AltosGPS(String[] words, int i, int version) throws ParseException {
79 AltosParse.word(words[i++], "GPS");
80 nsat = AltosParse.parse_int(words[i++]);
81 AltosParse.word(words[i++], "sat");
88 if ((words[i]).equals("unlocked")) {
91 } else if ((words[i]).equals("not-connected")) {
93 } else if (words.length >= 40) {
98 ParseGPSDate(words[i++]);
100 year = month = day = 0;
101 ParseGPSTime(words[i++]);
102 lat = AltosParse.parse_coord(words[i++]);
103 lon = AltosParse.parse_coord(words[i++]);
104 alt = AltosParse.parse_int(words[i++]);
105 if (version > 1 || (i < words.length && !words[i].equals("SAT"))) {
106 ground_speed = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "m/s(H)"));
107 course = AltosParse.parse_int(words[i++]);
108 climb_rate = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "m/s(V)"));
109 hdop = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "(hdop)"));
110 h_error = AltosParse.parse_int(words[i++]);
111 v_error = AltosParse.parse_int(words[i++]);
116 if (i < words.length) {
117 AltosParse.word(words[i++], "SAT");
118 int tracking_channels = 0;
119 if (words[i].equals("not-connected"))
120 tracking_channels = 0;
122 tracking_channels = AltosParse.parse_int(words[i]);
124 cc_gps_sat = new AltosGPS.AltosGPSSat[tracking_channels];
125 for (int chan = 0; chan < tracking_channels; chan++) {
126 cc_gps_sat[chan] = new AltosGPS.AltosGPSSat();
127 cc_gps_sat[chan].svid = AltosParse.parse_int(words[i++]);
128 /* Older versions included SiRF status bits */
131 cc_gps_sat[chan].c_n0 = AltosParse.parse_int(words[i++]);
134 cc_gps_sat = new AltosGPS.AltosGPSSat[0];
137 public void set_latitude(int in_lat) {
138 lat = in_lat / 10.0e7;
141 public void set_longitude(int in_lon) {
142 lon = in_lon / 10.0e7;
145 public void set_time(int hour, int minute, int second) {
151 public void set_date(int year, int month, int day) {
157 public void set_flags(int flags) {
161 public void set_altitude(int altitude) {
165 public void add_sat(int svid, int c_n0) {
166 if (cc_gps_sat == null) {
167 cc_gps_sat = new AltosGPS.AltosGPSSat[1];
169 AltosGPSSat[] new_gps_sat = new AltosGPS.AltosGPSSat[cc_gps_sat.length + 1];
170 for (int i = 0; i < cc_gps_sat.length; i++)
171 new_gps_sat[i] = cc_gps_sat[i];
172 cc_gps_sat = new_gps_sat;
174 AltosGPS.AltosGPSSat sat = new AltosGPS.AltosGPSSat();
177 cc_gps_sat[cc_gps_sat.length - 1] = sat;
185 public AltosGPS(AltosGPS old) {
188 connected = old.connected;
189 date_valid = old.date_valid;
190 lat = old.lat; /* degrees (+N -S) */
191 lon = old.lon; /* degrees (+E -W) */
192 alt = old.alt; /* m */
200 gps_extended = old.gps_extended; /* has extra data */
201 ground_speed = old.ground_speed; /* m/s */
202 course = old.course; /* degrees */
203 climb_rate = old.climb_rate; /* m/s */
204 hdop = old.hdop; /* unitless? */
205 h_error = old.h_error; /* m */
206 v_error = old.v_error; /* m */
208 if (old.cc_gps_sat != null) {
209 cc_gps_sat = new AltosGPSSat[old.cc_gps_sat.length];
210 for (int i = 0; i < old.cc_gps_sat.length; i++) {
211 cc_gps_sat[i] = new AltosGPSSat();
212 cc_gps_sat[i].svid = old.cc_gps_sat[i].svid;
213 cc_gps_sat[i].c_n0 = old.cc_gps_sat[i].c_n0;