altosui: Replace flight status table with labels, fix resize.
[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(10, 10, 10, 10);
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(10, 10, 10, 10);
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 GPS 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_ready);
158                 }
159                 public GPS (GridBagLayout layout, int y) {
160                         super (layout, y, "GPS Status");
161                 }
162         }
163
164         GPS gps;
165
166         String pos(double p, String pos, String neg) {
167                 String  h = pos;
168                 if (p < 0) {
169                         h = neg;
170                         p = -p;
171                 }
172                 int deg = (int) Math.floor(p);
173                 double min = (p - Math.floor(p)) * 60.0;
174                 return String.format("%s %4d° %9.6f", h, deg, min);
175         }
176
177         class PadLat extends LaunchValue {
178                 void show (AltosState state, int crc_errors) {
179                         value.setText(pos(state.pad_lat,"N", "S"));
180                 }
181                 public PadLat (GridBagLayout layout, int y) {
182                         super (layout, y, "Pad Latitude");
183                 }
184         }
185
186         PadLat pad_lat;
187
188         class PadLon extends LaunchValue {
189                 void show (AltosState state, int crc_errors) {
190                         value.setText(pos(state.pad_lon,"E", "W"));
191                 }
192                 public PadLon (GridBagLayout layout, int y) {
193                         super (layout, y, "Pad Longitude");
194                 }
195         }
196
197         PadLon pad_lon;
198
199         class PadAlt extends LaunchValue {
200                 void show (AltosState state, int crc_errors) {
201                         value.setText(String.format("%4.0f m", state.pad_alt));
202                 }
203                 public PadAlt (GridBagLayout layout, int y) {
204                         super (layout, y, "Pad Altitude");
205                 }
206         }
207
208         PadAlt pad_alt;
209
210         public void reset() {
211                 battery.reset();
212                 apogee.reset();
213                 main.reset();
214                 gps.reset();
215                 pad_lat.reset();
216                 pad_lon.reset();
217                 pad_alt.reset();
218         }
219
220         public void show(AltosState state, int crc_errors) {
221                 battery.show(state, crc_errors);
222                 apogee.show(state, crc_errors);
223                 main.show(state, crc_errors);
224                 gps.show(state, crc_errors);
225                 pad_lat.show(state, crc_errors);
226                 pad_lon.show(state, crc_errors);
227                 pad_alt.show(state, crc_errors);
228         }
229
230         public AltosPad() {
231                 layout = new GridBagLayout();
232
233                 label_font = new Font("Dialog", Font.PLAIN, 24);
234                 value_font = new Font("Monospaced", Font.PLAIN, 24);
235                 setLayout(layout);
236
237                 /* Elements in pad display:
238                  *
239                  * Battery voltage
240                  * Igniter continuity
241                  * GPS lock status and location
242                  * Pad altitude
243                  * RSSI
244                  */
245                 battery = new Battery(layout, 0);
246                 apogee = new Apogee(layout, 1);
247                 main = new Main(layout, 2);
248                 gps = new GPS(layout, 3);
249                 pad_lat = new PadLat(layout, 4);
250                 pad_lon = new PadLon(layout, 5);
251                 pad_alt = new PadAlt(layout, 6);
252         }
253 }