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_1.*;
26 import org.altusmetrum.altosuilib_1.*;
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);
78 GridBagLayout gridbag;
80 JButton addButton(int x, int y, String label) {
84 c = new GridBagConstraints();
85 c.gridx = x; c.gridy = y;
86 c.fill = GridBagConstraints.BOTH;
89 b = new JButton(label);
91 //Dimension ps = b.getPreferredSize();
93 gridbag.setConstraints(b, c);
102 AltosUIPreferences.set_component(this);
104 pane = getContentPane();
105 gridbag = new GridBagLayout();
106 pane.setLayout(gridbag);
110 b = addButton(0, 0, "Monitor Flight");
111 b.addActionListener(new ActionListener() {
112 public void actionPerformed(ActionEvent e) {
116 b.setToolTipText("Connect to TeleDongle and monitor telemetry");
117 b = addButton(1, 0, "Save Flight Data");
118 b.addActionListener(new ActionListener() {
119 public void actionPerformed(ActionEvent e) {
123 b.setToolTipText("Download and/or delete flight data from an altimeter");
124 b = addButton(2, 0, "Replay Flight");
125 b.addActionListener(new ActionListener() {
126 public void actionPerformed(ActionEvent e) {
130 b.setToolTipText("Watch an old flight in real-time");
131 b = addButton(3, 0, "Graph Data");
132 b.addActionListener(new ActionListener() {
133 public void actionPerformed(ActionEvent e) {
137 b.setToolTipText("Present flight data in a graph and table of statistics");
138 b = addButton(4, 0, "Export Data");
139 b.addActionListener(new ActionListener() {
140 public void actionPerformed(ActionEvent e) {
144 b.setToolTipText("Convert flight data for a spreadsheet or GoogleEarth");
145 b = addButton(0, 1, "Configure Altimeter");
146 b.addActionListener(new ActionListener() {
147 public void actionPerformed(ActionEvent e) {
148 ConfigureTeleMetrum();
151 b.setToolTipText("Set flight, storage and communication parameters");
152 b = addButton(1, 1, "Configure AltosUI");
153 b.addActionListener(new ActionListener() {
154 public void actionPerformed(ActionEvent e) {
158 b.setToolTipText("Global AltosUI settings");
160 b = addButton(2, 1, "Configure Ground Station");
161 b.addActionListener(new ActionListener() {
162 public void actionPerformed(ActionEvent e) {
163 ConfigureTeleDongle();
167 b = addButton(3, 1, "Flash Image");
168 b.addActionListener(new ActionListener() {
169 public void actionPerformed(ActionEvent e) {
173 b.setToolTipText("Replace the firmware in any AltusMetrum product");
175 b = addButton(4, 1, "Fire Igniter");
176 b.addActionListener(new ActionListener() {
177 public void actionPerformed(ActionEvent e) {
181 b.setToolTipText("Remote control of igniters for deployment testing");
182 b = addButton(0, 2, "Scan Channels");
183 b.addActionListener(new ActionListener() {
184 public void actionPerformed(ActionEvent e) {
188 b.setToolTipText("Find what channel an altimeter is sending telemetry on");
189 b = addButton(1, 2, "Load Maps");
190 b.addActionListener(new ActionListener() {
191 public void actionPerformed(ActionEvent e) {
195 b.setToolTipText("Download satellite images for off-line flight monitoring");
196 b = addButton(2, 2, "Monitor Idle");
197 b.addActionListener(new ActionListener() {
198 public void actionPerformed(ActionEvent e) {
202 b.setToolTipText("Check flight readiness of altimeter in idle mode");
204 // b = addButton(3, 2, "Launch Controller");
205 // b.addActionListener(new ActionListener() {
206 // public void actionPerformed(ActionEvent e) {
207 // LaunchController();
211 b = addButton(4, 2, "Quit");
212 b.addActionListener(new ActionListener() {
213 public void actionPerformed(ActionEvent e) {
217 b.setToolTipText("Close all active windows and terminate AltosUI");
227 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
228 addWindowListener(new WindowAdapter() {
230 public void windowClosing(WindowEvent e) {
235 setLocationByPlatform(false);
237 /* Insets aren't set before the window is visible */
241 private void ConnectToDevice() {
242 AltosDevice device = AltosDeviceUIDialog.show(AltosUI.this,
243 Altos.product_basestation);
246 telemetry_window(device);
249 void ConfigureCallsign() {
251 result = JOptionPane.showInputDialog(AltosUI.this,
252 "Configure Callsign",
253 AltosUIPreferences.callsign());
255 AltosUIPreferences.set_callsign(result);
258 void ConfigureTeleMetrum() {
259 new AltosConfig(AltosUI.this);
262 void ConfigureTeleDongle() {
263 new AltosConfigTD(AltosUI.this);
267 AltosFlashUI.show(AltosUI.this);
271 new AltosIgniteUI(AltosUI.this);
274 void ScanChannels() {
275 new AltosScanUI(AltosUI.this);
279 new AltosSiteMapPreload(AltosUI.this);
282 void LaunchController() {
283 new AltosLaunchUI(AltosUI.this);
287 * Replay a flight from telemetry data
289 private void Replay() {
290 AltosDataChooser chooser = new AltosDataChooser(
293 AltosRecordIterable iterable = chooser.runDialog();
294 if (iterable != null) {
295 AltosFlightReader reader = new AltosReplayReader(iterable.iterator(),
297 new AltosFlightUI(voice, reader);
301 /* Connect to TeleMetrum, either directly or through
302 * a TeleDongle over the packet link
304 private void SaveFlightData() {
305 new AltosEepromManage(AltosUI.this);
308 /* Load a flight log file and write out a CSV file containing
309 * all of the data in standard units
312 private void ExportData() {
313 AltosDataChooser chooser;
314 chooser = new AltosDataChooser(this);
315 AltosRecordIterable record_reader = chooser.runDialog();
316 if (record_reader == null)
318 new AltosCSVUI(AltosUI.this, record_reader, chooser.file());
321 /* Load a flight log CSV file and display a pretty graph.
324 private void GraphData() {
325 AltosDataChooser chooser;
326 chooser = new AltosDataChooser(this);
327 AltosRecordIterable record_reader = chooser.runDialog();
328 if (record_reader == null)
331 new AltosGraphUI(record_reader, chooser.file());
332 } catch (InterruptedException ie) {
333 } catch (IOException ie) {
337 private void ConfigureAltosUI() {
338 new AltosConfigureUI(AltosUI.this, voice);
341 private void IdleMonitor() {
343 new AltosIdleMonitorUI(this);
344 } catch (Exception e) {
348 static AltosRecordIterable open_logfile(File file) {
352 in = new FileInputStream(file);
353 if (file.getName().endsWith("eeprom"))
354 return new AltosEepromIterable(in);
355 else if (file.getName().endsWith("mega"))
356 return new AltosEepromMegaIterable(in);
358 return new AltosTelemetryIterable(in);
359 } catch (FileNotFoundException fe) {
360 System.out.printf("%s\n", fe.getMessage());
365 static AltosWriter open_csv(File file) {
367 return new AltosCSV(file);
368 } catch (FileNotFoundException fe) {
369 System.out.printf("%s\n", fe.getMessage());
374 static AltosWriter open_kml(File file) {
376 return new AltosKML(file);
377 } catch (FileNotFoundException fe) {
378 System.out.printf("%s\n", fe.getMessage());
383 static final int process_none = 0;
384 static final int process_csv = 1;
385 static final int process_kml = 2;
386 static final int process_graph = 3;
387 static final int process_replay = 4;
388 static final int process_summary = 5;
390 static boolean process_csv(File input) {
391 AltosRecordIterable iterable = open_logfile(input);
392 if (iterable == null)
395 File output = Altos.replace_extension(input,".csv");
396 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
397 if (input.equals(output)) {
398 System.out.printf("Not processing '%s'\n", input);
401 AltosWriter writer = open_csv(output);
404 writer.write(iterable);
410 static boolean process_kml(File input) {
411 AltosRecordIterable iterable = open_logfile(input);
412 if (iterable == null)
415 File output = Altos.replace_extension(input,".kml");
416 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
417 if (input.equals(output)) {
418 System.out.printf("Not processing '%s'\n", input);
421 AltosWriter writer = open_kml(output);
424 writer.write(iterable);
430 static AltosRecordIterable record_iterable(File file) {
433 in = new FileInputStream(file);
434 } catch (Exception e) {
435 System.out.printf("Failed to open file '%s'\n", file);
438 AltosRecordIterable recs;
439 //AltosReplayReader reader;
440 if (file.getName().endsWith("eeprom")) {
441 recs = new AltosEepromIterable(in);
442 } else if (file.getName().endsWith("mega")) {
443 recs = new AltosEepromMegaIterable(in);
445 recs = new AltosTelemetryIterable(in);
450 static AltosReplayReader replay_file(File file) {
451 AltosRecordIterable recs = record_iterable(file);
454 return new AltosReplayReader(recs.iterator(), file);
457 static boolean process_replay(File file) {
458 AltosReplayReader reader = replay_file(file);
461 AltosFlightUI flight_ui = new AltosFlightUI(new AltosVoice(), reader);
462 flight_ui.set_exit_on_close();
466 static boolean process_graph(File file) {
467 AltosRecordIterable recs = record_iterable(file);
471 new AltosGraphUI(recs, file);
473 } catch (InterruptedException ie) {
474 } catch (IOException ie) {
479 static boolean process_summary(File file) {
480 AltosRecordIterable iterable = record_iterable(file);
481 if (iterable == null)
484 AltosFlightStats stats = new AltosFlightStats(iterable);
485 if (stats.serial > 0)
486 System.out.printf("Serial: %5d\n", stats.serial);
487 if (stats.flight > 0)
488 System.out.printf("Flight: %5d\n", stats.flight);
490 System.out.printf("Date: %04d-%02d-%02d\n",
491 stats.year, stats.month, stats.day);
493 System.out.printf("Time: %02d:%02d:%02d UTC\n",
494 stats.hour, stats.minute, stats.second);
495 System.out.printf("Max height: %6.0f m %6.0f ft\n",
497 AltosConvert.meters_to_feet(stats.max_height));
498 System.out.printf("Max speed: %6.0f m/s %6.0f ft/s %6.4f Mach\n",
500 AltosConvert.meters_to_feet(stats.max_speed),
501 AltosConvert.meters_to_mach(stats.max_speed));
502 if (stats.max_acceleration != AltosRecord.MISSING) {
503 System.out.printf("Max accel: %6.0f m/s² %6.0f ft/s² %6.2f g\n",
504 stats.max_acceleration,
505 AltosConvert.meters_to_feet(stats.max_acceleration),
506 AltosConvert.meters_to_g(stats.max_acceleration));
508 System.out.printf("Drogue rate: %6.0f m/s %6.0f ft/s\n",
509 stats.state_baro_speed[Altos.ao_flight_drogue],
510 AltosConvert.meters_to_feet(stats.state_baro_speed[Altos.ao_flight_drogue]));
511 System.out.printf("Main rate: %6.0f m/s %6.0f ft/s\n",
512 stats.state_baro_speed[Altos.ao_flight_main],
513 AltosConvert.meters_to_feet(stats.state_baro_speed[Altos.ao_flight_main]));
514 System.out.printf("Flight time: %6.0f s\n",
515 stats.state_end[Altos.ao_flight_main] -
516 stats.state_start[Altos.ao_flight_boost]);
518 } catch (InterruptedException ie) {
519 } catch (IOException ie) {
524 public static void help(int code) {
525 System.out.printf("Usage: altosui [OPTION]... [FILE]...\n");
526 System.out.printf(" Options:\n");
527 System.out.printf(" --fetchmaps <lat> <lon>\tpre-fetch maps for site map view\n");
528 System.out.printf(" --replay <filename>\t\trelive the glory of past flights \n");
529 System.out.printf(" --graph <filename>\t\tgraph a flight\n");
530 System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n");
531 System.out.printf(" --kml\tgenerate KML output for use with Google Earth\n");
535 public static void main(final String[] args) {
539 UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
540 } catch (Exception e) {
542 /* Handle batch-mode */
543 if (args.length == 0) {
544 AltosUI altosui = new AltosUI();
546 java.util.List<AltosDevice> devices = AltosUSBDevice.list(Altos.product_basestation);
547 for (AltosDevice device : devices)
548 altosui.telemetry_window(device);
550 int process = process_none;
551 for (int i = 0; i < args.length; i++) {
552 if (args[i].equals("--help"))
554 else if (args[i].equals("--fetchmaps")) {
555 if (args.length < i + 3) {
558 double lat = Double.parseDouble(args[i+1]);
559 double lon = Double.parseDouble(args[i+2]);
560 AltosSiteMap.prefetchMaps(lat, lon, 5, 5);
563 } else if (args[i].equals("--replay"))
564 process = process_replay;
565 else if (args[i].equals("--kml"))
566 process = process_kml;
567 else if (args[i].equals("--csv"))
568 process = process_csv;
569 else if (args[i].equals("--graph"))
570 process = process_graph;
571 else if (args[i].equals("--summary"))
572 process = process_summary;
573 else if (args[i].startsWith("--"))
576 File file = new File(args[i]);
580 if (!process_graph(file))
584 if (!process_replay(file))
588 if (!process_kml(file))
592 if (!process_csv(file))
595 case process_summary:
596 if (!process_summary(file))