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 java.util.concurrent.*;
25 import org.altusmetrum.AltosLib.*;
27 public class AltosUI extends AltosFrame {
28 public AltosVoice voice = new AltosVoice();
30 public static boolean load_library(Frame frame) {
31 if (!Altos.load_library()) {
32 JOptionPane.showMessageDialog(frame,
33 String.format("No AltOS library in \"%s\"",
34 System.getProperty("java.library.path","<undefined>")),
35 "Cannot load device access library",
36 JOptionPane.ERROR_MESSAGE);
42 void telemetry_window(AltosDevice device) {
44 AltosFlightReader reader = new AltosTelemetryReader(new AltosSerial(device));
46 new AltosFlightUI(voice, reader, device.getSerial());
47 } catch (FileNotFoundException ee) {
48 JOptionPane.showMessageDialog(AltosUI.this,
50 "Cannot open target device",
51 JOptionPane.ERROR_MESSAGE);
52 } catch (AltosSerialInUseException si) {
53 JOptionPane.showMessageDialog(AltosUI.this,
54 String.format("Device \"%s\" already in use",
55 device.toShortString()),
57 JOptionPane.ERROR_MESSAGE);
58 } catch (IOException ee) {
59 JOptionPane.showMessageDialog(AltosUI.this,
60 device.toShortString(),
62 JOptionPane.ERROR_MESSAGE);
63 } catch (TimeoutException te) {
64 JOptionPane.showMessageDialog(this,
65 device.toShortString(),
67 JOptionPane.ERROR_MESSAGE);
68 } catch (InterruptedException ie) {
69 JOptionPane.showMessageDialog(this,
70 device.toShortString(),
71 "Interrupted exception",
72 JOptionPane.ERROR_MESSAGE);
77 GridBagLayout gridbag;
79 JButton addButton(int x, int y, String label) {
83 c = new GridBagConstraints();
84 c.gridx = x; c.gridy = y;
85 c.fill = GridBagConstraints.BOTH;
88 b = new JButton(label);
90 //Dimension ps = b.getPreferredSize();
92 gridbag.setConstraints(b, c);
101 AltosUIPreferences.set_component(this);
103 pane = getContentPane();
104 gridbag = new GridBagLayout();
105 pane.setLayout(gridbag);
109 b = addButton(0, 0, "Monitor Flight");
110 b.addActionListener(new ActionListener() {
111 public void actionPerformed(ActionEvent e) {
115 b.setToolTipText("Connect to TeleDongle and monitor telemetry");
116 b = addButton(1, 0, "Save Flight Data");
117 b.addActionListener(new ActionListener() {
118 public void actionPerformed(ActionEvent e) {
122 b.setToolTipText("Download and/or delete flight data from an altimeter");
123 b = addButton(2, 0, "Replay Flight");
124 b.addActionListener(new ActionListener() {
125 public void actionPerformed(ActionEvent e) {
129 b.setToolTipText("Watch an old flight in real-time");
130 b = addButton(3, 0, "Graph Data");
131 b.addActionListener(new ActionListener() {
132 public void actionPerformed(ActionEvent e) {
136 b.setToolTipText("Present flight data in a graph and table of statistics");
137 b = addButton(4, 0, "Export Data");
138 b.addActionListener(new ActionListener() {
139 public void actionPerformed(ActionEvent e) {
143 b.setToolTipText("Convert flight data for a spreadsheet or GoogleEarth");
144 b = addButton(0, 1, "Configure Altimeter");
145 b.addActionListener(new ActionListener() {
146 public void actionPerformed(ActionEvent e) {
147 ConfigureTeleMetrum();
150 b.setToolTipText("Set flight, storage and communication parameters");
151 b = addButton(1, 1, "Configure AltosUI");
152 b.addActionListener(new ActionListener() {
153 public void actionPerformed(ActionEvent e) {
157 b.setToolTipText("Global AltosUI settings");
159 b = addButton(2, 1, "Configure Ground Station");
160 b.addActionListener(new ActionListener() {
161 public void actionPerformed(ActionEvent e) {
162 ConfigureTeleDongle();
166 b = addButton(3, 1, "Flash Image");
167 b.addActionListener(new ActionListener() {
168 public void actionPerformed(ActionEvent e) {
172 b.setToolTipText("Replace the firmware in any AltusMetrum product");
174 b = addButton(4, 1, "Fire Igniter");
175 b.addActionListener(new ActionListener() {
176 public void actionPerformed(ActionEvent e) {
180 b.setToolTipText("Remote control of igniters for deployment testing");
181 b = addButton(0, 2, "Scan Channels");
182 b.addActionListener(new ActionListener() {
183 public void actionPerformed(ActionEvent e) {
187 b.setToolTipText("Find what channel an altimeter is sending telemetry on");
188 b = addButton(1, 2, "Load Maps");
189 b.addActionListener(new ActionListener() {
190 public void actionPerformed(ActionEvent e) {
194 b.setToolTipText("Download satellite images for off-line flight monitoring");
195 b = addButton(2, 2, "Monitor Idle");
196 b.addActionListener(new ActionListener() {
197 public void actionPerformed(ActionEvent e) {
201 b.setToolTipText("Check flight readiness of altimeter in idle mode");
203 // b = addButton(3, 2, "Launch Controller");
204 // b.addActionListener(new ActionListener() {
205 // public void actionPerformed(ActionEvent e) {
206 // LaunchController();
210 b = addButton(4, 2, "Quit");
211 b.addActionListener(new ActionListener() {
212 public void actionPerformed(ActionEvent e) {
216 b.setToolTipText("Close all active windows and terminate AltosUI");
228 Insets i = getInsets();
229 Dimension ps = rootPane.getPreferredSize();
230 ps.width += i.left + i.right;
231 ps.height += i.top + i.bottom;
232 setPreferredSize(ps);
234 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
235 addWindowListener(new WindowAdapter() {
237 public void windowClosing(WindowEvent e) {
243 private void ConnectToDevice() {
244 AltosDevice device = AltosDeviceDialog.show(AltosUI.this,
245 Altos.product_basestation);
248 telemetry_window(device);
251 void ConfigureCallsign() {
253 result = JOptionPane.showInputDialog(AltosUI.this,
254 "Configure Callsign",
255 AltosUIPreferences.callsign());
257 AltosUIPreferences.set_callsign(result);
260 void ConfigureTeleMetrum() {
261 new AltosConfig(AltosUI.this);
264 void ConfigureTeleDongle() {
265 new AltosConfigTD(AltosUI.this);
269 AltosFlashUI.show(AltosUI.this);
273 new AltosIgniteUI(AltosUI.this);
276 void ScanChannels() {
277 new AltosScanUI(AltosUI.this);
281 new AltosSiteMapPreload(AltosUI.this);
284 void LaunchController() {
285 new AltosLaunchUI(AltosUI.this);
289 * Replay a flight from telemetry data
291 private void Replay() {
292 AltosDataChooser chooser = new AltosDataChooser(
295 AltosRecordIterable iterable = chooser.runDialog();
296 if (iterable != null) {
297 AltosFlightReader reader = new AltosReplayReader(iterable.iterator(),
299 new AltosFlightUI(voice, reader);
303 /* Connect to TeleMetrum, either directly or through
304 * a TeleDongle over the packet link
306 private void SaveFlightData() {
307 new AltosEepromManage(AltosUI.this);
310 /* Load a flight log file and write out a CSV file containing
311 * all of the data in standard units
314 private void ExportData() {
315 AltosDataChooser chooser;
316 chooser = new AltosDataChooser(this);
317 AltosRecordIterable record_reader = chooser.runDialog();
318 if (record_reader == null)
320 new AltosCSVUI(AltosUI.this, record_reader, chooser.file());
323 /* Load a flight log CSV file and display a pretty graph.
326 private void GraphData() {
327 AltosDataChooser chooser;
328 chooser = new AltosDataChooser(this);
329 AltosRecordIterable record_reader = chooser.runDialog();
330 if (record_reader == null)
333 new AltosGraphUI(record_reader, chooser.filename());
334 } catch (InterruptedException ie) {
335 } catch (IOException ie) {
339 private void ConfigureAltosUI() {
340 new AltosConfigureUI(AltosUI.this, voice);
343 private void IdleMonitor() {
345 new AltosIdleMonitorUI(this);
346 } catch (Exception e) {
350 static AltosRecordIterable open_logfile(String filename) {
351 File file = new File (filename);
355 in = new FileInputStream(file);
356 if (filename.endsWith("eeprom"))
357 return new AltosEepromIterable(in);
358 else if (filename.endsWith("mega"))
359 return new AltosEepromMegaIterable(in);
361 return new AltosTelemetryIterable(in);
362 } catch (FileNotFoundException fe) {
363 System.out.printf("%s\n", fe.getMessage());
368 static AltosWriter open_csv(String filename) {
369 File file = new File (filename);
371 return new AltosCSV(file);
372 } catch (FileNotFoundException fe) {
373 System.out.printf("%s\n", fe.getMessage());
378 static AltosWriter open_kml(String filename) {
379 File file = new File (filename);
381 return new AltosKML(file);
382 } catch (FileNotFoundException fe) {
383 System.out.printf("%s\n", fe.getMessage());
388 static final int process_none = 0;
389 static final int process_csv = 1;
390 static final int process_kml = 2;
391 static final int process_graph = 3;
392 static final int process_replay = 4;
393 static final int process_summary = 5;
395 static boolean process_csv(String input) {
396 AltosRecordIterable iterable = open_logfile(input);
397 if (iterable == null)
400 String output = Altos.replace_extension(input,".csv");
401 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
402 if (input.equals(output)) {
403 System.out.printf("Not processing '%s'\n", input);
406 AltosWriter writer = open_csv(output);
409 writer.write(iterable);
415 static boolean process_kml(String input) {
416 AltosRecordIterable iterable = open_logfile(input);
417 if (iterable == null)
420 String output = Altos.replace_extension(input,".kml");
421 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
422 if (input.equals(output)) {
423 System.out.printf("Not processing '%s'\n", input);
426 AltosWriter writer = open_kml(output);
429 writer.write(iterable);
435 static AltosRecordIterable record_iterable(File file) {
438 in = new FileInputStream(file);
439 } catch (Exception e) {
440 System.out.printf("Failed to open file '%s'\n", file);
443 AltosRecordIterable recs;
444 //AltosReplayReader reader;
445 if (file.getName().endsWith("eeprom")) {
446 recs = new AltosEepromIterable(in);
447 } else if (file.getName().endsWith("mega")) {
448 recs = new AltosEepromMegaIterable(in);
450 recs = new AltosTelemetryIterable(in);
455 static AltosRecordIterable record_iterable_file(String filename) {
456 return record_iterable (new File(filename));
459 static AltosReplayReader replay_file(String filename) {
460 AltosRecordIterable recs = record_iterable_file(filename);
463 return new AltosReplayReader(recs.iterator(), new File(filename));
466 static boolean process_replay(String filename) {
467 AltosReplayReader reader = replay_file(filename);
470 AltosFlightUI flight_ui = new AltosFlightUI(new AltosVoice(), reader);
471 flight_ui.set_exit_on_close();
475 static boolean process_graph(String filename) {
476 AltosRecordIterable recs = record_iterable_file(filename);
480 new AltosGraphUI(recs, filename);
482 } catch (InterruptedException ie) {
483 } catch (IOException ie) {
488 static boolean process_summary(String filename) {
489 AltosRecordIterable iterable = record_iterable_file(filename);
490 if (iterable == null)
493 AltosFlightStats stats = new AltosFlightStats(iterable);
494 if (stats.serial > 0)
495 System.out.printf("Serial: %5d\n", stats.serial);
496 if (stats.flight > 0)
497 System.out.printf("Flight: %5d\n", stats.flight);
499 System.out.printf("Date: %04d-%02d-%02d\n",
500 stats.year, stats.month, stats.day);
502 System.out.printf("Time: %02d:%02d:%02d UTC\n",
503 stats.hour, stats.minute, stats.second);
504 System.out.printf("Max height: %6.0f m %6.0f ft\n",
506 AltosConvert.meters_to_feet(stats.max_height));
507 System.out.printf("Max speed: %6.0f m/s %6.0f ft/s %6.4f Mach\n",
509 AltosConvert.meters_to_feet(stats.max_speed),
510 AltosConvert.meters_to_mach(stats.max_speed));
511 if (stats.max_acceleration != AltosRecord.MISSING) {
512 System.out.printf("Max accel: %6.0f m/s² %6.0f ft/s² %6.2f g\n",
513 stats.max_acceleration,
514 AltosConvert.meters_to_feet(stats.max_acceleration),
515 AltosConvert.meters_to_g(stats.max_acceleration));
517 System.out.printf("Drogue rate: %6.0f m/s %6.0f ft/s\n",
518 stats.state_baro_speed[Altos.ao_flight_drogue],
519 AltosConvert.meters_to_feet(stats.state_baro_speed[Altos.ao_flight_drogue]));
520 System.out.printf("Main rate: %6.0f m/s %6.0f ft/s\n",
521 stats.state_baro_speed[Altos.ao_flight_main],
522 AltosConvert.meters_to_feet(stats.state_baro_speed[Altos.ao_flight_main]));
523 System.out.printf("Flight time: %6.0f s\n",
524 stats.state_end[Altos.ao_flight_main] -
525 stats.state_start[Altos.ao_flight_boost]);
527 } catch (InterruptedException ie) {
528 } catch (IOException ie) {
533 public static void help(int code) {
534 System.out.printf("Usage: altosui [OPTION]... [FILE]...\n");
535 System.out.printf(" Options:\n");
536 System.out.printf(" --fetchmaps <lat> <lon>\tpre-fetch maps for site map view\n");
537 System.out.printf(" --replay <filename>\t\trelive the glory of past flights \n");
538 System.out.printf(" --graph <filename>\t\tgraph a flight\n");
539 System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n");
540 System.out.printf(" --kml\tgenerate KML output for use with Google Earth\n");
544 public static void main(final String[] args) {
548 UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
549 } catch (Exception e) {
551 /* Handle batch-mode */
552 if (args.length == 0) {
553 AltosUI altosui = new AltosUI();
554 altosui.setVisible(true);
556 java.util.List<AltosDevice> devices = AltosUSBDevice.list(Altos.product_basestation);
557 for (AltosDevice device : devices)
558 altosui.telemetry_window(device);
560 int process = process_none;
561 for (int i = 0; i < args.length; i++) {
562 if (args[i].equals("--help"))
564 else if (args[i].equals("--fetchmaps")) {
565 if (args.length < i + 3) {
568 double lat = Double.parseDouble(args[i+1]);
569 double lon = Double.parseDouble(args[i+2]);
570 AltosSiteMap.prefetchMaps(lat, lon, 5, 5);
573 } else if (args[i].equals("--replay"))
574 process = process_replay;
575 else if (args[i].equals("--kml"))
576 process = process_kml;
577 else if (args[i].equals("--csv"))
578 process = process_csv;
579 else if (args[i].equals("--graph"))
580 process = process_graph;
581 else if (args[i].equals("--summary"))
582 process = process_summary;
583 else if (args[i].startsWith("--"))
589 if (!process_graph(args[i]))
593 if (!process_replay(args[i]))
597 if (!process_kml(args[i]))
601 if (!process_csv(args[i]))
604 case process_summary:
605 if (!process_summary(args[i]))