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