altosui/telegps: Switch to AltosUIIndicator and AltosUIFlightTab
[fw/altos] / altosui / AltosPad.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.util.*;
21 import org.altusmetrum.altoslib_4.*;
22 import org.altusmetrum.altosuilib_2.*;
23
24 public class AltosPad extends AltosUIFlightTab {
25
26         class Battery extends AltosUIVoltageIndicator {
27                 public double voltage(AltosState state) { return state.battery_voltage; }
28                 public double good() { return AltosLib.ao_battery_good; }
29                 public Battery (AltosUIFlightTab container, int y) { super(container, y, "Battery Voltage", 2); }
30         }
31
32         class Apogee extends AltosUIVoltageIndicator {
33                 public boolean hide(double v) { return v == AltosLib.MISSING; }
34                 public double voltage(AltosState state) { return state.apogee_voltage; }
35                 public double good() { return AltosLib.ao_igniter_good; }
36                 public Apogee (AltosUIFlightTab container, int y) { super(container, y, "Apogee Igniter Voltage", 2); }
37         }
38
39         class Main extends AltosUIVoltageIndicator {
40                 public boolean hide(double v) { return v == AltosLib.MISSING; }
41                 public double voltage(AltosState state) { return state.main_voltage; }
42                 public double good() { return AltosLib.ao_igniter_good; }
43                 public Main (AltosUIFlightTab container, int y) { super(container, y, "Main Igniter Voltage", 2); }
44         }
45
46         class LoggingReady extends AltosUIIndicator {
47                 public void show (AltosState state, AltosListenerState listener_state) {
48                         if (state == null || state.flight == AltosLib.MISSING) {
49                                 hide();
50                         } else {
51                                 if (state.flight != 0) {
52                                         if (state.state <= Altos.ao_flight_pad)
53                                                 show("Ready to record");
54                                         else if (state.state < Altos.ao_flight_landed ||
55                                                  state.state == AltosLib.ao_flight_stateless)
56                                                 show("Recording data");
57                                         else
58                                                 show("Recorded data");
59                                 } else
60                                         show("Storage full");
61                                 set_lights(state.flight != 0);
62                         }
63                 }
64                 public LoggingReady (AltosUIFlightTab container, int y) {
65                         super(container, y, "On-board Data Logging", 1, true, 2);
66                 }
67         }
68
69         class GPSLocked extends AltosUIIndicator {
70                 public void show (AltosState state, AltosListenerState listener_state) {
71                         if (state == null || state.gps == null)
72                                 hide();
73                         else {
74                                 int     sol = state.gps.nsat;
75                                 int     sat = state.gps.cc_gps_sat == null ? 0 : state.gps.cc_gps_sat.length;
76                                 show("%d in solution", sol, "%d in view", sat);
77                                 set_lights(state.gps.locked && sol >= 4);
78                         }
79                 }
80                 public GPSLocked (AltosUIFlightTab container, int y) {
81                         super (container, y, "GPS Locked", 2, true, 1);
82                 }
83         }
84
85         class GPSReady extends AltosUIIndicator {
86                 public void show (AltosState state, AltosListenerState listener_state) {
87                         if (state == null || state.gps == null)
88                                 hide();
89                         else {
90                                 if (state.gps_ready)
91                                         show("Ready");
92                                 else
93                                         show("Waiting %d", state.gps_waiting);
94                                 set_lights(state.gps_ready);
95                         }
96                 }
97                 public GPSReady (AltosUIFlightTab container, int y) {
98                         super (container, y, "GPS Ready", 1, true, 2);
99                 }
100         }
101
102         class ReceiverBattery extends AltosUIVoltageIndicator {
103
104                 public double voltage(AltosState state) { return AltosLib.MISSING; }
105
106                 public boolean hide(double v) { return v == AltosLib.MISSING; }
107                 public double good() { return AltosLib.ao_battery_good; }
108
109                 public double value(AltosState state, AltosListenerState listener_state, int i) {
110                         if (listener_state == null)
111                                 return AltosLib.MISSING;
112                         return listener_state.battery;
113                 }
114
115                 public ReceiverBattery (AltosUIFlightTab container, int y) {
116                         super(container, y, "Receiver Battery", 2);
117                 }
118         }
119
120         class PadLat extends AltosUIIndicator {
121
122                 double  last_lat = AltosLib.MISSING - 1;
123
124                 public void show (AltosState state, AltosListenerState listener_state) {
125                         double lat = AltosLib.MISSING;
126                         String label = null;
127
128                         if (state != null) {
129                                 if (state.state < AltosLib.ao_flight_pad && state.gps != null && state.gps.lat != AltosLib.MISSING) {
130                                         lat = state.gps.lat;
131                                         label = "Latitude";
132                                 } else {
133                                         lat = state.pad_lat;
134                                         label = "Pad Latitude";
135                                 }
136                         }
137                         if (lat != last_lat) {
138                                 if (lat != AltosLib.MISSING) {
139                                         show(AltosConvert.latitude.show(10, lat));
140                                         set_label(label);
141                                 } else
142                                         hide();
143                                 last_lat = lat;
144                         }
145                 }
146
147                 public void reset() {
148                         super.reset();
149                         last_lat = AltosLib.MISSING - 1;
150                 }
151
152                 public PadLat (AltosUIFlightTab container, int y) {
153                         super (container, y, "Pad Latitude", 1, false, 2);
154                 }
155         }
156
157         class PadLon extends AltosUIIndicator {
158
159                 double last_lon = AltosLib.MISSING - 1;
160
161                 public void show (AltosState state, AltosListenerState listener_state) {
162                         double lon = AltosLib.MISSING;
163                         String label = null;
164
165                         if (state != null) {
166                                 if (state.state < AltosLib.ao_flight_pad && state.gps != null && state.gps.lon != AltosLib.MISSING) {
167                                         lon = state.gps.lon;
168                                         label = "Longitude";
169                                 } else {
170                                         lon = state.pad_lon;
171                                         label = "Pad Longitude";
172                                 }
173                         }
174                         if (lon != last_lon) {
175                                 if (lon != AltosLib.MISSING) {
176                                         show(AltosConvert.longitude.show(10, lon));
177                                         set_label(label);
178                                 } else
179                                         hide();
180                                 last_lon = lon;
181                         }
182                 }
183
184                 public void reset() {
185                         super.reset();
186                         last_lon = AltosLib.MISSING - 1;
187                 }
188
189                 public PadLon (AltosUIFlightTab container, int y) {
190                         super (container, y, "Pad Longitude", 1, false, 2);
191                 }
192         }
193
194         class PadAlt extends AltosUIIndicator {
195
196                 double  last_alt = AltosLib.MISSING - 1;
197
198                 public void show (AltosState state, AltosListenerState listener_state) {
199                         double alt = AltosLib.MISSING;
200                         String label = null;
201
202                         if (state != null) {
203                                 if (state.state < AltosLib.ao_flight_pad && state.gps != null && state.gps.alt != AltosLib.MISSING) {
204                                         alt = state.gps.alt;
205                                         label = "Altitude";
206                                 } else {
207                                         alt = state.pad_alt;
208                                         label = "Pad Altitude";
209                                 }
210                         }
211                         if (alt != last_alt) {
212                                 if (alt != AltosLib.MISSING) {
213                                         show(AltosConvert.height.show(5, alt));
214                                         set_label(label);
215                                 } else
216                                         hide();
217                                 last_alt = alt;
218                         }
219                 }
220
221                 public void reset() {
222                         super.reset();
223                         last_alt =  AltosLib.MISSING - 1;
224                 }
225
226                 public PadAlt (AltosUIFlightTab container, int y) {
227                         super (container, y, "Pad Altitude", 1, false, 2);
228                 }
229         }
230         public String getName() { return "Pad"; }
231
232         public AltosPad() {
233                 int y = 0;
234                 add(new Battery(this, y++));
235                 add(new Apogee(this, y++));
236                 add(new Main(this, y++));
237                 add(new LoggingReady(this, y++));
238                 add(new GPSLocked(this, y++));
239                 add(new GPSReady(this, y++));
240                 add(new ReceiverBattery(this, y++));
241                 add(new PadLat(this, y++));
242                 add(new PadLon(this, y++));
243                 add(new PadAlt(this, y++));
244         }
245 }