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