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);
49 public void set_font() {
50 label.setFont(Altos.label_font);
51 value.setFont(Altos.value_font);
55 label.setVisible(false);
56 value.setVisible(false);
59 void show(String format, double v) {
61 value.setText(String.format(format, v));
65 public LandedValue (GridBagLayout layout, int y, String text) {
66 GridBagConstraints c = new GridBagConstraints();
69 label = new JLabel(text);
70 label.setFont(Altos.label_font);
71 label.setHorizontalAlignment(SwingConstants.LEFT);
72 c.gridx = 0; c.gridy = y;
73 c.insets = new Insets(10, 10, 10, 10);
74 c.anchor = GridBagConstraints.WEST;
76 c.fill = GridBagConstraints.VERTICAL;
77 layout.setConstraints(label, c);
80 value = new JTextField(Altos.text_width);
81 value.setFont(Altos.value_font);
82 value.setHorizontalAlignment(SwingConstants.RIGHT);
83 c.gridx = 1; c.gridy = y;
84 c.anchor = GridBagConstraints.WEST;
86 c.fill = GridBagConstraints.BOTH;
87 layout.setConstraints(value, c);
92 String pos(double p, String pos, String neg) {
98 int deg = (int) Math.floor(p);
99 double min = (p - Math.floor(p)) * 60.0;
100 return String.format("%s %4d° %9.6f", h, deg, min);
103 class Lat extends LandedValue {
104 void show (AltosState state, int crc_errors) {
106 if (state.gps != null && state.gps.connected)
107 value.setText(pos(state.gps.lat,"N", "S"));
109 value.setText("???");
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 value.setText(pos(state.gps.lon,"E", "W"));
124 value.setText("???");
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);
139 value.setText("???");
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("%6.0f m", state.from_pad.distance);
154 value.setText("???");
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("%6.0f m", 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("%6.0f m/s", 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("%6.0f m/s²", 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;