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 class LandedValue {
33 void show(AltosState state, AltosListenerState listener_state) {}
40 label.setVisible(true);
41 value.setVisible(true);
49 void show(AltosUnits units, double v) {
50 show(units.show(8, v));
53 void show(String format, double v) {
54 show(String.format(format, v));
57 public void set_font() {
58 label.setFont(Altos.label_font);
59 value.setFont(Altos.value_font);
63 label.setVisible(false);
64 value.setVisible(false);
67 public LandedValue (GridBagLayout layout, int y, String text) {
68 GridBagConstraints c = new GridBagConstraints();
71 label = new JLabel(text);
72 label.setFont(Altos.label_font);
73 label.setHorizontalAlignment(SwingConstants.LEFT);
74 c.gridx = 0; c.gridy = y;
75 c.insets = new Insets(10, 10, 10, 10);
76 c.anchor = GridBagConstraints.WEST;
78 c.fill = GridBagConstraints.VERTICAL;
79 layout.setConstraints(label, c);
82 value = new JTextField(Altos.text_width);
83 value.setFont(Altos.value_font);
84 value.setHorizontalAlignment(SwingConstants.RIGHT);
85 c.gridx = 1; c.gridy = y;
86 c.anchor = GridBagConstraints.WEST;
88 c.fill = GridBagConstraints.BOTH;
89 layout.setConstraints(value, c);
94 String pos(double p, String pos, String neg) {
100 int deg = (int) Math.floor(p);
101 double min = (p - Math.floor(p)) * 60.0;
102 return String.format("%s %4d° %9.6f", h, deg, min);
105 class Lat extends LandedValue {
106 void show (AltosState state, AltosListenerState listener_state) {
108 if (state.gps != null && state.gps.connected && state.gps.lat != AltosLib.MISSING)
109 show(pos(state.gps.lat,"N", "S"));
113 public Lat (GridBagLayout layout, int y) {
114 super (layout, y, "Latitude");
120 class Lon extends LandedValue {
121 void show (AltosState state, AltosListenerState listener_state) {
123 if (state.gps != null && state.gps.connected && state.gps.lon != AltosLib.MISSING)
124 show(pos(state.gps.lon,"E", "W"));
128 public Lon (GridBagLayout layout, int y) {
129 super (layout, y, "Longitude");
135 class Bearing extends LandedValue {
136 void show (AltosState state, AltosListenerState listener_state) {
138 if (state.from_pad != null)
139 show("%3.0f°", state.from_pad.bearing);
143 public Bearing (GridBagLayout layout, int y) {
144 super (layout, y, "Bearing");
150 class Distance extends LandedValue {
151 void show (AltosState state, AltosListenerState listener_state) {
153 if (state.from_pad != null)
154 show(AltosConvert.distance, state.from_pad.distance);
158 public Distance (GridBagLayout layout, int y) {
159 super (layout, y, "Distance");
165 class Height extends LandedValue {
166 void show (AltosState state, AltosListenerState listener_state) {
167 show(AltosConvert.height, state.max_height());
169 public Height (GridBagLayout layout, int y) {
170 super (layout, y, "Maximum Height");
176 class Speed extends LandedValue {
177 void show (AltosState state, AltosListenerState listener_state) {
178 show(AltosConvert.speed, state.max_speed());
180 public Speed (GridBagLayout layout, int y) {
181 super (layout, y, "Maximum Speed");
187 class Accel extends LandedValue {
188 void show (AltosState state, AltosListenerState listener_state) {
189 show(AltosConvert.accel, state.max_acceleration());
191 public Accel (GridBagLayout layout, int y) {
192 super (layout, y, "Maximum Acceleration");
198 public void reset() {
208 public void set_font() {
218 public void show(AltosState state, AltosListenerState listener_state) {
219 if (state.gps != null && state.gps.connected) {
220 bearing.show(state, listener_state);
221 distance.show(state, listener_state);
222 lat.show(state, listener_state);
223 lon.show(state, listener_state);
230 height.show(state, listener_state);
231 speed.show(state, listener_state);
232 accel.show(state, listener_state);
233 if (reader.backing_file() != null)
234 graph.setEnabled(true);
238 AltosFlightReader reader;
240 public void actionPerformed(ActionEvent e) {
241 String cmd = e.getActionCommand();
243 if (cmd.equals("graph")) {
244 File file = reader.backing_file();
246 String filename = file.getName();
248 AltosStateIterable states = null;
249 if (filename.endsWith("eeprom")) {
250 FileInputStream in = new FileInputStream(file);
251 states = new AltosEepromFile(in);
252 } else if (filename.endsWith("telem")) {
253 FileInputStream in = new FileInputStream(file);
254 states = new AltosTelemetryFile(in);
256 throw new FileNotFoundException(filename);
259 new AltosGraphUI(states, file);
260 } catch (InterruptedException ie) {
261 } catch (IOException ie) {
263 } catch (FileNotFoundException fe) {
264 JOptionPane.showMessageDialog(null,
267 JOptionPane.ERROR_MESSAGE);
273 public String getName() {
277 public AltosLanded(AltosFlightReader in_reader) {
278 layout = new GridBagLayout();
284 /* Elements in descent display */
285 bearing = new Bearing(layout, 0);
286 distance = new Distance(layout, 1);
287 lat = new Lat(layout, 2);
288 lon = new Lon(layout, 3);
289 height = new Height(layout, 4);
290 speed = new Speed(layout, 5);
291 accel = new Accel(layout, 6);
293 graph = new JButton ("Graph Flight");
294 graph.setActionCommand("graph");
295 graph.addActionListener(this);
296 graph.setEnabled(false);
298 GridBagConstraints c = new GridBagConstraints();
300 c.gridx = 0; c.gridy = 7;
301 c.insets = new Insets(10, 10, 10, 10);
302 c.anchor = GridBagConstraints.WEST;
305 c.fill = GridBagConstraints.VERTICAL;