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