altoslib/altosuilib: Change versions to altoslib:4, altosuilib:2
[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.awt.*;
21 import javax.swing.*;
22 import org.altusmetrum.altoslib_4.*;
23 import org.altusmetrum.altosuilib_2.*;
24
25 public class AltosPad extends JComponent implements AltosFlightDisplay {
26         GridBagLayout   layout;
27
28         public class LaunchStatus {
29                 JLabel          label;
30                 JTextField      value;
31                 AltosLights     lights;
32
33                 void show(AltosState state, AltosListenerState listener_state) {}
34
35                 void reset() {
36                         value.setText("");
37                         lights.set(false);
38                 }
39
40                 public void show() {
41                         label.setVisible(true);
42                         value.setVisible(true);
43                         lights.setVisible(true);
44                 }
45
46                 void show(String s) {
47                         show();
48                         value.setText(s);
49                 }
50
51                 void show(String format, double value) {
52                         show(String.format(format, value));
53                 }
54
55                 void show(String format, int value) {
56                         show(String.format(format, value));
57                 }
58
59                 public void hide() {
60                         label.setVisible(false);
61                         value.setVisible(false);
62                         lights.setVisible(false);
63                 }
64
65                 public void set_font() {
66                         label.setFont(Altos.label_font);
67                         value.setFont(Altos.value_font);
68                 }
69
70                 public void set_label(String text) {
71                         label.setText(text);
72                 }
73
74                 public LaunchStatus (GridBagLayout layout, int y, String text) {
75                         GridBagConstraints      c = new GridBagConstraints();
76                         c.weighty = 1;
77
78                         lights = new AltosLights();
79                         c.gridx = 0; c.gridy = y;
80                         c.anchor = GridBagConstraints.CENTER;
81                         c.fill = GridBagConstraints.VERTICAL;
82                         c.weightx = 0;
83                         layout.setConstraints(lights, c);
84                         add(lights);
85
86                         label = new JLabel(text);
87                         label.setFont(Altos.label_font);
88                         label.setHorizontalAlignment(SwingConstants.LEFT);
89                         c.gridx = 1; c.gridy = y;
90                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
91                         c.anchor = GridBagConstraints.WEST;
92                         c.fill = GridBagConstraints.VERTICAL;
93                         c.weightx = 0;
94                         layout.setConstraints(label, c);
95                         add(label);
96
97                         value = new JTextField(Altos.text_width);
98                         value.setFont(Altos.value_font);
99                         value.setHorizontalAlignment(SwingConstants.RIGHT);
100                         c.gridx = 2; c.gridy = y;
101                         c.anchor = GridBagConstraints.WEST;
102                         c.fill = GridBagConstraints.BOTH;
103                         c.weightx = 1;
104                         layout.setConstraints(value, c);
105                         add(value);
106
107                 }
108         }
109
110         public class LaunchValue {
111                 JLabel          label;
112                 JTextField      value;
113                 void show(AltosState state, AltosListenerState listener_state) {}
114
115                 void show() {
116                         label.setVisible(true);
117                         value.setVisible(true);
118                 }
119
120                 void hide() {
121                         label.setVisible(false);
122                         value.setVisible(false);
123                 }
124
125                 public void set_font() {
126                         label.setFont(Altos.label_font);
127                         value.setFont(Altos.value_font);
128                 }
129
130                 void show(String s) {
131                         show();
132                         value.setText(s);
133                 }
134
135                 void show(AltosUnits units, double v) {
136                         show(units.show(8, v));
137                 }
138
139                 void show(String format, double v) {
140                         show(String.format(format, v));
141                 }
142
143                 public void set_label(String text) {
144                         label.setText(text);
145                 }
146
147                 void reset() {
148                         value.setText("");
149                 }
150
151                 public LaunchValue (GridBagLayout layout, int y, String text) {
152                         GridBagConstraints      c = new GridBagConstraints();
153                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
154                         c.weighty = 1;
155
156                         label = new JLabel(text);
157                         label.setFont(Altos.label_font);
158                         label.setHorizontalAlignment(SwingConstants.LEFT);
159                         c.gridx = 1; c.gridy = y;
160                         c.anchor = GridBagConstraints.WEST;
161                         c.fill = GridBagConstraints.VERTICAL;
162                         c.weightx = 0;
163                         layout.setConstraints(label, c);
164                         add(label);
165
166                         value = new JTextField(Altos.text_width);
167                         value.setFont(Altos.value_font);
168                         value.setHorizontalAlignment(SwingConstants.RIGHT);
169                         c.gridx = 2; c.gridy = y;
170                         c.anchor = GridBagConstraints.EAST;
171                         c.fill = GridBagConstraints.BOTH;
172                         c.weightx = 1;
173                         layout.setConstraints(value, c);
174                         add(value);
175                 }
176         }
177
178         class Battery extends LaunchStatus {
179                 void show (AltosState state, AltosListenerState listener_state) {
180                         if (state == null || state.battery_voltage == AltosLib.MISSING)
181                                 hide();
182                         else {
183                                 show("%4.2f V", state.battery_voltage);
184                                 lights.set(state.battery_voltage >= AltosLib.ao_battery_good);
185                         }
186                 }
187                 public Battery (GridBagLayout layout, int y) {
188                         super(layout, y, "Battery Voltage");
189                 }
190         }
191
192         Battery battery;
193
194         class Apogee extends LaunchStatus {
195                 void show (AltosState state, AltosListenerState listener_state) {
196                         if (state == null || state.apogee_voltage == AltosLib.MISSING)
197                                 hide();
198                         else {
199                                 show("%4.2f V", state.apogee_voltage);
200                                 lights.set(state.apogee_voltage >= AltosLib.ao_igniter_good);
201                         }
202                 }
203                 public Apogee (GridBagLayout layout, int y) {
204                         super(layout, y, "Apogee Igniter Voltage");
205                 }
206         }
207
208         Apogee apogee;
209
210         class Main extends LaunchStatus {
211                 void show (AltosState state, AltosListenerState listener_state) {
212                         if (state == null || state.main_voltage == AltosLib.MISSING)
213                                 hide();
214                         else {
215                                 show("%4.2f V", state.main_voltage);
216                                 lights.set(state.main_voltage >= AltosLib.ao_igniter_good);
217                         }
218                 }
219                 public Main (GridBagLayout layout, int y) {
220                         super(layout, y, "Main Igniter Voltage");
221                 }
222         }
223
224         Main main;
225
226         class LoggingReady extends LaunchStatus {
227                 void show (AltosState state, AltosListenerState listener_state) {
228                         if (state == null || state.flight == AltosLib.MISSING) {
229                                 hide();
230                         } else {
231                                 if (state.flight != 0) {
232                                         if (state.state <= Altos.ao_flight_pad)
233                                                 show("Ready to record");
234                                         else if (state.state < Altos.ao_flight_landed)
235                                                 show("Recording data");
236                                         else
237                                                 show("Recorded data");
238                                 } else
239                                         show("Storage full");
240                                 lights.set(state.flight != 0);
241                         }
242                 }
243                 public LoggingReady (GridBagLayout layout, int y) {
244                         super(layout, y, "On-board Data Logging");
245                 }
246         }
247
248         LoggingReady logging_ready;
249
250         class GPSLocked extends LaunchStatus {
251                 void show (AltosState state, AltosListenerState listener_state) {
252                         if (state == null || state.gps == null)
253                                 hide();
254                         else {
255                                 show("%4d sats", state.gps.nsat);
256                                 lights.set(state.gps.locked && state.gps.nsat >= 4);
257                         }
258                 }
259                 public GPSLocked (GridBagLayout layout, int y) {
260                         super (layout, y, "GPS Locked");
261                 }
262         }
263
264         GPSLocked gps_locked;
265
266         class GPSReady extends LaunchStatus {
267                 void show (AltosState state, AltosListenerState listener_state) {
268                         if (state == null || state.gps == null)
269                                 hide();
270                         else {
271                                 if (state.gps_ready)
272                                         show("Ready");
273                                 else
274                                         show("Waiting %d", state.gps_waiting);
275                                 lights.set(state.gps_ready);
276                         }
277                 }
278                 public GPSReady (GridBagLayout layout, int y) {
279                         super (layout, y, "GPS Ready");
280                 }
281         }
282
283         GPSReady gps_ready;
284
285         class ReceiverBattery extends LaunchStatus {
286                 void show (AltosState state, AltosListenerState listener_state) {
287                         if (listener_state == null || listener_state.battery == AltosLib.MISSING)
288                                 hide();
289                         else {
290                                 show("%4.2f V", listener_state.battery);
291                                 lights.set(listener_state.battery > AltosLib.ao_battery_good);
292                         }
293                 }
294                 public ReceiverBattery (GridBagLayout layout, int y) {
295                         super(layout, y, "Receiver Battery");
296                 }
297         }
298
299         ReceiverBattery receiver_battery;
300
301         String pos(double p, String pos, String neg) {
302                 String  h = pos;
303                 if (p < 0) {
304                         h = neg;
305                         p = -p;
306                 }
307                 int deg = (int) Math.floor(p);
308                 double min = (p - Math.floor(p)) * 60.0;
309                 return String.format("%s %4d° %9.6f", h, deg, min);
310         }
311
312         class PadLat extends LaunchValue {
313                 void show (AltosState state, AltosListenerState listener_state) {
314                         double lat = AltosLib.MISSING;
315                         String label = null;
316
317                         if (state != null) {
318                                 if (state.state < AltosLib.ao_flight_pad && state.gps != null && state.gps.lat != AltosLib.MISSING) {
319                                         lat = state.gps.lat;
320                                         label = "Latitude";
321                                 } else {
322                                         lat = state.pad_lat;
323                                         label = "Pad Latitude";
324                                 }
325                         }
326                         if (lat != AltosLib.MISSING) {
327                                 show(pos(lat,"N", "S"));
328                                 set_label(label);
329                         } else
330                                 hide();
331                 }
332                 public PadLat (GridBagLayout layout, int y) {
333                         super (layout, y, "Pad Latitude");
334                 }
335         }
336
337         PadLat pad_lat;
338
339         class PadLon extends LaunchValue {
340                 void show (AltosState state, AltosListenerState listener_state) {
341                         double lon = AltosLib.MISSING;
342                         String label = null;
343
344                         if (state != null) {
345                                 if (state.state < AltosLib.ao_flight_pad && state.gps != null && state.gps.lon != AltosLib.MISSING) {
346                                         lon = state.gps.lon;
347                                         label = "Longitude";
348                                 } else {
349                                         lon = state.pad_lon;
350                                         label = "Pad Longitude";
351                                 }
352                         }
353                         if (lon != AltosLib.MISSING) {
354                                 show(pos(lon,"E", "W"));
355                                 set_label(label);
356                         } else
357                                 hide();
358                 }
359                 public PadLon (GridBagLayout layout, int y) {
360                         super (layout, y, "Pad Longitude");
361                 }
362         }
363
364         PadLon pad_lon;
365
366         class PadAlt extends LaunchValue {
367                 void show (AltosState state, AltosListenerState listener_state) {
368                         double alt = AltosLib.MISSING;
369                         String label = null;
370
371                         if (state != null) {
372                                 if (state.state < AltosLib.ao_flight_pad && state.gps != null && state.gps.alt != AltosLib.MISSING) {
373                                         alt = state.gps.alt;
374                                         label = "Altitude";
375                                 } else {
376                                         alt = state.pad_alt;
377                                         label = "Pad Altitude";
378                                 }
379                         }
380                         if (alt != AltosLib.MISSING) {
381                                 show("%4.0f m", state.gps.alt);
382                                 set_label(label);
383                         } else
384                                 hide();
385                 }
386                 public PadAlt (GridBagLayout layout, int y) {
387                         super (layout, y, "Pad Altitude");
388                 }
389         }
390
391         PadAlt pad_alt;
392
393         public void reset() {
394                 battery.reset();
395                 apogee.reset();
396                 main.reset();
397                 logging_ready.reset();
398                 gps_locked.reset();
399                 gps_ready.reset();
400                 receiver_battery.reset();
401                 pad_lat.reset();
402                 pad_lon.reset();
403                 pad_alt.reset();
404         }
405
406         public void set_font() {
407                 battery.set_font();
408                 apogee.set_font();
409                 main.set_font();
410                 logging_ready.set_font();
411                 gps_locked.set_font();
412                 gps_ready.set_font();
413                 receiver_battery.set_font();
414                 pad_lat.set_font();
415                 pad_lon.set_font();
416                 pad_alt.set_font();
417         }
418
419         public void show(AltosState state, AltosListenerState listener_state) {
420                 battery.show(state, listener_state);
421                 apogee.show(state, listener_state);
422                 main.show(state, listener_state);
423                 logging_ready.show(state, listener_state);
424                 pad_alt.show(state, listener_state);
425                 receiver_battery.show(state, listener_state);
426                 gps_locked.show(state, listener_state);
427                 gps_ready.show(state, listener_state);
428                 pad_lat.show(state, listener_state);
429                 pad_lon.show(state, listener_state);
430         }
431
432         public AltosPad() {
433                 layout = new GridBagLayout();
434
435                 setLayout(layout);
436
437                 /* Elements in pad display:
438                  *
439                  * Battery voltage
440                  * Igniter continuity
441                  * GPS lock status
442                  * GPS ready status
443                  * GPS location
444                  * Pad altitude
445                  * RSSI
446                  */
447                 battery = new Battery(layout, 0);
448                 apogee = new Apogee(layout, 1);
449                 main = new Main(layout, 2);
450                 logging_ready = new LoggingReady(layout, 3);
451                 gps_locked = new GPSLocked(layout, 4);
452                 gps_ready = new GPSReady(layout, 5);
453                 receiver_battery = new ReceiverBattery(layout, 6);
454                 pad_lat = new PadLat(layout, 7);
455                 pad_lon = new PadLon(layout, 8);
456                 pad_alt = new PadAlt(layout, 9);
457                 show(null, null);
458         }
459 }