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.
18 package org.altusmetrum.telegps;
22 import java.awt.event.*;
24 import org.altusmetrum.altoslib_4.*;
25 import org.altusmetrum.altosuilib_2.*;
27 public class TeleGPSInfo extends JComponent implements AltosFlightDisplay, HierarchyListener {
31 private AltosState last_state;
32 private AltosListenerState last_listener_state;
34 abstract class Value extends AltosUIUnitsIndicator {
35 public abstract void show(AltosState state, AltosListenerState listener_state);
37 public Value (Container container, int y, AltosUnits units, String text) {
38 super(container, y, units, text, 1, false, 2);
42 abstract class DualValue extends AltosUIUnitsIndicator {
43 public DualValue (Container container, int y, AltosUnits units, String text) {
44 super(container, y, units, text, 2, false, 1);
48 abstract class ValueHold extends DualValue {
51 last_values[1] = AltosLib.MISSING;
53 public ValueHold (Container container, int y, AltosUnits units, String text) {
54 super(container, y, units, text);
58 class Altitude extends ValueHold {
59 public double value(AltosState state, int i) {
61 return state.altitude();
63 return state.max_altitude();
66 public Altitude (Container container, int y) {
67 super (container, y, AltosConvert.height, "Altitude");
71 class AscentRate extends ValueHold {
72 public double value(AltosState state, int i) {
74 return state.gps_ascent_rate();
76 return state.max_gps_ascent_rate();
78 public AscentRate (Container container, int y) {
79 super (container, y, AltosConvert.speed, "Ascent Rate");
83 class GroundSpeed extends ValueHold {
84 public double value(AltosState state, int i) {
86 return state.gps_ground_speed();
88 return state.max_gps_ground_speed();
90 public GroundSpeed (Container container, int y) {
91 super (container, y, AltosConvert.speed, "Ground Speed");
95 class Course extends AltosUIIndicator {
97 public void show (AltosState state, AltosListenerState listener_state) {
98 double course = state.gps_course();
99 if (course != AltosLib.MISSING)
100 show( String.format("%3.0f°", course),
101 AltosConvert.bearing_to_words(
102 AltosConvert.BEARING_LONG,
105 public Course (Container container, int y) {
106 super (container, y, "Course", 2, false, 1);
110 class Lat extends AltosUIIndicator {
112 String pos(double p, String pos, String neg) {
118 int deg = (int) Math.floor(p);
119 double min = (p - Math.floor(p)) * 60.0;
120 return String.format("%s %4d° %9.6f", h, deg, min);
123 public void show (AltosState state, AltosListenerState listener_state) {
124 if (state.gps != null && state.gps.connected && state.gps.lat != AltosLib.MISSING)
125 show(pos(state.gps.lat,"N", "S"));
129 public Lat (Container container, int y) {
130 super (container, y, "Latitude", 1, false, 2);
134 class Lon extends AltosUIIndicator {
136 String pos(double p, String pos, String neg) {
142 int deg = (int) Math.floor(p);
143 double min = (p - Math.floor(p)) * 60.0;
144 return String.format("%s %4d° %9.6f", h, deg, min);
147 public void show (AltosState state, AltosListenerState listener_state) {
148 if (state.gps != null && state.gps.connected && state.gps.lon != AltosLib.MISSING)
149 show(pos(state.gps.lon,"E", "W"));
153 public Lon (Container container, int y) {
154 super (container, y, "Longitude", 1, false, 2);
158 class GPSLocked extends AltosUIIndicator {
160 public void show (AltosState state, AltosListenerState listener_state) {
161 if (state == null || state.gps == null)
164 int soln = state.gps.nsat;
165 int nsat = state.gps.cc_gps_sat != null ? state.gps.cc_gps_sat.length : 0;
166 show("%4d in solution", soln,
167 "%4d in view", nsat);
168 set_lights(state.gps.locked && soln >= 4);
171 public GPSLocked (Container container, int y) {
172 super (container, y, "GPS Locked", 2, true, 1);
176 LinkedList<AltosUIIndicator> indicators = new LinkedList<AltosUIIndicator>();
178 public void reset() {
179 for (AltosUIIndicator i : indicators)
183 public void font_size_changed(int font_size) {
184 cur.setFont(AltosUILib.label_font);
185 max.setFont(AltosUILib.label_font);
186 for (AltosUIIndicator i : indicators)
187 i.font_size_changed(font_size);
190 public void units_changed(boolean imperial_units) {
191 for (AltosUIIndicator i : indicators)
192 i.units_changed(imperial_units);
195 public void show(AltosState state, AltosListenerState listener_state) {
198 last_listener_state = listener_state;
202 for (AltosUIIndicator i : indicators)
203 i.show(state, listener_state);
206 public void labels(Container container, int y) {
207 GridBagLayout layout = (GridBagLayout)(container.getLayout());
208 GridBagConstraints c;
210 cur = new JLabel("Current");
211 cur.setFont(AltosUILib.label_font);
212 c = new GridBagConstraints();
213 c.gridx = 2; c.gridy = y;
214 c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
215 layout.setConstraints(cur, c);
218 max = new JLabel("Maximum");
219 max.setFont(AltosUILib.label_font);
220 c.gridx = 3; c.gridy = y;
221 layout.setConstraints(max, c);
225 public String getName() {
229 public void hierarchyChanged(HierarchyEvent e) {
230 if (last_state != null && isShowing()) {
231 AltosState state = last_state;
232 AltosListenerState listener_state = last_listener_state;
235 last_listener_state = null;
236 show(state, listener_state);
240 public TeleGPSInfo() {
241 setLayout(new GridBagLayout());
245 indicators.add(new Altitude(this, y++));
246 indicators.add(new GroundSpeed(this, y++));
247 indicators.add(new AscentRate(this, y++));
248 indicators.add(new Course(this, y++));
249 indicators.add(new Lat(this, y++));
250 indicators.add(new Lon(this, y++));
251 indicators.add(new GPSLocked(this, y++));
252 addHierarchyListener(this);