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