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