2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
22 import org.altusmetrum.altoslib_4.*;
23 import org.altusmetrum.altosuilib_2.*;
25 public class AltosAscent extends JComponent implements AltosFlightDisplay {
29 public class AscentStatus implements AltosFontListener, AltosUnitsListener {
37 value.setVisible(true);
38 lights.setVisible(true);
39 label.setVisible(true);
43 value.setVisible(false);
44 lights.setVisible(false);
45 label.setVisible(false);
48 void show(AltosState state, AltosListenerState listener_state) {}
57 show(units.show(8, v));
60 void show(String format, double v) {
61 show(String.format(format, v));
69 public void font_size_changed(int font_size) {
70 label.setFont(Altos.label_font);
71 value.setFont(Altos.value_font);
74 public void units_changed(boolean imperial_units) {
79 public AscentStatus (GridBagLayout layout, int y, AltosUnits units, String text) {
81 GridBagConstraints c = new GridBagConstraints();
84 lights = new AltosLights();
85 c.gridx = 0; c.gridy = y;
86 c.anchor = GridBagConstraints.CENTER;
87 c.fill = GridBagConstraints.VERTICAL;
89 layout.setConstraints(lights, c);
92 label = new JLabel(text);
93 label.setFont(Altos.label_font);
94 label.setHorizontalAlignment(SwingConstants.LEFT);
95 c.gridx = 1; c.gridy = y;
96 c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
97 c.anchor = GridBagConstraints.WEST;
98 c.fill = GridBagConstraints.VERTICAL;
100 layout.setConstraints(label, c);
103 value = new JTextField(Altos.text_width);
104 value.setFont(Altos.value_font);
105 value.setHorizontalAlignment(SwingConstants.RIGHT);
106 c.gridx = 2; c.gridy = y;
108 c.anchor = GridBagConstraints.WEST;
109 c.fill = GridBagConstraints.BOTH;
111 layout.setConstraints(value, c);
117 public abstract class AscentValue implements AltosFontListener, AltosUnitsListener {
123 abstract void show(AltosState state, AltosListenerState listener_state);
130 label.setVisible(true);
131 value.setVisible(true);
134 void show(String s) {
139 void show(double v) {
141 show(units.show(8, v));
144 void show(String format, double v) {
145 show(String.format(format, v));
149 label.setVisible(false);
150 value.setVisible(false);
153 public void font_size_changed(int font_size) {
154 label.setFont(Altos.label_font);
155 value.setFont(Altos.value_font);
158 public void units_changed(boolean imperial_units) {
163 public AscentValue (GridBagLayout layout, int y, AltosUnits units, String text) {
164 GridBagConstraints c = new GridBagConstraints();
167 label = new JLabel(text);
168 label.setFont(Altos.label_font);
169 label.setHorizontalAlignment(SwingConstants.LEFT);
170 c.gridx = 1; c.gridy = y;
171 c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
172 c.anchor = GridBagConstraints.WEST;
173 c.fill = GridBagConstraints.VERTICAL;
175 layout.setConstraints(label, c);
178 value = new JTextField(Altos.text_width);
179 value.setFont(Altos.value_font);
180 value.setHorizontalAlignment(SwingConstants.RIGHT);
181 c.gridx = 2; c.gridy = y;
182 c.anchor = GridBagConstraints.WEST;
183 c.fill = GridBagConstraints.BOTH;
186 layout.setConstraints(value, c);
190 public AscentValue (GridBagLayout layout, int y, String text) {
191 this(layout, y, null, text);
195 public class AscentValueHold implements AltosFontListener, AltosUnitsListener {
198 JTextField max_value;
203 void show(AltosState state, AltosListenerState listener_state) {}
207 max_value.setText("");
208 max = AltosLib.MISSING;
211 public void font_size_changed(int font_size) {
212 label.setFont(Altos.label_font);
213 value.setFont(Altos.value_font);
214 max_value.setFont(Altos.value_font);
217 public void units_changed(boolean imperial_units) {
221 void show(double v) {
223 if (v == AltosLib.MISSING) {
224 value.setText("Missing");
226 value.setText(units.show(8, v));
227 if (v > max || max == AltosLib.MISSING)
230 if (max == AltosLib.MISSING)
231 max_value.setText("Missing");
233 max_value.setText(units.show(8, v));
237 label.setVisible(false);
238 value.setVisible(false);
239 max_value.setVisible(false);
242 public AscentValueHold (GridBagLayout layout, int y, AltosUnits units, String text) {
244 GridBagConstraints c = new GridBagConstraints();
247 label = new JLabel(text);
248 label.setFont(Altos.label_font);
249 label.setHorizontalAlignment(SwingConstants.LEFT);
250 c.gridx = 1; c.gridy = y;
251 c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
252 c.anchor = GridBagConstraints.WEST;
253 c.fill = GridBagConstraints.VERTICAL;
255 layout.setConstraints(label, c);
258 value = new JTextField(Altos.text_width);
259 value.setFont(Altos.value_font);
260 value.setHorizontalAlignment(SwingConstants.RIGHT);
261 c.gridx = 2; c.gridy = y;
262 c.anchor = GridBagConstraints.EAST;
263 c.fill = GridBagConstraints.BOTH;
265 layout.setConstraints(value, c);
268 max_value = new JTextField(Altos.text_width);
269 max_value.setFont(Altos.value_font);
270 max_value.setHorizontalAlignment(SwingConstants.RIGHT);
271 c.gridx = 3; c.gridy = y;
272 c.anchor = GridBagConstraints.EAST;
273 c.fill = GridBagConstraints.BOTH;
275 layout.setConstraints(max_value, c);
280 class Height extends AscentValueHold {
281 void show (AltosState state, AltosListenerState listener_state) {
282 show(state.height());
284 public Height (GridBagLayout layout, int y) {
285 super (layout, y, AltosConvert.height, "Height");
291 class Speed extends AscentValueHold {
292 void show (AltosState state, AltosListenerState listener_state) {
295 public Speed (GridBagLayout layout, int y) {
296 super (layout, y, AltosConvert.speed, "Speed");
302 class Accel extends AscentValueHold {
303 void show (AltosState state, AltosListenerState listener_state) {
304 show(state.acceleration());
306 public Accel (GridBagLayout layout, int y) {
307 super (layout, y, AltosConvert.accel, "Acceleration");
313 class Orient extends AscentValueHold {
314 void show (AltosState state, AltosListenerState listener_state) {
315 show(state.orient());
317 public Orient (GridBagLayout layout, int y) {
318 super (layout, y, AltosConvert.orient, "Tilt Angle");
324 String pos(double p, String pos, String neg) {
330 int deg = (int) Math.floor(p);
331 double min = (p - Math.floor(p)) * 60.0;
332 return String.format("%s %4d° %9.6f", h, deg, min);
335 class Apogee extends AscentStatus {
336 void show (AltosState state, AltosListenerState listener_state) {
337 show("%4.2f V", state.apogee_voltage);
338 lights.set(state.apogee_voltage >= AltosLib.ao_igniter_good);
340 public Apogee (GridBagLayout layout, int y) {
341 super(layout, y, null, "Apogee Igniter Voltage");
347 class Main extends AscentStatus {
348 void show (AltosState state, AltosListenerState listener_state) {
349 show("%4.2f V", state.main_voltage);
350 lights.set(state.main_voltage >= AltosLib.ao_igniter_good);
352 public Main (GridBagLayout layout, int y) {
353 super(layout, y, null, "Main Igniter Voltage");
359 class Lat extends AscentValue {
360 void show (AltosState state, AltosListenerState listener_state) {
361 if (state.gps != null && state.gps.connected && state.gps.lat != AltosLib.MISSING)
362 show(pos(state.gps.lat,"N", "S"));
366 public Lat (GridBagLayout layout, int y) {
367 super (layout, y, "Latitude");
373 class Lon extends AscentValue {
374 void show (AltosState state, AltosListenerState listener_state) {
375 if (state.gps != null && state.gps.connected && state.gps.lon != AltosLib.MISSING)
376 show(pos(state.gps.lon,"E", "W"));
380 public Lon (GridBagLayout layout, int y) {
381 super (layout, y, "Longitude");
387 public void reset() {
398 public void font_size_changed(int font_size) {
399 cur.setFont(Altos.label_font);
400 max.setFont(Altos.label_font);
401 lat.font_size_changed(font_size);
402 lon.font_size_changed(font_size);
403 main.font_size_changed(font_size);
404 apogee.font_size_changed(font_size);
405 height.font_size_changed(font_size);
406 speed.font_size_changed(font_size);
407 accel.font_size_changed(font_size);
408 orient.font_size_changed(font_size);
411 public void units_changed(boolean imperial_units) {
412 lat.units_changed(imperial_units);
413 lon.units_changed(imperial_units);
414 main.units_changed(imperial_units);
415 apogee.units_changed(imperial_units);
416 height.units_changed(imperial_units);
417 speed.units_changed(imperial_units);
418 accel.units_changed(imperial_units);
419 orient.units_changed(imperial_units);
422 public void show(AltosState state, AltosListenerState listener_state) {
423 if (state.gps != null && state.gps.connected) {
424 lat.show(state, listener_state);
425 lon.show(state, listener_state);
430 height.show(state, listener_state);
431 if (state.main_voltage != AltosLib.MISSING)
432 main.show(state, listener_state);
435 if (state.apogee_voltage != AltosLib.MISSING)
436 apogee.show(state, listener_state);
439 speed.show(state, listener_state);
440 accel.show(state, listener_state);
441 if (state.orient() != AltosLib.MISSING)
442 orient.show(state, listener_state);
447 public void labels(GridBagLayout layout, int y) {
448 GridBagConstraints c;
450 cur = new JLabel("Current");
451 cur.setFont(Altos.label_font);
452 c = new GridBagConstraints();
453 c.gridx = 2; c.gridy = y;
454 c.insets = new Insets(Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad, Altos.tab_elt_pad);
455 layout.setConstraints(cur, c);
458 max = new JLabel("Maximum");
459 max.setFont(Altos.label_font);
460 c.gridx = 3; c.gridy = y;
461 layout.setConstraints(max, c);
465 public String getName() {
469 public AltosAscent() {
470 layout = new GridBagLayout();
474 /* Elements in ascent display:
482 height = new Height(layout, y++);
483 speed = new Speed(layout, y++);
484 accel = new Accel(layout, y++);
485 orient = new Orient(layout, y++);
486 lat = new Lat(layout, y++);
487 lon = new Lon(layout, y++);
488 apogee = new Apogee(layout, y++);
489 main = new Main(layout, y++);