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