Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / altosui / AltosDescent.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package AltosUI;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.LinkedBlockingQueue;
30 import org.altusmetrum.AltosLib.*;
31
32 public class AltosDescent extends JComponent implements AltosFlightDisplay {
33         GridBagLayout   layout;
34
35         public abstract class DescentStatus {
36                 JLabel          label;
37                 JTextField      value;
38                 AltosLights     lights;
39
40                 abstract void show(AltosState state, int crc_errors);
41
42                 void show() {
43                         label.setVisible(true);
44                         value.setVisible(true);
45                         lights.setVisible(true);
46                 }
47
48                 void hide() {
49                         label.setVisible(false);
50                         value.setVisible(false);
51                         lights.setVisible(false);
52                 }
53
54                 void reset() {
55                         value.setText("");
56                         lights.set(false);
57                 }
58
59                 void set_font() {
60                         label.setFont(Altos.label_font);
61                         value.setFont(Altos.value_font);
62                 }
63
64                 public DescentStatus (GridBagLayout layout, int y, String text) {
65                         GridBagConstraints      c = new GridBagConstraints();
66                         c.weighty = 1;
67
68                         lights = new AltosLights();
69                         c.gridx = 0; c.gridy = y;
70                         c.anchor = GridBagConstraints.CENTER;
71                         c.fill = GridBagConstraints.VERTICAL;
72                         c.weightx = 0;
73                         layout.setConstraints(lights, c);
74                         add(lights);
75
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;
83                         c.gridwidth = 3;
84                         c.weightx = 0;
85                         layout.setConstraints(label, c);
86                         add(label);
87
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;
92                         c.gridwidth = 1;
93                         c.anchor = GridBagConstraints.WEST;
94                         c.fill = GridBagConstraints.BOTH;
95                         c.weightx = 1;
96                         layout.setConstraints(value, c);
97                         add(value);
98
99                 }
100         }
101
102         public abstract class DescentValue {
103                 JLabel          label;
104                 JTextField      value;
105
106                 void reset() {
107                         value.setText("");
108                 }
109
110                 abstract void show(AltosState state, int crc_errors);
111
112                 void show() {
113                         label.setVisible(true);
114                         value.setVisible(true);
115                 }
116
117                 void hide() {
118                         label.setVisible(false);
119                         value.setVisible(false);
120                 }
121
122                 void show(AltosUnits units, double v) {
123                         value.setText(units.show(8, v));
124                 }
125
126                 void show(String format, double v) {
127                         value.setText(String.format(format, v));
128                 }
129
130                 void show(String v) {
131                         value.setText(v);
132                 }
133
134                 void set_font() {
135                         label.setFont(Altos.label_font);
136                         value.setFont(Altos.value_font);
137                 }
138
139                 public DescentValue (GridBagLayout layout, int x, int y, String text) {
140                         GridBagConstraints      c = new GridBagConstraints();
141                         c.weighty = 1;
142
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;
150                         c.weightx = 0;
151                         add(label, c);
152
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;
157                         c.gridwidth = 1;
158                         c.anchor = GridBagConstraints.WEST;
159                         c.fill = GridBagConstraints.BOTH;
160                         c.weightx = 1;
161                         add(value, c);
162                 }
163         }
164
165         public abstract class DescentDualValue {
166                 JLabel          label;
167                 JTextField      value1;
168                 JTextField      value2;
169
170                 void reset() {
171                         value1.setText("");
172                         value2.setText("");
173                 }
174
175                 void show() {
176                         label.setVisible(true);
177                         value1.setVisible(true);
178                         value2.setVisible(true);
179                 }
180
181                 void hide() {
182                         label.setVisible(false);
183                         value1.setVisible(false);
184                         value2.setVisible(false);
185                 }
186
187                 void set_font() {
188                         label.setFont(Altos.label_font);
189                         value1.setFont(Altos.value_font);
190                         value2.setFont(Altos.value_font);
191                 }
192
193                 abstract void show(AltosState state, int crc_errors);
194
195                 void show(String v1, String v2) {
196                         show();
197                         value1.setText(v1);
198                         value2.setText(v2);
199                 }
200                 void show(String f1, double v1, String f2, double v2) {
201                         show();
202                         value1.setText(String.format(f1, v1));
203                         value2.setText(String.format(f2, v2));
204                 }
205
206                 public DescentDualValue (GridBagLayout layout, int x, int y, String text) {
207                         GridBagConstraints      c = new GridBagConstraints();
208                         c.weighty = 1;
209
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;
217                         c.weightx = 0;
218                         layout.setConstraints(label, c);
219                         add(label);
220
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;
227                         c.weightx = 1;
228                         layout.setConstraints(value1, c);
229                         add(value1);
230
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;
237                         c.weightx = 1;
238                         c.gridwidth = 1;
239                         layout.setConstraints(value2, c);
240                         add(value2);
241                 }
242         }
243
244         class Height extends DescentValue {
245                 void show (AltosState state, int crc_errors) {
246                         show(AltosConvert.height, state.height);
247                 }
248                 public Height (GridBagLayout layout, int x, int y) {
249                         super (layout, x, y, "Height");
250                 }
251         }
252
253         Height  height;
254
255         class Speed extends DescentValue {
256                 void show (AltosState state, int crc_errors) {
257                         double speed = state.speed;
258                         if (!state.ascent)
259                                 speed = state.baro_speed;
260                         show(AltosConvert.speed, speed);
261                 }
262                 public Speed (GridBagLayout layout, int x, int y) {
263                         super (layout, x, y, "Speed");
264                 }
265         }
266
267         Speed   speed;
268
269         String pos(double p, String pos, String neg) {
270                 String  h = pos;
271                 if (p < 0) {
272                         h = neg;
273                         p = -p;
274                 }
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);
278         }
279
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"));
284                         else
285                                 show("???");
286                 }
287                 public Lat (GridBagLayout layout, int x, int y) {
288                         super (layout, x, y, "Latitude");
289                 }
290         }
291
292         Lat lat;
293
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"));
298                         else
299                                 show("???");
300                 }
301                 public Lon (GridBagLayout layout, int x, int y) {
302                         super (layout, x, y, "Longitude");
303                 }
304         }
305
306         Lon lon;
307
308         class Apogee extends DescentStatus {
309                 void show (AltosState state, int crc_errors) {
310                         show();
311                         value.setText(String.format("%4.2f V", state.drogue_sense));
312                         lights.set(state.drogue_sense > 3.2);
313                 }
314                 public Apogee (GridBagLayout layout, int y) {
315                         super(layout, y, "Apogee Igniter Voltage");
316                 }
317         }
318
319         Apogee apogee;
320
321         class Main extends DescentStatus {
322                 void show (AltosState state, int crc_errors) {
323                         show();
324                         value.setText(String.format("%4.2f V", state.main_sense));
325                         lights.set(state.main_sense > 3.2);
326                 }
327                 public Main (GridBagLayout layout, int y) {
328                         super(layout, y, "Main Igniter Voltage");
329                 }
330         }
331
332         Main main;
333
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));
340                         } else {
341                                 show("???", "???");
342                         }
343                 }
344                 public Bearing (GridBagLayout layout, int x, int y) {
345                         super (layout, x, y, "Bearing");
346                 }
347         }
348
349         Bearing bearing;
350
351         class Range extends DescentValue {
352                 void show (AltosState state, int crc_errors) {
353                         show(AltosConvert.distance, state.range);
354                 }
355                 public Range (GridBagLayout layout, int x, int y) {
356                         super (layout, x, y, "Range");
357                 }
358         }
359
360         Range range;
361
362         class Elevation extends DescentValue {
363                 void show (AltosState state, int crc_errors) {
364                         show("%3.0f°", state.elevation);
365                 }
366                 public Elevation (GridBagLayout layout, int x, int y) {
367                         super (layout, x, y, "Elevation");
368                 }
369         }
370
371         Elevation elevation;
372
373         public void reset() {
374                 lat.reset();
375                 lon.reset();
376                 height.reset();
377                 speed.reset();
378                 bearing.reset();
379                 range.reset();
380                 elevation.reset();
381                 main.reset();
382                 apogee.reset();
383         }
384
385         public void set_font() {
386                 lat.set_font();
387                 lon.set_font();
388                 height.set_font();
389                 speed.set_font();
390                 bearing.set_font();
391                 range.set_font();
392                 elevation.set_font();
393                 main.set_font();
394                 apogee.set_font();
395         }
396
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);
406                 } else {
407                         bearing.hide();
408                         range.hide();
409                         elevation.hide();
410                         lat.hide();
411                         lon.hide();
412                 }
413                 if (state.main_sense != AltosRecord.MISSING)
414                         main.show(state, crc_errors);
415                 else
416                         main.hide();
417                 if (state.drogue_sense != AltosRecord.MISSING)
418                         apogee.show(state, crc_errors);
419                 else
420                         apogee.hide();
421         }
422
423         public AltosDescent() {
424                 layout = new GridBagLayout();
425
426                 setLayout(layout);
427
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);
436
437                 apogee = new Apogee(layout, 4);
438                 main = new Main(layout, 5);
439         }
440 }