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