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_4.*;
26 import org.altusmetrum.altosuilib_2.*;
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);
106 AltosUIPreferences.set_component(this);
108 pane = getContentPane();
109 gridbag = new GridBagLayout();
110 pane.setLayout(gridbag);
114 b = addButton(0, 0, "Monitor Flight");
115 b.addActionListener(new ActionListener() {
116 public void actionPerformed(ActionEvent e) {
120 b.setToolTipText("Connect to TeleDongle and monitor telemetry");
121 b = addButton(1, 0, "Save Flight Data");
122 b.addActionListener(new ActionListener() {
123 public void actionPerformed(ActionEvent e) {
127 b.setToolTipText("Download and/or delete flight data from an altimeter");
128 b = addButton(2, 0, "Replay Flight");
129 b.addActionListener(new ActionListener() {
130 public void actionPerformed(ActionEvent e) {
134 b.setToolTipText("Watch an old flight in real-time");
135 b = addButton(3, 0, "Graph Data");
136 b.addActionListener(new ActionListener() {
137 public void actionPerformed(ActionEvent e) {
141 b.setToolTipText("Present flight data in a graph and table of statistics");
142 b = addButton(4, 0, "Export Data");
143 b.addActionListener(new ActionListener() {
144 public void actionPerformed(ActionEvent e) {
148 b.setToolTipText("Convert flight data for a spreadsheet or GoogleEarth");
149 b = addButton(0, 1, "Configure Altimeter");
150 b.addActionListener(new ActionListener() {
151 public void actionPerformed(ActionEvent e) {
152 ConfigureTeleMetrum();
155 b.setToolTipText("Set flight, storage and communication parameters");
156 b = addButton(1, 1, "Configure AltosUI");
157 b.addActionListener(new ActionListener() {
158 public void actionPerformed(ActionEvent e) {
162 b.setToolTipText("Global AltosUI settings");
164 b = addButton(2, 1, "Configure Ground Station");
165 b.addActionListener(new ActionListener() {
166 public void actionPerformed(ActionEvent e) {
167 ConfigureTeleDongle();
171 b = addButton(3, 1, "Flash Image");
172 b.addActionListener(new ActionListener() {
173 public void actionPerformed(ActionEvent e) {
177 b.setToolTipText("Replace the firmware in any AltusMetrum product");
179 b = addButton(4, 1, "Fire Igniter");
180 b.addActionListener(new ActionListener() {
181 public void actionPerformed(ActionEvent e) {
185 b.setToolTipText("Remote control of igniters for deployment testing");
186 b = addButton(0, 2, "Scan Channels");
187 b.addActionListener(new ActionListener() {
188 public void actionPerformed(ActionEvent e) {
192 b.setToolTipText("Find what channel an altimeter is sending telemetry on");
193 b = addButton(1, 2, "Load Maps");
194 b.addActionListener(new ActionListener() {
195 public void actionPerformed(ActionEvent e) {
199 b.setToolTipText("Download satellite images for off-line flight monitoring");
200 b = addButton(2, 2, "Monitor Idle");
201 b.addActionListener(new ActionListener() {
202 public void actionPerformed(ActionEvent e) {
206 b.setToolTipText("Check flight readiness of altimeter in idle mode");
208 // b = addButton(3, 2, "Launch Controller");
209 // b.addActionListener(new ActionListener() {
210 // public void actionPerformed(ActionEvent e) {
211 // LaunchController();
215 b = addButton(4, 2, "Quit");
216 b.addActionListener(new ActionListener() {
217 public void actionPerformed(ActionEvent e) {
221 b.setToolTipText("Close all active windows and terminate AltosUI");
231 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
232 addWindowListener(new WindowAdapter() {
234 public void windowClosing(WindowEvent e) {
239 setLocationByPlatform(false);
241 /* Insets aren't set before the window is visible */
245 private void ConnectToDevice() {
246 AltosDevice device = AltosDeviceUIDialog.show(AltosUI.this,
247 Altos.product_basestation);
250 telemetry_window(device);
253 void ConfigureCallsign() {
255 result = JOptionPane.showInputDialog(AltosUI.this,
256 "Configure Callsign",
257 AltosUIPreferences.callsign());
259 AltosUIPreferences.set_callsign(result);
262 void ConfigureTeleMetrum() {
263 new AltosConfig(AltosUI.this);
266 void ConfigureTeleDongle() {
267 new AltosConfigTD(AltosUI.this);
271 AltosFlashUI.show(AltosUI.this);
275 new AltosIgniteUI(AltosUI.this);
278 void ScanChannels() {
279 new AltosScanUI(AltosUI.this, true);
283 new AltosUIMapPreload(AltosUI.this);
286 void LaunchController() {
287 new AltosLaunchUI(AltosUI.this);
291 * Replay a flight from telemetry data
293 private void Replay() {
294 AltosDataChooser chooser = new AltosDataChooser(
297 Iterable<AltosState> states = chooser.runDialog();
298 if (states != null) {
299 AltosFlightReader reader = new AltosReplayReader(states.iterator(),
301 new AltosFlightUI(voice, reader);
305 /* Connect to TeleMetrum, either directly or through
306 * a TeleDongle over the packet link
308 private void SaveFlightData() {
309 new AltosEepromManage(AltosUI.this, AltosLib.product_any);
312 /* Load a flight log file and write out a CSV file containing
313 * all of the data in standard units
316 private void ExportData() {
317 AltosDataChooser chooser;
318 chooser = new AltosDataChooser(this);
319 AltosStateIterable states = chooser.runDialog();
322 new AltosCSVUI(AltosUI.this, states, chooser.file());
325 /* Load a flight log CSV file and display a pretty graph.
328 private void GraphData() {
329 AltosDataChooser chooser;
330 chooser = new AltosDataChooser(this);
331 AltosStateIterable states = chooser.runDialog();
335 new AltosGraphUI(states, chooser.file());
336 } catch (InterruptedException ie) {
337 } catch (IOException ie) {
341 private void ConfigureAltosUI() {
342 new AltosConfigureUI(AltosUI.this, voice);
345 private void IdleMonitor() {
347 new AltosIdleMonitorUI(this);
348 } catch (Exception e) {
352 static AltosStateIterable open_logfile(File file) {
356 in = new FileInputStream(file);
357 if (file.getName().endsWith("telem"))
358 return new AltosTelemetryFile(in);
360 return new AltosEepromFile(in);
361 } catch (FileNotFoundException fe) {
362 System.out.printf("%s\n", fe.getMessage());
367 static AltosWriter open_csv(File file) {
369 return new AltosCSV(file);
370 } catch (FileNotFoundException fe) {
371 System.out.printf("%s\n", fe.getMessage());
376 static AltosWriter open_kml(File file) {
378 return new AltosKML(file);
379 } catch (FileNotFoundException fe) {
380 System.out.printf("%s\n", fe.getMessage());
385 static final int process_none = 0;
386 static final int process_csv = 1;
387 static final int process_kml = 2;
388 static final int process_graph = 3;
389 static final int process_replay = 4;
390 static final int process_summary = 5;
391 static final int process_cat = 6;
393 static boolean process_csv(File input) {
394 AltosStateIterable states = open_logfile(input);
398 File output = Altos.replace_extension(input,".csv");
399 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
400 if (input.equals(output)) {
401 System.out.printf("Not processing '%s'\n", input);
404 AltosWriter writer = open_csv(output);
407 writer.write(states);
413 static boolean process_kml(File input) {
414 AltosStateIterable states = open_logfile(input);
418 File output = Altos.replace_extension(input,".kml");
419 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
420 if (input.equals(output)) {
421 System.out.printf("Not processing '%s'\n", input);
424 AltosWriter writer = open_kml(output);
427 writer.write(states);
433 static AltosStateIterable record_iterable(File file) {
436 in = new FileInputStream(file);
437 } catch (Exception e) {
438 System.out.printf("Failed to open file '%s'\n", file);
441 if (file.getName().endsWith("telem"))
442 return new AltosTelemetryFile(in);
444 return new AltosEepromFile(in);
447 static AltosReplayReader replay_file(File file) {
448 AltosStateIterable states = record_iterable(file);
451 return new AltosReplayReader(states.iterator(), file);
454 static boolean process_replay(File file) {
455 AltosReplayReader reader = replay_file(file);
458 AltosFlightUI flight_ui = new AltosFlightUI(new AltosVoice(), reader);
459 flight_ui.set_exit_on_close();
463 static boolean process_graph(File file) {
464 AltosStateIterable states = record_iterable(file);
468 new AltosGraphUI(states, file);
470 } catch (InterruptedException ie) {
471 } catch (IOException ie) {
476 static boolean process_summary(File file) {
477 AltosStateIterable states = record_iterable(file);
481 AltosFlightStats stats = new AltosFlightStats(states);
482 if (stats.serial > 0)
483 System.out.printf("Serial: %5d\n", stats.serial);
484 if (stats.flight > 0)
485 System.out.printf("Flight: %5d\n", stats.flight);
487 System.out.printf("Date: %04d-%02d-%02d\n",
488 stats.year, stats.month, stats.day);
490 System.out.printf("Time: %02d:%02d:%02d UTC\n",
491 stats.hour, stats.minute, stats.second);
492 System.out.printf("Max height: %6.0f m %6.0f ft\n",
494 AltosConvert.meters_to_feet(stats.max_height));
495 System.out.printf("Max speed: %6.0f m/s %6.0f ft/s %6.4f Mach\n",
497 AltosConvert.meters_to_feet(stats.max_speed),
498 AltosConvert.meters_to_mach(stats.max_speed));
499 if (stats.max_acceleration != AltosLib.MISSING) {
500 System.out.printf("Max accel: %6.0f m/s² %6.0f ft/s² %6.2f g\n",
501 stats.max_acceleration,
502 AltosConvert.meters_to_feet(stats.max_acceleration),
503 AltosConvert.meters_to_g(stats.max_acceleration));
505 System.out.printf("Drogue rate: %6.0f m/s %6.0f ft/s\n",
506 stats.state_speed[Altos.ao_flight_drogue],
507 AltosConvert.meters_to_feet(stats.state_speed[Altos.ao_flight_drogue]));
508 System.out.printf("Main rate: %6.0f m/s %6.0f ft/s\n",
509 stats.state_speed[Altos.ao_flight_main],
510 AltosConvert.meters_to_feet(stats.state_speed[Altos.ao_flight_main]));
511 System.out.printf("Flight time: %6.0f s\n",
512 stats.state_end[Altos.ao_flight_main] -
513 stats.state_start[Altos.ao_flight_boost]);
515 } catch (InterruptedException ie) {
516 } catch (IOException ie) {
521 static boolean process_cat(File file) {
523 AltosStateIterable eef = record_iterable(file);
525 System.out.printf ("process cat\n");
526 for (AltosState state : eef) {
527 System.out.printf ("tick %d state %d height %g\n",
528 state.tick, state.state, state.height());
529 if ((state.set & AltosState.set_gps) != 0)
530 System.out.printf ("time %g lat %g lon %g alt %g\n",
531 state.time_since_boost(),
537 } catch (Exception e) {
538 System.out.printf("Failed to open file '%s'\n", file);
544 public static void help(int code) {
545 System.out.printf("Usage: altosui [OPTION]... [FILE]...\n");
546 System.out.printf(" Options:\n");
547 System.out.printf(" --fetchmaps <lat> <lon>\tpre-fetch maps for site map view\n");
548 System.out.printf(" --replay <filename>\t\trelive the glory of past flights \n");
549 System.out.printf(" --graph <filename>\t\tgraph a flight\n");
550 System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n");
551 System.out.printf(" --kml\tgenerate KML output for use with Google Earth\n");
555 public static void main(final String[] args) {
559 UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
560 } catch (Exception e) {
562 /* Handle batch-mode */
563 if (args.length == 0) {
564 AltosUI altosui = new AltosUI();
566 java.util.List<AltosDevice> devices = AltosUSBDevice.list(Altos.product_basestation);
568 for (AltosDevice device : devices)
569 altosui.telemetry_window(device);
571 int process = process_none;
572 for (int i = 0; i < args.length; i++) {
573 if (args[i].equals("--help"))
575 else if (args[i].equals("--fetchmaps")) {
576 if (args.length < i + 3) {
579 double lat = Double.parseDouble(args[i+1]);
580 double lon = Double.parseDouble(args[i+2]);
581 // AltosSiteMap.prefetchMaps(lat, lon);
584 } else if (args[i].equals("--replay"))
585 process = process_replay;
586 else if (args[i].equals("--kml"))
587 process = process_kml;
588 else if (args[i].equals("--csv"))
589 process = process_csv;
590 else if (args[i].equals("--graph"))
591 process = process_graph;
592 else if (args[i].equals("--summary"))
593 process = process_summary;
594 else if (args[i].equals("--cat"))
595 process = process_cat;
596 else if (args[i].startsWith("--"))
599 File file = new File(args[i]);
603 if (!process_graph(file))
607 if (!process_replay(file))
611 if (!process_kml(file))
615 if (!process_csv(file))
618 case process_summary:
619 if (!process_summary(file))
623 if (!process_cat(file))