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.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.LinkedBlockingQueue;
30 import org.altusmetrum.AltosLib.*;
32 public class AltosLanded extends JComponent implements AltosFlightDisplay, ActionListener {
35 public class LandedValue {
38 void show(AltosState state, int crc_errors) {}
45 label.setVisible(true);
46 value.setVisible(true);
54 void show(AltosUnits units, double v) {
55 show(units.show(8, v));
58 void show(String format, double v) {
59 show(String.format(format, v));
62 public void set_font() {
63 label.setFont(Altos.label_font);
64 value.setFont(Altos.value_font);
68 label.setVisible(false);
69 value.setVisible(false);
72 public LandedValue (GridBagLayout layout, int y, String text) {
73 GridBagConstraints c = new GridBagConstraints();
76 label = new JLabel(text);
77 label.setFont(Altos.label_font);
78 label.setHorizontalAlignment(SwingConstants.LEFT);
79 c.gridx = 0; c.gridy = y;
80 c.insets = new Insets(10, 10, 10, 10);
81 c.anchor = GridBagConstraints.WEST;
83 c.fill = GridBagConstraints.VERTICAL;
84 layout.setConstraints(label, c);
87 value = new JTextField(Altos.text_width);
88 value.setFont(Altos.value_font);
89 value.setHorizontalAlignment(SwingConstants.RIGHT);
90 c.gridx = 1; c.gridy = y;
91 c.anchor = GridBagConstraints.WEST;
93 c.fill = GridBagConstraints.BOTH;
94 layout.setConstraints(value, c);
99 String pos(double p, String pos, String neg) {
105 int deg = (int) Math.floor(p);
106 double min = (p - Math.floor(p)) * 60.0;
107 return String.format("%s %4d° %9.6f", h, deg, min);
110 class Lat extends LandedValue {
111 void show (AltosState state, int crc_errors) {
112 if (state.gps != null && state.gps.connected)
113 show(pos(state.gps.lat,"N", "S"));
117 public Lat (GridBagLayout layout, int y) {
118 super (layout, y, "Latitude");
124 class Lon extends LandedValue {
125 void show (AltosState state, int crc_errors) {
127 if (state.gps != null && state.gps.connected)
128 show(pos(state.gps.lon,"E", "W"));
132 public Lon (GridBagLayout layout, int y) {
133 super (layout, y, "Longitude");
139 class Bearing extends LandedValue {
140 void show (AltosState state, int crc_errors) {
142 if (state.from_pad != null)
143 show("%3.0f°", state.from_pad.bearing);
147 public Bearing (GridBagLayout layout, int y) {
148 super (layout, y, "Bearing");
154 class Distance extends LandedValue {
155 void show (AltosState state, int crc_errors) {
157 if (state.from_pad != null)
158 show(AltosConvert.distance, state.from_pad.distance);
162 public Distance (GridBagLayout layout, int y) {
163 super (layout, y, "Distance");
169 class Height extends LandedValue {
170 void show (AltosState state, int crc_errors) {
171 show(AltosConvert.height, state.max_height);
173 public Height (GridBagLayout layout, int y) {
174 super (layout, y, "Maximum Height");
180 class Speed extends LandedValue {
181 void show (AltosState state, int crc_errors) {
182 show(AltosConvert.speed, state.max_speed);
184 public Speed (GridBagLayout layout, int y) {
185 super (layout, y, "Maximum Speed");
191 class Accel extends LandedValue {
192 void show (AltosState state, int crc_errors) {
193 show(AltosConvert.accel, state.max_acceleration);
195 public Accel (GridBagLayout layout, int y) {
196 super (layout, y, "Maximum Acceleration");
202 public void reset() {
212 public void set_font() {
222 public void show(AltosState state, int crc_errors) {
223 if (state.gps != null && state.gps.connected) {
224 bearing.show(state, crc_errors);
225 distance.show(state, crc_errors);
226 lat.show(state, crc_errors);
227 lon.show(state, crc_errors);
234 height.show(state, crc_errors);
235 speed.show(state, crc_errors);
236 accel.show(state, crc_errors);
237 if (reader.backing_file() != null)
238 graph.setEnabled(true);
242 AltosFlightReader reader;
244 public void actionPerformed(ActionEvent e) {
245 String cmd = e.getActionCommand();
247 if (cmd.equals("graph")) {
248 File file = reader.backing_file();
250 String filename = file.getName();
252 AltosRecordIterable records = null;
253 if (filename.endsWith("eeprom")) {
254 FileInputStream in = new FileInputStream(file);
255 records = new AltosEepromIterable(in);
256 } else if (filename.endsWith("telem")) {
257 FileInputStream in = new FileInputStream(file);
258 records = new AltosTelemetryIterable(in);
260 throw new FileNotFoundException(filename);
263 new AltosGraphUI(records, filename);
264 } catch (InterruptedException ie) {
265 } catch (IOException ie) {
267 } catch (FileNotFoundException fe) {
268 JOptionPane.showMessageDialog(null,
271 JOptionPane.ERROR_MESSAGE);
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;