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