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