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.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.*;
33 public class AltosUI extends JFrame {
34 public AltosVoice voice = new AltosVoice();
36 public static boolean load_library(Frame frame) {
37 if (!Altos.load_library()) {
38 JOptionPane.showMessageDialog(frame,
39 String.format("No AltOS library in \"%s\"",
40 System.getProperty("java.library.path","<undefined>")),
41 "Cannot load device access library",
42 JOptionPane.ERROR_MESSAGE);
48 void telemetry_window(AltosDevice device) {
50 AltosFlightReader reader = new AltosTelemetryReader(device);
52 new AltosFlightUI(voice, reader, device.getSerial());
53 } catch (FileNotFoundException ee) {
54 JOptionPane.showMessageDialog(AltosUI.this,
55 String.format("Cannot open device \"%s\"",
56 device.toShortString()),
57 "Cannot open target device",
58 JOptionPane.ERROR_MESSAGE);
59 } catch (AltosSerialInUseException si) {
60 JOptionPane.showMessageDialog(AltosUI.this,
61 String.format("Device \"%s\" already in use",
62 device.toShortString()),
64 JOptionPane.ERROR_MESSAGE);
65 } catch (IOException ee) {
66 JOptionPane.showMessageDialog(AltosUI.this,
67 device.toShortString(),
69 JOptionPane.ERROR_MESSAGE);
70 } catch (TimeoutException te) {
71 JOptionPane.showMessageDialog(this,
72 device.toShortString(),
74 JOptionPane.ERROR_MESSAGE);
75 } catch (InterruptedException ie) {
76 JOptionPane.showMessageDialog(this,
77 device.toShortString(),
78 "Interrupted exception",
79 JOptionPane.ERROR_MESSAGE);
84 GridBagLayout gridbag;
86 JButton addButton(int x, int y, String label) {
90 c = new GridBagConstraints();
91 c.gridx = x; c.gridy = y;
92 c.fill = GridBagConstraints.BOTH;
95 b = new JButton(label);
97 Dimension ps = b.getPreferredSize();
99 gridbag.setConstraints(b, c);
108 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
110 setIconImage(new ImageIcon(imgURL).getImage());
112 AltosPreferences.set_component(this);
114 pane = getContentPane();
115 gridbag = new GridBagLayout();
116 pane.setLayout(gridbag);
120 b = addButton(0, 0, "Monitor Flight");
121 b.addActionListener(new ActionListener() {
122 public void actionPerformed(ActionEvent e) {
126 b = addButton(1, 0, "Save Flight Data");
127 b.addActionListener(new ActionListener() {
128 public void actionPerformed(ActionEvent e) {
132 b = addButton(2, 0, "Replay Flight");
133 b.addActionListener(new ActionListener() {
134 public void actionPerformed(ActionEvent e) {
138 b = addButton(3, 0, "Graph Data");
139 b.addActionListener(new ActionListener() {
140 public void actionPerformed(ActionEvent e) {
144 b = addButton(4, 0, "Export Data");
145 b.addActionListener(new ActionListener() {
146 public void actionPerformed(ActionEvent e) {
150 b = addButton(0, 1, "Configure Altimeter");
151 b.addActionListener(new ActionListener() {
152 public void actionPerformed(ActionEvent e) {
153 ConfigureTeleMetrum();
157 b = addButton(1, 1, "Configure AltosUI");
158 b.addActionListener(new ActionListener() {
159 public void actionPerformed(ActionEvent e) {
164 b = addButton(2, 1, "Flash Image");
165 b.addActionListener(new ActionListener() {
166 public void actionPerformed(ActionEvent e) {
171 b = addButton(3, 1, "Fire Igniter");
172 b.addActionListener(new ActionListener() {
173 public void actionPerformed(ActionEvent e) {
178 b = addButton(4, 1, "Quit");
179 b.addActionListener(new ActionListener() {
180 public void actionPerformed(ActionEvent e) {
186 b = addButton(0, 2, "Scan Channels");
187 b.addActionListener(new ActionListener() {
188 public void actionPerformed(ActionEvent e) {
193 b = addButton(1, 2, "Load Maps");
194 b.addActionListener(new ActionListener() {
195 public void actionPerformed(ActionEvent e) {
200 b = addButton(2, 2, "Monitor Idle");
201 b.addActionListener(new ActionListener() {
202 public void actionPerformed(ActionEvent e) {
217 Insets i = getInsets();
218 Dimension ps = rootPane.getPreferredSize();
219 ps.width += i.left + i.right;
220 ps.height += i.top + i.bottom;
221 setPreferredSize(ps);
223 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
224 addWindowListener(new WindowAdapter() {
226 public void windowClosing(WindowEvent e) {
232 private void ConnectToDevice() {
233 AltosDevice device = AltosDeviceDialog.show(AltosUI.this,
234 Altos.product_basestation);
237 telemetry_window(device);
240 void ConfigureCallsign() {
242 result = JOptionPane.showInputDialog(AltosUI.this,
243 "Configure Callsign",
244 AltosPreferences.callsign());
246 AltosPreferences.set_callsign(result);
249 void ConfigureTeleMetrum() {
250 new AltosConfig(AltosUI.this);
254 AltosFlashUI.show(AltosUI.this);
258 new AltosIgniteUI(AltosUI.this);
261 void ScanChannels() {
262 new AltosScanUI(AltosUI.this);
266 new AltosSiteMapPreload(AltosUI.this);
270 * Replay a flight from telemetry data
272 private void Replay() {
273 AltosDataChooser chooser = new AltosDataChooser(
276 AltosRecordIterable iterable = chooser.runDialog();
277 if (iterable != null) {
278 AltosFlightReader reader = new AltosReplayReader(iterable.iterator(),
280 new AltosFlightUI(voice, reader);
284 /* Connect to TeleMetrum, either directly or through
285 * a TeleDongle over the packet link
287 private void SaveFlightData() {
288 new AltosEepromManage(AltosUI.this);
291 /* Load a flight log file and write out a CSV file containing
292 * all of the data in standard units
295 private void ExportData() {
296 AltosDataChooser chooser;
297 chooser = new AltosDataChooser(this);
298 AltosRecordIterable record_reader = chooser.runDialog();
299 if (record_reader == null)
301 new AltosCSVUI(AltosUI.this, record_reader, chooser.file());
304 /* Load a flight log CSV file and display a pretty graph.
307 private void GraphData() {
308 AltosDataChooser chooser;
309 chooser = new AltosDataChooser(this);
310 AltosRecordIterable record_reader = chooser.runDialog();
311 if (record_reader == null)
314 new AltosGraphUI(record_reader, chooser.filename());
315 } catch (InterruptedException ie) {
316 } catch (IOException ie) {
320 private void ConfigureAltosUI() {
321 new AltosConfigureUI(AltosUI.this, voice);
324 private void IdleMonitor() {
326 new AltosIdleMonitorUI(this);
327 } catch (Exception e) {
331 static AltosRecordIterable open_logfile(String filename) {
332 File file = new File (filename);
336 in = new FileInputStream(file);
337 if (filename.endsWith("eeprom"))
338 return new AltosEepromIterable(in);
340 return new AltosTelemetryIterable(in);
341 } catch (FileNotFoundException fe) {
342 System.out.printf("Cannot open '%s'\n", filename);
347 static AltosWriter open_csv(String filename) {
348 File file = new File (filename);
350 return new AltosCSV(file);
351 } catch (FileNotFoundException fe) {
352 System.out.printf("Cannot open '%s'\n", filename);
357 static AltosWriter open_kml(String filename) {
358 File file = new File (filename);
360 return new AltosKML(file);
361 } catch (FileNotFoundException fe) {
362 System.out.printf("Cannot open '%s'\n", filename);
367 static final int process_none = 0;
368 static final int process_csv = 1;
369 static final int process_kml = 2;
370 static final int process_graph = 3;
371 static final int process_replay = 4;
372 static final int process_summary = 5;
374 static void process_csv(String input) {
375 AltosRecordIterable iterable = open_logfile(input);
376 if (iterable == null)
379 String output = Altos.replace_extension(input,".csv");
380 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
381 if (input.equals(output)) {
382 System.out.printf("Not processing '%s'\n", input);
384 AltosWriter writer = open_csv(output);
387 writer.write(iterable);
392 static void process_kml(String input) {
393 AltosRecordIterable iterable = open_logfile(input);
394 if (iterable == null)
397 String output = Altos.replace_extension(input,".kml");
398 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
399 if (input.equals(output)) {
400 System.out.printf("Not processing '%s'\n", input);
402 AltosWriter writer = open_kml(output);
405 writer.write(iterable);
410 static AltosRecordIterable record_iterable_file(String filename) {
413 in = new FileInputStream(filename);
414 } catch (Exception e) {
415 System.out.printf("Failed to open file '%s'\n", filename);
418 AltosRecordIterable recs;
419 AltosReplayReader reader;
420 if (filename.endsWith("eeprom")) {
421 recs = new AltosEepromIterable(in);
423 recs = new AltosTelemetryIterable(in);
428 static AltosReplayReader replay_file(String filename) {
429 AltosRecordIterable recs = record_iterable_file(filename);
432 return new AltosReplayReader(recs.iterator(), new File(filename));
435 static void process_replay(String filename) {
436 AltosReplayReader reader = replay_file(filename);
437 AltosFlightUI flight_ui = new AltosFlightUI(new AltosVoice(), reader);
438 flight_ui.set_exit_on_close();
441 static void process_graph(String filename) {
442 AltosRecordIterable recs = record_iterable_file(filename);
446 new AltosGraphUI(recs, filename);
447 } catch (InterruptedException ie) {
448 } catch (IOException ie) {
452 static void process_summary(String filename) {
453 AltosReplayReader reader = replay_file(filename);
455 AltosFlightStats stats = new AltosFlightStats(reader);
456 if (stats.serial > 0)
457 System.out.printf("Serial: %5d\n", stats.serial);
458 if (stats.flight > 0)
459 System.out.printf("Flight: %5d\n", stats.flight);
461 System.out.printf("Date: %04d-%02d-%02d\n",
462 stats.year, stats.month, stats.day);
464 System.out.printf("Time: %02d:%02d:%02d UTC\n",
465 stats.hour, stats.minute, stats.second);
466 System.out.printf("Max height: %6.0f m %6.0f ft\n",
468 AltosConvert.meters_to_feet(stats.max_height));
469 System.out.printf("Max speed: %6.0f m/s %6.0f ft/s %6.4f Mach\n",
471 AltosConvert.meters_to_feet(stats.max_speed),
472 AltosConvert.meters_to_mach(stats.max_speed));
473 if (stats.max_acceleration != AltosRecord.MISSING) {
474 System.out.printf("Max accel: %6.0f m/s² %6.0f ft/s² %6.2f g\n",
475 stats.max_acceleration,
476 AltosConvert.meters_to_feet(stats.max_acceleration),
477 AltosConvert.meters_to_g(stats.max_acceleration));
479 System.out.printf("Drogue rate: %6.0f m/s %6.0f ft/s\n",
480 stats.state_baro_speed[Altos.ao_flight_drogue],
481 AltosConvert.meters_to_feet(stats.state_baro_speed[Altos.ao_flight_drogue]));
482 System.out.printf("Main rate: %6.0f m/s %6.0f ft/s\n",
483 stats.state_baro_speed[Altos.ao_flight_main],
484 AltosConvert.meters_to_feet(stats.state_baro_speed[Altos.ao_flight_main]));
485 System.out.printf("Flight time: %6.0f s\n",
486 stats.state_end[Altos.ao_flight_main] -
487 stats.state_start[Altos.ao_flight_boost]);
488 } catch (InterruptedException ie) {
489 } catch (IOException ie) {
493 public static void help(int code) {
494 System.out.printf("Usage: altosui [OPTION]... [FILE]...\n");
495 System.out.printf(" Options:\n");
496 System.out.printf(" --fetchmaps <lat> <lon>\tpre-fetch maps for site map view\n");
497 System.out.printf(" --replay <filename>\t\trelive the glory of past flights \n");
498 System.out.printf(" --graph <filename>\t\tgraph a flight\n");
499 System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n");
500 System.out.printf(" --kml\tgenerate KML output for use with Google Earth\n");
504 public static void main(final String[] args) {
505 /* Handle batch-mode */
506 if (args.length == 0) {
507 AltosUI altosui = new AltosUI();
508 altosui.setVisible(true);
510 java.util.List<AltosDevice> devices = AltosUSBDevice.list(Altos.product_basestation);
511 for (AltosDevice device : devices)
512 altosui.telemetry_window(device);
514 int process = process_none;
515 for (int i = 0; i < args.length; i++) {
516 if (args[i].equals("--help"))
518 else if (args[i].equals("--fetchmaps")) {
519 if (args.length < i + 3) {
522 double lat = Double.parseDouble(args[i+1]);
523 double lon = Double.parseDouble(args[i+2]);
524 AltosSiteMap.prefetchMaps(lat, lon, 5, 5);
527 } else if (args[i].equals("--replay"))
528 process = process_replay;
529 else if (args[i].equals("--kml"))
530 process = process_kml;
531 else if (args[i].equals("--csv"))
532 process = process_csv;
533 else if (args[i].equals("--graph"))
534 process = process_graph;
535 else if (args[i].equals("--summary"))
536 process = process_summary;
537 else if (args[i].startsWith("--"))
543 process_graph(args[i]);
546 process_replay(args[i]);
549 process_kml(args[i]);
552 process_csv(args[i]);
554 case process_summary:
555 process_summary(args[i]);