Merge branch 'buttonbox'
[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 LaunchStatus (GridBagLayout layout, int y, String text) {
46                         GridBagConstraints      c = new GridBagConstraints();
47                         c.weighty = 1;
48
49                         lights = new AltosLights();
50                         c.gridx = 0; c.gridy = y;
51                         c.anchor = GridBagConstraints.CENTER;
52                         c.fill = GridBagConstraints.VERTICAL;
53                         c.weightx = 0;
54                         layout.setConstraints(lights, c);
55                         add(lights);
56
57                         label = new JLabel(text);
58                         label.setFont(Altos.label_font);
59                         label.setHorizontalAlignment(SwingConstants.LEFT);
60                         c.gridx = 1; c.gridy = y;
61                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
62                         c.anchor = GridBagConstraints.WEST;
63                         c.fill = GridBagConstraints.VERTICAL;
64                         c.weightx = 0;
65                         layout.setConstraints(label, c);
66                         add(label);
67
68                         value = new JTextField(Altos.text_width);
69                         value.setFont(Altos.value_font);
70                         value.setHorizontalAlignment(SwingConstants.RIGHT);
71                         c.gridx = 2; c.gridy = y;
72                         c.anchor = GridBagConstraints.WEST;
73                         c.fill = GridBagConstraints.BOTH;
74                         c.weightx = 1;
75                         layout.setConstraints(value, c);
76                         add(value);
77
78                 }
79         }
80
81         public class LaunchValue {
82                 JLabel          label;
83                 JTextField      value;
84                 void show(AltosState state, int crc_errors) {}
85
86                 void reset() {
87                         value.setText("");
88                 }
89                 public LaunchValue (GridBagLayout layout, int y, String text) {
90                         GridBagConstraints      c = new GridBagConstraints();
91                         c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
92                         c.weighty = 1;
93
94                         label = new JLabel(text);
95                         label.setFont(Altos.label_font);
96                         label.setHorizontalAlignment(SwingConstants.LEFT);
97                         c.gridx = 1; c.gridy = y;
98                         c.anchor = GridBagConstraints.WEST;
99                         c.fill = GridBagConstraints.VERTICAL;
100                         c.weightx = 0;
101                         layout.setConstraints(label, c);
102                         add(label);
103
104                         value = new JTextField(Altos.text_width);
105                         value.setFont(Altos.value_font);
106                         value.setHorizontalAlignment(SwingConstants.RIGHT);
107                         c.gridx = 2; c.gridy = y;
108                         c.anchor = GridBagConstraints.EAST;
109                         c.fill = GridBagConstraints.BOTH;
110                         c.weightx = 1;
111                         layout.setConstraints(value, c);
112                         add(value);
113                 }
114         }
115
116         class Battery extends LaunchStatus {
117                 void show (AltosState state, int crc_errors) {
118                         value.setText(String.format("%4.2f V", state.battery));
119                         lights.set(state.battery > 3.7);
120                 }
121                 public Battery (GridBagLayout layout, int y) {
122                         super(layout, y, "Battery Voltage");
123                 }
124         }
125
126         Battery battery;
127
128         class Apogee extends LaunchStatus {
129                 void show (AltosState state, int crc_errors) {
130                         value.setText(String.format("%4.2f V", state.drogue_sense));
131                         lights.set(state.drogue_sense > 3.2);
132                 }
133                 public Apogee (GridBagLayout layout, int y) {
134                         super(layout, y, "Apogee Igniter Voltage");
135                 }
136         }
137
138         Apogee apogee;
139
140         class Main extends LaunchStatus {
141                 void show (AltosState state, int crc_errors) {
142                         value.setText(String.format("%4.2f V", state.main_sense));
143                         lights.set(state.main_sense > 3.2);
144                 }
145                 public Main (GridBagLayout layout, int y) {
146                         super(layout, y, "Main Igniter Voltage");
147                 }
148         }
149
150         Main main;
151
152         class GPSLocked extends LaunchStatus {
153                 void show (AltosState state, int crc_errors) {
154                         value.setText(String.format("%4d sats", state.gps.nsat));
155                         lights.set(state.gps.locked);
156                 }
157                 public GPSLocked (GridBagLayout layout, int y) {
158                         super (layout, y, "GPS Locked");
159                 }
160         }
161
162         GPSLocked gps_locked;
163
164         class GPSReady extends LaunchStatus {
165                 void show (AltosState state, int crc_errors) {
166                         if (state.gps_ready)
167                                 value.setText("Ready");
168                         else
169                                 value.setText(String.format("Waiting %d", state.gps_waiting));
170                         lights.set(state.gps_ready);
171                 }
172                 public GPSReady (GridBagLayout layout, int y) {
173                         super (layout, y, "GPS Ready");
174                 }
175         }
176
177         GPSReady gps_ready;
178
179         String pos(double p, String pos, String neg) {
180                 String  h = pos;
181                 if (p < 0) {
182                         h = neg;
183                         p = -p;
184                 }
185                 int deg = (int) Math.floor(p);
186                 double min = (p - Math.floor(p)) * 60.0;
187                 return String.format("%s %4d° %9.6f", h, deg, min);
188         }
189
190         class PadLat extends LaunchValue {
191                 void show (AltosState state, int crc_errors) {
192                         value.setText(pos(state.pad_lat,"N", "S"));
193                 }
194                 public PadLat (GridBagLayout layout, int y) {
195                         super (layout, y, "Pad Latitude");
196                 }
197         }
198
199         PadLat pad_lat;
200
201         class PadLon extends LaunchValue {
202                 void show (AltosState state, int crc_errors) {
203                         value.setText(pos(state.pad_lon,"E", "W"));
204                 }
205                 public PadLon (GridBagLayout layout, int y) {
206                         super (layout, y, "Pad Longitude");
207                 }
208         }
209
210         PadLon pad_lon;
211
212         class PadAlt extends LaunchValue {
213                 void show (AltosState state, int crc_errors) {
214                         value.setText(String.format("%4.0f m", state.pad_alt));
215                 }
216                 public PadAlt (GridBagLayout layout, int y) {
217                         super (layout, y, "Pad Altitude");
218                 }
219         }
220
221         PadAlt pad_alt;
222
223         public void reset() {
224                 battery.reset();
225                 apogee.reset();
226                 main.reset();
227                 gps_locked.reset();
228                 gps_ready.reset();
229                 pad_lat.reset();
230                 pad_lon.reset();
231                 pad_alt.reset();
232         }
233
234         public void show(AltosState state, int crc_errors) {
235                 battery.show(state, crc_errors);
236                 apogee.show(state, crc_errors);
237                 main.show(state, crc_errors);
238                 gps_locked.show(state, crc_errors);
239                 gps_ready.show(state, crc_errors);
240                 pad_lat.show(state, crc_errors);
241                 pad_lon.show(state, crc_errors);
242                 pad_alt.show(state, crc_errors);
243         }
244
245         public AltosPad() {
246                 layout = new GridBagLayout();
247
248                 setLayout(layout);
249
250                 /* Elements in pad display:
251                  *
252                  * Battery voltage
253                  * Igniter continuity
254                  * GPS lock status
255                  * GPS ready status
256                  * GPS location
257                  * Pad altitude
258                  * RSSI
259                  */
260                 battery = new Battery(layout, 0);
261                 apogee = new Apogee(layout, 1);
262                 main = new Main(layout, 2);
263                 gps_locked = new GPSLocked(layout, 3);
264                 gps_ready = new GPSReady(layout, 4);
265                 pad_lat = new PadLat(layout, 5);
266                 pad_lon = new PadLon(layout, 6);
267                 pad_alt = new PadAlt(layout, 7);
268         }
269 }