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);
49 label.setVisible(false);
50 value.setVisible(false);
51 lights.setVisible(false);
60 label.setFont(Altos.label_font);
61 value.setFont(Altos.value_font);
64 public DescentStatus (GridBagLayout layout, int y, String text) {
65 GridBagConstraints c = new GridBagConstraints();
68 lights = new AltosLights();
69 c.gridx = 0; c.gridy = y;
70 c.anchor = GridBagConstraints.CENTER;
71 c.fill = GridBagConstraints.VERTICAL;
73 layout.setConstraints(lights, c);
76 label = new JLabel(text);
77 label.setFont(Altos.label_font);
78 label.setHorizontalAlignment(SwingConstants.LEFT);
79 c.gridx = 1; c.gridy = y;
80 c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
81 c.anchor = GridBagConstraints.WEST;
82 c.fill = GridBagConstraints.VERTICAL;
85 layout.setConstraints(label, c);
88 value = new JTextField(Altos.text_width);
89 value.setFont(Altos.value_font);
90 value.setHorizontalAlignment(SwingConstants.RIGHT);
91 c.gridx = 4; c.gridy = y;
93 c.anchor = GridBagConstraints.WEST;
94 c.fill = GridBagConstraints.BOTH;
96 layout.setConstraints(value, c);
102 public abstract class DescentValue {
110 abstract void show(AltosState state, int crc_errors);
113 label.setVisible(true);
114 value.setVisible(true);
118 label.setVisible(false);
119 value.setVisible(false);
122 void show(AltosUnits units, double v) {
123 value.setText(units.show(8, v));
126 void show(String format, double v) {
127 value.setText(String.format(format, v));
130 void show(String v) {
135 label.setFont(Altos.label_font);
136 value.setFont(Altos.value_font);
139 public DescentValue (GridBagLayout layout, int x, int y, String text) {
140 GridBagConstraints c = new GridBagConstraints();
143 label = new JLabel(text);
144 label.setFont(Altos.label_font);
145 label.setHorizontalAlignment(SwingConstants.LEFT);
146 c.gridx = x + 1; c.gridy = y;
147 c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
148 c.anchor = GridBagConstraints.WEST;
149 c.fill = GridBagConstraints.VERTICAL;
153 value = new JTextField(Altos.text_width);
154 value.setFont(Altos.value_font);
155 value.setHorizontalAlignment(SwingConstants.RIGHT);
156 c.gridx = x + 2; c.gridy = y;
158 c.anchor = GridBagConstraints.WEST;
159 c.fill = GridBagConstraints.BOTH;
165 public abstract class DescentDualValue {
176 label.setVisible(true);
177 value1.setVisible(true);
178 value2.setVisible(true);
182 label.setVisible(false);
183 value1.setVisible(false);
184 value2.setVisible(false);
188 label.setFont(Altos.label_font);
189 value1.setFont(Altos.value_font);
190 value2.setFont(Altos.value_font);
193 abstract void show(AltosState state, int crc_errors);
195 void show(String v1, String v2) {
200 void show(String f1, double v1, String f2, double v2) {
202 value1.setText(String.format(f1, v1));
203 value2.setText(String.format(f2, v2));
206 public DescentDualValue (GridBagLayout layout, int x, int y, String text) {
207 GridBagConstraints c = new GridBagConstraints();
210 label = new JLabel(text);
211 label.setFont(Altos.label_font);
212 label.setHorizontalAlignment(SwingConstants.LEFT);
213 c.gridx = x + 1; c.gridy = y;
214 c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
215 c.anchor = GridBagConstraints.WEST;
216 c.fill = GridBagConstraints.VERTICAL;
218 layout.setConstraints(label, c);
221 value1 = new JTextField(Altos.text_width);
222 value1.setFont(Altos.value_font);
223 value1.setHorizontalAlignment(SwingConstants.RIGHT);
224 c.gridx = x + 2; c.gridy = y;
225 c.anchor = GridBagConstraints.WEST;
226 c.fill = GridBagConstraints.BOTH;
228 layout.setConstraints(value1, c);
231 value2 = new JTextField(Altos.text_width);
232 value2.setFont(Altos.value_font);
233 value2.setHorizontalAlignment(SwingConstants.RIGHT);
234 c.gridx = x + 4; c.gridy = y;
235 c.anchor = GridBagConstraints.WEST;
236 c.fill = GridBagConstraints.BOTH;
239 layout.setConstraints(value2, c);
244 class Height extends DescentValue {
245 void show (AltosState state, int crc_errors) {
246 show(AltosConvert.height, state.height);
248 public Height (GridBagLayout layout, int x, int y) {
249 super (layout, x, y, "Height");
255 class Speed extends DescentValue {
256 void show (AltosState state, int crc_errors) {
257 double speed = state.speed;
259 speed = state.baro_speed;
260 show(AltosConvert.speed, 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, int crc_errors) {
282 if (state.gps != null && state.gps.connected)
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, int crc_errors) {
296 if (state.gps != null && state.gps.connected)
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 Apogee extends DescentStatus {
309 void show (AltosState state, int crc_errors) {
311 value.setText(String.format("%4.2f V", state.drogue_sense));
312 lights.set(state.drogue_sense > 3.2);
314 public Apogee (GridBagLayout layout, int y) {
315 super(layout, y, "Apogee Igniter Voltage");
321 class Main extends DescentStatus {
322 void show (AltosState state, int crc_errors) {
324 value.setText(String.format("%4.2f V", state.main_sense));
325 lights.set(state.main_sense > 3.2);
327 public Main (GridBagLayout layout, int y) {
328 super(layout, y, "Main Igniter Voltage");
334 class Bearing extends DescentDualValue {
335 void show (AltosState state, int crc_errors) {
336 if (state.from_pad != null) {
337 show( String.format("%3.0f°", state.from_pad.bearing),
338 state.from_pad.bearing_words(
339 AltosGreatCircle.BEARING_LONG));
344 public Bearing (GridBagLayout layout, int x, int y) {
345 super (layout, x, y, "Bearing");
351 class Range extends DescentValue {
352 void show (AltosState state, int crc_errors) {
353 show(AltosConvert.distance, state.range);
355 public Range (GridBagLayout layout, int x, int y) {
356 super (layout, x, y, "Range");
362 class Elevation extends DescentValue {
363 void show (AltosState state, int crc_errors) {
364 show("%3.0f°", state.elevation);
366 public Elevation (GridBagLayout layout, int x, int y) {
367 super (layout, x, y, "Elevation");
373 public void reset() {
385 public void set_font() {
392 elevation.set_font();
397 public void show(AltosState state, int crc_errors) {
398 height.show(state, crc_errors);
399 speed.show(state, crc_errors);
400 if (state.gps != null && state.gps.connected) {
401 bearing.show(state, crc_errors);
402 range.show(state, crc_errors);
403 elevation.show(state, crc_errors);
404 lat.show(state, crc_errors);
405 lon.show(state, crc_errors);
413 if (state.main_sense != AltosRecord.MISSING)
414 main.show(state, crc_errors);
417 if (state.drogue_sense != AltosRecord.MISSING)
418 apogee.show(state, crc_errors);
423 public AltosDescent() {
424 layout = new GridBagLayout();
428 /* Elements in descent display */
429 speed = new Speed(layout, 0, 0);
430 height = new Height(layout, 2, 0);
431 elevation = new Elevation(layout, 0, 1);
432 range = new Range(layout, 2, 1);
433 bearing = new Bearing(layout, 0, 2);
434 lat = new Lat(layout, 0, 3);
435 lon = new Lon(layout, 2, 3);
437 apogee = new Apogee(layout, 4);
438 main = new Main(layout, 5);