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