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);
43 public void set_font() {
44 label.setFont(Altos.label_font);
45 value.setFont(Altos.value_font);
49 label.setVisible(false);
50 value.setVisible(false);
53 void show(String format, double v) {
55 value.setText(String.format(format, v));
59 public LandedValue (GridBagLayout layout, int y, String text) {
60 GridBagConstraints c = new GridBagConstraints();
63 label = new JLabel(text);
64 label.setFont(Altos.label_font);
65 label.setHorizontalAlignment(SwingConstants.LEFT);
66 c.gridx = 0; c.gridy = y;
67 c.insets = new Insets(10, 10, 10, 10);
68 c.anchor = GridBagConstraints.WEST;
70 c.fill = GridBagConstraints.VERTICAL;
71 layout.setConstraints(label, c);
74 value = new JTextField(Altos.text_width);
75 value.setFont(Altos.value_font);
76 value.setHorizontalAlignment(SwingConstants.RIGHT);
77 c.gridx = 1; c.gridy = y;
78 c.anchor = GridBagConstraints.WEST;
80 c.fill = GridBagConstraints.BOTH;
81 layout.setConstraints(value, c);
86 String pos(double p, String pos, String neg) {
92 int deg = (int) Math.floor(p);
93 double min = (p - Math.floor(p)) * 60.0;
94 return String.format("%s %4d° %9.6f", h, deg, min);
97 class Lat extends LandedValue {
98 void show (AltosState state, int crc_errors) {
100 if (state.gps != null && state.gps.connected)
101 value.setText(pos(state.gps.lat,"N", "S"));
103 value.setText("???");
105 public Lat (GridBagLayout layout, int y) {
106 super (layout, y, "Latitude");
112 class Lon extends LandedValue {
113 void show (AltosState state, int crc_errors) {
115 if (state.gps != null && state.gps.connected)
116 value.setText(pos(state.gps.lon,"E", "W"));
118 value.setText("???");
120 public Lon (GridBagLayout layout, int y) {
121 super (layout, y, "Longitude");
127 class Bearing extends LandedValue {
128 void show (AltosState state, int crc_errors) {
130 if (state.from_pad != null)
131 show("%3.0f°", state.from_pad.bearing);
133 value.setText("???");
135 public Bearing (GridBagLayout layout, int y) {
136 super (layout, y, "Bearing");
142 class Distance extends LandedValue {
143 void show (AltosState state, int crc_errors) {
145 if (state.from_pad != null)
146 show("%6.0f m", state.from_pad.distance);
148 value.setText("???");
150 public Distance (GridBagLayout layout, int y) {
151 super (layout, y, "Distance");
157 class Height extends LandedValue {
158 void show (AltosState state, int crc_errors) {
159 show("%6.0f m", state.max_height);
161 public Height (GridBagLayout layout, int y) {
162 super (layout, y, "Maximum Height");
168 class Speed extends LandedValue {
169 void show (AltosState state, int crc_errors) {
170 show("%6.0f m/s", state.max_speed);
172 public Speed (GridBagLayout layout, int y) {
173 super (layout, y, "Maximum Speed");
179 class Accel extends LandedValue {
180 void show (AltosState state, int crc_errors) {
181 show("%6.0f m/s²", state.max_acceleration);
183 public Accel (GridBagLayout layout, int y) {
184 super (layout, y, "Maximum Acceleration");
190 public void reset() {
200 public void set_font() {
210 public void show(AltosState state, int crc_errors) {
211 if (state.gps != null && state.gps.connected) {
212 bearing.show(state, crc_errors);
213 distance.show(state, crc_errors);
214 lat.show(state, crc_errors);
215 lon.show(state, crc_errors);
222 height.show(state, crc_errors);
223 speed.show(state, crc_errors);
224 accel.show(state, crc_errors);
225 if (reader.backing_file() != null)
226 graph.setEnabled(true);
230 AltosFlightReader reader;
232 public void actionPerformed(ActionEvent e) {
233 String cmd = e.getActionCommand();
235 if (cmd.equals("graph")) {
236 File file = reader.backing_file();
238 String filename = file.getName();
240 AltosRecordIterable records = null;
241 if (filename.endsWith("eeprom")) {
242 FileInputStream in = new FileInputStream(file);
243 records = new AltosEepromIterable(in);
244 } else if (filename.endsWith("telem")) {
245 FileInputStream in = new FileInputStream(file);
246 records = new AltosTelemetryIterable(in);
248 throw new FileNotFoundException(filename);
251 new AltosGraphUI(records, filename);
252 } catch (InterruptedException ie) {
253 } catch (IOException ie) {
255 } catch (FileNotFoundException fe) {
256 JOptionPane.showMessageDialog(null,
259 JOptionPane.ERROR_MESSAGE);
265 public AltosLanded(AltosFlightReader in_reader) {
266 layout = new GridBagLayout();
272 /* Elements in descent display */
273 bearing = new Bearing(layout, 0);
274 distance = new Distance(layout, 1);
275 lat = new Lat(layout, 2);
276 lon = new Lon(layout, 3);
277 height = new Height(layout, 4);
278 speed = new Speed(layout, 5);
279 accel = new Accel(layout, 6);
281 graph = new JButton ("Graph Flight");
282 graph.setActionCommand("graph");
283 graph.addActionListener(this);
284 graph.setEnabled(false);
286 GridBagConstraints c = new GridBagConstraints();
288 c.gridx = 0; c.gridy = 7;
289 c.insets = new Insets(10, 10, 10, 10);
290 c.anchor = GridBagConstraints.WEST;
293 c.fill = GridBagConstraints.VERTICAL;