summaryrefslogtreecommitdiff
path: root/ao-tools/altosui/AltosGPS.java
blob: 42f4dc83249fb9b62fcc99b5054f6ce6f78fa887 (plain) (blame)
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
/*
 * Copyright © 2010 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

package altosui;

import java.lang.*;
import java.text.*;
import altosui.AltosParse;


public class AltosGPS {
	public class AltosGPSSat {
		int	svid;
		int	c_n0;
	}

	int	nsat;
	boolean	gps_locked;
	boolean	gps_connected;
	double	lat;		/* degrees (+N -S) */
	double	lon;		/* degrees (+E -W) */
	int	alt;		/* m */
	int	year;
	int	month;
	int	day;
	int	hour;
	int	minute;
	int	second;

	int	gps_extended;	/* has extra data */
	double	ground_speed;	/* m/s */
	int	course;		/* degrees */
	double	climb_rate;	/* m/s */
	double	hdop;		/* unitless? */
	int	h_error;	/* m */
	int	v_error;	/* m */

	AltosGPSSat[] cc_gps_sat;	/* tracking data */

	void ParseGPSTime(String date, String time) throws ParseException {
		String[] ymd = date.split("-");
		if (ymd.length != 3)
			throw new ParseException("error parsing GPS date " + date + " got " + ymd.length, 0);
		year = AltosParse.parse_int(ymd[0]);
		month = AltosParse.parse_int(ymd[1]);
		day = AltosParse.parse_int(ymd[2]);

		String[] hms = time.split(":");
		if (hms.length != 3)
			throw new ParseException("Error parsing GPS time " + time + " got " + hms.length, 0);
		hour = AltosParse.parse_int(hms[0]);
		minute = AltosParse.parse_int(hms[1]);
		second = AltosParse.parse_int(hms[2]);
	}

	void ClearGPSTime() {
		year = month = day = 0;
		hour = minute = second = 0;
	}

	public AltosGPS(String[] words, int i) throws ParseException {
		AltosParse.word(words[i++], "GPS");
		nsat = AltosParse.parse_int(words[i++]);
		AltosParse.word(words[i++], "sat");

		gps_connected = false;
		gps_locked = false;
		lat = lon = 0;
		alt = 0;
		ClearGPSTime();
		if ((words[i]).equals("unlocked")) {
			gps_connected = true;
			i++;
		} else if ((words[i]).equals("not-connected")) {
			i++;
		} else if (words.length >= 40) {
			gps_locked = true;
			gps_connected = true;

			ParseGPSTime(words[i], words[i+1]); i += 2;
			lat = AltosParse.parse_coord(words[i++]);
			lon = AltosParse.parse_coord(words[i++]);
			alt = AltosParse.parse_int(AltosParse.strip_suffix(words[i++], "m"));
			ground_speed = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "m/s(H)"));
			course = AltosParse.parse_int(AltosParse.strip_suffix(words[i++], "°"));
			climb_rate = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "m/s(V)"));
			hdop = AltosParse.parse_double(AltosParse.strip_suffix(words[i++], "(hdop)"));
			h_error = AltosParse.parse_int(AltosParse.strip_suffix(words[i++], "(herr)"));
			v_error = AltosParse.parse_int(AltosParse.strip_suffix(words[i++], "(verr)"));
		} else {
			i++;
		}
		AltosParse.word(words[i++], "SAT");
		int tracking_channels = 0;
		if (words[i].equals("not-connected"))
			tracking_channels = 0;
		else
			tracking_channels = AltosParse.parse_int(words[i]);
		i++;
		cc_gps_sat = new AltosGPS.AltosGPSSat[tracking_channels];
		for (int chan = 0; chan < tracking_channels; chan++) {
			cc_gps_sat[chan] = new AltosGPS.AltosGPSSat();
			cc_gps_sat[chan].svid = AltosParse.parse_int(words[i++]);
			cc_gps_sat[chan].c_n0 = AltosParse.parse_int(words[i++]);
		}
	}

	public void set_latitude(int in_lat) {
		lat = in_lat / 10.0e7;
	}

	public void set_longitude(int in_lon) {
		lon = in_lon / 10.0e7;
	}

	public void set_time(int hour, int minute, int second) {
		hour = hour;
		minute = minute;
		second = second;
	}

	public void set_date(int year, int month, int day) {
		year = year;
		month = month;
		day = day;
	}

	public void set_flags(int flags) {
		flags = flags;
	}

	public void set_altitude(int altitude) {
		altitude = altitude;
	}

	public void add_sat(int svid, int c_n0) {
		if (cc_gps_sat == null) {
			cc_gps_sat = new AltosGPS.AltosGPSSat[1];
		} else {
			AltosGPSSat[] new_gps_sat = new AltosGPS.AltosGPSSat[cc_gps_sat.length + 1];
			for (int i = 0; i < cc_gps_sat.length; i++)
				new_gps_sat[i] = cc_gps_sat[i];
			cc_gps_sat = new_gps_sat;
		}
		AltosGPS.AltosGPSSat	sat = new AltosGPS.AltosGPSSat();
		sat.svid = svid;
		sat.c_n0 = c_n0;
		cc_gps_sat[cc_gps_sat.length - 1] = sat;
	}

	public AltosGPS() {
		ClearGPSTime();
		cc_gps_sat = null;
	}
}