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