66cb4cfca83f3409d72763d05703a796f07b1515
[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 LaunchStatus (GridBagLayout layout, int y, String text) {
70                         GridBagConstraints      c = new GridBagConstraints();
71                         c.weighty = 1;
72
73                         lights = new AltosLights();
74                         c.gridx = 0; c.gridy = y;
75                         c.anchor = GridBagConstraints.CENTER;
76                         c.fill = GridBagConstraints.VERTICAL;
77                         c.weightx = 0;
78                         layout.setConstraints(lights, c);
79                         add(lights);
80
81                         label = new JLabel(text);
82                         label.setFont(Altos.label_font);
83                         label.setHorizontalAlignment(SwingConstants.LEFT);
84                         c.gridx = 1; c.gridy = y;
85                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
86                         c.anchor = GridBagConstraints.WEST;
87                         c.fill = GridBagConstraints.VERTICAL;
88                         c.weightx = 0;
89                         layout.setConstraints(label, c);
90                         add(label);
91
92                         value = new JTextField(Altos.text_width);
93                         value.setFont(Altos.value_font);
94                         value.setHorizontalAlignment(SwingConstants.RIGHT);
95                         c.gridx = 2; c.gridy = y;
96                         c.anchor = GridBagConstraints.WEST;
97                         c.fill = GridBagConstraints.BOTH;
98                         c.weightx = 1;
99                         layout.setConstraints(value, c);
100                         add(value);
101
102                 }
103         }
104
105         public class LaunchValue {
106                 JLabel          label;
107                 JTextField      value;
108                 void show(AltosState state, int crc_errors) {}
109
110                 void show() {
111                         label.setVisible(true);
112                         value.setVisible(true);
113                 }
114
115                 void hide() {
116                         label.setVisible(false);
117                         value.setVisible(false);
118                 }
119
120                 public void set_font() {
121                         label.setFont(Altos.label_font);
122                         value.setFont(Altos.value_font);
123                 }
124
125                 void show(String s) {
126                         show();
127                         value.setText(s);
128                 }
129
130                 void show(AltosUnits units, double v) {
131                         show(units.show(8, v));
132                 }
133
134                 void show(String format, double v) {
135                         show(String.format(format, v));
136                 }
137
138                 void reset() {
139                         value.setText("");
140                 }
141
142                 public LaunchValue (GridBagLayout layout, int y, String text) {
143                         GridBagConstraints      c = new GridBagConstraints();
144                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
145                         c.weighty = 1;
146
147                         label = new JLabel(text);
148                         label.setFont(Altos.label_font);
149                         label.setHorizontalAlignment(SwingConstants.LEFT);
150                         c.gridx = 1; c.gridy = y;
151                         c.anchor = GridBagConstraints.WEST;
152                         c.fill = GridBagConstraints.VERTICAL;
153                         c.weightx = 0;
154                         layout.setConstraints(label, c);
155                         add(label);
156
157                         value = new JTextField(Altos.text_width);
158                         value.setFont(Altos.value_font);
159                         value.setHorizontalAlignment(SwingConstants.RIGHT);
160                         c.gridx = 2; c.gridy = y;
161                         c.anchor = GridBagConstraints.EAST;
162                         c.fill = GridBagConstraints.BOTH;
163                         c.weightx = 1;
164                         layout.setConstraints(value, c);
165                         add(value);
166                 }
167         }
168
169         class Battery extends LaunchStatus {
170                 void show (AltosState state, int crc_errors) {
171                         if (state.battery == AltosRecord.MISSING)
172                                 hide();
173                         else {
174                                 show("%4.2f V", state.battery);
175                                 lights.set(state.battery > 3.7);
176                         }
177                 }
178                 public Battery (GridBagLayout layout, int y) {
179                         super(layout, y, "Battery Voltage");
180                 }
181         }
182
183         Battery battery;
184
185         class Apogee extends LaunchStatus {
186                 void show (AltosState state, int crc_errors) {
187                         show("%4.2f V", state.drogue_sense);
188                         lights.set(state.drogue_sense > 3.2);
189                 }
190                 public Apogee (GridBagLayout layout, int y) {
191                         super(layout, y, "Apogee Igniter Voltage");
192                 }
193         }
194
195         Apogee apogee;
196
197         class Main extends LaunchStatus {
198                 void show (AltosState state, int crc_errors) {
199                         show("%4.2f V", state.main_sense);
200                         lights.set(state.main_sense > 3.2);
201                 }
202                 public Main (GridBagLayout layout, int y) {
203                         super(layout, y, "Main Igniter Voltage");
204                 }
205         }
206
207         Main main;
208
209         class LoggingReady extends LaunchStatus {
210                 void show (AltosState state, int crc_errors) {
211                         if (state.data.flight != 0) {
212                                 if (state.data.state <= Altos.ao_flight_pad)
213                                         show("Ready to record");
214                                 else if (state.data.state < Altos.ao_flight_landed)
215                                         show("Recording data");
216                                 else
217                                         show("Recorded data");
218                         }
219                         else
220                                 show("Storage full");
221                         lights.set(state.data.flight != 0);
222                 }
223                 public LoggingReady (GridBagLayout layout, int y) {
224                         super(layout, y, "On-board Data Logging");
225                 }
226         }
227
228         LoggingReady logging_ready;
229
230         class GPSLocked extends LaunchStatus {
231                 void show (AltosState state, int crc_errors) {
232                         show("%4d sats", state.gps.nsat);
233                         lights.set(state.gps.locked && state.gps.nsat >= 4);
234                 }
235                 public GPSLocked (GridBagLayout layout, int y) {
236                         super (layout, y, "GPS Locked");
237                 }
238         }
239
240         GPSLocked gps_locked;
241
242         class GPSReady extends LaunchStatus {
243                 void show (AltosState state, int crc_errors) {
244                         if (state.gps_ready)
245                                 show("Ready");
246                         else
247                                 show("Waiting %d", state.gps_waiting);
248                         lights.set(state.gps_ready);
249                 }
250                 public GPSReady (GridBagLayout layout, int y) {
251                         super (layout, y, "GPS Ready");
252                 }
253         }
254
255         GPSReady gps_ready;
256
257         String pos(double p, String pos, String neg) {
258                 String  h = pos;
259                 if (p < 0) {
260                         h = neg;
261                         p = -p;
262                 }
263                 int deg = (int) Math.floor(p);
264                 double min = (p - Math.floor(p)) * 60.0;
265                 return String.format("%s %4d° %9.6f", h, deg, min);
266         }
267
268         class PadLat extends LaunchValue {
269                 void show (AltosState state, int crc_errors) {
270                         show(pos(state.pad_lat,"N", "S"));
271                 }
272                 public PadLat (GridBagLayout layout, int y) {
273                         super (layout, y, "Pad Latitude");
274                 }
275         }
276
277         PadLat pad_lat;
278
279         class PadLon extends LaunchValue {
280                 void show (AltosState state, int crc_errors) {
281                         show(pos(state.pad_lon,"E", "W"));
282                 }
283                 public PadLon (GridBagLayout layout, int y) {
284                         super (layout, y, "Pad Longitude");
285                 }
286         }
287
288         PadLon pad_lon;
289
290         class PadAlt extends LaunchValue {
291                 void show (AltosState state, int crc_errors) {
292                         if (state.pad_alt == AltosRecord.MISSING)
293                                 hide();
294                         else
295                                 show("%4.0f m", state.pad_alt);
296                 }
297                 public PadAlt (GridBagLayout layout, int y) {
298                         super (layout, y, "Pad Altitude");
299                 }
300         }
301
302         PadAlt pad_alt;
303
304         public void reset() {
305                 battery.reset();
306                 apogee.reset();
307                 main.reset();
308                 logging_ready.reset();
309                 gps_locked.reset();
310                 gps_ready.reset();
311                 pad_lat.reset();
312                 pad_lon.reset();
313                 pad_alt.reset();
314         }
315
316         public void set_font() {
317                 battery.set_font();
318                 apogee.set_font();
319                 main.set_font();
320                 logging_ready.set_font();
321                 gps_locked.set_font();
322                 gps_ready.set_font();
323                 pad_lat.set_font();
324                 pad_lon.set_font();
325                 pad_alt.set_font();
326         }
327         
328         public void show(AltosState state, int crc_errors) {
329                 battery.show(state, crc_errors);
330                 if (state.drogue_sense == AltosRecord.MISSING)
331                         apogee.hide();
332                 else
333                         apogee.show(state, crc_errors);
334                 if (state.main_sense == AltosRecord.MISSING)
335                         main.hide();
336                 else
337                         main.show(state, crc_errors);
338                 logging_ready.show(state, crc_errors);
339                 pad_alt.show(state, crc_errors);
340                 if (state.gps != null && state.gps.connected) {
341                         gps_locked.show(state, crc_errors);
342                         gps_ready.show(state, crc_errors);
343                         pad_lat.show(state, crc_errors);
344                         pad_lon.show(state, crc_errors);
345                 } else {
346                         gps_locked.hide();
347                         gps_ready.hide();
348                         pad_lat.hide();
349                         pad_lon.hide();
350                 }
351         }
352
353         public AltosPad() {
354                 layout = new GridBagLayout();
355
356                 setLayout(layout);
357
358                 /* Elements in pad display:
359                  *
360                  * Battery voltage
361                  * Igniter continuity
362                  * GPS lock status
363                  * GPS ready status
364                  * GPS location
365                  * Pad altitude
366                  * RSSI
367                  */
368                 battery = new Battery(layout, 0);
369                 apogee = new Apogee(layout, 1);
370                 main = new Main(layout, 2);
371                 logging_ready = new LoggingReady(layout, 3);
372                 gps_locked = new GPSLocked(layout, 4);
373                 gps_ready = new GPSReady(layout, 5);
374                 pad_lat = new PadLat(layout, 6);
375                 pad_lon = new PadLon(layout, 7);
376                 pad_alt = new PadAlt(layout, 8);
377         }
378 }