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.LinkedBlockingQueue;
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);
74 GridBagLayout gridbag;
76 JButton addButton(int x, int y, String label) {
80 c = new GridBagConstraints();
81 c.gridx = x; c.gridy = y;
82 c.fill = GridBagConstraints.BOTH;
85 b = new JButton(label);
87 Dimension ps = b.getPreferredSize();
89 gridbag.setConstraints(b, c);
98 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
100 setIconImage(new ImageIcon(imgURL).getImage());
102 AltosPreferences.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 = addButton(1, 0, "Save Flight Data");
117 b.addActionListener(new ActionListener() {
118 public void actionPerformed(ActionEvent e) {
122 b = addButton(2, 0, "Replay Flight");
123 b.addActionListener(new ActionListener() {
124 public void actionPerformed(ActionEvent e) {
128 b = addButton(3, 0, "Graph Data");
129 b.addActionListener(new ActionListener() {
130 public void actionPerformed(ActionEvent e) {
134 b = addButton(4, 0, "Export Data");
135 b.addActionListener(new ActionListener() {
136 public void actionPerformed(ActionEvent e) {
140 b = addButton(0, 1, "Configure TeleMetrum");
141 b.addActionListener(new ActionListener() {
142 public void actionPerformed(ActionEvent e) {
143 ConfigureTeleMetrum();
147 b = addButton(1, 1, "Configure AltosUI");
148 b.addActionListener(new ActionListener() {
149 public void actionPerformed(ActionEvent e) {
154 b = addButton(2, 1, "Flash Image");
155 b.addActionListener(new ActionListener() {
156 public void actionPerformed(ActionEvent e) {
161 b = addButton(3, 1, "Fire Igniter");
162 b.addActionListener(new ActionListener() {
163 public void actionPerformed(ActionEvent e) {
168 b = addButton(4, 1, "Quit");
169 b.addActionListener(new ActionListener() {
170 public void actionPerformed(ActionEvent e) {
176 b = addButton(0, 2, "Scan Channels");
177 b.addActionListener(new ActionListener() {
178 public void actionPerformed(ActionEvent e) {
183 b = addButton(1, 2, "Load Maps");
184 b.addActionListener(new ActionListener() {
185 public void actionPerformed(ActionEvent e) {
190 b = addButton(2, 2, "Monitor Idle");
191 b.addActionListener(new ActionListener() {
192 public void actionPerformed(ActionEvent e) {
207 Insets i = getInsets();
208 Dimension ps = rootPane.getPreferredSize();
209 ps.width += i.left + i.right;
210 ps.height += i.top + i.bottom;
211 setPreferredSize(ps);
213 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
214 addWindowListener(new WindowAdapter() {
216 public void windowClosing(WindowEvent e) {
222 private void ConnectToDevice() {
223 AltosDevice device = AltosDeviceDialog.show(AltosUI.this,
224 Altos.product_basestation);
227 telemetry_window(device);
230 void ConfigureCallsign() {
232 result = JOptionPane.showInputDialog(AltosUI.this,
233 "Configure Callsign",
234 AltosPreferences.callsign());
236 AltosPreferences.set_callsign(result);
239 void ConfigureTeleMetrum() {
240 new AltosConfig(AltosUI.this);
244 AltosFlashUI.show(AltosUI.this);
248 new AltosIgniteUI(AltosUI.this);
251 void ScanChannels() {
252 new AltosScanUI(AltosUI.this);
256 new AltosSiteMapPreload(AltosUI.this);
260 * Replay a flight from telemetry data
262 private void Replay() {
263 AltosDataChooser chooser = new AltosDataChooser(
266 AltosRecordIterable iterable = chooser.runDialog();
267 if (iterable != null) {
268 AltosFlightReader reader = new AltosReplayReader(iterable.iterator(),
270 new AltosFlightUI(voice, reader);
274 /* Connect to TeleMetrum, either directly or through
275 * a TeleDongle over the packet link
277 private void SaveFlightData() {
278 new AltosEepromManage(AltosUI.this);
281 /* Load a flight log file and write out a CSV file containing
282 * all of the data in standard units
285 private void ExportData() {
286 AltosDataChooser chooser;
287 chooser = new AltosDataChooser(this);
288 AltosRecordIterable record_reader = chooser.runDialog();
289 if (record_reader == null)
291 new AltosCSVUI(AltosUI.this, record_reader, chooser.file());
294 /* Load a flight log CSV file and display a pretty graph.
297 private void GraphData() {
298 AltosDataChooser chooser;
299 chooser = new AltosDataChooser(this);
300 AltosRecordIterable record_reader = chooser.runDialog();
301 if (record_reader == null)
303 new AltosGraphUI(record_reader);
306 private void ConfigureAltosUI() {
307 new AltosConfigureUI(AltosUI.this, voice);
310 private void IdleMonitor() {
312 new AltosIdleMonitorUI(this);
313 } catch (Exception e) {
317 static AltosRecordIterable open_logfile(String filename) {
318 File file = new File (filename);
322 in = new FileInputStream(file);
323 if (filename.endsWith("eeprom"))
324 return new AltosEepromIterable(in);
326 return new AltosTelemetryIterable(in);
327 } catch (FileNotFoundException fe) {
328 System.out.printf("Cannot open '%s'\n", filename);
333 static AltosWriter open_csv(String filename) {
334 File file = new File (filename);
336 return new AltosCSV(file);
337 } catch (FileNotFoundException fe) {
338 System.out.printf("Cannot open '%s'\n", filename);
343 static AltosWriter open_kml(String filename) {
344 File file = new File (filename);
346 return new AltosKML(file);
347 } catch (FileNotFoundException fe) {
348 System.out.printf("Cannot open '%s'\n", filename);
353 static final int process_csv = 1;
354 static final int process_kml = 2;
356 static void process_file(String input, int process) {
357 AltosRecordIterable iterable = open_logfile(input);
358 if (iterable == null)
361 process = process_csv;
362 if ((process & process_csv) != 0) {
363 String output = Altos.replace_extension(input,".csv");
364 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
365 if (input.equals(output)) {
366 System.out.printf("Not processing '%s'\n", input);
368 AltosWriter writer = open_csv("/dev/stdout");
369 if (writer != null) {
370 writer.write(iterable);
375 if ((process & process_kml) != 0) {
376 String output = Altos.replace_extension(input,".kml");
377 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
378 if (input.equals(output)) {
379 System.out.printf("Not processing '%s'\n", input);
381 AltosWriter writer = open_kml(output);
384 writer.write(iterable);
390 public static void main(final String[] args) {
392 /* Handle batch-mode */
393 if (args.length == 1 && args[0].equals("--help")) {
394 System.out.printf("Usage: altosui [OPTION]... [FILE]...\n");
395 System.out.printf(" Options:\n");
396 System.out.printf(" --fetchmaps <lat> <lon>\tpre-fetch maps for site map view\n");
397 System.out.printf(" --replay <filename>\t\trelive the glory of past flights \n");
398 System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n");
399 System.out.printf(" --kml\tgenerate KML output for use with Google Earth\n");
400 } else if (args.length == 3 && args[0].equals("--fetchmaps")) {
401 double lat = Double.parseDouble(args[1]);
402 double lon = Double.parseDouble(args[2]);
403 AltosSiteMap.prefetchMaps(lat, lon, 5, 5);
404 } else if (args.length == 2 && args[0].equals("--replay")) {
405 String filename = args[1];
408 in = new FileInputStream(filename);
409 } catch (Exception e) {
410 System.out.printf("Failed to open file '%s'\n", filename);
413 AltosRecordIterable recs;
414 AltosReplayReader reader;
415 if (filename.endsWith("eeprom")) {
416 recs = new AltosEepromIterable(in);
418 recs = new AltosTelemetryIterable(in);
420 reader = new AltosReplayReader(recs.iterator(), filename);
421 AltosFlightUI flight_ui = new AltosFlightUI(new AltosVoice(), reader);
422 flight_ui.set_exit_on_close();
424 } else if (args.length > 0) {
425 for (int i = 0; i < args.length; i++) {
426 if (args[i].equals("--kml"))
427 process |= process_kml;
428 else if (args[i].equals("--csv"))
429 process |= process_csv;
431 process_file(args[i], process);
434 AltosUI altosui = new AltosUI();
435 altosui.setVisible(true);
437 java.util.List<AltosDevice> devices = AltosUSBDevice.list(Altos.product_basestation);
438 for (AltosDevice device : devices)
439 altosui.telemetry_window(device);