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.
23 public class AltosGPS {
24 public class AltosGPSSat {
33 double lat; /* degrees (+N -S) */
34 double lon; /* degrees (+E -W) */
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? */
51 AltosGPSSat[] cc_gps_sat; /* tracking data */
53 void ParseGPSDate(String date) throws ParseException {
54 String[] ymd = date.split("-");
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]);
62 void ParseGPSTime(String time) throws ParseException {
63 String[] hms = time.split(":");
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]);
72 year = month = day = 0;
73 hour = minute = second = 0;
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");
86 if ((words[i]).equals("unlocked")) {
89 } else if ((words[i]).equals("not-connected")) {
91 } else if (words.length >= 40) {
96 ParseGPSDate(words[i++]);
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++]);
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;
120 tracking_channels = AltosParse.parse_int(words[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 */
129 cc_gps_sat[chan].c_n0 = AltosParse.parse_int(words[i++]);
132 cc_gps_sat = new AltosGPS.AltosGPSSat[0];
135 public void set_latitude(int in_lat) {
136 lat = in_lat / 10.0e7;
139 public void set_longitude(int in_lon) {
140 lon = in_lon / 10.0e7;
143 public void set_time(int hour, int minute, int second) {
149 public void set_date(int year, int month, int day) {
155 public void set_flags(int flags) {
159 public void set_altitude(int altitude) {
163 public void add_sat(int svid, int c_n0) {
164 if (cc_gps_sat == null) {
165 cc_gps_sat = new AltosGPS.AltosGPSSat[1];
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;
172 AltosGPS.AltosGPSSat sat = new AltosGPS.AltosGPSSat();
175 cc_gps_sat[cc_gps_sat.length - 1] = sat;
183 public AltosGPS(AltosGPS old) {
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 */
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 */
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;