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