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 Altimter");
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)
313 new AltosGraphUI(record_reader);
316 private void ConfigureAltosUI() {
317 new AltosConfigureUI(AltosUI.this, voice);
320 private void IdleMonitor() {
322 new AltosIdleMonitorUI(this);
323 } catch (Exception e) {
327 static AltosRecordIterable open_logfile(String filename) {
328 File file = new File (filename);
332 in = new FileInputStream(file);
333 if (filename.endsWith("eeprom"))
334 return new AltosEepromIterable(in);
336 return new AltosTelemetryIterable(in);
337 } catch (FileNotFoundException fe) {
338 System.out.printf("Cannot open '%s'\n", filename);
343 static AltosWriter open_csv(String filename) {
344 File file = new File (filename);
346 return new AltosCSV(file);
347 } catch (FileNotFoundException fe) {
348 System.out.printf("Cannot open '%s'\n", filename);
353 static AltosWriter open_kml(String filename) {
354 File file = new File (filename);
356 return new AltosKML(file);
357 } catch (FileNotFoundException fe) {
358 System.out.printf("Cannot open '%s'\n", filename);
363 static final int process_csv = 1;
364 static final int process_kml = 2;
366 static void process_file(String input, int process) {
367 AltosRecordIterable iterable = open_logfile(input);
368 if (iterable == null)
371 process = process_csv;
372 if ((process & process_csv) != 0) {
373 String output = Altos.replace_extension(input,".csv");
374 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
375 if (input.equals(output)) {
376 System.out.printf("Not processing '%s'\n", input);
378 AltosWriter writer = open_csv("/dev/stdout");
379 if (writer != null) {
380 writer.write(iterable);
385 if ((process & process_kml) != 0) {
386 String output = Altos.replace_extension(input,".kml");
387 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
388 if (input.equals(output)) {
389 System.out.printf("Not processing '%s'\n", input);
391 AltosWriter writer = open_kml(output);
394 writer.write(iterable);
400 public static void main(final String[] args) {
402 /* Handle batch-mode */
403 if (args.length == 1 && args[0].equals("--help")) {
404 System.out.printf("Usage: altosui [OPTION]... [FILE]...\n");
405 System.out.printf(" Options:\n");
406 System.out.printf(" --fetchmaps <lat> <lon>\tpre-fetch maps for site map view\n");
407 System.out.printf(" --replay <filename>\t\trelive the glory of past flights \n");
408 System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n");
409 System.out.printf(" --kml\tgenerate KML output for use with Google Earth\n");
410 } else if (args.length == 3 && args[0].equals("--fetchmaps")) {
411 double lat = Double.parseDouble(args[1]);
412 double lon = Double.parseDouble(args[2]);
413 AltosSiteMap.prefetchMaps(lat, lon, 5, 5);
414 } else if (args.length == 2 && args[0].equals("--replay")) {
415 String filename = args[1];
418 in = new FileInputStream(filename);
419 } catch (Exception e) {
420 System.out.printf("Failed to open file '%s'\n", filename);
423 AltosRecordIterable recs;
424 AltosReplayReader reader;
425 if (filename.endsWith("eeprom")) {
426 recs = new AltosEepromIterable(in);
428 recs = new AltosTelemetryIterable(in);
430 reader = new AltosReplayReader(recs.iterator(), filename);
431 AltosFlightUI flight_ui = new AltosFlightUI(new AltosVoice(), reader);
432 flight_ui.set_exit_on_close();
434 } else if (args.length > 0) {
435 for (int i = 0; i < args.length; i++) {
436 if (args[i].equals("--kml"))
437 process |= process_kml;
438 else if (args[i].equals("--csv"))
439 process |= process_csv;
441 process_file(args[i], process);
444 AltosUI altosui = new AltosUI();
445 altosui.setVisible(true);
447 java.util.List<AltosDevice> devices = AltosUSBDevice.list(Altos.product_basestation);
448 for (AltosDevice device : devices)
449 altosui.telemetry_window(device);