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.*;
26 public class AltosLanded extends JComponent implements AltosFlightDisplay, ActionListener {
29 public class LandedValue {
32 void show(AltosState state, int crc_errors) {}
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, int crc_errors) {
106 if (state.gps != null && state.gps.connected)
107 show(pos(state.gps.lat,"N", "S"));
111 public Lat (GridBagLayout layout, int y) {
112 super (layout, y, "Latitude");
118 class Lon extends LandedValue {
119 void show (AltosState state, int crc_errors) {
121 if (state.gps != null && state.gps.connected)
122 show(pos(state.gps.lon,"E", "W"));
126 public Lon (GridBagLayout layout, int y) {
127 super (layout, y, "Longitude");
133 class Bearing extends LandedValue {
134 void show (AltosState state, int crc_errors) {
136 if (state.from_pad != null)
137 show("%3.0f°", state.from_pad.bearing);
141 public Bearing (GridBagLayout layout, int y) {
142 super (layout, y, "Bearing");
148 class Distance extends LandedValue {
149 void show (AltosState state, int crc_errors) {
151 if (state.from_pad != null)
152 show(AltosConvert.distance, state.from_pad.distance);
156 public Distance (GridBagLayout layout, int y) {
157 super (layout, y, "Distance");
163 class Height extends LandedValue {
164 void show (AltosState state, int crc_errors) {
165 show(AltosConvert.height, state.max_height);
167 public Height (GridBagLayout layout, int y) {
168 super (layout, y, "Maximum Height");
174 class Speed extends LandedValue {
175 void show (AltosState state, int crc_errors) {
176 show(AltosConvert.speed, state.max_speed);
178 public Speed (GridBagLayout layout, int y) {
179 super (layout, y, "Maximum Speed");
185 class Accel extends LandedValue {
186 void show (AltosState state, int crc_errors) {
187 show(AltosConvert.accel, state.max_acceleration);
189 public Accel (GridBagLayout layout, int y) {
190 super (layout, y, "Maximum Acceleration");
196 public void reset() {
206 public void set_font() {
216 public void show(AltosState state, int crc_errors) {
217 if (state.gps != null && state.gps.connected) {
218 bearing.show(state, crc_errors);
219 distance.show(state, crc_errors);
220 lat.show(state, crc_errors);
221 lon.show(state, crc_errors);
228 height.show(state, crc_errors);
229 speed.show(state, crc_errors);
230 accel.show(state, crc_errors);
231 if (reader.backing_file() != null)
232 graph.setEnabled(true);
236 AltosFlightReader reader;
238 public void actionPerformed(ActionEvent e) {
239 String cmd = e.getActionCommand();
241 if (cmd.equals("graph")) {
242 File file = reader.backing_file();
244 String filename = file.getName();
246 AltosRecordIterable records = null;
247 if (filename.endsWith("eeprom")) {
248 FileInputStream in = new FileInputStream(file);
249 records = new AltosEepromIterable(in);
250 } else if (filename.endsWith("telem")) {
251 FileInputStream in = new FileInputStream(file);
252 records = new AltosTelemetryIterable(in);
254 throw new FileNotFoundException(filename);
257 new AltosGraphUI(records, filename);
258 } catch (InterruptedException ie) {
259 } catch (IOException ie) {
261 } catch (FileNotFoundException fe) {
262 JOptionPane.showMessageDialog(null,
265 JOptionPane.ERROR_MESSAGE);
271 public AltosLanded(AltosFlightReader in_reader) {
272 layout = new GridBagLayout();
278 /* Elements in descent display */
279 bearing = new Bearing(layout, 0);
280 distance = new Distance(layout, 1);
281 lat = new Lat(layout, 2);
282 lon = new Lon(layout, 3);
283 height = new Height(layout, 4);
284 speed = new Speed(layout, 5);
285 accel = new Accel(layout, 6);
287 graph = new JButton ("Graph Flight");
288 graph.setActionCommand("graph");
289 graph.addActionListener(this);
290 graph.setEnabled(false);
292 GridBagConstraints c = new GridBagConstraints();
294 c.gridx = 0; c.gridy = 7;
295 c.insets = new Insets(10, 10, 10, 10);
296 c.anchor = GridBagConstraints.WEST;
299 c.fill = GridBagConstraints.VERTICAL;