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