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