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 org.altusmetrum.altoslib_2.*;
24 public class AltosDescent extends JComponent implements AltosFlightDisplay {
27 public abstract class DescentStatus {
32 abstract void show(AltosState state, AltosListenerState listener_state);
35 label.setVisible(true);
36 value.setVisible(true);
37 lights.setVisible(true);
45 void show(String format, double value) {
46 show(String.format(format, value));
50 label.setVisible(false);
51 value.setVisible(false);
52 lights.setVisible(false);
61 label.setFont(Altos.label_font);
62 value.setFont(Altos.value_font);
65 public DescentStatus (GridBagLayout layout, int y, String text) {
66 GridBagConstraints c = new GridBagConstraints();
69 lights = new AltosLights();
70 c.gridx = 0; c.gridy = y;
71 c.anchor = GridBagConstraints.CENTER;
72 c.fill = GridBagConstraints.VERTICAL;
74 layout.setConstraints(lights, c);
77 label = new JLabel(text);
78 label.setFont(Altos.label_font);
79 label.setHorizontalAlignment(SwingConstants.LEFT);
80 c.gridx = 1; c.gridy = y;
81 c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
82 c.anchor = GridBagConstraints.WEST;
83 c.fill = GridBagConstraints.VERTICAL;
86 layout.setConstraints(label, c);
89 value = new JTextField(Altos.text_width);
90 value.setFont(Altos.value_font);
91 value.setHorizontalAlignment(SwingConstants.RIGHT);
92 c.gridx = 4; c.gridy = y;
94 c.anchor = GridBagConstraints.WEST;
95 c.fill = GridBagConstraints.BOTH;
97 layout.setConstraints(value, c);
103 public abstract class DescentValue {
111 abstract void show(AltosState state, AltosListenerState listener_state);
114 label.setVisible(true);
115 value.setVisible(true);
119 label.setVisible(false);
120 value.setVisible(false);
123 void show(String v) {
128 void show(AltosUnits units, double v) {
129 show(units.show(8, v));
132 void show(String format, double v) {
133 show(String.format(format, v));
137 label.setFont(Altos.label_font);
138 value.setFont(Altos.value_font);
141 public DescentValue (GridBagLayout layout, int x, int y, String text) {
142 GridBagConstraints c = new GridBagConstraints();
145 label = new JLabel(text);
146 label.setFont(Altos.label_font);
147 label.setHorizontalAlignment(SwingConstants.LEFT);
148 c.gridx = x + 1; c.gridy = y;
149 c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
150 c.anchor = GridBagConstraints.WEST;
151 c.fill = GridBagConstraints.VERTICAL;
155 value = new JTextField(Altos.text_width);
156 value.setFont(Altos.value_font);
157 value.setHorizontalAlignment(SwingConstants.RIGHT);
158 c.gridx = x + 2; c.gridy = y;
160 c.anchor = GridBagConstraints.WEST;
161 c.fill = GridBagConstraints.BOTH;
167 public abstract class DescentDualValue {
178 label.setVisible(true);
179 value1.setVisible(true);
180 value2.setVisible(true);
184 label.setVisible(false);
185 value1.setVisible(false);
186 value2.setVisible(false);
190 label.setFont(Altos.label_font);
191 value1.setFont(Altos.value_font);
192 value2.setFont(Altos.value_font);
195 abstract void show(AltosState state, AltosListenerState listener_state);
197 void show(String v1, String v2) {
202 void show(String f1, double v1, String f2, double v2) {
204 value1.setText(String.format(f1, v1));
205 value2.setText(String.format(f2, v2));
208 public DescentDualValue (GridBagLayout layout, int x, int y, String text) {
209 GridBagConstraints c = new GridBagConstraints();
212 label = new JLabel(text);
213 label.setFont(Altos.label_font);
214 label.setHorizontalAlignment(SwingConstants.LEFT);
215 c.gridx = x + 1; c.gridy = y;
216 c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
217 c.anchor = GridBagConstraints.WEST;
218 c.fill = GridBagConstraints.VERTICAL;
220 layout.setConstraints(label, c);
223 value1 = new JTextField(Altos.text_width);
224 value1.setFont(Altos.value_font);
225 value1.setHorizontalAlignment(SwingConstants.RIGHT);
226 c.gridx = x + 2; c.gridy = y;
227 c.anchor = GridBagConstraints.WEST;
228 c.fill = GridBagConstraints.BOTH;
230 layout.setConstraints(value1, c);
233 value2 = new JTextField(Altos.text_width);
234 value2.setFont(Altos.value_font);
235 value2.setHorizontalAlignment(SwingConstants.RIGHT);
236 c.gridx = x + 4; c.gridy = y;
237 c.anchor = GridBagConstraints.WEST;
238 c.fill = GridBagConstraints.BOTH;
241 layout.setConstraints(value2, c);
246 class Height extends DescentValue {
247 void show (AltosState state, AltosListenerState listener_state) {
248 show(AltosConvert.height, state.height());
250 public Height (GridBagLayout layout, int x, int y) {
251 super (layout, x, y, "Height");
257 class Speed extends DescentValue {
258 void show (AltosState state, AltosListenerState listener_state) {
259 show(AltosConvert.speed, state.speed());
261 public Speed (GridBagLayout layout, int x, int y) {
262 super (layout, x, y, "Speed");
268 String pos(double p, String pos, String neg) {
274 int deg = (int) Math.floor(p);
275 double min = (p - Math.floor(p)) * 60.0;
276 return String.format("%s %d° %9.6f", h, deg, min);
279 class Lat extends DescentValue {
280 void show (AltosState state, AltosListenerState listener_state) {
281 if (state.gps != null && state.gps.connected && state.gps.lat != AltosLib.MISSING)
282 show(pos(state.gps.lat,"N", "S"));
286 public Lat (GridBagLayout layout, int x, int y) {
287 super (layout, x, y, "Latitude");
293 class Lon extends DescentValue {
294 void show (AltosState state, AltosListenerState listener_state) {
295 if (state.gps != null && state.gps.connected && state.gps.lon != AltosLib.MISSING)
296 show(pos(state.gps.lon,"W", "E"));
300 public Lon (GridBagLayout layout, int x, int y) {
301 super (layout, x, y, "Longitude");
307 class Distance extends DescentValue {
308 void show(AltosState state, AltosListenerState listener_state) {
309 if (state.from_pad != null)
310 show(AltosConvert.distance, state.from_pad.distance);
315 public Distance (GridBagLayout layout, int x, int y) {
316 super(layout, x, y, "Ground Distance");
323 class Apogee extends DescentStatus {
324 void show (AltosState state, AltosListenerState listener_state) {
325 show("%4.2f V", state.apogee_voltage);
326 lights.set(state.apogee_voltage >= AltosLib.ao_igniter_good);
328 public Apogee (GridBagLayout layout, int y) {
329 super(layout, y, "Apogee Igniter Voltage");
335 class Main extends DescentStatus {
336 void show (AltosState state, AltosListenerState listener_state) {
337 show("%4.2f V", state.main_voltage);
338 lights.set(state.main_voltage >= AltosLib.ao_igniter_good);
340 public Main (GridBagLayout layout, int y) {
341 super(layout, y, "Main Igniter Voltage");
347 class Bearing extends DescentDualValue {
348 void show (AltosState state, AltosListenerState listener_state) {
349 if (state.from_pad != null) {
350 show( String.format("%3.0f°", state.from_pad.bearing),
351 state.from_pad.bearing_words(
352 AltosGreatCircle.BEARING_LONG));
357 public Bearing (GridBagLayout layout, int x, int y) {
358 super (layout, x, y, "Bearing");
364 class Range extends DescentValue {
365 void show (AltosState state, AltosListenerState listener_state) {
366 show(AltosConvert.distance, state.range);
368 public Range (GridBagLayout layout, int x, int y) {
369 super (layout, x, y, "Range");
375 class Elevation extends DescentValue {
376 void show (AltosState state, AltosListenerState listener_state) {
377 show("%3.0f°", state.elevation);
379 public Elevation (GridBagLayout layout, int x, int y) {
380 super (layout, x, y, "Elevation");
386 public void reset() {
399 public void set_font() {
407 elevation.set_font();
412 public void show(AltosState state, AltosListenerState listener_state) {
413 height.show(state, listener_state);
414 speed.show(state, listener_state);
415 if (state.gps != null && state.gps.connected) {
416 bearing.show(state, listener_state);
417 range.show(state, listener_state);
418 distance.show(state, listener_state);
419 elevation.show(state, listener_state);
420 lat.show(state, listener_state);
421 lon.show(state, listener_state);
430 if (state.main_voltage != AltosLib.MISSING)
431 main.show(state, listener_state);
434 if (state.apogee_voltage != AltosLib.MISSING)
435 apogee.show(state, listener_state);
440 public String getName() {
444 public AltosDescent() {
445 layout = new GridBagLayout();
449 /* Elements in descent display */
450 speed = new Speed(layout, 0, 0);
451 height = new Height(layout, 2, 0);
452 elevation = new Elevation(layout, 0, 1);
453 range = new Range(layout, 2, 1);
454 bearing = new Bearing(layout, 0, 2);
455 distance = new Distance(layout, 0, 3);
456 lat = new Lat(layout, 0, 4);
457 lon = new Lon(layout, 2, 4);
459 apogee = new Apogee(layout, 5);
460 main = new Main(layout, 6);