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