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