altos: Support staging by going back to boost as needed
[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
31 public class AltosDescent extends JComponent implements AltosFlightDisplay {
32         GridBagLayout   layout;
33
34         public abstract class DescentStatus {
35                 JLabel          label;
36                 JTextField      value;
37                 AltosLights     lights;
38
39                 abstract void show(AltosState state, int crc_errors);
40
41                 void show() {
42                         label.setVisible(true);
43                         value.setVisible(true);
44                         lights.setVisible(true);
45                 }
46
47                 void hide() {
48                         label.setVisible(false);
49                         value.setVisible(false);
50                         lights.setVisible(false);
51                 }
52
53                 void reset() {
54                         value.setText("");
55                         lights.set(false);
56                 }
57
58                 void set_font() {
59                         label.setFont(Altos.label_font);
60                         value.setFont(Altos.value_font);
61                 }
62
63                 public DescentStatus (GridBagLayout layout, int y, String text) {
64                         GridBagConstraints      c = new GridBagConstraints();
65                         c.weighty = 1;
66
67                         lights = new AltosLights();
68                         c.gridx = 0; c.gridy = y;
69                         c.anchor = GridBagConstraints.CENTER;
70                         c.fill = GridBagConstraints.VERTICAL;
71                         c.weightx = 0;
72                         layout.setConstraints(lights, c);
73                         add(lights);
74
75                         label = new JLabel(text);
76                         label.setFont(Altos.label_font);
77                         label.setHorizontalAlignment(SwingConstants.LEFT);
78                         c.gridx = 1; c.gridy = y;
79                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
80                         c.anchor = GridBagConstraints.WEST;
81                         c.fill = GridBagConstraints.VERTICAL;
82                         c.gridwidth = 3;
83                         c.weightx = 0;
84                         layout.setConstraints(label, c);
85                         add(label);
86
87                         value = new JTextField(Altos.text_width);
88                         value.setFont(Altos.value_font);
89                         value.setHorizontalAlignment(SwingConstants.RIGHT);
90                         c.gridx = 4; c.gridy = y;
91                         c.gridwidth = 1;
92                         c.anchor = GridBagConstraints.WEST;
93                         c.fill = GridBagConstraints.BOTH;
94                         c.weightx = 1;
95                         layout.setConstraints(value, c);
96                         add(value);
97
98                 }
99         }
100
101         public abstract class DescentValue {
102                 JLabel          label;
103                 JTextField      value;
104
105                 void reset() {
106                         value.setText("");
107                 }
108
109                 abstract void show(AltosState state, int crc_errors);
110
111                 void show() {
112                         label.setVisible(true);
113                         value.setVisible(true);
114                 }
115
116                 void hide() {
117                         label.setVisible(false);
118                         value.setVisible(false);
119                 }
120
121                 void show(String format, double v) {
122                         value.setText(String.format(format, v));
123                 }
124
125                 void show(String v) {
126                         value.setText(v);
127                 }
128
129                 void set_font() {
130                         label.setFont(Altos.label_font);
131                         value.setFont(Altos.value_font);
132                 }
133
134                 public DescentValue (GridBagLayout layout, int x, int y, String text) {
135                         GridBagConstraints      c = new GridBagConstraints();
136                         c.weighty = 1;
137
138                         label = new JLabel(text);
139                         label.setFont(Altos.label_font);
140                         label.setHorizontalAlignment(SwingConstants.LEFT);
141                         c.gridx = x + 1; c.gridy = y;
142                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
143                         c.anchor = GridBagConstraints.WEST;
144                         c.fill = GridBagConstraints.VERTICAL;
145                         c.weightx = 0;
146                         add(label, c);
147
148                         value = new JTextField(Altos.text_width);
149                         value.setFont(Altos.value_font);
150                         value.setHorizontalAlignment(SwingConstants.RIGHT);
151                         c.gridx = x + 2; c.gridy = y;
152                         c.gridwidth = 1;
153                         c.anchor = GridBagConstraints.WEST;
154                         c.fill = GridBagConstraints.BOTH;
155                         c.weightx = 1;
156                         add(value, c);
157                 }
158         }
159
160         public abstract class DescentDualValue {
161                 JLabel          label;
162                 JTextField      value1;
163                 JTextField      value2;
164
165                 void reset() {
166                         value1.setText("");
167                         value2.setText("");
168                 }
169
170                 void show() {
171                         label.setVisible(true);
172                         value1.setVisible(true);
173                         value2.setVisible(true);
174                 }
175
176                 void hide() {
177                         label.setVisible(false);
178                         value1.setVisible(false);
179                         value2.setVisible(false);
180                 }
181
182                 void set_font() {
183                         label.setFont(Altos.label_font);
184                         value1.setFont(Altos.value_font);
185                         value2.setFont(Altos.value_font);
186                 }
187
188                 abstract void show(AltosState state, int crc_errors);
189
190                 void show(String v1, String v2) {
191                         show();
192                         value1.setText(v1);
193                         value2.setText(v2);
194                 }
195                 void show(String f1, double v1, String f2, double v2) {
196                         show();
197                         value1.setText(String.format(f1, v1));
198                         value2.setText(String.format(f2, v2));
199                 }
200
201                 public DescentDualValue (GridBagLayout layout, int x, int y, String text) {
202                         GridBagConstraints      c = new GridBagConstraints();
203                         c.weighty = 1;
204
205                         label = new JLabel(text);
206                         label.setFont(Altos.label_font);
207                         label.setHorizontalAlignment(SwingConstants.LEFT);
208                         c.gridx = x + 1; c.gridy = y;
209                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
210                         c.anchor = GridBagConstraints.WEST;
211                         c.fill = GridBagConstraints.VERTICAL;
212                         c.weightx = 0;
213                         layout.setConstraints(label, c);
214                         add(label);
215
216                         value1 = new JTextField(Altos.text_width);
217                         value1.setFont(Altos.value_font);
218                         value1.setHorizontalAlignment(SwingConstants.RIGHT);
219                         c.gridx = x + 2; c.gridy = y;
220                         c.anchor = GridBagConstraints.WEST;
221                         c.fill = GridBagConstraints.BOTH;
222                         c.weightx = 1;
223                         layout.setConstraints(value1, c);
224                         add(value1);
225
226                         value2 = new JTextField(Altos.text_width);
227                         value2.setFont(Altos.value_font);
228                         value2.setHorizontalAlignment(SwingConstants.RIGHT);
229                         c.gridx = x + 4; c.gridy = y;
230                         c.anchor = GridBagConstraints.WEST;
231                         c.fill = GridBagConstraints.BOTH;
232                         c.weightx = 1;
233                         c.gridwidth = 1;
234                         layout.setConstraints(value2, c);
235                         add(value2);
236                 }
237         }
238
239         class Height extends DescentValue {
240                 void show (AltosState state, int crc_errors) {
241                         show("%6.0f m", state.height);
242                 }
243                 public Height (GridBagLayout layout, int x, int y) {
244                         super (layout, x, y, "Height");
245                 }
246         }
247
248         Height  height;
249
250         class Speed extends DescentValue {
251                 void show (AltosState state, int crc_errors) {
252                         double speed = state.speed;
253                         if (!state.ascent)
254                                 speed = state.baro_speed;
255                         show("%6.0f m/s", speed);
256                 }
257                 public Speed (GridBagLayout layout, int x, int y) {
258                         super (layout, x, y, "Speed");
259                 }
260         }
261
262         Speed   speed;
263
264         String pos(double p, String pos, String neg) {
265                 String  h = pos;
266                 if (p < 0) {
267                         h = neg;
268                         p = -p;
269                 }
270                 int deg = (int) Math.floor(p);
271                 double min = (p - Math.floor(p)) * 60.0;
272                 return String.format("%s %d° %9.6f", h, deg, min);
273         }
274
275         class Lat extends DescentValue {
276                 void show (AltosState state, int crc_errors) {
277                         if (state.gps != null && state.gps.connected)
278                                 show(pos(state.gps.lat,"N", "S"));
279                         else
280                                 show("???");
281                 }
282                 public Lat (GridBagLayout layout, int x, int y) {
283                         super (layout, x, y, "Latitude");
284                 }
285         }
286
287         Lat lat;
288
289         class Lon extends DescentValue {
290                 void show (AltosState state, int crc_errors) {
291                         if (state.gps != null && state.gps.connected)
292                                 show(pos(state.gps.lon,"W", "E"));
293                         else
294                                 show("???");
295                 }
296                 public Lon (GridBagLayout layout, int x, int y) {
297                         super (layout, x, y, "Longitude");
298                 }
299         }
300
301         Lon lon;
302
303         class Apogee extends DescentStatus {
304                 void show (AltosState state, int crc_errors) {
305                         show();
306                         value.setText(String.format("%4.2f V", state.drogue_sense));
307                         lights.set(state.drogue_sense > 3.2);
308                 }
309                 public Apogee (GridBagLayout layout, int y) {
310                         super(layout, y, "Apogee Igniter Voltage");
311                 }
312         }
313
314         Apogee apogee;
315
316         class Main extends DescentStatus {
317                 void show (AltosState state, int crc_errors) {
318                         show();
319                         value.setText(String.format("%4.2f V", state.main_sense));
320                         lights.set(state.main_sense > 3.2);
321                 }
322                 public Main (GridBagLayout layout, int y) {
323                         super(layout, y, "Main Igniter Voltage");
324                 }
325         }
326
327         Main main;
328
329         class Bearing extends DescentDualValue {
330                 void show (AltosState state, int crc_errors) {
331                         if (state.from_pad != null) {
332                                 show( String.format("%3.0f°", state.from_pad.bearing),
333                                       state.from_pad.bearing_words(
334                                               AltosGreatCircle.BEARING_LONG));
335                         } else {
336                                 show("???", "???");
337                         }
338                 }
339                 public Bearing (GridBagLayout layout, int x, int y) {
340                         super (layout, x, y, "Bearing");
341                 }
342         }
343
344         Bearing bearing;
345
346         class Range extends DescentValue {
347                 void show (AltosState state, int crc_errors) {
348                         show("%6.0f m", state.range);
349                 }
350                 public Range (GridBagLayout layout, int x, int y) {
351                         super (layout, x, y, "Range");
352                 }
353         }
354
355         Range range;
356
357         class Elevation extends DescentValue {
358                 void show (AltosState state, int crc_errors) {
359                         show("%3.0f°", state.elevation);
360                 }
361                 public Elevation (GridBagLayout layout, int x, int y) {
362                         super (layout, x, y, "Elevation");
363                 }
364         }
365
366         Elevation elevation;
367
368         public void reset() {
369                 lat.reset();
370                 lon.reset();
371                 height.reset();
372                 speed.reset();
373                 bearing.reset();
374                 range.reset();
375                 elevation.reset();
376                 main.reset();
377                 apogee.reset();
378         }
379
380         public void set_font() {
381                 lat.set_font();
382                 lon.set_font();
383                 height.set_font();
384                 speed.set_font();
385                 bearing.set_font();
386                 range.set_font();
387                 elevation.set_font();
388                 main.set_font();
389                 apogee.set_font();
390         }
391
392         public void show(AltosState state, int crc_errors) {
393                 height.show(state, crc_errors);
394                 speed.show(state, crc_errors);
395                 if (state.gps != null && state.gps.connected) {
396                         bearing.show(state, crc_errors);
397                         range.show(state, crc_errors);
398                         elevation.show(state, crc_errors);
399                         lat.show(state, crc_errors);
400                         lon.show(state, crc_errors);
401                 } else {
402                         bearing.hide();
403                         range.hide();
404                         elevation.hide();
405                         lat.hide();
406                         lon.hide();
407                 }
408                 if (state.main_sense != AltosRecord.MISSING)
409                         main.show(state, crc_errors);
410                 else
411                         main.hide();
412                 if (state.drogue_sense != AltosRecord.MISSING)
413                         apogee.show(state, crc_errors);
414                 else
415                         apogee.hide();
416         }
417
418         public AltosDescent() {
419                 layout = new GridBagLayout();
420
421                 setLayout(layout);
422
423                 /* Elements in descent display */
424                 speed = new Speed(layout, 0, 0);
425                 height = new Height(layout, 2, 0);
426                 elevation = new Elevation(layout, 0, 1);
427                 range = new Range(layout, 2, 1);
428                 bearing = new Bearing(layout, 0, 2);
429                 lat = new Lat(layout, 0, 3);
430                 lon = new Lon(layout, 2, 3);
431
432                 apogee = new Apogee(layout, 4);
433                 main = new Main(layout, 5);
434         }
435 }