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