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_4.*;
25 import org.altusmetrum.altosuilib_2.*;
27 public class AltosLanded extends JComponent implements AltosFlightDisplay, ActionListener {
30 public abstract class LandedValue implements AltosFontListener, AltosUnitsListener {
36 abstract void show(AltosState state, AltosListenerState listener_state);
43 label.setVisible(true);
44 value.setVisible(true);
54 if (v == AltosLib.MISSING)
57 show(units.show(8, v));
60 void show(String format, double v) {
61 show(String.format(format, v));
64 public void font_size_changed(int font_size) {
65 label.setFont(Altos.label_font);
66 value.setFont(Altos.value_font);
69 public void units_changed(boolean imperial_units) {
75 label.setVisible(false);
76 value.setVisible(false);
79 public LandedValue (GridBagLayout layout, int y, AltosUnits units, String text) {
82 GridBagConstraints c = new GridBagConstraints();
85 label = new JLabel(text);
86 label.setFont(Altos.label_font);
87 label.setHorizontalAlignment(SwingConstants.LEFT);
88 c.gridx = 0; c.gridy = y;
89 c.insets = new Insets(10, 10, 10, 10);
90 c.anchor = GridBagConstraints.WEST;
92 c.fill = GridBagConstraints.VERTICAL;
93 layout.setConstraints(label, c);
96 value = new JTextField(Altos.text_width);
97 value.setFont(Altos.value_font);
98 value.setHorizontalAlignment(SwingConstants.RIGHT);
99 c.gridx = 1; c.gridy = y;
100 c.anchor = GridBagConstraints.WEST;
102 c.fill = GridBagConstraints.BOTH;
103 layout.setConstraints(value, c);
107 public LandedValue (GridBagLayout layout, int y, String text) {
108 this(layout, y, null, text);
112 String pos(double p, String pos, String neg) {
118 int deg = (int) Math.floor(p);
119 double min = (p - Math.floor(p)) * 60.0;
120 return String.format("%s %4d° %9.6f", h, deg, min);
123 class Lat extends LandedValue {
124 void show (AltosState state, AltosListenerState listener_state) {
126 if (state.gps != null && state.gps.connected && state.gps.lat != AltosLib.MISSING)
127 show(pos(state.gps.lat,"N", "S"));
131 public Lat (GridBagLayout layout, int y) {
132 super (layout, y, "Latitude");
138 class Lon extends LandedValue {
139 void show (AltosState state, AltosListenerState listener_state) {
141 if (state.gps != null && state.gps.connected && state.gps.lon != AltosLib.MISSING)
142 show(pos(state.gps.lon,"E", "W"));
146 public Lon (GridBagLayout layout, int y) {
147 super (layout, y, "Longitude");
153 class Bearing extends LandedValue {
154 void show (AltosState state, AltosListenerState listener_state) {
156 if (state.from_pad != null)
157 show("%3.0f°", state.from_pad.bearing);
161 public Bearing (GridBagLayout layout, int y) {
162 super (layout, y, "Bearing");
168 class Distance extends LandedValue {
169 void show (AltosState state, AltosListenerState listener_state) {
171 if (state.from_pad != null)
172 show(state.from_pad.distance);
176 public Distance (GridBagLayout layout, int y) {
177 super (layout, y, AltosConvert.distance, "Distance");
183 class Height extends LandedValue {
184 void show (AltosState state, AltosListenerState listener_state) {
185 show(state.max_height());
187 public Height (GridBagLayout layout, int y) {
188 super (layout, y, AltosConvert.height, "Maximum Height");
194 class Speed extends LandedValue {
195 void show (AltosState state, AltosListenerState listener_state) {
196 show(state.max_speed());
198 public Speed (GridBagLayout layout, int y) {
199 super (layout, y, AltosConvert.speed, "Maximum Speed");
205 class Accel extends LandedValue {
206 void show (AltosState state, AltosListenerState listener_state) {
207 show(state.max_acceleration());
209 public Accel (GridBagLayout layout, int y) {
210 super (layout, y, AltosConvert.accel, "Maximum Acceleration");
216 public void reset() {
226 public void font_size_changed(int font_size) {
227 lat.font_size_changed(font_size);
228 lon.font_size_changed(font_size);
229 bearing.font_size_changed(font_size);
230 distance.font_size_changed(font_size);
231 height.font_size_changed(font_size);
232 speed.font_size_changed(font_size);
233 accel.font_size_changed(font_size);
236 public void units_changed(boolean imperial_units) {
237 lat.units_changed(imperial_units);
238 lon.units_changed(imperial_units);
239 bearing.units_changed(imperial_units);
240 distance.units_changed(imperial_units);
241 height.units_changed(imperial_units);
242 speed.units_changed(imperial_units);
243 accel.units_changed(imperial_units);
246 public void show(AltosState state, AltosListenerState listener_state) {
247 if (state.gps != null && state.gps.connected) {
248 bearing.show(state, listener_state);
249 distance.show(state, listener_state);
250 lat.show(state, listener_state);
251 lon.show(state, listener_state);
258 height.show(state, listener_state);
259 speed.show(state, listener_state);
260 accel.show(state, listener_state);
261 if (reader.backing_file() != null)
262 graph.setEnabled(true);
266 AltosFlightReader reader;
268 public void actionPerformed(ActionEvent e) {
269 String cmd = e.getActionCommand();
271 if (cmd.equals("graph")) {
272 File file = reader.backing_file();
274 String filename = file.getName();
276 AltosStateIterable states = null;
277 if (filename.endsWith("eeprom")) {
278 FileInputStream in = new FileInputStream(file);
279 states = new AltosEepromFile(in);
280 } else if (filename.endsWith("telem")) {
281 FileInputStream in = new FileInputStream(file);
282 states = new AltosTelemetryFile(in);
284 throw new FileNotFoundException(filename);
287 new AltosGraphUI(states, file);
288 } catch (InterruptedException ie) {
289 } catch (IOException ie) {
291 } catch (FileNotFoundException fe) {
292 JOptionPane.showMessageDialog(null,
295 JOptionPane.ERROR_MESSAGE);
301 public String getName() {
305 public AltosLanded(AltosFlightReader in_reader) {
306 layout = new GridBagLayout();
312 /* Elements in descent display */
313 bearing = new Bearing(layout, 0);
314 distance = new Distance(layout, 1);
315 lat = new Lat(layout, 2);
316 lon = new Lon(layout, 3);
317 height = new Height(layout, 4);
318 speed = new Speed(layout, 5);
319 accel = new Accel(layout, 6);
321 graph = new JButton ("Graph Flight");
322 graph.setActionCommand("graph");
323 graph.addActionListener(this);
324 graph.setEnabled(false);
326 GridBagConstraints c = new GridBagConstraints();
328 c.gridx = 0; c.gridy = 7;
329 c.insets = new Insets(10, 10, 10, 10);
330 c.anchor = GridBagConstraints.WEST;
333 c.fill = GridBagConstraints.VERTICAL;