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