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_9.*;
26 import org.altusmetrum.altosuilib_9.*;
28 public class AltosUI extends AltosUIFrame {
29 public AltosVoice voice = new AltosVoice();
31 public static boolean load_library(Frame frame) {
32 if (!Altos.load_library()) {
33 JOptionPane.showMessageDialog(frame,
34 String.format("No AltOS library in \"%s\"",
35 System.getProperty("java.library.path","<undefined>")),
36 "Cannot load device access library",
37 JOptionPane.ERROR_MESSAGE);
43 void telemetry_window(AltosDevice device) {
45 AltosFlightReader reader = new AltosTelemetryReader(new AltosSerial(device));
47 new AltosFlightUI(voice, reader, device.getSerial());
48 } catch (FileNotFoundException ee) {
49 JOptionPane.showMessageDialog(AltosUI.this,
51 String.format ("Cannot open %s", device.toShortString()),
52 JOptionPane.ERROR_MESSAGE);
53 } catch (AltosSerialInUseException si) {
54 JOptionPane.showMessageDialog(AltosUI.this,
55 String.format("Device \"%s\" already in use",
56 device.toShortString()),
58 JOptionPane.ERROR_MESSAGE);
59 } catch (IOException ee) {
60 JOptionPane.showMessageDialog(AltosUI.this,
61 String.format ("Unknown I/O error on %s", device.toShortString()),
63 JOptionPane.ERROR_MESSAGE);
64 } catch (TimeoutException te) {
65 JOptionPane.showMessageDialog(this,
66 String.format ("Timeout on %s", device.toShortString()),
68 JOptionPane.ERROR_MESSAGE);
69 } catch (InterruptedException ie) {
70 JOptionPane.showMessageDialog(this,
71 String.format("Interrupted %s", device.toShortString()),
72 "Interrupted exception",
73 JOptionPane.ERROR_MESSAGE);
77 public void scan_device_selected(AltosDevice device) {
78 telemetry_window(device);
82 GridBagLayout gridbag;
84 JButton addButton(int x, int y, String label) {
88 c = new GridBagConstraints();
89 c.gridx = x; c.gridy = y;
90 c.fill = GridBagConstraints.BOTH;
93 b = new JButton(label);
95 //Dimension ps = b.getPreferredSize();
97 gridbag.setConstraints(b, c);
102 /* OSXAdapter interfaces */
103 public void macosx_file_handler(String path) {
104 process_graph(new File(path));
107 public void macosx_quit_handler() {
111 public void macosx_preferences_handler() {
119 register_for_macosx_events();
121 AltosUIPreferences.set_component(this);
123 pane = getContentPane();
124 gridbag = new GridBagLayout();
125 pane.setLayout(gridbag);
129 b = addButton(0, 0, "Monitor Flight");
130 b.addActionListener(new ActionListener() {
131 public void actionPerformed(ActionEvent e) {
135 b.setToolTipText("Connect to TeleDongle and monitor telemetry");
136 b = addButton(1, 0, "Save Flight Data");
137 b.addActionListener(new ActionListener() {
138 public void actionPerformed(ActionEvent e) {
142 b.setToolTipText("Download and/or delete flight data from an altimeter");
143 b = addButton(2, 0, "Replay Flight");
144 b.addActionListener(new ActionListener() {
145 public void actionPerformed(ActionEvent e) {
149 b.setToolTipText("Watch an old flight in real-time");
150 b = addButton(3, 0, "Graph Data");
151 b.addActionListener(new ActionListener() {
152 public void actionPerformed(ActionEvent e) {
156 b.setToolTipText("Present flight data in a graph and table of statistics");
157 b = addButton(4, 0, "Export Data");
158 b.addActionListener(new ActionListener() {
159 public void actionPerformed(ActionEvent e) {
163 b.setToolTipText("Convert flight data for a spreadsheet or GoogleEarth");
164 b = addButton(0, 1, "Configure Altimeter");
165 b.addActionListener(new ActionListener() {
166 public void actionPerformed(ActionEvent e) {
167 ConfigureTeleMetrum();
170 b.setToolTipText("Set flight, storage and communication parameters");
171 b = addButton(1, 1, "Configure AltosUI");
172 b.addActionListener(new ActionListener() {
173 public void actionPerformed(ActionEvent e) {
177 b.setToolTipText("Global AltosUI settings");
179 b = addButton(2, 1, "Configure Ground Station");
180 b.addActionListener(new ActionListener() {
181 public void actionPerformed(ActionEvent e) {
182 ConfigureTeleDongle();
186 b = addButton(3, 1, "Flash Image");
187 b.addActionListener(new ActionListener() {
188 public void actionPerformed(ActionEvent e) {
192 b.setToolTipText("Replace the firmware in any AltusMetrum product");
194 b = addButton(4, 1, "Fire Igniter");
195 b.addActionListener(new ActionListener() {
196 public void actionPerformed(ActionEvent e) {
200 b.setToolTipText("Remote control of igniters for deployment testing");
201 b = addButton(0, 2, "Scan Channels");
202 b.addActionListener(new ActionListener() {
203 public void actionPerformed(ActionEvent e) {
207 b.setToolTipText("Find what channel an altimeter is sending telemetry on");
208 b = addButton(1, 2, "Load Maps");
209 b.addActionListener(new ActionListener() {
210 public void actionPerformed(ActionEvent e) {
214 b.setToolTipText("Download satellite images for off-line flight monitoring");
215 b = addButton(2, 2, "Monitor Idle");
216 b.addActionListener(new ActionListener() {
217 public void actionPerformed(ActionEvent e) {
221 b.setToolTipText("Check flight readiness of altimeter in idle mode");
223 // b = addButton(3, 2, "Launch Controller");
224 // b.addActionListener(new ActionListener() {
225 // public void actionPerformed(ActionEvent e) {
226 // LaunchController();
230 b = addButton(4, 2, "Quit");
231 b.addActionListener(new ActionListener() {
232 public void actionPerformed(ActionEvent e) {
236 b.setToolTipText("Close all active windows and terminate AltosUI");
246 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
247 addWindowListener(new WindowAdapter() {
249 public void windowClosing(WindowEvent e) {
254 setLocationByPlatform(false);
256 /* Insets aren't set before the window is visible */
260 private void ConnectToDevice() {
261 AltosDevice device = AltosDeviceUIDialog.show(AltosUI.this,
262 Altos.product_basestation);
265 telemetry_window(device);
268 void ConfigureCallsign() {
270 result = JOptionPane.showInputDialog(AltosUI.this,
271 "Configure Callsign",
272 AltosUIPreferences.callsign());
274 AltosUIPreferences.set_callsign(result);
277 void ConfigureTeleMetrum() {
278 new AltosConfig(AltosUI.this);
281 void ConfigureTeleDongle() {
282 new AltosConfigTD(AltosUI.this);
286 AltosFlashUI.show(AltosUI.this);
290 new AltosIgniteUI(AltosUI.this);
293 void ScanChannels() {
294 new AltosScanUI(AltosUI.this, true);
298 new AltosUIMapPreloadNew(AltosUI.this);
301 void LaunchController() {
302 new AltosLaunchUI(AltosUI.this);
306 * Replay a flight from telemetry data
308 private void Replay() {
309 AltosDataChooser chooser = new AltosDataChooser(
312 Iterable<AltosState> states = chooser.runDialog();
313 if (states != null) {
314 AltosFlightReader reader = new AltosReplayReader(states.iterator(),
316 new AltosFlightUI(voice, reader);
320 /* Connect to TeleMetrum, either directly or through
321 * a TeleDongle over the packet link
323 private void SaveFlightData() {
324 new AltosEepromManage(AltosUI.this, AltosLib.product_any);
327 /* Load a flight log file and write out a CSV file containing
328 * all of the data in standard units
331 private void ExportData() {
332 AltosDataChooser chooser;
333 chooser = new AltosDataChooser(this);
334 AltosStateIterable states = chooser.runDialog();
337 new AltosCSVUI(AltosUI.this, states, chooser.file());
340 /* Load a flight log CSV file and display a pretty graph.
343 private void GraphData() {
344 AltosDataChooser chooser;
345 chooser = new AltosDataChooser(this);
346 AltosStateIterable states = chooser.runDialog();
350 new AltosGraphUI(states, chooser.file());
351 } catch (InterruptedException ie) {
352 } catch (IOException ie) {
356 private void ConfigureAltosUI() {
357 new AltosConfigureUI(AltosUI.this, voice);
360 private void IdleMonitor() {
362 new AltosIdleMonitorUI(this);
363 } catch (Exception e) {
367 static AltosStateIterable open_logfile(File file) {
371 in = new FileInputStream(file);
372 if (file.getName().endsWith("telem"))
373 return new AltosTelemetryFile(in);
375 return new AltosEepromFile(in);
376 } catch (FileNotFoundException fe) {
377 System.out.printf("%s\n", fe.getMessage());
382 static AltosWriter open_csv(File file) {
384 return new AltosCSV(file);
385 } catch (FileNotFoundException fe) {
386 System.out.printf("%s\n", fe.getMessage());
391 static AltosWriter open_kml(File file) {
393 return new AltosKML(file);
394 } catch (FileNotFoundException fe) {
395 System.out.printf("%s\n", fe.getMessage());
400 static final int process_none = 0;
401 static final int process_csv = 1;
402 static final int process_kml = 2;
403 static final int process_graph = 3;
404 static final int process_replay = 4;
405 static final int process_summary = 5;
406 static final int process_cat = 6;
408 static boolean process_csv(File input) {
409 AltosStateIterable states = open_logfile(input);
413 File output = Altos.replace_extension(input,".csv");
414 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
415 if (input.equals(output)) {
416 System.out.printf("Not processing '%s'\n", input);
419 AltosWriter writer = open_csv(output);
422 writer.write(states);
428 static boolean process_kml(File input) {
429 AltosStateIterable states = open_logfile(input);
433 File output = Altos.replace_extension(input,".kml");
434 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
435 if (input.equals(output)) {
436 System.out.printf("Not processing '%s'\n", input);
439 AltosWriter writer = open_kml(output);
442 writer.write(states);
448 static AltosStateIterable record_iterable(File file) {
451 in = new FileInputStream(file);
452 } catch (Exception e) {
453 System.out.printf("Failed to open file '%s'\n", file);
456 if (file.getName().endsWith("telem"))
457 return new AltosTelemetryFile(in);
459 return new AltosEepromFile(in);
462 static AltosReplayReader replay_file(File file) {
463 AltosStateIterable states = record_iterable(file);
466 return new AltosReplayReader(states.iterator(), file);
469 static boolean process_replay(File file) {
470 AltosReplayReader reader = replay_file(file);
473 AltosFlightUI flight_ui = new AltosFlightUI(new AltosVoice(), reader);
477 static boolean process_graph(File file) {
478 AltosStateIterable states = record_iterable(file);
482 new AltosGraphUI(states, file);
484 } catch (InterruptedException ie) {
485 } catch (IOException ie) {
490 static boolean process_summary(File file) {
491 AltosStateIterable states = record_iterable(file);
495 AltosFlightStats stats = new AltosFlightStats(states);
496 if (stats.serial > 0)
497 System.out.printf("Serial: %5d\n", stats.serial);
498 if (stats.flight > 0)
499 System.out.printf("Flight: %5d\n", stats.flight);
501 System.out.printf("Date: %04d-%02d-%02d\n",
502 stats.year, stats.month, stats.day);
504 System.out.printf("Time: %02d:%02d:%02d UTC\n",
505 stats.hour, stats.minute, stats.second);
506 System.out.printf("Max height: %6.0f m %6.0f ft\n",
508 AltosConvert.meters_to_feet(stats.max_height));
509 System.out.printf("Max speed: %6.0f m/s %6.0f ft/s %6.4f Mach\n",
511 AltosConvert.meters_to_feet(stats.max_speed),
512 AltosConvert.meters_to_mach(stats.max_speed));
513 if (stats.max_acceleration != AltosLib.MISSING) {
514 System.out.printf("Max accel: %6.0f m/s² %6.0f ft/s² %6.2f g\n",
515 stats.max_acceleration,
516 AltosConvert.meters_to_feet(stats.max_acceleration),
517 AltosConvert.meters_to_g(stats.max_acceleration));
519 System.out.printf("Drogue rate: %6.0f m/s %6.0f ft/s\n",
520 stats.state_speed[Altos.ao_flight_drogue],
521 AltosConvert.meters_to_feet(stats.state_speed[Altos.ao_flight_drogue]));
522 System.out.printf("Main rate: %6.0f m/s %6.0f ft/s\n",
523 stats.state_speed[Altos.ao_flight_main],
524 AltosConvert.meters_to_feet(stats.state_speed[Altos.ao_flight_main]));
525 System.out.printf("Flight time: %6.0f s\n",
526 stats.state_end[Altos.ao_flight_main] -
527 stats.state_start[Altos.ao_flight_boost]);
529 } catch (InterruptedException ie) {
530 } catch (IOException ie) {
535 static boolean process_cat(File file) {
537 AltosStateIterable eef = record_iterable(file);
539 System.out.printf ("process cat\n");
540 for (AltosState state : eef) {
541 System.out.printf ("tick %d state %d height %g\n",
542 state.tick, state.state(), state.height());
543 if ((state.set & AltosState.set_gps) != 0)
544 System.out.printf ("time %g lat %g lon %g alt %g\n",
545 state.time_since_boost(),
551 } catch (Exception e) {
552 System.out.printf("Failed to open file '%s'\n", file);
558 public static void help(int code) {
559 System.out.printf("Usage: altosui [OPTION]... [FILE]...\n");
560 System.out.printf(" Options:\n");
561 System.out.printf(" --replay <filename>\t\trelive the glory of past flights \n");
562 System.out.printf(" --graph <filename>\t\tgraph a flight\n");
563 System.out.printf(" --summary <filename>\t\tText summary of a flight\n");
564 System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n");
565 System.out.printf(" --kml\tgenerate KML output for use with Google Earth\n");
569 public static void main(final String[] args) {
573 UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
574 } catch (Exception e) {
576 AltosUI altosui = null;
578 /* Handle batch-mode */
579 if (args.length == 0) {
580 altosui = new AltosUI();
581 java.util.List<AltosDevice> devices = AltosUSBDevice.list(Altos.product_basestation);
583 for (AltosDevice device : devices)
584 altosui.telemetry_window(device);
586 int process = process_none;
587 for (int i = 0; i < args.length; i++) {
588 if (args[i].equals("--help"))
590 else if (args[i].equals("--replay"))
591 process = process_replay;
592 else if (args[i].equals("--kml"))
593 process = process_kml;
594 else if (args[i].equals("--csv"))
595 process = process_csv;
596 else if (args[i].equals("--graph"))
597 process = process_graph;
598 else if (args[i].equals("--summary"))
599 process = process_summary;
600 else if (args[i].equals("--cat"))
601 process = process_cat;
602 else if (args[i].startsWith("--"))
605 File file = new File(args[i]);
610 altosui = new AltosUI();
611 if (!process_graph(file))
616 altosui = new AltosUI();
617 if (!process_replay(file))
621 if (!process_kml(file))
625 if (!process_csv(file))
628 case process_summary:
629 if (!process_summary(file))
633 if (!process_cat(file))