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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 import java.awt.event.*;
25 import java.util.concurrent.*;
26 import org.altusmetrum.altoslib_14.*;
27 import org.altusmetrum.altosuilib_14.*;
29 public class AltosUI extends AltosUIFrame implements AltosEepromGrapher {
30 public AltosVoice voice = new AltosVoice();
32 public static boolean load_library(Frame frame) {
33 if (!Altos.load_library()) {
34 JOptionPane.showMessageDialog(frame,
35 String.format("No AltOS library in \"%s\"",
36 System.getProperty("java.library.path","<undefined>")),
37 "Cannot load device access library",
38 JOptionPane.ERROR_MESSAGE);
44 void telemetry_window(AltosDevice device) {
46 AltosFlightReader reader = new AltosTelemetryReader(new AltosSerial(device));
48 new AltosFlightUI(voice, reader, device.getSerial());
49 } catch (FileNotFoundException ee) {
50 JOptionPane.showMessageDialog(AltosUI.this,
52 String.format ("Cannot open %s", device.toShortString()),
53 JOptionPane.ERROR_MESSAGE);
54 } catch (AltosSerialInUseException si) {
55 JOptionPane.showMessageDialog(AltosUI.this,
56 String.format("Device \"%s\" already in use",
57 device.toShortString()),
59 JOptionPane.ERROR_MESSAGE);
60 } catch (IOException ee) {
61 JOptionPane.showMessageDialog(AltosUI.this,
62 String.format ("Unknown I/O error on %s", device.toShortString()),
64 JOptionPane.ERROR_MESSAGE);
65 } catch (TimeoutException te) {
66 JOptionPane.showMessageDialog(this,
67 String.format ("Timeout on %s", device.toShortString()),
69 JOptionPane.ERROR_MESSAGE);
70 } catch (InterruptedException ie) {
71 JOptionPane.showMessageDialog(this,
72 String.format("Interrupted %s", device.toShortString()),
73 "Interrupted exception",
74 JOptionPane.ERROR_MESSAGE);
78 public void scan_device_selected(AltosDevice device) {
79 telemetry_window(device);
83 GridBagLayout gridbag;
85 JButton addButton(int x, int y, String label) {
89 c = new GridBagConstraints();
90 c.gridx = x; c.gridy = y;
91 c.fill = GridBagConstraints.BOTH;
94 b = new JButton(label);
96 //Dimension ps = b.getPreferredSize();
98 gridbag.setConstraints(b, c);
103 /* OSXAdapter interfaces */
104 public void macosx_file_handler(String path) {
105 process_graph(null, new File(path));
108 public void macosx_quit_handler() {
112 public void macosx_preferences_handler() {
120 register_for_macosx_events();
122 AltosUIPreferences.set_component(this);
124 pane = getContentPane();
125 gridbag = new GridBagLayout();
126 pane.setLayout(gridbag);
130 b = addButton(0, 0, "Monitor Flight");
131 b.addActionListener(new ActionListener() {
132 public void actionPerformed(ActionEvent e) {
136 b.setToolTipText("Connect to TeleDongle and monitor telemetry");
137 b = addButton(1, 0, "Save Flight Data");
138 b.addActionListener(new ActionListener() {
139 public void actionPerformed(ActionEvent e) {
143 b.setToolTipText("Download and/or delete flight data from an altimeter");
144 b = addButton(2, 0, "Replay Flight");
145 b.addActionListener(new ActionListener() {
146 public void actionPerformed(ActionEvent e) {
150 b.setToolTipText("Watch an old flight in real-time");
151 b = addButton(3, 0, "Graph Data");
152 b.addActionListener(new ActionListener() {
153 public void actionPerformed(ActionEvent e) {
157 b.setToolTipText("Present flight data in a graph and table of statistics");
158 b = addButton(4, 0, "Export Data");
159 b.addActionListener(new ActionListener() {
160 public void actionPerformed(ActionEvent e) {
164 b.setToolTipText("Convert flight data for a spreadsheet or GoogleEarth");
165 b = addButton(0, 1, "Configure Altimeter");
166 b.addActionListener(new ActionListener() {
167 public void actionPerformed(ActionEvent e) {
168 ConfigureTeleMetrum();
171 b.setToolTipText("Set flight, storage and communication parameters");
172 b = addButton(1, 1, "Configure AltosUI");
173 b.addActionListener(new ActionListener() {
174 public void actionPerformed(ActionEvent e) {
178 b.setToolTipText("Global AltosUI settings");
180 b = addButton(2, 1, "Configure Ground Station");
181 b.addActionListener(new ActionListener() {
182 public void actionPerformed(ActionEvent e) {
183 ConfigureTeleDongle();
187 b = addButton(3, 1, "Flash Image");
188 b.addActionListener(new ActionListener() {
189 public void actionPerformed(ActionEvent e) {
193 b.setToolTipText("Replace the firmware in any AltusMetrum product");
195 b = addButton(4, 1, "Fire Igniter");
196 b.addActionListener(new ActionListener() {
197 public void actionPerformed(ActionEvent e) {
201 b.setToolTipText("Remote control of igniters for deployment testing");
202 b = addButton(0, 2, "Scan Channels");
203 b.addActionListener(new ActionListener() {
204 public void actionPerformed(ActionEvent e) {
208 b.setToolTipText("Find what channel an altimeter is sending telemetry on");
209 b = addButton(1, 2, "Load Maps");
210 b.addActionListener(new ActionListener() {
211 public void actionPerformed(ActionEvent e) {
215 b.setToolTipText("Download satellite images for off-line flight monitoring");
216 b = addButton(2, 2, "Monitor Idle");
217 b.addActionListener(new ActionListener() {
218 public void actionPerformed(ActionEvent e) {
222 b.setToolTipText("Check flight readiness of altimeter in idle mode");
224 // b = addButton(3, 2, "Launch Controller");
225 // b.addActionListener(new ActionListener() {
226 // public void actionPerformed(ActionEvent e) {
227 // LaunchController();
231 b = addButton(4, 2, "Quit");
232 b.addActionListener(new ActionListener() {
233 public void actionPerformed(ActionEvent e) {
237 b.setToolTipText("Close all active windows and terminate AltosUI");
247 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
248 addWindowListener(new WindowAdapter() {
250 public void windowClosing(WindowEvent e) {
255 setLocationByPlatform(false);
257 /* Insets aren't set before the window is visible */
261 private void ConnectToDevice() {
262 AltosDevice device = AltosDeviceUIDialog.show(AltosUI.this,
263 Altos.product_basestation);
266 telemetry_window(device);
269 void ConfigureCallsign() {
271 result = JOptionPane.showInputDialog(AltosUI.this,
272 "Configure Callsign",
273 AltosUIPreferences.callsign());
275 AltosUIPreferences.set_callsign(result);
278 void ConfigureTeleMetrum() {
279 new AltosConfigFC(AltosUI.this);
282 void ConfigureTeleDongle() {
283 new AltosConfigTD(AltosUI.this);
287 AltosFlashUI.show(AltosUI.this);
291 new AltosIgniteUI(AltosUI.this);
294 void ScanChannels() {
295 new AltosScanUI(AltosUI.this, true);
299 new AltosUIMapPreload(AltosUI.this);
302 void LaunchController() {
303 new AltosLaunchUI(AltosUI.this);
307 * Replay a flight from telemetry data
309 private void Replay() {
310 AltosDataChooser chooser = new AltosDataChooser(
313 AltosRecordSet set = chooser.runDialog();
315 AltosReplayReader reader = new AltosReplayReader(set, chooser.file());
316 new AltosFlightUI(voice, reader);
320 /* Connect to TeleMetrum, either directly or through
321 * a TeleDongle over the packet link
324 public void graph_flights(AltosEepromList flights) {
325 for (AltosEepromLog flight : flights) {
326 if (flight.graph_selected && flight.file != null) {
327 process_graph(this, flight.file);
332 private void SaveFlightData() {
333 new AltosEepromManage(this, this, AltosLib.product_any);
336 private static AltosFlightSeries make_series(AltosRecordSet set) {
337 AltosFlightSeries series = new AltosFlightSeries(set.cal_data());
338 set.capture_series(series);
343 /* Load a flight log file and write out a CSV file containing
344 * all of the data in standard units
347 private void ExportData() {
348 AltosDataChooser chooser;
349 chooser = new AltosDataChooser(this);
350 AltosRecordSet set = chooser.runDialog();
353 AltosFlightSeries series = make_series(set);
354 new AltosCSVUI(AltosUI.this, series, chooser.file());
357 private static boolean graph_file(AltosUI altosui, AltosRecordSet set, File file) {
361 JOptionPane.showMessageDialog(altosui,
362 String.format("Failed to parse file %s", file),
364 JOptionPane.ERROR_MESSAGE);
368 new AltosGraphUI(set, file);
370 } catch (InterruptedException ie) {
371 } catch (IOException ie) {
376 /* Load a flight log CSV file and display a pretty graph.
379 private void GraphData() {
380 AltosDataChooser chooser;
381 chooser = new AltosDataChooser(this);
382 AltosRecordSet set = chooser.runDialog();
383 graph_file(this, set, chooser.file());
386 private void ConfigureAltosUI() {
387 new AltosConfigureUI(AltosUI.this, voice);
390 private void IdleMonitor() {
392 new AltosIdleMonitorUI(this);
393 } catch (Exception e) {
397 static AltosWriter open_csv(File file) {
399 return new AltosCSV(file);
400 } catch (FileNotFoundException fe) {
401 System.out.printf("%s\n", fe.getMessage());
406 static AltosWriter open_kml(File file) {
408 return new AltosKML(file);
409 } catch (FileNotFoundException fe) {
410 System.out.printf("%s\n", fe.getMessage());
415 static AltosRecordSet record_set(File input) {
417 return AltosLib.record_set(input);
418 } catch (IOException ie) {
419 String message = ie.getMessage();
421 message = String.format("%s (I/O error)", input.toString());
422 System.err.printf("%s: %s\n", input.toString(), message);
427 static final int process_none = 0;
428 static final int process_csv = 1;
429 static final int process_kml = 2;
430 static final int process_graph = 3;
431 static final int process_replay = 4;
432 static final int process_summary = 5;
433 static final int process_oneline = 6;
435 static boolean process_csv(File input) {
436 AltosRecordSet set = record_set(input);
440 File output = Altos.replace_extension(input,".csv");
441 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
442 if (input.equals(output)) {
443 System.out.printf("Not processing '%s'\n", input);
446 AltosWriter writer = open_csv(output);
449 AltosFlightSeries series = make_series(set);
450 writer.write(series);
456 static boolean process_kml(File input) {
457 AltosRecordSet set = record_set(input);
461 File output = Altos.replace_extension(input,".kml");
462 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
463 if (input.equals(output)) {
464 System.out.printf("Not processing '%s'\n", input);
467 AltosWriter writer = open_kml(output);
470 AltosFlightSeries series = make_series(set);
472 writer.write(series);
478 static AltosReplayReader replay_file(File file) {
479 AltosRecordSet set = record_set(file);
482 return new AltosReplayReader(set, file);
485 static boolean process_replay(File file) {
486 AltosReplayReader reader = replay_file(file);
489 AltosFlightUI flight_ui = new AltosFlightUI(new AltosVoice(), reader);
493 static boolean process_graph(AltosUI altosui, File file) {
494 AltosRecordSet set = record_set(file);
495 return graph_file(altosui, set, file);
498 static boolean process_summary(File file) {
499 AltosRecordSet set = record_set(file);
502 System.out.printf("%s:\n", file.toString());
503 AltosFlightSeries series = make_series(set);
504 AltosFlightStats stats = new AltosFlightStats(series);
505 if (stats.serial != AltosLib.MISSING)
506 System.out.printf("Serial: %5d\n", stats.serial);
507 if (stats.flight != AltosLib.MISSING)
508 System.out.printf("Flight: %5d\n", stats.flight);
509 if (stats.year != AltosLib.MISSING)
510 System.out.printf("Date: %04d-%02d-%02d\n",
511 stats.year, stats.month, stats.day);
512 if (stats.hour != AltosLib.MISSING)
513 System.out.printf("Time: %02d:%02d:%02d UTC\n",
514 stats.hour, stats.minute, stats.second);
515 if (stats.max_height != AltosLib.MISSING)
516 System.out.printf("Max height: %6.0f m %6.0f ft\n",
518 AltosConvert.meters_to_feet(stats.max_height));
519 if (stats.max_speed != AltosLib.MISSING)
520 System.out.printf("Max speed: %6.0f m/s %6.0f ft/s %6.4f Mach\n",
522 AltosConvert.meters_to_feet(stats.max_speed),
523 AltosConvert.meters_to_mach(stats.max_speed));
524 if (stats.max_acceleration != AltosLib.MISSING) {
525 System.out.printf("Max accel: %6.0f m/s² %6.0f ft/s² %6.2f g\n",
526 stats.max_acceleration,
527 AltosConvert.meters_to_feet(stats.max_acceleration),
528 AltosConvert.meters_to_g(stats.max_acceleration));
530 if (stats.state_speed[Altos.ao_flight_drogue] != AltosLib.MISSING)
531 System.out.printf("Drogue rate: %6.0f m/s %6.0f ft/s\n",
532 stats.state_speed[Altos.ao_flight_drogue],
533 AltosConvert.meters_to_feet(stats.state_speed[Altos.ao_flight_drogue]));
534 if (stats.state_speed[Altos.ao_flight_main] != AltosLib.MISSING)
535 System.out.printf("Main rate: %6.0f m/s %6.0f ft/s\n",
536 stats.state_speed[Altos.ao_flight_main],
537 AltosConvert.meters_to_feet(stats.state_speed[Altos.ao_flight_main]));
538 if (stats.landed_time != AltosLib.MISSING &&
539 stats.boost_time != AltosLib.MISSING &&
540 stats.landed_time > stats.boost_time)
541 System.out.printf("Flight time: %6.0f s\n",
544 System.out.printf("\n");
548 static boolean process_oneline(File file) {
549 AltosRecordSet set = record_set(file);
552 System.out.printf("%s", file.toString());
553 AltosFlightSeries series = make_series(set);
554 AltosFlightStats stats = new AltosFlightStats(series);
555 if (stats.max_height != AltosLib.MISSING)
556 System.out.printf(" height %6.0f m", stats.max_height);
557 if (stats.max_speed != AltosLib.MISSING)
558 System.out.printf(" speed %6.0f m/s", stats.max_speed);
559 if (stats.state_enter_speed[AltosLib.ao_flight_drogue] != AltosLib.MISSING)
560 System.out.printf(" drogue-deploy %6.0f m/s", stats.state_enter_speed[AltosLib.ao_flight_drogue]);
561 if (stats.max_acceleration != AltosLib.MISSING)
562 System.out.printf(" accel %6.0f m/s²", stats.max_acceleration);
563 System.out.printf("\n");
567 public static void help(int code) {
568 System.out.printf("Usage: altosui [OPTION]... [FILE]...\n");
569 System.out.printf(" Options:\n");
570 System.out.printf(" --replay <filename>\t\trelive the glory of past flights \n");
571 System.out.printf(" --graph <filename>\t\tgraph a flight\n");
572 System.out.printf(" --summary <filename>\t\tText summary of a flight\n");
573 System.out.printf(" --oneline <filename>\t\tOne line summary of a flight\n");
574 System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n");
575 System.out.printf(" --kml\tgenerate KML output for use with Google Earth\n");
579 public static void main(final String[] args) {
583 UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
584 } catch (Exception e) {
586 AltosUI altosui = null;
588 /* Handle batch-mode */
589 if (args.length == 0) {
590 altosui = new AltosUI();
591 java.util.List<AltosDevice> devices = AltosUSBDevice.list(Altos.product_basestation);
593 for (AltosDevice device : devices)
594 altosui.telemetry_window(device);
596 int process = process_none;
597 for (int i = 0; i < args.length; i++) {
598 if (args[i].equals("--help"))
600 else if (args[i].equals("--replay"))
601 process = process_replay;
602 else if (args[i].equals("--kml"))
603 process = process_kml;
604 else if (args[i].equals("--csv"))
605 process = process_csv;
606 else if (args[i].equals("--graph"))
607 process = process_graph;
608 else if (args[i].equals("--summary"))
609 process = process_summary;
610 else if (args[i].equals("--oneline"))
611 process = process_oneline;
612 else if (args[i].startsWith("--"))
615 File file = new File(args[i]);
619 altosui = new AltosUI();
621 if (!process_graph(null, file))
625 if (!process_replay(file))
629 if (!process_kml(file))
633 if (!process_csv(file))
636 case process_summary:
637 if (!process_summary(file))
640 case process_oneline:
641 if (!process_oneline(file))