altosui: Add 'On-board Data Logging' indicator to pad tab
[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 java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.LinkedBlockingQueue;
30
31 public class AltosPad extends JComponent implements AltosFlightDisplay {
32         GridBagLayout   layout;
33
34         public class LaunchStatus {
35                 JLabel          label;
36                 JTextField      value;
37                 AltosLights     lights;
38
39                 void show(AltosState state, int crc_errors) {}
40                 void reset() {
41                         value.setText("");
42                         lights.set(false);
43                 }
44
45                 public void show() {
46                         label.setVisible(true);
47                         value.setVisible(true);
48                         lights.setVisible(true);
49                 }
50
51                 public void hide() {
52                         label.setVisible(false);
53                         value.setVisible(false);
54                         lights.setVisible(false);
55                 }
56
57                 public LaunchStatus (GridBagLayout layout, int y, String text) {
58                         GridBagConstraints      c = new GridBagConstraints();
59                         c.weighty = 1;
60
61                         lights = new AltosLights();
62                         c.gridx = 0; c.gridy = y;
63                         c.anchor = GridBagConstraints.CENTER;
64                         c.fill = GridBagConstraints.VERTICAL;
65                         c.weightx = 0;
66                         layout.setConstraints(lights, c);
67                         add(lights);
68
69                         label = new JLabel(text);
70                         label.setFont(Altos.label_font);
71                         label.setHorizontalAlignment(SwingConstants.LEFT);
72                         c.gridx = 1; c.gridy = y;
73                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
74                         c.anchor = GridBagConstraints.WEST;
75                         c.fill = GridBagConstraints.VERTICAL;
76                         c.weightx = 0;
77                         layout.setConstraints(label, c);
78                         add(label);
79
80                         value = new JTextField(Altos.text_width);
81                         value.setFont(Altos.value_font);
82                         value.setHorizontalAlignment(SwingConstants.RIGHT);
83                         c.gridx = 2; c.gridy = y;
84                         c.anchor = GridBagConstraints.WEST;
85                         c.fill = GridBagConstraints.BOTH;
86                         c.weightx = 1;
87                         layout.setConstraints(value, c);
88                         add(value);
89
90                 }
91         }
92
93         public class LaunchValue {
94                 JLabel          label;
95                 JTextField      value;
96                 void show(AltosState state, int crc_errors) {}
97
98                 void show() {
99                         label.setVisible(true);
100                         value.setVisible(true);
101                 }
102
103                 void hide() {
104                         label.setVisible(false);
105                         value.setVisible(false);
106                 }
107
108                 void reset() {
109                         value.setText("");
110                 }
111                 public LaunchValue (GridBagLayout layout, int y, String text) {
112                         GridBagConstraints      c = new GridBagConstraints();
113                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
114                         c.weighty = 1;
115
116                         label = new JLabel(text);
117                         label.setFont(Altos.label_font);
118                         label.setHorizontalAlignment(SwingConstants.LEFT);
119                         c.gridx = 1; c.gridy = y;
120                         c.anchor = GridBagConstraints.WEST;
121                         c.fill = GridBagConstraints.VERTICAL;
122                         c.weightx = 0;
123                         layout.setConstraints(label, c);
124                         add(label);
125
126                         value = new JTextField(Altos.text_width);
127                         value.setFont(Altos.value_font);
128                         value.setHorizontalAlignment(SwingConstants.RIGHT);
129                         c.gridx = 2; c.gridy = y;
130                         c.anchor = GridBagConstraints.EAST;
131                         c.fill = GridBagConstraints.BOTH;
132                         c.weightx = 1;
133                         layout.setConstraints(value, c);
134                         add(value);
135                 }
136         }
137
138         class Battery extends LaunchStatus {
139                 void show (AltosState state, int crc_errors) {
140                         value.setText(String.format("%4.2f V", state.battery));
141                         lights.set(state.battery > 3.7);
142                 }
143                 public Battery (GridBagLayout layout, int y) {
144                         super(layout, y, "Battery Voltage");
145                 }
146         }
147
148         Battery battery;
149
150         class Apogee extends LaunchStatus {
151                 void show (AltosState state, int crc_errors) {
152                         show();
153                         value.setText(String.format("%4.2f V", state.drogue_sense));
154                         lights.set(state.drogue_sense > 3.2);
155                 }
156                 public Apogee (GridBagLayout layout, int y) {
157                         super(layout, y, "Apogee Igniter Voltage");
158                 }
159         }
160
161         Apogee apogee;
162
163         class Main extends LaunchStatus {
164                 void show (AltosState state, int crc_errors) {
165                         show();
166                         value.setText(String.format("%4.2f V", state.main_sense));
167                         lights.set(state.main_sense > 3.2);
168                 }
169                 public Main (GridBagLayout layout, int y) {
170                         super(layout, y, "Main Igniter Voltage");
171                 }
172         }
173
174         Main main;
175
176         class LoggingReady extends LaunchStatus {
177                 void show (AltosState state, int crc_errors) {
178                         show();
179                         System.out.printf("flight %d state %d\n", state.data.flight, state.data.state);
180                         if (state.data.flight != 0) {
181                                 if (state.data.state <= Altos.ao_flight_pad)
182                                         value.setText("Ready to record");
183                                 else if (state.data.state < Altos.ao_flight_landed)
184                                         value.setText("Recording data");
185                                 else
186                                         value.setText("Recorded data");
187                         }
188                         else
189                                 value.setText("Storage full");
190                         lights.set(state.data.flight != 0);
191                 }
192                 public LoggingReady (GridBagLayout layout, int y) {
193                         super(layout, y, "On-board Data Logging");
194                 }
195         }
196
197         LoggingReady logging_ready;
198
199         class GPSLocked extends LaunchStatus {
200                 void show (AltosState state, int crc_errors) {
201                         show();
202                         value.setText(String.format("%4d sats", state.gps.nsat));
203                         lights.set(state.gps.locked && state.gps.nsat >= 4);
204                 }
205                 public GPSLocked (GridBagLayout layout, int y) {
206                         super (layout, y, "GPS Locked");
207                 }
208         }
209
210         GPSLocked gps_locked;
211
212         class GPSReady extends LaunchStatus {
213                 void show (AltosState state, int crc_errors) {
214                         show();
215                         if (state.gps_ready)
216                                 value.setText("Ready");
217                         else
218                                 value.setText(String.format("Waiting %d", state.gps_waiting));
219                         lights.set(state.gps_ready);
220                 }
221                 public GPSReady (GridBagLayout layout, int y) {
222                         super (layout, y, "GPS Ready");
223                 }
224         }
225
226         GPSReady gps_ready;
227
228         String pos(double p, String pos, String neg) {
229                 String  h = pos;
230                 if (p < 0) {
231                         h = neg;
232                         p = -p;
233                 }
234                 int deg = (int) Math.floor(p);
235                 double min = (p - Math.floor(p)) * 60.0;
236                 return String.format("%s %4d° %9.6f", h, deg, min);
237         }
238
239         class PadLat extends LaunchValue {
240                 void show (AltosState state, int crc_errors) {
241                         show();
242                         value.setText(pos(state.pad_lat,"N", "S"));
243                 }
244                 public PadLat (GridBagLayout layout, int y) {
245                         super (layout, y, "Pad Latitude");
246                 }
247         }
248
249         PadLat pad_lat;
250
251         class PadLon extends LaunchValue {
252                 void show (AltosState state, int crc_errors) {
253                         show();
254                         value.setText(pos(state.pad_lon,"E", "W"));
255                 }
256                 public PadLon (GridBagLayout layout, int y) {
257                         super (layout, y, "Pad Longitude");
258                 }
259         }
260
261         PadLon pad_lon;
262
263         class PadAlt extends LaunchValue {
264                 void show (AltosState state, int crc_errors) {
265                         value.setText(String.format("%4.0f m", state.pad_alt));
266                 }
267                 public PadAlt (GridBagLayout layout, int y) {
268                         super (layout, y, "Pad Altitude");
269                 }
270         }
271
272         PadAlt pad_alt;
273
274         public void reset() {
275                 battery.reset();
276                 apogee.reset();
277                 main.reset();
278                 logging_ready.reset();
279                 gps_locked.reset();
280                 gps_ready.reset();
281                 pad_lat.reset();
282                 pad_lon.reset();
283                 pad_alt.reset();
284         }
285
286         public void show(AltosState state, int crc_errors) {
287                 battery.show(state, crc_errors);
288                 if (state.drogue_sense == AltosRecord.MISSING)
289                         apogee.hide();
290                 else
291                         apogee.show(state, crc_errors);
292                 if (state.main_sense == AltosRecord.MISSING)
293                         main.hide();
294                 else
295                         main.show(state, crc_errors);
296                 logging_ready.show(state, crc_errors);
297                 pad_alt.show(state, crc_errors);
298                 if (state.gps != null && state.gps.connected) {
299                         gps_locked.show(state, crc_errors);
300                         gps_ready.show(state, crc_errors);
301                         pad_lat.show(state, crc_errors);
302                         pad_lon.show(state, crc_errors);
303                 } else {
304                         gps_locked.hide();
305                         gps_ready.hide();
306                         pad_lat.hide();
307                         pad_lon.hide();
308                 }
309         }
310
311         public AltosPad() {
312                 layout = new GridBagLayout();
313
314                 setLayout(layout);
315
316                 /* Elements in pad display:
317                  *
318                  * Battery voltage
319                  * Igniter continuity
320                  * GPS lock status
321                  * GPS ready status
322                  * GPS location
323                  * Pad altitude
324                  * RSSI
325                  */
326                 battery = new Battery(layout, 0);
327                 apogee = new Apogee(layout, 1);
328                 main = new Main(layout, 2);
329                 logging_ready = new LoggingReady(layout, 3);
330                 gps_locked = new GPSLocked(layout, 4);
331                 gps_ready = new GPSReady(layout, 5);
332                 pad_lat = new PadLat(layout, 6);
333                 pad_lon = new PadLon(layout, 7);
334                 pad_alt = new PadAlt(layout, 8);
335         }
336 }