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