Switch AltosUI to libaltos for device access
[fw/altos] / ao-tools / 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 import altosui.AltosParse;
23
24
25 public class AltosGPS {
26         public class AltosGPSTime {
27                 int year;
28                 int month;
29                 int day;
30                 int hour;
31                 int minute;
32                 int second;
33
34                 public AltosGPSTime(String date, String time) throws ParseException {
35                         String[] ymd = date.split("-");
36                         if (ymd.length != 3)
37                                 throw new ParseException("error parsing GPS date " + date + " got " + ymd.length, 0);
38                         year = AltosParse.parse_int(ymd[0]);
39                         month = AltosParse.parse_int(ymd[1]);
40                         day = AltosParse.parse_int(ymd[2]);
41
42                         String[] hms = time.split(":");
43                         if (hms.length != 3)
44                                 throw new ParseException("Error parsing GPS time " + time + " got " + hms.length, 0);
45                         hour = AltosParse.parse_int(hms[0]);
46                         minute = AltosParse.parse_int(hms[1]);
47                         second = AltosParse.parse_int(hms[2]);
48                 }
49
50                 public AltosGPSTime() {
51                         year = month = day = 0;
52                         hour = minute = second = 0;
53                 }
54
55         }
56
57         public class AltosGPSSat {
58                 int     svid;
59                 int     c_n0;
60         }
61
62         int     nsat;
63         boolean gps_locked;
64         boolean gps_connected;
65         AltosGPSTime gps_time;
66         double  lat;            /* degrees (+N -S) */
67         double  lon;            /* degrees (+E -W) */
68         int     alt;            /* m */
69
70         int     gps_extended;   /* has extra data */
71         double  ground_speed;   /* m/s */
72         int     course;         /* degrees */
73         double  climb_rate;     /* m/s */
74         double  hdop;           /* unitless? */
75         int     h_error;        /* m */
76         int     v_error;        /* m */
77
78         AltosGPSSat[] cc_gps_sat;       /* tracking data */
79
80         public AltosGPS(String[] words, int i) throws ParseException {
81                 AltosParse.word(words[i++], "GPS");
82                 nsat = AltosParse.parse_int(words[i++]);
83                 AltosParse.word(words[i++], "sat");
84
85                 gps_connected = false;
86                 gps_locked = false;
87                 lat = lon = 0;
88                 alt = 0;
89                 if ((words[i]).equals("unlocked")) {
90                         gps_connected = true;
91                         gps_time = new AltosGPSTime();
92                         i++;
93                 } else if ((words[i]).equals("not-connected")) {
94                         gps_time = new AltosGPSTime();
95                         i++;
96                 } else if (words.length >= 40) {
97                         gps_locked = true;
98                         gps_connected = true;
99
100                         gps_time = new AltosGPSTime(words[i], words[i+1]); i += 2;
101                         lat = AltosParse.parse_coord(words[i++]);
102                         lon = AltosParse.parse_coord(words[i++]);
103                         alt = AltosParse.parse_int(AltosParse.strip_suffix(words[i++], "m"));
104                         ground_speed = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "m/s(H)"));
105                         course = AltosParse.parse_int(AltosParse.strip_suffix(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(AltosParse.strip_suffix(words[i++], "(herr)"));
109                         v_error = AltosParse.parse_int(AltosParse.strip_suffix(words[i++], "(verr)"));
110                 } else {
111                         gps_time = new AltosGPSTime();
112                         i++;
113                 }
114                 AltosParse.word(words[i++], "SAT");
115                 int tracking_channels = 0;
116                 if (words[i].equals("not-connected"))
117                         tracking_channels = 0;
118                 else
119                         tracking_channels = AltosParse.parse_int(words[i]);
120                 i++;
121                 cc_gps_sat = new AltosGPS.AltosGPSSat[tracking_channels];
122                 for (int chan = 0; chan < tracking_channels; chan++) {
123                         cc_gps_sat[chan] = new AltosGPS.AltosGPSSat();
124                         cc_gps_sat[chan].svid = AltosParse.parse_int(words[i++]);
125                         cc_gps_sat[chan].c_n0 = AltosParse.parse_int(words[i++]);
126                 }
127         }
128 }