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