altosui: Display current GPS in 'pad' tab for 'startup' staten
[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_1.*;
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, int crc_errors) {}
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, int crc_errors) {}
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, int crc_errors) {
179                         if (state.battery == AltosRecord.MISSING)
180                                 hide();
181                         else {
182                                 show("%4.2f V", state.battery);
183                                 lights.set(state.battery > 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, int crc_errors) {
195                         show("%4.2f V", state.drogue_sense);
196                         lights.set(state.drogue_sense > 3.2);
197                 }
198                 public Apogee (GridBagLayout layout, int y) {
199                         super(layout, y, "Apogee Igniter Voltage");
200                 }
201         }
202
203         Apogee apogee;
204
205         class Main extends LaunchStatus {
206                 void show (AltosState state, int crc_errors) {
207                         show("%4.2f V", state.main_sense);
208                         lights.set(state.main_sense > 3.2);
209                 }
210                 public Main (GridBagLayout layout, int y) {
211                         super(layout, y, "Main Igniter Voltage");
212                 }
213         }
214
215         Main main;
216
217         class LoggingReady extends LaunchStatus {
218                 void show (AltosState state, int crc_errors) {
219                         if (state.data.flight != 0) {
220                                 if (state.data.state <= Altos.ao_flight_pad)
221                                         show("Ready to record");
222                                 else if (state.data.state < Altos.ao_flight_landed)
223                                         show("Recording data");
224                                 else
225                                         show("Recorded data");
226                         }
227                         else
228                                 show("Storage full");
229                         lights.set(state.data.flight != 0);
230                 }
231                 public LoggingReady (GridBagLayout layout, int y) {
232                         super(layout, y, "On-board Data Logging");
233                 }
234         }
235
236         LoggingReady logging_ready;
237
238         class GPSLocked extends LaunchStatus {
239                 void show (AltosState state, int crc_errors) {
240                         show("%4d sats", state.gps.nsat);
241                         lights.set(state.gps.locked && state.gps.nsat >= 4);
242                 }
243                 public GPSLocked (GridBagLayout layout, int y) {
244                         super (layout, y, "GPS Locked");
245                 }
246         }
247
248         GPSLocked gps_locked;
249
250         class GPSReady extends LaunchStatus {
251                 void show (AltosState state, int crc_errors) {
252                         if (state.gps_ready)
253                                 show("Ready");
254                         else
255                                 show("Waiting %d", state.gps_waiting);
256                         lights.set(state.gps_ready);
257                 }
258                 public GPSReady (GridBagLayout layout, int y) {
259                         super (layout, y, "GPS Ready");
260                 }
261         }
262
263         GPSReady gps_ready;
264
265         String pos(double p, String pos, String neg) {
266                 String  h = pos;
267                 if (p < 0) {
268                         h = neg;
269                         p = -p;
270                 }
271                 int deg = (int) Math.floor(p);
272                 double min = (p - Math.floor(p)) * 60.0;
273                 return String.format("%s %4d° %9.6f", h, deg, min);
274         }
275
276         class PadLat extends LaunchValue {
277                 void show (AltosState state, int crc_errors) {
278                         if (state.state < AltosLib.ao_flight_pad && state.gps != null) {
279                                 show(pos(state.gps.lat,"N", "S"));
280                                 set_label("Latitude");
281                         } else { 
282                                 show(pos(state.pad_lat,"N", "S"));
283                                 set_label("Pad Latitude");
284                         }
285                 }
286                 public PadLat (GridBagLayout layout, int y) {
287                         super (layout, y, "Pad Latitude");
288                 }
289         }
290
291         PadLat pad_lat;
292
293         class PadLon extends LaunchValue {
294                 void show (AltosState state, int crc_errors) {
295                         if (state.state < AltosLib.ao_flight_pad && state.gps != null) {
296                                 show(pos(state.gps.lon,"E", "W"));
297                                 set_label("Longitude");
298                         } else { 
299                                 show(pos(state.pad_lon,"E", "W"));
300                                 set_label("Pad Longitude");
301                         }
302                 }
303                 public PadLon (GridBagLayout layout, int y) {
304                         super (layout, y, "Pad Longitude");
305                 }
306         }
307
308         PadLon pad_lon;
309
310         class PadAlt extends LaunchValue {
311                 void show (AltosState state, int crc_errors) {
312                         if (state.state < AltosLib.ao_flight_pad && state.gps != null) {
313                                 show("%4.0f m", state.gps.alt);
314                                 set_label("Altitude");
315                         } else {
316                                 if (state.pad_alt == AltosRecord.MISSING)
317                                         hide();
318                                 else {
319                                         show("%4.0f m", state.pad_alt);
320                                         set_label("Pad Altitude");
321                                 }
322                         }
323                 }
324                 public PadAlt (GridBagLayout layout, int y) {
325                         super (layout, y, "Pad Altitude");
326                 }
327         }
328
329         PadAlt pad_alt;
330
331         public void reset() {
332                 battery.reset();
333                 apogee.reset();
334                 main.reset();
335                 logging_ready.reset();
336                 gps_locked.reset();
337                 gps_ready.reset();
338                 pad_lat.reset();
339                 pad_lon.reset();
340                 pad_alt.reset();
341         }
342
343         public void set_font() {
344                 battery.set_font();
345                 apogee.set_font();
346                 main.set_font();
347                 logging_ready.set_font();
348                 gps_locked.set_font();
349                 gps_ready.set_font();
350                 pad_lat.set_font();
351                 pad_lon.set_font();
352                 pad_alt.set_font();
353         }
354         
355         public void show(AltosState state, int crc_errors) {
356                 battery.show(state, crc_errors);
357                 if (state.drogue_sense == AltosRecord.MISSING)
358                         apogee.hide();
359                 else
360                         apogee.show(state, crc_errors);
361                 if (state.main_sense == AltosRecord.MISSING)
362                         main.hide();
363                 else
364                         main.show(state, crc_errors);
365                 logging_ready.show(state, crc_errors);
366                 pad_alt.show(state, crc_errors);
367                 if (state.gps != null && state.gps.connected) {
368                         gps_locked.show(state, crc_errors);
369                         gps_ready.show(state, crc_errors);
370                         pad_lat.show(state, crc_errors);
371                         pad_lon.show(state, crc_errors);
372                 } else {
373                         gps_locked.hide();
374                         gps_ready.hide();
375                         pad_lat.hide();
376                         pad_lon.hide();
377                 }
378         }
379
380         public AltosPad() {
381                 layout = new GridBagLayout();
382
383                 setLayout(layout);
384
385                 /* Elements in pad display:
386                  *
387                  * Battery voltage
388                  * Igniter continuity
389                  * GPS lock status
390                  * GPS ready status
391                  * GPS location
392                  * Pad altitude
393                  * RSSI
394                  */
395                 battery = new Battery(layout, 0);
396                 apogee = new Apogee(layout, 1);
397                 main = new Main(layout, 2);
398                 logging_ready = new LoggingReady(layout, 3);
399                 gps_locked = new GPSLocked(layout, 4);
400                 gps_ready = new GPSReady(layout, 5);
401                 pad_lat = new PadLat(layout, 6);
402                 pad_lon = new PadLon(layout, 7);
403                 pad_alt = new PadAlt(layout, 8);
404         }
405 }