664c5ea662ec1439d58cada5a60336325f7a844c
[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(String format, double v) {
123                         value.setText(String.format(format, v));
124                 }
125
126                 void show(String v) {
127                         value.setText(v);
128                 }
129
130                 void set_font() {
131                         label.setFont(Altos.label_font);
132                         value.setFont(Altos.value_font);
133                 }
134
135                 public DescentValue (GridBagLayout layout, int x, int y, String text) {
136                         GridBagConstraints      c = new GridBagConstraints();
137                         c.weighty = 1;
138
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;
146                         c.weightx = 0;
147                         add(label, c);
148
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;
153                         c.gridwidth = 1;
154                         c.anchor = GridBagConstraints.WEST;
155                         c.fill = GridBagConstraints.BOTH;
156                         c.weightx = 1;
157                         add(value, c);
158                 }
159         }
160
161         public abstract class DescentDualValue {
162                 JLabel          label;
163                 JTextField      value1;
164                 JTextField      value2;
165
166                 void reset() {
167                         value1.setText("");
168                         value2.setText("");
169                 }
170
171                 void show() {
172                         label.setVisible(true);
173                         value1.setVisible(true);
174                         value2.setVisible(true);
175                 }
176
177                 void hide() {
178                         label.setVisible(false);
179                         value1.setVisible(false);
180                         value2.setVisible(false);
181                 }
182
183                 void set_font() {
184                         label.setFont(Altos.label_font);
185                         value1.setFont(Altos.value_font);
186                         value2.setFont(Altos.value_font);
187                 }
188
189                 abstract void show(AltosState state, int crc_errors);
190
191                 void show(String v1, String v2) {
192                         show();
193                         value1.setText(v1);
194                         value2.setText(v2);
195                 }
196                 void show(String f1, double v1, String f2, double v2) {
197                         show();
198                         value1.setText(String.format(f1, v1));
199                         value2.setText(String.format(f2, v2));
200                 }
201
202                 public DescentDualValue (GridBagLayout layout, int x, int y, String text) {
203                         GridBagConstraints      c = new GridBagConstraints();
204                         c.weighty = 1;
205
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;
213                         c.weightx = 0;
214                         layout.setConstraints(label, c);
215                         add(label);
216
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;
223                         c.weightx = 1;
224                         layout.setConstraints(value1, c);
225                         add(value1);
226
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;
233                         c.weightx = 1;
234                         c.gridwidth = 1;
235                         layout.setConstraints(value2, c);
236                         add(value2);
237                 }
238         }
239
240         class Height extends DescentValue {
241                 void show (AltosState state, int crc_errors) {
242                         show("%6.0f m", state.height);
243                 }
244                 public Height (GridBagLayout layout, int x, int y) {
245                         super (layout, x, y, "Height");
246                 }
247         }
248
249         Height  height;
250
251         class Speed extends DescentValue {
252                 void show (AltosState state, int crc_errors) {
253                         double speed = state.speed;
254                         if (!state.ascent)
255                                 speed = state.baro_speed;
256                         show("%6.0f m/s", speed);
257                 }
258                 public Speed (GridBagLayout layout, int x, int y) {
259                         super (layout, x, y, "Speed");
260                 }
261         }
262
263         Speed   speed;
264
265         String pos(double p, String pos, String neg) {
266                 String  h = pos;
267                 if (p < 0) {
268                         h = neg;
269                         p = -p;
270                 }
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);
274         }
275
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"));
280                         else
281                                 show("???");
282                 }
283                 public Lat (GridBagLayout layout, int x, int y) {
284                         super (layout, x, y, "Latitude");
285                 }
286         }
287
288         Lat lat;
289
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"));
294                         else
295                                 show("???");
296                 }
297                 public Lon (GridBagLayout layout, int x, int y) {
298                         super (layout, x, y, "Longitude");
299                 }
300         }
301
302         Lon lon;
303
304         class Apogee extends DescentStatus {
305                 void show (AltosState state, int crc_errors) {
306                         show();
307                         value.setText(String.format("%4.2f V", state.drogue_sense));
308                         lights.set(state.drogue_sense > 3.2);
309                 }
310                 public Apogee (GridBagLayout layout, int y) {
311                         super(layout, y, "Apogee Igniter Voltage");
312                 }
313         }
314
315         Apogee apogee;
316
317         class Main extends DescentStatus {
318                 void show (AltosState state, int crc_errors) {
319                         show();
320                         value.setText(String.format("%4.2f V", state.main_sense));
321                         lights.set(state.main_sense > 3.2);
322                 }
323                 public Main (GridBagLayout layout, int y) {
324                         super(layout, y, "Main Igniter Voltage");
325                 }
326         }
327
328         Main main;
329
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));
336                         } else {
337                                 show("???", "???");
338                         }
339                 }
340                 public Bearing (GridBagLayout layout, int x, int y) {
341                         super (layout, x, y, "Bearing");
342                 }
343         }
344
345         Bearing bearing;
346
347         class Range extends DescentValue {
348                 void show (AltosState state, int crc_errors) {
349                         show("%6.0f m", state.range);
350                 }
351                 public Range (GridBagLayout layout, int x, int y) {
352                         super (layout, x, y, "Range");
353                 }
354         }
355
356         Range range;
357
358         class Elevation extends DescentValue {
359                 void show (AltosState state, int crc_errors) {
360                         show("%3.0f°", state.elevation);
361                 }
362                 public Elevation (GridBagLayout layout, int x, int y) {
363                         super (layout, x, y, "Elevation");
364                 }
365         }
366
367         Elevation elevation;
368
369         public void reset() {
370                 lat.reset();
371                 lon.reset();
372                 height.reset();
373                 speed.reset();
374                 bearing.reset();
375                 range.reset();
376                 elevation.reset();
377                 main.reset();
378                 apogee.reset();
379         }
380
381         public void set_font() {
382                 lat.set_font();
383                 lon.set_font();
384                 height.set_font();
385                 speed.set_font();
386                 bearing.set_font();
387                 range.set_font();
388                 elevation.set_font();
389                 main.set_font();
390                 apogee.set_font();
391         }
392
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);
402                 } else {
403                         bearing.hide();
404                         range.hide();
405                         elevation.hide();
406                         lat.hide();
407                         lon.hide();
408                 }
409                 if (state.main_sense != AltosRecord.MISSING)
410                         main.show(state, crc_errors);
411                 else
412                         main.hide();
413                 if (state.drogue_sense != AltosRecord.MISSING)
414                         apogee.show(state, crc_errors);
415                 else
416                         apogee.hide();
417         }
418
419         public AltosDescent() {
420                 layout = new GridBagLayout();
421
422                 setLayout(layout);
423
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);
432
433                 apogee = new Apogee(layout, 4);
434                 main = new Main(layout, 5);
435         }
436 }