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_4.*;
23 import org.altusmetrum.altosuilib_2.*;
25 public class AltosDescent extends JComponent implements AltosFlightDisplay {
28 public abstract class DescentStatus {
33 abstract void show(AltosState state, AltosListenerState listener_state);
36 label.setVisible(true);
37 value.setVisible(true);
38 lights.setVisible(true);
46 void show(String format, double value) {
47 show(String.format(format, value));
51 label.setVisible(false);
52 value.setVisible(false);
53 lights.setVisible(false);
62 label.setFont(Altos.label_font);
63 value.setFont(Altos.value_font);
66 public DescentStatus (GridBagLayout layout, int y, String text) {
67 GridBagConstraints c = new GridBagConstraints();
70 lights = new AltosLights();
71 c.gridx = 0; c.gridy = y;
72 c.anchor = GridBagConstraints.CENTER;
73 c.fill = GridBagConstraints.VERTICAL;
75 layout.setConstraints(lights, c);
78 label = new JLabel(text);
79 label.setFont(Altos.label_font);
80 label.setHorizontalAlignment(SwingConstants.LEFT);
81 c.gridx = 1; c.gridy = y;
82 c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
83 c.anchor = GridBagConstraints.WEST;
84 c.fill = GridBagConstraints.VERTICAL;
87 layout.setConstraints(label, c);
90 value = new JTextField(Altos.text_width);
91 value.setFont(Altos.value_font);
92 value.setHorizontalAlignment(SwingConstants.RIGHT);
93 c.gridx = 4; c.gridy = y;
95 c.anchor = GridBagConstraints.WEST;
96 c.fill = GridBagConstraints.BOTH;
98 layout.setConstraints(value, c);
104 public abstract class DescentValue {
112 abstract void show(AltosState state, AltosListenerState listener_state);
115 label.setVisible(true);
116 value.setVisible(true);
120 label.setVisible(false);
121 value.setVisible(false);
124 void show(String v) {
129 void show(AltosUnits units, double v) {
130 show(units.show(8, v));
133 void show(String format, double v) {
134 show(String.format(format, v));
138 label.setFont(Altos.label_font);
139 value.setFont(Altos.value_font);
142 public DescentValue (GridBagLayout layout, int x, int y, String text) {
143 GridBagConstraints c = new GridBagConstraints();
146 label = new JLabel(text);
147 label.setFont(Altos.label_font);
148 label.setHorizontalAlignment(SwingConstants.LEFT);
149 c.gridx = x + 1; c.gridy = y;
150 c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
151 c.anchor = GridBagConstraints.WEST;
152 c.fill = GridBagConstraints.VERTICAL;
156 value = new JTextField(Altos.text_width);
157 value.setFont(Altos.value_font);
158 value.setHorizontalAlignment(SwingConstants.RIGHT);
159 c.gridx = x + 2; c.gridy = y;
161 c.anchor = GridBagConstraints.WEST;
162 c.fill = GridBagConstraints.BOTH;
168 public abstract class DescentDualValue {
179 label.setVisible(true);
180 value1.setVisible(true);
181 value2.setVisible(true);
185 label.setVisible(false);
186 value1.setVisible(false);
187 value2.setVisible(false);
191 label.setFont(Altos.label_font);
192 value1.setFont(Altos.value_font);
193 value2.setFont(Altos.value_font);
196 abstract void show(AltosState state, AltosListenerState listener_state);
198 void show(String v1, String v2) {
203 void show(String f1, double v1, String f2, double v2) {
205 value1.setText(String.format(f1, v1));
206 value2.setText(String.format(f2, v2));
209 public DescentDualValue (GridBagLayout layout, int x, int y, String text) {
210 GridBagConstraints c = new GridBagConstraints();
213 label = new JLabel(text);
214 label.setFont(Altos.label_font);
215 label.setHorizontalAlignment(SwingConstants.LEFT);
216 c.gridx = x + 1; c.gridy = y;
217 c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
218 c.anchor = GridBagConstraints.WEST;
219 c.fill = GridBagConstraints.VERTICAL;
221 layout.setConstraints(label, c);
224 value1 = new JTextField(Altos.text_width);
225 value1.setFont(Altos.value_font);
226 value1.setHorizontalAlignment(SwingConstants.RIGHT);
227 c.gridx = x + 2; c.gridy = y;
228 c.anchor = GridBagConstraints.WEST;
229 c.fill = GridBagConstraints.BOTH;
231 layout.setConstraints(value1, c);
234 value2 = new JTextField(Altos.text_width);
235 value2.setFont(Altos.value_font);
236 value2.setHorizontalAlignment(SwingConstants.RIGHT);
237 c.gridx = x + 4; c.gridy = y;
238 c.anchor = GridBagConstraints.WEST;
239 c.fill = GridBagConstraints.BOTH;
242 layout.setConstraints(value2, c);
247 class Height extends DescentValue {
248 void show (AltosState state, AltosListenerState listener_state) {
249 show(AltosConvert.height, state.height());
251 public Height (GridBagLayout layout, int x, int y) {
252 super (layout, x, y, "Height");
258 class Speed extends DescentValue {
259 void show (AltosState state, AltosListenerState listener_state) {
260 show(AltosConvert.speed, state.speed());
262 public Speed (GridBagLayout layout, int x, int y) {
263 super (layout, x, y, "Speed");
269 String pos(double p, String pos, String neg) {
275 int deg = (int) Math.floor(p);
276 double min = (p - Math.floor(p)) * 60.0;
277 return String.format("%s %d° %9.6f", h, deg, min);
280 class Lat extends DescentValue {
281 void show (AltosState state, AltosListenerState listener_state) {
282 if (state.gps != null && state.gps.connected && state.gps.lat != AltosLib.MISSING)
283 show(pos(state.gps.lat,"N", "S"));
287 public Lat (GridBagLayout layout, int x, int y) {
288 super (layout, x, y, "Latitude");
294 class Lon extends DescentValue {
295 void show (AltosState state, AltosListenerState listener_state) {
296 if (state.gps != null && state.gps.connected && state.gps.lon != AltosLib.MISSING)
297 show(pos(state.gps.lon,"W", "E"));
301 public Lon (GridBagLayout layout, int x, int y) {
302 super (layout, x, y, "Longitude");
308 class Distance extends DescentValue {
309 void show(AltosState state, AltosListenerState listener_state) {
310 if (state.from_pad != null)
311 show(AltosConvert.distance, state.from_pad.distance);
316 public Distance (GridBagLayout layout, int x, int y) {
317 super(layout, x, y, "Ground Distance");
324 class Apogee extends DescentStatus {
325 void show (AltosState state, AltosListenerState listener_state) {
326 show("%4.2f V", state.apogee_voltage);
327 lights.set(state.apogee_voltage >= AltosLib.ao_igniter_good);
329 public Apogee (GridBagLayout layout, int y) {
330 super(layout, y, "Apogee Igniter Voltage");
336 class Main extends DescentStatus {
337 void show (AltosState state, AltosListenerState listener_state) {
338 show("%4.2f V", state.main_voltage);
339 lights.set(state.main_voltage >= AltosLib.ao_igniter_good);
341 public Main (GridBagLayout layout, int y) {
342 super(layout, y, "Main Igniter Voltage");
348 class Bearing extends DescentDualValue {
349 void show (AltosState state, AltosListenerState listener_state) {
350 if (state.from_pad != null) {
351 show( String.format("%3.0f°", state.from_pad.bearing),
352 state.from_pad.bearing_words(
353 AltosGreatCircle.BEARING_LONG));
358 public Bearing (GridBagLayout layout, int x, int y) {
359 super (layout, x, y, "Bearing");
365 class Range extends DescentValue {
366 void show (AltosState state, AltosListenerState listener_state) {
367 show(AltosConvert.distance, state.range);
369 public Range (GridBagLayout layout, int x, int y) {
370 super (layout, x, y, "Range");
376 class Elevation extends DescentValue {
377 void show (AltosState state, AltosListenerState listener_state) {
378 show("%3.0f°", state.elevation);
380 public Elevation (GridBagLayout layout, int x, int y) {
381 super (layout, x, y, "Elevation");
387 public void reset() {
400 public void set_font() {
408 elevation.set_font();
413 public void show(AltosState state, AltosListenerState listener_state) {
414 height.show(state, listener_state);
415 speed.show(state, listener_state);
416 if (state.gps != null && state.gps.connected) {
417 bearing.show(state, listener_state);
418 range.show(state, listener_state);
419 distance.show(state, listener_state);
420 elevation.show(state, listener_state);
421 lat.show(state, listener_state);
422 lon.show(state, listener_state);
431 if (state.main_voltage != AltosLib.MISSING)
432 main.show(state, listener_state);
435 if (state.apogee_voltage != AltosLib.MISSING)
436 apogee.show(state, listener_state);
441 public String getName() {
445 public AltosDescent() {
446 layout = new GridBagLayout();
450 /* Elements in descent display */
451 speed = new Speed(layout, 0, 0);
452 height = new Height(layout, 2, 0);
453 elevation = new Elevation(layout, 0, 1);
454 range = new Range(layout, 2, 1);
455 bearing = new Bearing(layout, 0, 2);
456 distance = new Distance(layout, 0, 3);
457 lat = new Lat(layout, 0, 4);
458 lon = new Lon(layout, 2, 4);
460 apogee = new Apogee(layout, 5);
461 main = new Main(layout, 6);