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.
21 import java.awt.event.*;
24 import org.altusmetrum.altoslib_4.*;
25 import org.altusmetrum.altosuilib_2.*;
27 public class AltosLanded extends JComponent implements AltosFlightDisplay, ActionListener {
30 public abstract class LandedValue implements AltosFontListener, AltosUnitsListener {
36 abstract void show(AltosState state, AltosListenerState listener_state);
43 label.setVisible(true);
44 value.setVisible(true);
54 show(units.show(8, v));
57 void show(String format, double v) {
58 show(String.format(format, v));
61 public void font_size_changed(int font_size) {
62 label.setFont(Altos.label_font);
63 value.setFont(Altos.value_font);
66 public void units_changed(boolean imperial_units) {
72 label.setVisible(false);
73 value.setVisible(false);
76 public LandedValue (GridBagLayout layout, int y, AltosUnits units, String text) {
79 GridBagConstraints c = new GridBagConstraints();
82 label = new JLabel(text);
83 label.setFont(Altos.label_font);
84 label.setHorizontalAlignment(SwingConstants.LEFT);
85 c.gridx = 0; c.gridy = y;
86 c.insets = new Insets(10, 10, 10, 10);
87 c.anchor = GridBagConstraints.WEST;
89 c.fill = GridBagConstraints.VERTICAL;
90 layout.setConstraints(label, c);
93 value = new JTextField(Altos.text_width);
94 value.setFont(Altos.value_font);
95 value.setHorizontalAlignment(SwingConstants.RIGHT);
96 c.gridx = 1; c.gridy = y;
97 c.anchor = GridBagConstraints.WEST;
99 c.fill = GridBagConstraints.BOTH;
100 layout.setConstraints(value, c);
104 public LandedValue (GridBagLayout layout, int y, String text) {
105 this(layout, y, null, text);
109 String pos(double p, String pos, String neg) {
115 int deg = (int) Math.floor(p);
116 double min = (p - Math.floor(p)) * 60.0;
117 return String.format("%s %4d° %9.6f", h, deg, min);
120 class Lat extends LandedValue {
121 void show (AltosState state, AltosListenerState listener_state) {
123 if (state.gps != null && state.gps.connected && state.gps.lat != AltosLib.MISSING)
124 show(pos(state.gps.lat,"N", "S"));
128 public Lat (GridBagLayout layout, int y) {
129 super (layout, y, "Latitude");
135 class Lon extends LandedValue {
136 void show (AltosState state, AltosListenerState listener_state) {
138 if (state.gps != null && state.gps.connected && state.gps.lon != AltosLib.MISSING)
139 show(pos(state.gps.lon,"E", "W"));
143 public Lon (GridBagLayout layout, int y) {
144 super (layout, y, "Longitude");
150 class Bearing extends LandedValue {
151 void show (AltosState state, AltosListenerState listener_state) {
153 if (state.from_pad != null)
154 show("%3.0f°", state.from_pad.bearing);
158 public Bearing (GridBagLayout layout, int y) {
159 super (layout, y, "Bearing");
165 class Distance extends LandedValue {
166 void show (AltosState state, AltosListenerState listener_state) {
168 if (state.from_pad != null)
169 show(state.from_pad.distance);
173 public Distance (GridBagLayout layout, int y) {
174 super (layout, y, AltosConvert.distance, "Distance");
180 class Height extends LandedValue {
181 void show (AltosState state, AltosListenerState listener_state) {
182 show(state.max_height());
184 public Height (GridBagLayout layout, int y) {
185 super (layout, y, AltosConvert.height, "Maximum Height");
191 class Speed extends LandedValue {
192 void show (AltosState state, AltosListenerState listener_state) {
193 show(state.max_speed());
195 public Speed (GridBagLayout layout, int y) {
196 super (layout, y, AltosConvert.speed, "Maximum Speed");
202 class Accel extends LandedValue {
203 void show (AltosState state, AltosListenerState listener_state) {
204 show(state.max_acceleration());
206 public Accel (GridBagLayout layout, int y) {
207 super (layout, y, AltosConvert.accel, "Maximum Acceleration");
213 public void reset() {
223 public void font_size_changed(int font_size) {
224 lat.font_size_changed(font_size);
225 lon.font_size_changed(font_size);
226 bearing.font_size_changed(font_size);
227 distance.font_size_changed(font_size);
228 height.font_size_changed(font_size);
229 speed.font_size_changed(font_size);
230 accel.font_size_changed(font_size);
233 public void units_changed(boolean imperial_units) {
234 lat.units_changed(imperial_units);
235 lon.units_changed(imperial_units);
236 bearing.units_changed(imperial_units);
237 distance.units_changed(imperial_units);
238 height.units_changed(imperial_units);
239 speed.units_changed(imperial_units);
240 accel.units_changed(imperial_units);
243 public void show(AltosState state, AltosListenerState listener_state) {
244 if (state.gps != null && state.gps.connected) {
245 bearing.show(state, listener_state);
246 distance.show(state, listener_state);
247 lat.show(state, listener_state);
248 lon.show(state, listener_state);
255 height.show(state, listener_state);
256 speed.show(state, listener_state);
257 accel.show(state, listener_state);
258 if (reader.backing_file() != null)
259 graph.setEnabled(true);
263 AltosFlightReader reader;
265 public void actionPerformed(ActionEvent e) {
266 String cmd = e.getActionCommand();
268 if (cmd.equals("graph")) {
269 File file = reader.backing_file();
271 String filename = file.getName();
273 AltosStateIterable states = null;
274 if (filename.endsWith("eeprom")) {
275 FileInputStream in = new FileInputStream(file);
276 states = new AltosEepromFile(in);
277 } else if (filename.endsWith("telem")) {
278 FileInputStream in = new FileInputStream(file);
279 states = new AltosTelemetryFile(in);
281 throw new FileNotFoundException(filename);
284 new AltosGraphUI(states, file);
285 } catch (InterruptedException ie) {
286 } catch (IOException ie) {
288 } catch (FileNotFoundException fe) {
289 JOptionPane.showMessageDialog(null,
292 JOptionPane.ERROR_MESSAGE);
298 public String getName() {
302 public AltosLanded(AltosFlightReader in_reader) {
303 layout = new GridBagLayout();
309 /* Elements in descent display */
310 bearing = new Bearing(layout, 0);
311 distance = new Distance(layout, 1);
312 lat = new Lat(layout, 2);
313 lon = new Lon(layout, 3);
314 height = new Height(layout, 4);
315 speed = new Speed(layout, 5);
316 accel = new Accel(layout, 6);
318 graph = new JButton ("Graph Flight");
319 graph.setActionCommand("graph");
320 graph.addActionListener(this);
321 graph.setEnabled(false);
323 GridBagConstraints c = new GridBagConstraints();
325 c.gridx = 0; c.gridy = 7;
326 c.insets = new Insets(10, 10, 10, 10);
327 c.anchor = GridBagConstraints.WEST;
330 c.fill = GridBagConstraints.VERTICAL;