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.
21 import java.awt.event.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.LinkedBlockingQueue;
30 import org.altusmetrum.AltosLib.*;
32 public class AltosDescent extends JComponent implements AltosFlightDisplay {
35 public abstract class DescentStatus {
40 abstract void show(AltosState state, int crc_errors);
43 label.setVisible(true);
44 value.setVisible(true);
45 lights.setVisible(true);
53 void show(String format, double value) {
54 show(String.format(format, value));
58 label.setVisible(false);
59 value.setVisible(false);
60 lights.setVisible(false);
69 label.setFont(Altos.label_font);
70 value.setFont(Altos.value_font);
73 public DescentStatus (GridBagLayout layout, int y, String text) {
74 GridBagConstraints c = new GridBagConstraints();
77 lights = new AltosLights();
78 c.gridx = 0; c.gridy = y;
79 c.anchor = GridBagConstraints.CENTER;
80 c.fill = GridBagConstraints.VERTICAL;
82 layout.setConstraints(lights, c);
85 label = new JLabel(text);
86 label.setFont(Altos.label_font);
87 label.setHorizontalAlignment(SwingConstants.LEFT);
88 c.gridx = 1; c.gridy = y;
89 c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
90 c.anchor = GridBagConstraints.WEST;
91 c.fill = GridBagConstraints.VERTICAL;
94 layout.setConstraints(label, c);
97 value = new JTextField(Altos.text_width);
98 value.setFont(Altos.value_font);
99 value.setHorizontalAlignment(SwingConstants.RIGHT);
100 c.gridx = 4; c.gridy = y;
102 c.anchor = GridBagConstraints.WEST;
103 c.fill = GridBagConstraints.BOTH;
105 layout.setConstraints(value, c);
111 public abstract class DescentValue {
119 abstract void show(AltosState state, int crc_errors);
122 label.setVisible(true);
123 value.setVisible(true);
127 label.setVisible(false);
128 value.setVisible(false);
131 void show(String v) {
136 void show(AltosUnits units, double v) {
137 show(units.show(8, v));
140 void show(String format, double v) {
141 show(String.format(format, v));
145 label.setFont(Altos.label_font);
146 value.setFont(Altos.value_font);
149 public DescentValue (GridBagLayout layout, int x, int y, String text) {
150 GridBagConstraints c = new GridBagConstraints();
153 label = new JLabel(text);
154 label.setFont(Altos.label_font);
155 label.setHorizontalAlignment(SwingConstants.LEFT);
156 c.gridx = x + 1; c.gridy = y;
157 c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
158 c.anchor = GridBagConstraints.WEST;
159 c.fill = GridBagConstraints.VERTICAL;
163 value = new JTextField(Altos.text_width);
164 value.setFont(Altos.value_font);
165 value.setHorizontalAlignment(SwingConstants.RIGHT);
166 c.gridx = x + 2; c.gridy = y;
168 c.anchor = GridBagConstraints.WEST;
169 c.fill = GridBagConstraints.BOTH;
175 public abstract class DescentDualValue {
186 label.setVisible(true);
187 value1.setVisible(true);
188 value2.setVisible(true);
192 label.setVisible(false);
193 value1.setVisible(false);
194 value2.setVisible(false);
198 label.setFont(Altos.label_font);
199 value1.setFont(Altos.value_font);
200 value2.setFont(Altos.value_font);
203 abstract void show(AltosState state, int crc_errors);
205 void show(String v1, String v2) {
210 void show(String f1, double v1, String f2, double v2) {
212 value1.setText(String.format(f1, v1));
213 value2.setText(String.format(f2, v2));
216 public DescentDualValue (GridBagLayout layout, int x, int y, String text) {
217 GridBagConstraints c = new GridBagConstraints();
220 label = new JLabel(text);
221 label.setFont(Altos.label_font);
222 label.setHorizontalAlignment(SwingConstants.LEFT);
223 c.gridx = x + 1; c.gridy = y;
224 c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
225 c.anchor = GridBagConstraints.WEST;
226 c.fill = GridBagConstraints.VERTICAL;
228 layout.setConstraints(label, c);
231 value1 = new JTextField(Altos.text_width);
232 value1.setFont(Altos.value_font);
233 value1.setHorizontalAlignment(SwingConstants.RIGHT);
234 c.gridx = x + 2; c.gridy = y;
235 c.anchor = GridBagConstraints.WEST;
236 c.fill = GridBagConstraints.BOTH;
238 layout.setConstraints(value1, c);
241 value2 = new JTextField(Altos.text_width);
242 value2.setFont(Altos.value_font);
243 value2.setHorizontalAlignment(SwingConstants.RIGHT);
244 c.gridx = x + 4; c.gridy = y;
245 c.anchor = GridBagConstraints.WEST;
246 c.fill = GridBagConstraints.BOTH;
249 layout.setConstraints(value2, c);
254 class Height extends DescentValue {
255 void show (AltosState state, int crc_errors) {
256 show(AltosConvert.height, state.height);
258 public Height (GridBagLayout layout, int x, int y) {
259 super (layout, x, y, "Height");
265 class Speed extends DescentValue {
266 void show (AltosState state, int crc_errors) {
267 double speed = state.speed;
269 speed = state.baro_speed;
270 show(AltosConvert.speed, speed);
272 public Speed (GridBagLayout layout, int x, int y) {
273 super (layout, x, y, "Speed");
279 String pos(double p, String pos, String neg) {
285 int deg = (int) Math.floor(p);
286 double min = (p - Math.floor(p)) * 60.0;
287 return String.format("%s %d° %9.6f", h, deg, min);
290 class Lat extends DescentValue {
291 void show (AltosState state, int crc_errors) {
292 if (state.gps != null && state.gps.connected)
293 show(pos(state.gps.lat,"N", "S"));
297 public Lat (GridBagLayout layout, int x, int y) {
298 super (layout, x, y, "Latitude");
304 class Lon extends DescentValue {
305 void show (AltosState state, int crc_errors) {
306 if (state.gps != null && state.gps.connected)
307 show(pos(state.gps.lon,"W", "E"));
311 public Lon (GridBagLayout layout, int x, int y) {
312 super (layout, x, y, "Longitude");
318 class Distance extends DescentValue {
319 void show(AltosState state, int crc_errors) {
320 show(AltosConvert.distance, state.from_pad.distance);
323 public Distance (GridBagLayout layout, int x, int y) {
324 super(layout, x, y, "Ground Distance");
331 class Apogee extends DescentStatus {
332 void show (AltosState state, int crc_errors) {
333 show("%4.2f V", state.drogue_sense);
334 lights.set(state.drogue_sense > 3.2);
336 public Apogee (GridBagLayout layout, int y) {
337 super(layout, y, "Apogee Igniter Voltage");
343 class Main extends DescentStatus {
344 void show (AltosState state, int crc_errors) {
345 show("%4.2f V", state.main_sense);
346 lights.set(state.main_sense > 3.2);
348 public Main (GridBagLayout layout, int y) {
349 super(layout, y, "Main Igniter Voltage");
355 class Bearing extends DescentDualValue {
356 void show (AltosState state, int crc_errors) {
357 if (state.from_pad != null) {
358 show( String.format("%3.0f°", state.from_pad.bearing),
359 state.from_pad.bearing_words(
360 AltosGreatCircle.BEARING_LONG));
365 public Bearing (GridBagLayout layout, int x, int y) {
366 super (layout, x, y, "Bearing");
372 class Range extends DescentValue {
373 void show (AltosState state, int crc_errors) {
374 show(AltosConvert.distance, state.range);
376 public Range (GridBagLayout layout, int x, int y) {
377 super (layout, x, y, "Range");
383 class Elevation extends DescentValue {
384 void show (AltosState state, int crc_errors) {
385 show("%3.0f°", state.elevation);
387 public Elevation (GridBagLayout layout, int x, int y) {
388 super (layout, x, y, "Elevation");
394 public void reset() {
407 public void set_font() {
415 elevation.set_font();
420 public void show(AltosState state, int crc_errors) {
421 height.show(state, crc_errors);
422 speed.show(state, crc_errors);
423 if (state.gps != null && state.gps.connected) {
424 bearing.show(state, crc_errors);
425 range.show(state, crc_errors);
426 distance.show(state, crc_errors);
427 elevation.show(state, crc_errors);
428 lat.show(state, crc_errors);
429 lon.show(state, crc_errors);
438 if (state.main_sense != AltosRecord.MISSING)
439 main.show(state, crc_errors);
442 if (state.drogue_sense != AltosRecord.MISSING)
443 apogee.show(state, crc_errors);
448 public AltosDescent() {
449 layout = new GridBagLayout();
453 /* Elements in descent display */
454 speed = new Speed(layout, 0, 0);
455 height = new Height(layout, 2, 0);
456 elevation = new Elevation(layout, 0, 1);
457 range = new Range(layout, 2, 1);
458 bearing = new Bearing(layout, 0, 2);
459 distance = new Distance(layout, 0, 3);
460 lat = new Lat(layout, 0, 4);
461 lon = new Lon(layout, 2, 4);
463 apogee = new Apogee(layout, 5);
464 main = new Main(layout, 6);