altosui/telegps: Reduce CPU time needed for flight displays
[fw/altos] / telegps / TeleGPSInfo.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 org.altusmetrum.telegps;
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 TeleGPSInfo extends JComponent implements AltosFlightDisplay, HierarchyListener {
27         GridBagLayout   layout;
28         JLabel                  cur, max;
29
30         private AltosState              last_state;
31         private AltosListenerState      last_listener_state;
32
33         public abstract class Info implements AltosFontListener, AltosUnitsListener {
34                 JLabel          label;
35                 JTextField      value;
36                 AltosLights     lights;
37                 double          v;
38                 AltosUnits      units;
39
40                 void show() {
41                         value.setVisible(true);
42                         lights.setVisible(true);
43                         label.setVisible(true);
44                 }
45
46                 void hide() {
47                         value.setVisible(false);
48                         lights.setVisible(false);
49                         label.setVisible(false);
50                 }
51
52                 abstract void show(AltosState state, AltosListenerState listener_state);
53
54                 void show(String s) {
55                         show();
56                         value.setText(s);
57                 }
58
59                 void show(double v) {
60                         this.v = v;
61                         show(units.show(8, v));
62                 }
63
64                 void show(String format, double v) {
65                         show(String.format(format, v));
66                 }
67
68                 void show(String format, int v) {
69                         show(String.format(format, v));
70                 }
71
72                 void reset() {
73                         lights.set(false);
74                         value.setText("");
75                 }
76
77                 public void font_size_changed(int font_size) {
78                         label.setFont(AltosUILib.label_font);
79                         value.setFont(AltosUILib.value_font);
80                 }
81
82                 public void units_changed(boolean imperial_units) {
83                         if (units != null)
84                                 show(v);
85                 }
86
87                 public Info (GridBagLayout layout, int y, AltosUnits units, String text) {
88                         this.units = units;
89
90                         GridBagConstraints      c = new GridBagConstraints();
91                         c.weighty = 1;
92
93                         lights = new AltosLights();
94                         c.gridx = 0; c.gridy = y;
95                         c.anchor = GridBagConstraints.CENTER;
96                         c.fill = GridBagConstraints.VERTICAL;
97                         c.weightx = 0;
98                         layout.setConstraints(lights, c);
99                         add(lights);
100
101                         label = new JLabel(text);
102                         label.setFont(AltosUILib.label_font);
103                         label.setHorizontalAlignment(SwingConstants.LEFT);
104                         c.gridx = 1; c.gridy = y;
105                         c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
106                         c.anchor = GridBagConstraints.WEST;
107                         c.fill = GridBagConstraints.VERTICAL;
108                         c.weightx = 0;
109                         layout.setConstraints(label, c);
110                         add(label);
111
112                         value = new JTextField(AltosUILib.text_width);
113                         value.setFont(AltosUILib.value_font);
114                         value.setHorizontalAlignment(SwingConstants.RIGHT);
115                         c.gridx = 2; c.gridy = y;
116                         c.gridwidth = 2;
117                         c.anchor = GridBagConstraints.WEST;
118                         c.fill = GridBagConstraints.BOTH;
119                         c.weightx = 1;
120                         layout.setConstraints(value, c);
121                         add(value);
122                 }
123
124                 public Info (GridBagLayout layout, int y, String text) {
125                         this(layout, y, null, text);
126                 }
127         }
128
129         public abstract class Value implements AltosFontListener, AltosUnitsListener {
130                 JLabel          label;
131                 JTextField      value;
132                 AltosUnits      units;
133                 double          v = AltosLib.MISSING;
134
135                 abstract void show(AltosState state, AltosListenerState listener_state);
136
137                 void reset() {
138                         value.setText("");
139                 }
140
141                 void show() {
142                         label.setVisible(true);
143                         value.setVisible(true);
144                 }
145
146                 void show(String s) {
147                         show();
148                         value.setText(s);
149                 }
150
151                 void show(double v) {
152                         this.v = v;
153                         show(units.show(8, v));
154                 }
155
156                 void show(String format, double v) {
157                         show(String.format(format, v));
158                 }
159
160                 void hide() {
161                         label.setVisible(false);
162                         value.setVisible(false);
163                 }
164                 public void font_size_changed(int font_size) {
165                         label.setFont(AltosUILib.label_font);
166                         value.setFont(AltosUILib.value_font);
167                 }
168                 public void units_changed(boolean imperial_units) {
169                         if (units != null)
170                                 show(v);
171                 }
172
173                 public Value (GridBagLayout layout, int y, String text) {
174                         GridBagConstraints      c = new GridBagConstraints();
175                         c.weighty = 1;
176
177                         label = new JLabel(text);
178                         label.setFont(AltosUILib.label_font);
179                         label.setHorizontalAlignment(SwingConstants.LEFT);
180                         c.gridx = 1; c.gridy = y;
181                         c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
182                         c.anchor = GridBagConstraints.WEST;
183                         c.fill = GridBagConstraints.VERTICAL;
184                         c.weightx = 0;
185                         layout.setConstraints(label, c);
186                         add(label);
187
188                         value = new JTextField(AltosUILib.text_width);
189                         value.setFont(AltosUILib.value_font);
190                         value.setHorizontalAlignment(SwingConstants.RIGHT);
191                         c.gridx = 2; c.gridy = y;
192                         c.anchor = GridBagConstraints.WEST;
193                         c.fill = GridBagConstraints.BOTH;
194                         c.gridwidth = 2;
195                         c.weightx = 1;
196                         layout.setConstraints(value, c);
197                         add(value);
198                 }
199         }
200
201         public abstract class DualValue implements AltosFontListener, AltosUnitsListener {
202                 AltosLights     lights;
203                 JLabel          label;
204                 JTextField      value1;
205                 JTextField      value2;
206
207                 void reset() {
208                         if (lights != null)
209                                 lights.set(false);
210                         value1.setText("");
211                         value2.setText("");
212                 }
213
214                 void show() {
215                         if (lights != null)
216                                 lights.setVisible(true);
217                         label.setVisible(true);
218                         value1.setVisible(true);
219                         value2.setVisible(true);
220                 }
221
222                 void hide() {
223                         if (lights != null)
224                                 lights.setVisible(false);
225                         label.setVisible(false);
226                         value1.setVisible(false);
227                         value2.setVisible(false);
228                 }
229
230                 public void font_size_changed(int font_size) {
231                         label.setFont(AltosUILib.label_font);
232                         value1.setFont(AltosUILib.value_font);
233                         value2.setFont(AltosUILib.value_font);
234                 }
235
236                 public void units_changed(boolean imperial_units) {
237                 }
238
239                 abstract void show(AltosState state, AltosListenerState listener_state);
240
241                 void show(String v1, String v2) {
242                         show();
243                         value1.setText(v1);
244                         value2.setText(v2);
245                 }
246
247                 void show(String f1, double v1, String f2, double v2) {
248                         show();
249                         value1.setText(String.format(f1, v1));
250                         value2.setText(String.format(f2, v2));
251                 }
252
253                 void show(String f1, int v1, String f2, int v2) {
254                         show();
255                         value1.setText(String.format(f1, v1));
256                         value2.setText(String.format(f2, v2));
257                 }
258
259                 public DualValue (GridBagLayout layout, int y, String text, boolean want_lights) {
260                         GridBagConstraints      c = new GridBagConstraints();
261                         c.weighty = 1;
262
263                         if (want_lights) {
264                                 lights = new AltosLights();
265                                 c.gridx = 0; c.gridy = y;
266                                 c.anchor = GridBagConstraints.CENTER;
267                                 c.fill = GridBagConstraints.VERTICAL;
268                                 c.weightx = 0;
269                                 layout.setConstraints(lights, c);
270                                 add(lights);
271                         }
272
273                         label = new JLabel(text);
274                         label.setFont(AltosUILib.label_font);
275                         label.setHorizontalAlignment(SwingConstants.LEFT);
276                         c.gridx = 1; c.gridy = y;
277                         c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
278                         c.anchor = GridBagConstraints.WEST;
279                         c.fill = GridBagConstraints.VERTICAL;
280                         c.weightx = 0;
281                         layout.setConstraints(label, c);
282                         add(label);
283
284                         value1 = new JTextField(AltosUILib.text_width);
285                         value1.setFont(AltosUILib.value_font);
286                         value1.setHorizontalAlignment(SwingConstants.RIGHT);
287                         value1.setEditable(false);
288                         c.gridx = 2; c.gridy = y;
289                         c.anchor = GridBagConstraints.WEST;
290                         c.fill = GridBagConstraints.BOTH;
291                         c.weightx = 1;
292                         layout.setConstraints(value1, c);
293                         add(value1);
294
295                         value2 = new JTextField(AltosUILib.text_width);
296                         value2.setFont(AltosUILib.value_font);
297                         value2.setHorizontalAlignment(SwingConstants.RIGHT);
298                         value1.setEditable(false);
299                         c.gridx = 3; c.gridy = y;
300                         c.anchor = GridBagConstraints.WEST;
301                         c.fill = GridBagConstraints.BOTH;
302                         c.weightx = 1;
303                         c.gridwidth = 1;
304                         layout.setConstraints(value2, c);
305                         add(value2);
306                 }
307         }
308
309         abstract public class ValueHold implements AltosFontListener, AltosUnitsListener {
310                 JLabel          label;
311                 JTextField      value;
312                 JTextField      max_value;
313                 double          v;
314                 double          max;
315                 AltosUnits      units;
316
317                 abstract void show(AltosState state, AltosListenerState listener_state);
318
319                 void reset() {
320                         value.setText("");
321                         max_value.setText("");
322                         max = AltosLib.MISSING;
323                 }
324
325                 public void font_size_changed(int font_size) {
326                         label.setFont(AltosUILib.label_font);
327                         value.setFont(AltosUILib.value_font);
328                         max_value.setFont(AltosUILib.value_font);
329                 }
330
331                 public void units_changed(boolean imperial_units) {
332                         show(v, max);
333                 }
334
335                 void show(double v, double max) {
336                         this.v = v;
337                         this.max = max;
338                         if (v == AltosLib.MISSING)
339                                 value.setText("Missing");
340                         else
341                                 value.setText(units.show(8, v));
342                         if (max == AltosLib.MISSING)
343                                 max_value.setText("Missing");
344                         else
345                                 max_value.setText(units.show(8, max));
346                 }
347
348                 void hide() {
349                         label.setVisible(false);
350                         value.setVisible(false);
351                         max_value.setVisible(false);
352                 }
353
354                 public ValueHold (GridBagLayout layout, int y, AltosUnits units, String text) {
355                         this.units = units;
356                         GridBagConstraints      c = new GridBagConstraints();
357                         c.weighty = 1;
358
359                         label = new JLabel(text);
360                         label.setFont(AltosUILib.label_font);
361                         label.setHorizontalAlignment(SwingConstants.LEFT);
362                         c.gridx = 1; c.gridy = y;
363                         c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
364                         c.anchor = GridBagConstraints.WEST;
365                         c.fill = GridBagConstraints.VERTICAL;
366                         c.weightx = 0;
367                         layout.setConstraints(label, c);
368                         add(label);
369
370                         value = new JTextField(AltosUILib.text_width);
371                         value.setEditable(false);
372                         value.setFont(AltosUILib.value_font);
373                         value.setHorizontalAlignment(SwingConstants.RIGHT);
374                         c.gridx = 2; c.gridy = y;
375                         c.anchor = GridBagConstraints.EAST;
376                         c.fill = GridBagConstraints.BOTH;
377                         c.weightx = 1;
378                         layout.setConstraints(value, c);
379                         add(value);
380
381                         max_value = new JTextField(AltosUILib.text_width);
382                         max_value.setEditable(false);
383                         max_value.setFont(AltosUILib.value_font);
384                         max_value.setHorizontalAlignment(SwingConstants.RIGHT);
385                         c.gridx = 3; c.gridy = y;
386                         c.anchor = GridBagConstraints.EAST;
387                         c.fill = GridBagConstraints.BOTH;
388                         c.weightx = 1;
389                         layout.setConstraints(max_value, c);
390                         add(max_value);
391                 }
392         }
393
394
395         class Altitude extends ValueHold {
396                 void show (AltosState state, AltosListenerState listener_state) {
397                         show(state.altitude(), state.max_altitude());
398                 }
399                 public Altitude (GridBagLayout layout, int y) {
400                         super (layout, y, AltosConvert.height, "Altitude");
401                 }
402         }
403
404         Altitude        altitude;
405
406         class AscentRate extends ValueHold {
407                 void show (AltosState state, AltosListenerState listener_state) {
408                         show(state.gps_ascent_rate(), state.max_gps_ascent_rate());
409                 }
410                 public AscentRate (GridBagLayout layout, int y) {
411                         super (layout, y, AltosConvert.speed, "Ascent Rate");
412                 }
413         }
414
415         AscentRate      ascent_rate;
416
417         class GroundSpeed extends ValueHold {
418                 void show (AltosState state, AltosListenerState listener_state) {
419                         show(state.gps_ground_speed(), state.max_gps_ground_speed());
420                 }
421                 public GroundSpeed (GridBagLayout layout, int y) {
422                         super (layout, y, AltosConvert.speed, "Ground Speed");
423                 }
424         }
425
426         GroundSpeed     ground_speed;
427
428         String pos(double p, String pos, String neg) {
429                 String  h = pos;
430                 if (p < 0) {
431                         h = neg;
432                         p = -p;
433                 }
434                 int deg = (int) Math.floor(p);
435                 double min = (p - Math.floor(p)) * 60.0;
436                 return String.format("%s %4d° %9.6f", h, deg, min);
437         }
438
439         class Course extends DualValue {
440                 void show (AltosState state, AltosListenerState listener_state) {
441                         double  course = state.gps_course();
442                         if (course != AltosLib.MISSING)
443                                 show( String.format("%3.0f°", course),
444                                       AltosConvert.bearing_to_words(
445                                               AltosConvert.BEARING_LONG,
446                                               course));
447                 }
448                 public Course (GridBagLayout layout, int y) {
449                         super (layout, y, "Course", false);
450                 }
451         }
452
453         Course          course;
454
455         class Lat extends Value {
456                 void show (AltosState state, AltosListenerState listener_state) {
457                         if (state.gps != null && state.gps.connected && state.gps.lat != AltosLib.MISSING)
458                                 show(pos(state.gps.lat,"N", "S"));
459                         else
460                                 show("???");
461                 }
462                 public Lat (GridBagLayout layout, int y) {
463                         super (layout, y, "Latitude");
464                 }
465         }
466
467         Lat lat;
468
469         class Lon extends Value {
470                 void show (AltosState state, AltosListenerState listener_state) {
471                         if (state.gps != null && state.gps.connected && state.gps.lon != AltosLib.MISSING)
472                                 show(pos(state.gps.lon,"E", "W"));
473                         else
474                                 show("???");
475                 }
476                 public Lon (GridBagLayout layout, int y) {
477                         super (layout, y, "Longitude");
478                 }
479         }
480
481         Lon lon;
482
483         class GPSLocked extends DualValue {
484                 void show (AltosState state, AltosListenerState listener_state) {
485                         if (state == null || state.gps == null)
486                                 hide();
487                         else {
488                                 int soln = state.gps.nsat;
489                                 int nsat = state.gps.cc_gps_sat != null ? state.gps.cc_gps_sat.length : 0;
490                                 show("%4d in solution", soln,
491                                      "%4d in view", nsat);
492                                 lights.set(state.gps.locked && soln >= 4);
493                         }
494                 }
495                 public GPSLocked (GridBagLayout layout, int y) {
496                         super (layout, y, "GPS Locked", true);
497                 }
498         }
499
500         GPSLocked gps_locked;
501
502         public void reset() {
503                 lat.reset();
504                 lon.reset();
505                 altitude.reset();
506                 ground_speed.reset();
507                 ascent_rate.reset();
508                 course.reset();
509                 gps_locked.reset();
510         }
511
512         public void font_size_changed(int font_size) {
513                 cur.setFont(AltosUILib.label_font);
514                 max.setFont(AltosUILib.label_font);
515                 lat.font_size_changed(font_size);
516                 lon.font_size_changed(font_size);
517                 altitude.font_size_changed(font_size);
518                 ground_speed.font_size_changed(font_size);
519                 ascent_rate.font_size_changed(font_size);
520                 course.font_size_changed(font_size);
521                 gps_locked.font_size_changed(font_size);
522         }
523
524         public void units_changed(boolean imperial_units) {
525                 lat.units_changed(imperial_units);
526                 lon.units_changed(imperial_units);
527                 altitude.units_changed(imperial_units);
528                 ground_speed.units_changed(imperial_units);
529                 ascent_rate.units_changed(imperial_units);
530                 course.units_changed(imperial_units);
531                 gps_locked.units_changed(imperial_units);
532         }
533
534         public void show(AltosState state, AltosListenerState listener_state) {
535                 if (!isShowing()) {
536                         last_state = state;
537                         last_listener_state = listener_state;
538                         return;
539                 }
540
541                 if (state.gps != null && state.gps.connected) {
542                         lat.show(state, listener_state);
543                         lon.show(state, listener_state);
544                 } else {
545                         lat.hide();
546                         lon.hide();
547                 }
548                 altitude.show(state, listener_state);
549                 ground_speed.show(state, listener_state);
550                 ascent_rate.show(state, listener_state);
551                 course.show(state, listener_state);
552                 gps_locked.show(state, listener_state);
553         }
554
555         public void labels(GridBagLayout layout, int y) {
556                 GridBagConstraints      c;
557
558                 cur = new JLabel("Current");
559                 cur.setFont(AltosUILib.label_font);
560                 c = new GridBagConstraints();
561                 c.gridx = 2; c.gridy = y;
562                 c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
563                 layout.setConstraints(cur, c);
564                 add(cur);
565
566                 max = new JLabel("Maximum");
567                 max.setFont(AltosUILib.label_font);
568                 c.gridx = 3; c.gridy = y;
569                 layout.setConstraints(max, c);
570                 add(max);
571         }
572
573         public String getName() {
574                 return "Info";
575         }
576
577         public void hierarchyChanged(HierarchyEvent e) {
578                 if (last_state != null && isShowing()) {
579                         AltosState              state = last_state;
580                         AltosListenerState      listener_state = last_listener_state;
581
582                         last_state = null;
583                         last_listener_state = null;
584                         show(state, listener_state);
585                 }
586         }
587
588         public TeleGPSInfo() {
589                 layout = new GridBagLayout();
590
591                 setLayout(layout);
592
593                 /* Elements in ascent display:
594                  *
595                  * lat
596                  * lon
597                  * height
598                  */
599                 int y = 0;
600                 labels(layout, y++);
601                 altitude = new Altitude(layout, y++);
602                 ground_speed = new GroundSpeed(layout, y++);
603                 ascent_rate = new AscentRate(layout, y++);
604                 course = new Course(layout, y++);
605                 lat = new Lat(layout, y++);
606                 lon = new Lon(layout, y++);
607                 gps_locked = new GPSLocked(layout, y++);
608                 addHierarchyListener(this);
609         }
610 }