Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / 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         JLabel                  cur, max;
34
35         public class AscentStatus {
36                 JLabel          label;
37                 JTextField      value;
38                 AltosLights     lights;
39
40                 void show() {
41                         value.setVisible(true);
42                         lights.setVisible(true);
43                         label.setVisible(true);
44                 }
45
46                 void hide() {
47                         value.setVisible(false);
48                         lights.setVisible(false);
49                         label.setVisible(false);
50                 }
51
52                 void show(AltosState state, int crc_errors) {}
53                 void reset() {
54                         value.setText("");
55                         lights.set(false);
56                 }
57
58                 void set_font() {
59                         label.setFont(Altos.label_font);
60                         value.setFont(Altos.value_font);
61                 }
62
63                 public AscentStatus (GridBagLayout layout, int y, String text) {
64                         GridBagConstraints      c = new GridBagConstraints();
65                         c.weighty = 1;
66
67                         lights = new AltosLights();
68                         c.gridx = 0; c.gridy = y;
69                         c.anchor = GridBagConstraints.CENTER;
70                         c.fill = GridBagConstraints.VERTICAL;
71                         c.weightx = 0;
72                         layout.setConstraints(lights, c);
73                         add(lights);
74
75                         label = new JLabel(text);
76                         label.setFont(Altos.label_font);
77                         label.setHorizontalAlignment(SwingConstants.LEFT);
78                         c.gridx = 1; c.gridy = y;
79                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
80                         c.anchor = GridBagConstraints.WEST;
81                         c.fill = GridBagConstraints.VERTICAL;
82                         c.weightx = 0;
83                         layout.setConstraints(label, c);
84                         add(label);
85
86                         value = new JTextField(Altos.text_width);
87                         value.setFont(Altos.value_font);
88                         value.setHorizontalAlignment(SwingConstants.RIGHT);
89                         c.gridx = 2; c.gridy = y;
90                         c.gridwidth = 2;
91                         c.anchor = GridBagConstraints.WEST;
92                         c.fill = GridBagConstraints.BOTH;
93                         c.weightx = 1;
94                         layout.setConstraints(value, c);
95                         add(value);
96
97                 }
98         }
99
100         public class AscentValue {
101                 JLabel          label;
102                 JTextField      value;
103                 void show(AltosState state, int crc_errors) {}
104
105                 void reset() {
106                         value.setText("");
107                 }
108
109                 void show() {
110                         label.setVisible(true);
111                         value.setVisible(true);
112                 }
113
114                 void hide() {
115                         label.setVisible(false);
116                         value.setVisible(false);
117                 }
118                 void set_font() {
119                         label.setFont(Altos.label_font);
120                         value.setFont(Altos.value_font);
121                 }
122
123                 public AscentValue (GridBagLayout layout, int y, String text) {
124                         GridBagConstraints      c = new GridBagConstraints();
125                         c.weighty = 1;
126
127                         label = new JLabel(text);
128                         label.setFont(Altos.label_font);
129                         label.setHorizontalAlignment(SwingConstants.LEFT);
130                         c.gridx = 1; c.gridy = y;
131                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
132                         c.anchor = GridBagConstraints.WEST;
133                         c.fill = GridBagConstraints.VERTICAL;
134                         c.weightx = 0;
135                         layout.setConstraints(label, c);
136                         add(label);
137
138                         value = new JTextField(Altos.text_width);
139                         value.setFont(Altos.value_font);
140                         value.setHorizontalAlignment(SwingConstants.RIGHT);
141                         c.gridx = 2; c.gridy = y;
142                         c.anchor = GridBagConstraints.WEST;
143                         c.fill = GridBagConstraints.BOTH;
144                         c.gridwidth = 2;
145                         c.weightx = 1;
146                         layout.setConstraints(value, c);
147                         add(value);
148                 }
149         }
150
151         public class AscentValueHold {
152                 JLabel          label;
153                 JTextField      value;
154                 JTextField      max_value;
155                 double          max;
156
157                 void show(AltosState state, int crc_errors) {}
158
159                 void reset() {
160                         value.setText("");
161                         max_value.setText("");
162                         max = AltosRecord.MISSING;
163                 }
164
165                 void set_font() {
166                         label.setFont(Altos.label_font);
167                         value.setFont(Altos.value_font);
168                         max_value.setFont(Altos.value_font);
169                 }
170
171                 void show(String format, double v) {
172                         if (v == AltosRecord.MISSING) {
173                                 value.setText("Missing");
174                                 max_value.setText("Missing");
175                         } else {
176                                 value.setText(String.format(format, v));
177                                 if (v > max || max == AltosRecord.MISSING) {
178                                         max_value.setText(String.format(format, v));
179                                         max = v;
180                                 }
181                         }
182                 }
183                 public AscentValueHold (GridBagLayout layout, int y, String text) {
184                         GridBagConstraints      c = new GridBagConstraints();
185                         c.weighty = 1;
186
187                         label = new JLabel(text);
188                         label.setFont(Altos.label_font);
189                         label.setHorizontalAlignment(SwingConstants.LEFT);
190                         c.gridx = 1; c.gridy = y;
191                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
192                         c.anchor = GridBagConstraints.WEST;
193                         c.fill = GridBagConstraints.VERTICAL;
194                         c.weightx = 0;
195                         layout.setConstraints(label, c);
196                         add(label);
197
198                         value = new JTextField(Altos.text_width);
199                         value.setFont(Altos.value_font);
200                         value.setHorizontalAlignment(SwingConstants.RIGHT);
201                         c.gridx = 2; c.gridy = y;
202                         c.anchor = GridBagConstraints.EAST;
203                         c.fill = GridBagConstraints.BOTH;
204                         c.weightx = 1;
205                         layout.setConstraints(value, c);
206                         add(value);
207
208                         max_value = new JTextField(Altos.text_width);
209                         max_value.setFont(Altos.value_font);
210                         max_value.setHorizontalAlignment(SwingConstants.RIGHT);
211                         c.gridx = 3; c.gridy = y;
212                         c.anchor = GridBagConstraints.EAST;
213                         c.fill = GridBagConstraints.BOTH;
214                         c.weightx = 1;
215                         layout.setConstraints(max_value, c);
216                         add(max_value);
217                 }
218         }
219
220
221         class Height extends AscentValueHold {
222                 void show (AltosState state, int crc_errors) {
223                         show("%6.0f m", state.height);
224                 }
225                 public Height (GridBagLayout layout, int y) {
226                         super (layout, y, "Height");
227                 }
228         }
229
230         Height  height;
231
232         class Speed extends AscentValueHold {
233                 void show (AltosState state, int crc_errors) {
234                         double speed = state.speed;
235                         if (!state.ascent)
236                                 speed = state.baro_speed;
237                         show("%6.0f m/s", speed);
238                 }
239                 public Speed (GridBagLayout layout, int y) {
240                         super (layout, y, "Speed");
241                 }
242         }
243
244         Speed   speed;
245
246         class Accel extends AscentValueHold {
247                 void show (AltosState state, int crc_errors) {
248                         show("%6.0f m/s²", state.acceleration);
249                 }
250                 public Accel (GridBagLayout layout, int y) {
251                         super (layout, y, "Acceleration");
252                 }
253         }
254
255         Accel   accel;
256
257         String pos(double p, String pos, String neg) {
258                 String  h = pos;
259                 if (p < 0) {
260                         h = neg;
261                         p = -p;
262                 }
263                 int deg = (int) Math.floor(p);
264                 double min = (p - Math.floor(p)) * 60.0;
265                 return String.format("%s %4d° %9.6f", h, deg, min);
266         }
267
268         class Apogee extends AscentStatus {
269                 void show (AltosState state, int crc_errors) {
270                         show();
271                         value.setText(String.format("%4.2f V", state.drogue_sense));
272                         lights.set(state.drogue_sense > 3.2);
273                 }
274                 public Apogee (GridBagLayout layout, int y) {
275                         super(layout, y, "Apogee Igniter Voltage");
276                 }
277         }
278
279         Apogee apogee;
280
281         class Main extends AscentStatus {
282                 void show (AltosState state, int crc_errors) {
283                         show();
284                         value.setText(String.format("%4.2f V", state.main_sense));
285                         lights.set(state.main_sense > 3.2);
286                 }
287                 public Main (GridBagLayout layout, int y) {
288                         super(layout, y, "Main Igniter Voltage");
289                 }
290         }
291
292         Main main;
293
294         class Lat extends AscentValue {
295                 void show (AltosState state, int crc_errors) {
296                         show();
297                         if (state.gps != null)
298                                 value.setText(pos(state.gps.lat,"N", "S"));
299                         else
300                                 value.setText("???");
301                 }
302                 public Lat (GridBagLayout layout, int y) {
303                         super (layout, y, "Latitude");
304                 }
305         }
306
307         Lat lat;
308
309         class Lon extends AscentValue {
310                 void show (AltosState state, int crc_errors) {
311                         show();
312                         if (state.gps != null)
313                                 value.setText(pos(state.gps.lon,"E", "W"));
314                         else
315                                 value.setText("???");
316                 }
317                 public Lon (GridBagLayout layout, int y) {
318                         super (layout, y, "Longitude");
319                 }
320         }
321
322         Lon lon;
323
324         public void reset() {
325                 lat.reset();
326                 lon.reset();
327                 main.reset();
328                 apogee.reset();
329                 height.reset();
330                 speed.reset();
331                 accel.reset();
332         }
333
334         public void set_font() {
335                 cur.setFont(Altos.label_font);
336                 max.setFont(Altos.label_font);
337                 lat.set_font();
338                 lon.set_font();
339                 main.set_font();
340                 apogee.set_font();
341                 height.set_font();
342                 speed.set_font();
343                 accel.set_font();
344         }
345
346         public void show(AltosState state, int crc_errors) {
347                 if (state.gps != null && state.gps.connected) {
348                         lat.show(state, crc_errors);
349                         lon.show(state, crc_errors);
350                 } else {
351                         lat.hide();
352                         lon.hide();
353                 }
354                 height.show(state, crc_errors);
355                 if (state.main_sense != AltosRecord.MISSING)
356                         main.show(state, crc_errors);
357                 else
358                         main.hide();
359                 if (state.drogue_sense != AltosRecord.MISSING)
360                         apogee.show(state, crc_errors);
361                 else
362                         apogee.hide();
363                 speed.show(state, crc_errors);
364                 accel.show(state, crc_errors);
365         }
366
367         public void labels(GridBagLayout layout, int y) {
368                 GridBagConstraints      c;
369
370                 cur = new JLabel("Current");
371                 cur.setFont(Altos.label_font);
372                 c = new GridBagConstraints();
373                 c.gridx = 2; c.gridy = y;
374                 c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
375                 layout.setConstraints(cur, c);
376                 add(cur);
377
378                 max = new JLabel("Maximum");
379                 max.setFont(Altos.label_font);
380                 c.gridx = 3; c.gridy = y;
381                 layout.setConstraints(max, c);
382                 add(max);
383         }
384
385         public AltosAscent() {
386                 layout = new GridBagLayout();
387
388                 setLayout(layout);
389
390                 /* Elements in ascent display:
391                  *
392                  * lat
393                  * lon
394                  * height
395                  */
396                 labels(layout, 0);
397                 height = new Height(layout, 1);
398                 speed = new Speed(layout, 2);
399                 accel = new Accel(layout, 3);
400                 lat = new Lat(layout, 4);
401                 lon = new Lon(layout, 5);
402                 apogee = new Apogee(layout, 6);
403                 main = new Main(layout, 7);
404         }
405 }