telegps: Add status tab
[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.util.*;
21 import java.awt.*;
22 import java.awt.event.*;
23 import javax.swing.*;
24 import org.altusmetrum.altoslib_4.*;
25 import org.altusmetrum.altosuilib_2.*;
26
27 public class TeleGPSInfo extends JComponent implements AltosFlightDisplay, HierarchyListener {
28
29         JLabel                          cur, max;
30
31         private AltosState              last_state;
32         private AltosListenerState      last_listener_state;
33
34         abstract class Value extends AltosUIUnitsIndicator {
35                 public abstract void show(AltosState state, AltosListenerState listener_state);
36
37                 public Value (Container container, int y, AltosUnits units, String text) {
38                         super(container, y, units, text, 1, false, 2);
39                 }
40         }
41
42         abstract class DualValue extends AltosUIUnitsIndicator {
43                 public DualValue (Container container, int y, AltosUnits units, String text) {
44                         super(container, y, units, text, 2, false, 1);
45                 }
46         }
47
48         abstract class ValueHold extends DualValue {
49                 public void reset() {
50                         super.reset();
51                         last_values[1] = AltosLib.MISSING;
52                 }
53                 public ValueHold (Container container, int y, AltosUnits units, String text) {
54                         super(container, y, units, text);
55                 }
56         }
57
58         class Altitude extends ValueHold {
59                 public double value(AltosState state, int i) {
60                         if (i == 0)
61                                 return state.altitude();
62                         else
63                                 return state.max_altitude();
64                 }
65
66                 public Altitude (Container container, int y) {
67                         super (container, y, AltosConvert.height, "Altitude");
68                 }
69         }
70
71         class AscentRate extends ValueHold {
72                 public double value(AltosState state, int i) {
73                         if (i == 0)
74                                 return state.gps_ascent_rate();
75                         else
76                                 return state.max_gps_ascent_rate();
77                 }
78                 public AscentRate (Container container, int y) {
79                         super (container, y, AltosConvert.speed, "Ascent Rate");
80                 }
81         }
82
83         class GroundSpeed extends ValueHold {
84                 public double value(AltosState state, int i) {
85                         if (i == 0)
86                                 return state.gps_ground_speed();
87                         else
88                                 return state.max_gps_ground_speed();
89                 }
90                 public GroundSpeed (Container container, int y) {
91                         super (container, y, AltosConvert.speed, "Ground Speed");
92                 }
93         }
94
95         class Course extends AltosUIIndicator {
96
97                 public void show (AltosState state, AltosListenerState listener_state) {
98                         double  course = state.gps_course();
99                         if (course != AltosLib.MISSING)
100                                 show( String.format("%3.0f°", course),
101                                       AltosConvert.bearing_to_words(
102                                               AltosConvert.BEARING_LONG,
103                                               course));
104                 }
105                 public Course (Container container, int y) {
106                         super (container, y, "Course", 2, false, 1);
107                 }
108         }
109
110         class Lat extends AltosUIIndicator {
111
112                 String pos(double p, String pos, String neg) {
113                         String  h = pos;
114                         if (p < 0) {
115                                 h = neg;
116                                 p = -p;
117                         }
118                         int deg = (int) Math.floor(p);
119                         double min = (p - Math.floor(p)) * 60.0;
120                         return String.format("%s %4d° %9.6f", h, deg, min);
121                 }
122
123                 public void show (AltosState state, AltosListenerState listener_state) {
124                         if (state.gps != null && state.gps.connected && state.gps.lat != AltosLib.MISSING)
125                                 show(pos(state.gps.lat,"N", "S"));
126                         else
127                                 show("???");
128                 }
129                 public Lat (Container container, int y) {
130                         super (container, y, "Latitude", 1, false, 2);
131                 }
132         }
133
134         class Lon extends AltosUIIndicator {
135
136                 String pos(double p, String pos, String neg) {
137                         String  h = pos;
138                         if (p < 0) {
139                                 h = neg;
140                                 p = -p;
141                         }
142                         int deg = (int) Math.floor(p);
143                         double min = (p - Math.floor(p)) * 60.0;
144                         return String.format("%s %4d° %9.6f", h, deg, min);
145                 }
146
147                 public void show (AltosState state, AltosListenerState listener_state) {
148                         if (state.gps != null && state.gps.connected && state.gps.lon != AltosLib.MISSING)
149                                 show(pos(state.gps.lon,"E", "W"));
150                         else
151                                 show("???");
152                 }
153                 public Lon (Container container, int y) {
154                         super (container, y, "Longitude", 1, false, 2);
155                 }
156         }
157
158         class GPSLocked extends AltosUIIndicator {
159
160                 public void show (AltosState state, AltosListenerState listener_state) {
161                         if (state == null || state.gps == null)
162                                 hide();
163                         else {
164                                 int soln = state.gps.nsat;
165                                 int nsat = state.gps.cc_gps_sat != null ? state.gps.cc_gps_sat.length : 0;
166                                 show("%4d in solution", soln,
167                                      "%4d in view", nsat);
168                                 set_lights(state.gps.locked && soln >= 4);
169                         }
170                 }
171                 public GPSLocked (Container container, int y) {
172                         super (container, y, "GPS Locked", 2, true, 1);
173                 }
174         }
175
176         LinkedList<AltosUIIndicator> indicators = new LinkedList<AltosUIIndicator>();
177
178         public void reset() {
179                 for (AltosUIIndicator i : indicators)
180                         i.reset();
181         }
182
183         public void font_size_changed(int font_size) {
184                 cur.setFont(AltosUILib.label_font);
185                 max.setFont(AltosUILib.label_font);
186                 for (AltosUIIndicator i : indicators)
187                         i.font_size_changed(font_size);
188         }
189
190         public void units_changed(boolean imperial_units) {
191                 for (AltosUIIndicator i : indicators)
192                         i.units_changed(imperial_units);
193         }
194
195         public void show(AltosState state, AltosListenerState listener_state) {
196                 if (!isShowing()) {
197                         last_state = state;
198                         last_listener_state = listener_state;
199                         return;
200                 }
201
202                 for (AltosUIIndicator i : indicators)
203                         i.show(state, listener_state);
204         }
205
206         public void labels(Container container, int y) {
207                 GridBagLayout           layout = (GridBagLayout)(container.getLayout());
208                 GridBagConstraints      c;
209
210                 cur = new JLabel("Current");
211                 cur.setFont(AltosUILib.label_font);
212                 c = new GridBagConstraints();
213                 c.gridx = 2; c.gridy = y;
214                 c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
215                 layout.setConstraints(cur, c);
216                 add(cur);
217
218                 max = new JLabel("Maximum");
219                 max.setFont(AltosUILib.label_font);
220                 c.gridx = 3; c.gridy = y;
221                 layout.setConstraints(max, c);
222                 add(max);
223         }
224
225         public String getName() {
226                 return "Location";
227         }
228
229         public void hierarchyChanged(HierarchyEvent e) {
230                 if (last_state != null && isShowing()) {
231                         AltosState              state = last_state;
232                         AltosListenerState      listener_state = last_listener_state;
233
234                         last_state = null;
235                         last_listener_state = null;
236                         show(state, listener_state);
237                 }
238         }
239
240         public TeleGPSInfo() {
241                 setLayout(new GridBagLayout());
242
243                 int y = 0;
244                 labels(this, y++);
245                 indicators.add(new Altitude(this, y++));
246                 indicators.add(new GroundSpeed(this, y++));
247                 indicators.add(new AscentRate(this, y++));
248                 indicators.add(new Course(this, y++));
249                 indicators.add(new Lat(this, y++));
250                 indicators.add(new Lon(this, y++));
251                 indicators.add(new GPSLocked(this, y++));
252                 addHierarchyListener(this);
253         }
254 }