altos: Switch ao_rssi.c __xdata to __pdata
[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                         value.setText(String.format("%4.2f V", state.drogue_sense));
153                         lights.set(state.drogue_sense > 3.2);
154                 }
155                 public Apogee (GridBagLayout layout, int y) {
156                         super(layout, y, "Apogee Igniter Voltage");
157                 }
158         }
159
160         Apogee apogee;
161
162         class Main extends LaunchStatus {
163                 void show (AltosState state, int crc_errors) {
164                         value.setText(String.format("%4.2f V", state.main_sense));
165                         lights.set(state.main_sense > 3.2);
166                 }
167                 public Main (GridBagLayout layout, int y) {
168                         super(layout, y, "Main Igniter Voltage");
169                 }
170         }
171
172         Main main;
173
174         class GPSLocked extends LaunchStatus {
175                 void show (AltosState state, int crc_errors) {
176                         show();
177                         value.setText(String.format("%4d sats", state.gps.nsat));
178                         lights.set(state.gps.locked && state.gps.nsat >= 4);
179                 }
180                 public GPSLocked (GridBagLayout layout, int y) {
181                         super (layout, y, "GPS Locked");
182                 }
183         }
184
185         GPSLocked gps_locked;
186
187         class GPSReady extends LaunchStatus {
188                 void show (AltosState state, int crc_errors) {
189                         show();
190                         if (state.gps_ready)
191                                 value.setText("Ready");
192                         else
193                                 value.setText(String.format("Waiting %d", state.gps_waiting));
194                         lights.set(state.gps_ready);
195                 }
196                 public GPSReady (GridBagLayout layout, int y) {
197                         super (layout, y, "GPS Ready");
198                 }
199         }
200
201         GPSReady gps_ready;
202
203         String pos(double p, String pos, String neg) {
204                 String  h = pos;
205                 if (p < 0) {
206                         h = neg;
207                         p = -p;
208                 }
209                 int deg = (int) Math.floor(p);
210                 double min = (p - Math.floor(p)) * 60.0;
211                 return String.format("%s %4d° %9.6f", h, deg, min);
212         }
213
214         class PadLat extends LaunchValue {
215                 void show (AltosState state, int crc_errors) {
216                         show();
217                         value.setText(pos(state.pad_lat,"N", "S"));
218                 }
219                 public PadLat (GridBagLayout layout, int y) {
220                         super (layout, y, "Pad Latitude");
221                 }
222         }
223
224         PadLat pad_lat;
225
226         class PadLon extends LaunchValue {
227                 void show (AltosState state, int crc_errors) {
228                         show();
229                         value.setText(pos(state.pad_lon,"E", "W"));
230                 }
231                 public PadLon (GridBagLayout layout, int y) {
232                         super (layout, y, "Pad Longitude");
233                 }
234         }
235
236         PadLon pad_lon;
237
238         class PadAlt extends LaunchValue {
239                 void show (AltosState state, int crc_errors) {
240                         value.setText(String.format("%4.0f m", state.pad_alt));
241                 }
242                 public PadAlt (GridBagLayout layout, int y) {
243                         super (layout, y, "Pad Altitude");
244                 }
245         }
246
247         PadAlt pad_alt;
248
249         public void reset() {
250                 battery.reset();
251                 apogee.reset();
252                 main.reset();
253                 gps_locked.reset();
254                 gps_ready.reset();
255                 pad_lat.reset();
256                 pad_lon.reset();
257                 pad_alt.reset();
258         }
259
260         public void show(AltosState state, int crc_errors) {
261                 battery.show(state, crc_errors);
262                 apogee.show(state, crc_errors);
263                 main.show(state, crc_errors);
264                 pad_alt.show(state, crc_errors);
265                 if (state.gps != null) {
266                         gps_locked.show(state, crc_errors);
267                         gps_ready.show(state, crc_errors);
268                         pad_lat.show(state, crc_errors);
269                         pad_lon.show(state, crc_errors);
270                 } else {
271                         gps_locked.hide();
272                         gps_ready.hide();
273                         pad_lat.hide();
274                         pad_lon.hide();
275                 }
276         }
277
278         public AltosPad() {
279                 layout = new GridBagLayout();
280
281                 setLayout(layout);
282
283                 /* Elements in pad display:
284                  *
285                  * Battery voltage
286                  * Igniter continuity
287                  * GPS lock status
288                  * GPS ready status
289                  * GPS location
290                  * Pad altitude
291                  * RSSI
292                  */
293                 battery = new Battery(layout, 0);
294                 apogee = new Apogee(layout, 1);
295                 main = new Main(layout, 2);
296                 gps_locked = new GPSLocked(layout, 3);
297                 gps_ready = new GPSReady(layout, 4);
298                 pad_lat = new PadLat(layout, 5);
299                 pad_lon = new PadLon(layout, 6);
300                 pad_alt = new PadAlt(layout, 7);
301         }
302 }