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_3.*;
26 public class AltosLanded extends JComponent implements AltosFlightDisplay, ActionListener {
29 public class LandedValue {
32 void show(AltosState state, AltosListenerState listener_state) {}
39 label.setVisible(true);
40 value.setVisible(true);
48 void show(AltosUnits units, double v) {
49 show(units.show(8, v));
52 void show(String format, double v) {
53 show(String.format(format, v));
56 public void set_font() {
57 label.setFont(Altos.label_font);
58 value.setFont(Altos.value_font);
62 label.setVisible(false);
63 value.setVisible(false);
66 public LandedValue (GridBagLayout layout, int y, String text) {
67 GridBagConstraints c = new GridBagConstraints();
70 label = new JLabel(text);
71 label.setFont(Altos.label_font);
72 label.setHorizontalAlignment(SwingConstants.LEFT);
73 c.gridx = 0; c.gridy = y;
74 c.insets = new Insets(10, 10, 10, 10);
75 c.anchor = GridBagConstraints.WEST;
77 c.fill = GridBagConstraints.VERTICAL;
78 layout.setConstraints(label, c);
81 value = new JTextField(Altos.text_width);
82 value.setFont(Altos.value_font);
83 value.setHorizontalAlignment(SwingConstants.RIGHT);
84 c.gridx = 1; c.gridy = y;
85 c.anchor = GridBagConstraints.WEST;
87 c.fill = GridBagConstraints.BOTH;
88 layout.setConstraints(value, c);
93 String pos(double p, String pos, String neg) {
99 int deg = (int) Math.floor(p);
100 double min = (p - Math.floor(p)) * 60.0;
101 return String.format("%s %4d° %9.6f", h, deg, min);
104 class Lat extends LandedValue {
105 void show (AltosState state, AltosListenerState listener_state) {
107 if (state.gps != null && state.gps.connected && state.gps.lat != AltosLib.MISSING)
108 show(pos(state.gps.lat,"N", "S"));
112 public Lat (GridBagLayout layout, int y) {
113 super (layout, y, "Latitude");
119 class Lon extends LandedValue {
120 void show (AltosState state, AltosListenerState listener_state) {
122 if (state.gps != null && state.gps.connected && state.gps.lon != AltosLib.MISSING)
123 show(pos(state.gps.lon,"E", "W"));
127 public Lon (GridBagLayout layout, int y) {
128 super (layout, y, "Longitude");
134 class Bearing extends LandedValue {
135 void show (AltosState state, AltosListenerState listener_state) {
137 if (state.from_pad != null)
138 show("%3.0f°", state.from_pad.bearing);
142 public Bearing (GridBagLayout layout, int y) {
143 super (layout, y, "Bearing");
149 class Distance extends LandedValue {
150 void show (AltosState state, AltosListenerState listener_state) {
152 if (state.from_pad != null)
153 show(AltosConvert.distance, state.from_pad.distance);
157 public Distance (GridBagLayout layout, int y) {
158 super (layout, y, "Distance");
164 class Height extends LandedValue {
165 void show (AltosState state, AltosListenerState listener_state) {
166 show(AltosConvert.height, state.max_height());
168 public Height (GridBagLayout layout, int y) {
169 super (layout, y, "Maximum Height");
175 class Speed extends LandedValue {
176 void show (AltosState state, AltosListenerState listener_state) {
177 show(AltosConvert.speed, state.max_speed());
179 public Speed (GridBagLayout layout, int y) {
180 super (layout, y, "Maximum Speed");
186 class Accel extends LandedValue {
187 void show (AltosState state, AltosListenerState listener_state) {
188 show(AltosConvert.accel, state.max_acceleration());
190 public Accel (GridBagLayout layout, int y) {
191 super (layout, y, "Maximum Acceleration");
197 public void reset() {
207 public void set_font() {
217 public void show(AltosState state, AltosListenerState listener_state) {
218 if (state.gps != null && state.gps.connected) {
219 bearing.show(state, listener_state);
220 distance.show(state, listener_state);
221 lat.show(state, listener_state);
222 lon.show(state, listener_state);
229 height.show(state, listener_state);
230 speed.show(state, listener_state);
231 accel.show(state, listener_state);
232 if (reader.backing_file() != null)
233 graph.setEnabled(true);
237 AltosFlightReader reader;
239 public void actionPerformed(ActionEvent e) {
240 String cmd = e.getActionCommand();
242 if (cmd.equals("graph")) {
243 File file = reader.backing_file();
245 String filename = file.getName();
247 AltosStateIterable states = null;
248 if (filename.endsWith("eeprom")) {
249 FileInputStream in = new FileInputStream(file);
250 states = new AltosEepromFile(in);
251 } else if (filename.endsWith("telem")) {
252 FileInputStream in = new FileInputStream(file);
253 states = new AltosTelemetryFile(in);
255 throw new FileNotFoundException(filename);
258 new AltosGraphUI(states, file);
259 } catch (InterruptedException ie) {
260 } catch (IOException ie) {
262 } catch (FileNotFoundException fe) {
263 JOptionPane.showMessageDialog(null,
266 JOptionPane.ERROR_MESSAGE);
272 public String getName() {
276 public AltosLanded(AltosFlightReader in_reader) {
277 layout = new GridBagLayout();
283 /* Elements in descent display */
284 bearing = new Bearing(layout, 0);
285 distance = new Distance(layout, 1);
286 lat = new Lat(layout, 2);
287 lon = new Lon(layout, 3);
288 height = new Height(layout, 4);
289 speed = new Speed(layout, 5);
290 accel = new Accel(layout, 6);
292 graph = new JButton ("Graph Flight");
293 graph.setActionCommand("graph");
294 graph.addActionListener(this);
295 graph.setEnabled(false);
297 GridBagConstraints c = new GridBagConstraints();
299 c.gridx = 0; c.gridy = 7;
300 c.insets = new Insets(10, 10, 10, 10);
301 c.anchor = GridBagConstraints.WEST;
304 c.fill = GridBagConstraints.VERTICAL;