altoslib,altosuilib: Bump library version numbers
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package altosui;
20
21 import java.util.*;
22 import org.altusmetrum.altoslib_12.*;
23 import org.altusmetrum.altosuilib_12.*;
24
25 public class AltosPad extends AltosUIFlightTab {
26
27         class Battery extends AltosUIVoltageIndicator {
28                 public double voltage(AltosState state) { return state.battery_voltage; }
29                 public double good() { return AltosLib.ao_battery_good; }
30                 public Battery (AltosUIFlightTab container, int y) { super(container, y, "Battery Voltage", 2); }
31         }
32
33         class Apogee extends AltosUIVoltageIndicator {
34                 public boolean hide(double v) { return v == AltosLib.MISSING; }
35                 public double voltage(AltosState state) { return state.apogee_voltage; }
36                 public double good() { return AltosLib.ao_igniter_good; }
37                 public Apogee (AltosUIFlightTab container, int y) { super(container, y, "Apogee Igniter Voltage", 2); }
38         }
39
40         class Main extends AltosUIVoltageIndicator {
41                 public boolean hide(double v) { return v == AltosLib.MISSING; }
42                 public double voltage(AltosState state) { return state.main_voltage; }
43                 public double good() { return AltosLib.ao_igniter_good; }
44                 public Main (AltosUIFlightTab container, int y) { super(container, y, "Main Igniter Voltage", 2); }
45         }
46
47         class LoggingReady extends AltosUIIndicator {
48                 public void show (AltosState state, AltosListenerState listener_state) {
49                         if (state == null || state.cal_data.flight == AltosLib.MISSING) {
50                                 hide();
51                         } else {
52                                 if (state.cal_data.flight != 0) {
53                                         if (state.state() <= Altos.ao_flight_pad)
54                                                 show("Ready to record");
55                                         else if (state.state() < Altos.ao_flight_landed ||
56                                                  state.state() == AltosLib.ao_flight_stateless)
57                                                 show("Recording data");
58                                         else
59                                                 show("Recorded data");
60                                 } else
61                                         show("Storage full");
62                                 set_lights(state.cal_data.flight != 0);
63                         }
64                 }
65                 public LoggingReady (AltosUIFlightTab container, int y) {
66                         super(container, y, "On-board Data Logging", 1, true, 2);
67                 }
68         }
69
70         class GPSLocked extends AltosUIIndicator {
71                 public void show (AltosState state, AltosListenerState listener_state) {
72                         if (state == null || state.gps == null)
73                                 hide();
74                         else {
75                                 int     sol = state.gps.nsat;
76                                 int     sat = state.gps.cc_gps_sat == null ? 0 : state.gps.cc_gps_sat.length;
77                                 show("%d in solution", sol, "%d in view", sat);
78                                 set_lights(state.gps.locked && sol >= 4);
79                         }
80                 }
81                 public GPSLocked (AltosUIFlightTab container, int y) {
82                         super (container, y, "GPS Locked", 2, true, 1);
83                 }
84         }
85
86         class GPSReady extends AltosUIIndicator {
87                 public void show (AltosState state, AltosListenerState listener_state) {
88                         if (state == null || state.gps == null)
89                                 hide();
90                         else {
91                                 if (state.gps_ready)
92                                         show("Ready");
93                                 else
94                                         show("Waiting %d", state.gps_waiting);
95                                 set_lights(state.gps_ready);
96                         }
97                 }
98                 public GPSReady (AltosUIFlightTab container, int y) {
99                         super (container, y, "GPS Ready", 1, true, 2);
100                 }
101         }
102
103         class ReceiverBattery extends AltosUIVoltageIndicator {
104
105                 double  last_voltage = AltosLib.MISSING;
106
107                 public double voltage(AltosState state) {
108                         return last_voltage;
109                 }
110
111                 public double good() { return AltosLib.ao_battery_good; }
112
113                 public boolean hide(AltosState state, AltosListenerState listener_state, int i) {
114                         return value(state, listener_state, i) == AltosLib.MISSING;
115                 }
116
117                 public double value(AltosState state, AltosListenerState listener_state, int i) {
118                         if (listener_state == null)
119                                 last_voltage = AltosLib.MISSING;
120                         else
121                                 last_voltage = listener_state.battery;
122                         return last_voltage;
123                 }
124
125                 public ReceiverBattery (AltosUIFlightTab container, int y) {
126                         super(container, y, "Receiver Battery", 2);
127                 }
128         }
129
130         boolean report_pad(AltosState state) {
131                 if (state.state() == AltosLib.ao_flight_stateless ||
132                     state.state() < AltosLib.ao_flight_pad)
133                 {
134                         return false;
135                 }
136                 return true;
137         }
138
139         class PadLat extends AltosUIIndicator {
140
141                 double  last_lat = AltosLib.MISSING - 1;
142
143                 public void show (AltosState state, AltosListenerState listener_state) {
144                         double lat = AltosLib.MISSING;
145                         String label = null;
146
147                         if (state != null) {
148                                 if (report_pad(state)) {
149                                         lat = state.pad_lat;
150                                         label = "Pad Latitude";
151                                 } else if (state.gps != null) {
152                                         lat = state.gps.lat;
153                                         label = "Latitude";
154                                 }
155                         }
156                         if (lat != last_lat) {
157                                 if (lat != AltosLib.MISSING) {
158                                         show(AltosConvert.latitude.show(10, lat));
159                                         set_label(label);
160                                 } else
161                                         hide();
162                                 last_lat = lat;
163                         }
164                 }
165
166                 public void reset() {
167                         super.reset();
168                         last_lat = AltosLib.MISSING - 1;
169                 }
170
171                 public PadLat (AltosUIFlightTab container, int y) {
172                         super (container, y, "Pad Latitude", 1, false, 2);
173                 }
174         }
175
176         class PadLon extends AltosUIIndicator {
177
178                 double last_lon = AltosLib.MISSING - 1;
179
180                 public void show (AltosState state, AltosListenerState listener_state) {
181                         double lon = AltosLib.MISSING;
182                         String label = null;
183
184                         if (state != null) {
185                                 if (report_pad(state)) {
186                                         lon = state.pad_lon;
187                                         label = "Pad Longitude";
188                                 } else if (state.gps != null) {
189                                         lon = state.gps.lon;
190                                         label = "Longitude";
191                                 }
192                         }
193                         if (lon != last_lon) {
194                                 if (lon != AltosLib.MISSING) {
195                                         show(AltosConvert.longitude.show(10, lon));
196                                         set_label(label);
197                                 } else
198                                         hide();
199                                 last_lon = lon;
200                         }
201                 }
202
203                 public void reset() {
204                         super.reset();
205                         last_lon = AltosLib.MISSING - 1;
206                 }
207
208                 public PadLon (AltosUIFlightTab container, int y) {
209                         super (container, y, "Pad Longitude", 1, false, 2);
210                 }
211         }
212
213         class PadAlt extends AltosUIUnitsIndicator {
214
215                 public double value(AltosState state, int i) {
216                         if (report_pad(state))
217                                 return state.pad_alt;
218                         else if (state.gps != null)
219                                 return state.gps.alt;
220                         else
221                                 return state.altitude();
222                 }
223
224                 public void show (AltosState state, AltosListenerState listener_state) {
225                         String label = "Altitude";
226
227                         if (state != null && report_pad(state))
228                                 label = "Pad Altitude";
229                         set_label(label);
230                         super.show(state, listener_state);
231                 }
232
233                 public PadAlt (AltosUIFlightTab container, int y) {
234                         super (container, y, AltosConvert.height, "Pad Altitude", 1, false, 2);
235                 }
236         }
237         public String getName() { return "Pad"; }
238
239         public AltosPad() {
240                 int y = 0;
241                 add(new Battery(this, y++));
242                 add(new ReceiverBattery(this, y++));
243                 add(new Apogee(this, y++));
244                 add(new Main(this, y++));
245                 add(new LoggingReady(this, y++));
246                 add(new GPSLocked(this, y++));
247                 add(new GPSReady(this, y++));
248                 add(new PadLat(this, y++));
249                 add(new PadLon(this, y++));
250                 add(new PadAlt(this, y++));
251         }
252 }