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