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(String format, double v) {
123 value.setText(String.format(format, v));
126 void show(String v) {
131 label.setFont(Altos.label_font);
132 value.setFont(Altos.value_font);
135 public DescentValue (GridBagLayout layout, int x, int y, String text) {
136 GridBagConstraints c = new GridBagConstraints();
139 label = new JLabel(text);
140 label.setFont(Altos.label_font);
141 label.setHorizontalAlignment(SwingConstants.LEFT);
142 c.gridx = x + 1; c.gridy = y;
143 c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
144 c.anchor = GridBagConstraints.WEST;
145 c.fill = GridBagConstraints.VERTICAL;
149 value = new JTextField(Altos.text_width);
150 value.setFont(Altos.value_font);
151 value.setHorizontalAlignment(SwingConstants.RIGHT);
152 c.gridx = x + 2; c.gridy = y;
154 c.anchor = GridBagConstraints.WEST;
155 c.fill = GridBagConstraints.BOTH;
161 public abstract class DescentDualValue {
172 label.setVisible(true);
173 value1.setVisible(true);
174 value2.setVisible(true);
178 label.setVisible(false);
179 value1.setVisible(false);
180 value2.setVisible(false);
184 label.setFont(Altos.label_font);
185 value1.setFont(Altos.value_font);
186 value2.setFont(Altos.value_font);
189 abstract void show(AltosState state, int crc_errors);
191 void show(String v1, String v2) {
196 void show(String f1, double v1, String f2, double v2) {
198 value1.setText(String.format(f1, v1));
199 value2.setText(String.format(f2, v2));
202 public DescentDualValue (GridBagLayout layout, int x, int y, String text) {
203 GridBagConstraints c = new GridBagConstraints();
206 label = new JLabel(text);
207 label.setFont(Altos.label_font);
208 label.setHorizontalAlignment(SwingConstants.LEFT);
209 c.gridx = x + 1; c.gridy = y;
210 c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
211 c.anchor = GridBagConstraints.WEST;
212 c.fill = GridBagConstraints.VERTICAL;
214 layout.setConstraints(label, c);
217 value1 = new JTextField(Altos.text_width);
218 value1.setFont(Altos.value_font);
219 value1.setHorizontalAlignment(SwingConstants.RIGHT);
220 c.gridx = x + 2; c.gridy = y;
221 c.anchor = GridBagConstraints.WEST;
222 c.fill = GridBagConstraints.BOTH;
224 layout.setConstraints(value1, c);
227 value2 = new JTextField(Altos.text_width);
228 value2.setFont(Altos.value_font);
229 value2.setHorizontalAlignment(SwingConstants.RIGHT);
230 c.gridx = x + 4; c.gridy = y;
231 c.anchor = GridBagConstraints.WEST;
232 c.fill = GridBagConstraints.BOTH;
235 layout.setConstraints(value2, c);
240 class Height extends DescentValue {
241 void show (AltosState state, int crc_errors) {
242 show("%6.0f m", state.height);
244 public Height (GridBagLayout layout, int x, int y) {
245 super (layout, x, y, "Height");
251 class Speed extends DescentValue {
252 void show (AltosState state, int crc_errors) {
253 double speed = state.speed;
255 speed = state.baro_speed;
256 show("%6.0f m/s", speed);
258 public Speed (GridBagLayout layout, int x, int y) {
259 super (layout, x, y, "Speed");
265 String pos(double p, String pos, String neg) {
271 int deg = (int) Math.floor(p);
272 double min = (p - Math.floor(p)) * 60.0;
273 return String.format("%s %d° %9.6f", h, deg, min);
276 class Lat extends DescentValue {
277 void show (AltosState state, int crc_errors) {
278 if (state.gps != null && state.gps.connected)
279 show(pos(state.gps.lat,"N", "S"));
283 public Lat (GridBagLayout layout, int x, int y) {
284 super (layout, x, y, "Latitude");
290 class Lon extends DescentValue {
291 void show (AltosState state, int crc_errors) {
292 if (state.gps != null && state.gps.connected)
293 show(pos(state.gps.lon,"W", "E"));
297 public Lon (GridBagLayout layout, int x, int y) {
298 super (layout, x, y, "Longitude");
304 class Apogee extends DescentStatus {
305 void show (AltosState state, int crc_errors) {
307 value.setText(String.format("%4.2f V", state.drogue_sense));
308 lights.set(state.drogue_sense > 3.2);
310 public Apogee (GridBagLayout layout, int y) {
311 super(layout, y, "Apogee Igniter Voltage");
317 class Main extends DescentStatus {
318 void show (AltosState state, int crc_errors) {
320 value.setText(String.format("%4.2f V", state.main_sense));
321 lights.set(state.main_sense > 3.2);
323 public Main (GridBagLayout layout, int y) {
324 super(layout, y, "Main Igniter Voltage");
330 class Bearing extends DescentDualValue {
331 void show (AltosState state, int crc_errors) {
332 if (state.from_pad != null) {
333 show( String.format("%3.0f°", state.from_pad.bearing),
334 state.from_pad.bearing_words(
335 AltosGreatCircle.BEARING_LONG));
340 public Bearing (GridBagLayout layout, int x, int y) {
341 super (layout, x, y, "Bearing");
347 class Range extends DescentValue {
348 void show (AltosState state, int crc_errors) {
349 show("%6.0f m", state.range);
351 public Range (GridBagLayout layout, int x, int y) {
352 super (layout, x, y, "Range");
358 class Elevation extends DescentValue {
359 void show (AltosState state, int crc_errors) {
360 show("%3.0f°", state.elevation);
362 public Elevation (GridBagLayout layout, int x, int y) {
363 super (layout, x, y, "Elevation");
369 public void reset() {
381 public void set_font() {
388 elevation.set_font();
393 public void show(AltosState state, int crc_errors) {
394 height.show(state, crc_errors);
395 speed.show(state, crc_errors);
396 if (state.gps != null && state.gps.connected) {
397 bearing.show(state, crc_errors);
398 range.show(state, crc_errors);
399 elevation.show(state, crc_errors);
400 lat.show(state, crc_errors);
401 lon.show(state, crc_errors);
409 if (state.main_sense != AltosRecord.MISSING)
410 main.show(state, crc_errors);
413 if (state.drogue_sense != AltosRecord.MISSING)
414 apogee.show(state, crc_errors);
419 public AltosDescent() {
420 layout = new GridBagLayout();
424 /* Elements in descent display */
425 speed = new Speed(layout, 0, 0);
426 height = new Height(layout, 2, 0);
427 elevation = new Elevation(layout, 0, 1);
428 range = new Range(layout, 2, 1);
429 bearing = new Bearing(layout, 0, 2);
430 lat = new Lat(layout, 0, 3);
431 lon = new Lon(layout, 2, 3);
433 apogee = new Apogee(layout, 4);
434 main = new Main(layout, 5);