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