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