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