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;
31 public class AltosLanded extends JComponent implements AltosFlightDisplay, ActionListener {
34 public class LandedValue {
37 void show(AltosState state, int crc_errors) {}
44 label.setVisible(true);
45 value.setVisible(true);
48 public void set_font() {
49 label.setFont(Altos.label_font);
50 value.setFont(Altos.value_font);
54 label.setVisible(false);
55 value.setVisible(false);
58 void show(String format, double v) {
60 value.setText(String.format(format, v));
64 public LandedValue (GridBagLayout layout, int y, String text) {
65 GridBagConstraints c = new GridBagConstraints();
68 label = new JLabel(text);
69 label.setFont(Altos.label_font);
70 label.setHorizontalAlignment(SwingConstants.LEFT);
71 c.gridx = 0; c.gridy = y;
72 c.insets = new Insets(10, 10, 10, 10);
73 c.anchor = GridBagConstraints.WEST;
75 c.fill = GridBagConstraints.VERTICAL;
76 layout.setConstraints(label, c);
79 value = new JTextField(Altos.text_width);
80 value.setFont(Altos.value_font);
81 value.setHorizontalAlignment(SwingConstants.RIGHT);
82 c.gridx = 1; c.gridy = y;
83 c.anchor = GridBagConstraints.WEST;
85 c.fill = GridBagConstraints.BOTH;
86 layout.setConstraints(value, c);
91 String pos(double p, String pos, String neg) {
97 int deg = (int) Math.floor(p);
98 double min = (p - Math.floor(p)) * 60.0;
99 return String.format("%s %4d° %9.6f", h, deg, min);
102 class Lat extends LandedValue {
103 void show (AltosState state, int crc_errors) {
105 if (state.gps != null && state.gps.connected)
106 value.setText(pos(state.gps.lat,"N", "S"));
108 value.setText("???");
110 public Lat (GridBagLayout layout, int y) {
111 super (layout, y, "Latitude");
117 class Lon extends LandedValue {
118 void show (AltosState state, int crc_errors) {
120 if (state.gps != null && state.gps.connected)
121 value.setText(pos(state.gps.lon,"E", "W"));
123 value.setText("???");
125 public Lon (GridBagLayout layout, int y) {
126 super (layout, y, "Longitude");
132 class Bearing extends LandedValue {
133 void show (AltosState state, int crc_errors) {
135 if (state.from_pad != null)
136 show("%3.0f°", state.from_pad.bearing);
138 value.setText("???");
140 public Bearing (GridBagLayout layout, int y) {
141 super (layout, y, "Bearing");
147 class Distance extends LandedValue {
148 void show (AltosState state, int crc_errors) {
150 if (state.from_pad != null)
151 show("%6.0f m", state.from_pad.distance);
153 value.setText("???");
155 public Distance (GridBagLayout layout, int y) {
156 super (layout, y, "Distance");
162 class Height extends LandedValue {
163 void show (AltosState state, int crc_errors) {
164 show("%6.0f m", state.max_height);
166 public Height (GridBagLayout layout, int y) {
167 super (layout, y, "Maximum Height");
173 class Speed extends LandedValue {
174 void show (AltosState state, int crc_errors) {
175 show("%6.0f m/s", state.max_speed);
177 public Speed (GridBagLayout layout, int y) {
178 super (layout, y, "Maximum Speed");
184 class Accel extends LandedValue {
185 void show (AltosState state, int crc_errors) {
186 show("%6.0f m/s²", state.max_acceleration);
188 public Accel (GridBagLayout layout, int y) {
189 super (layout, y, "Maximum Acceleration");
195 public void reset() {
205 public void set_font() {
215 public void show(AltosState state, int crc_errors) {
216 if (state.gps != null && state.gps.connected) {
217 bearing.show(state, crc_errors);
218 distance.show(state, crc_errors);
219 lat.show(state, crc_errors);
220 lon.show(state, crc_errors);
227 height.show(state, crc_errors);
228 speed.show(state, crc_errors);
229 accel.show(state, crc_errors);
230 if (reader.backing_file() != null)
231 graph.setEnabled(true);
235 AltosFlightReader reader;
237 public void actionPerformed(ActionEvent e) {
238 String cmd = e.getActionCommand();
240 if (cmd.equals("graph")) {
241 File file = reader.backing_file();
243 String filename = file.getName();
245 AltosRecordIterable records = null;
246 if (filename.endsWith("eeprom")) {
247 FileInputStream in = new FileInputStream(file);
248 records = new AltosEepromIterable(in);
249 } else if (filename.endsWith("telem")) {
250 FileInputStream in = new FileInputStream(file);
251 records = new AltosTelemetryIterable(in);
253 throw new FileNotFoundException();
256 new AltosGraphUI(records, filename);
257 } catch (InterruptedException ie) {
258 } catch (IOException ie) {
260 } catch (FileNotFoundException fe) {
261 JOptionPane.showMessageDialog(null,
264 JOptionPane.ERROR_MESSAGE);
270 public AltosLanded(AltosFlightReader in_reader) {
271 layout = new GridBagLayout();
277 /* Elements in descent display */
278 bearing = new Bearing(layout, 0);
279 distance = new Distance(layout, 1);
280 lat = new Lat(layout, 2);
281 lon = new Lon(layout, 3);
282 height = new Height(layout, 4);
283 speed = new Speed(layout, 5);
284 accel = new Accel(layout, 6);
286 graph = new JButton ("Graph Flight");
287 graph.setActionCommand("graph");
288 graph.addActionListener(this);
289 graph.setEnabled(false);
291 GridBagConstraints c = new GridBagConstraints();
293 c.gridx = 0; c.gridy = 7;
294 c.insets = new Insets(10, 10, 10, 10);
295 c.anchor = GridBagConstraints.WEST;
298 c.fill = GridBagConstraints.VERTICAL;