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 (!AltosDevice.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\"",
57 "Cannot open target device",
58 JOptionPane.ERROR_MESSAGE);
59 } catch (IOException ee) {
60 JOptionPane.showMessageDialog(AltosUI.this,
63 JOptionPane.ERROR_MESSAGE);
68 GridBagLayout gridbag;
70 JButton addButton(int x, int y, String label) {
74 c = new GridBagConstraints();
75 c.gridx = x; c.gridy = y;
76 c.fill = GridBagConstraints.BOTH;
79 b = new JButton(label);
81 Dimension ps = b.getPreferredSize();
83 gridbag.setConstraints(b, c);
92 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
94 setIconImage(new ImageIcon(imgURL).getImage());
96 AltosPreferences.init(this);
98 pane = getContentPane();
99 gridbag = new GridBagLayout();
100 pane.setLayout(gridbag);
104 b = addButton(0, 0, "Monitor Flight");
105 b.addActionListener(new ActionListener() {
106 public void actionPerformed(ActionEvent e) {
110 b = addButton(1, 0, "Save Flight Data");
111 b.addActionListener(new ActionListener() {
112 public void actionPerformed(ActionEvent e) {
116 b = addButton(2, 0, "Replay Flight");
117 b.addActionListener(new ActionListener() {
118 public void actionPerformed(ActionEvent e) {
122 b = addButton(0, 1, "Graph Data");
123 b.addActionListener(new ActionListener() {
124 public void actionPerformed(ActionEvent e) {
128 b = addButton(1, 1, "Export Data");
129 b.addActionListener(new ActionListener() {
130 public void actionPerformed(ActionEvent e) {
134 b = addButton(2, 1, "Configure TeleMetrum");
135 b.addActionListener(new ActionListener() {
136 public void actionPerformed(ActionEvent e) {
137 ConfigureTeleMetrum();
153 Insets i = getInsets();
154 Dimension ps = rootPane.getPreferredSize();
155 ps.width += i.left + i.right;
156 ps.height += i.top + i.bottom;
157 setPreferredSize(ps);
159 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
160 addWindowListener(new WindowAdapter() {
162 public void windowClosing(WindowEvent e) {
168 private void ConnectToDevice() {
169 AltosDevice device = AltosDeviceDialog.show(AltosUI.this,
170 AltosDevice.product_basestation);
173 telemetry_window(device);
176 void ConfigureCallsign() {
178 result = JOptionPane.showInputDialog(AltosUI.this,
179 "Configure Callsign",
180 AltosPreferences.callsign());
182 AltosPreferences.set_callsign(result);
185 void ConfigureTeleMetrum() {
186 new AltosConfig(AltosUI.this);
190 new AltosFlashUI(AltosUI.this);
194 * Replay a flight from telemetry data
196 private void Replay() {
197 AltosLogfileChooser chooser = new AltosLogfileChooser(
199 AltosRecordIterable iterable = chooser.runDialog();
200 if (iterable != null) {
201 AltosFlightReader reader = new AltosReplayReader(iterable.iterator(),
203 new AltosFlightUI(voice, reader);
207 /* Connect to TeleMetrum, either directly or through
208 * a TeleDongle over the packet link
210 private void SaveFlightData() {
211 new AltosEepromDownload(AltosUI.this);
214 /* Load a flight log file and write out a CSV file containing
215 * all of the data in standard units
218 private void ExportData() {
219 new AltosCSVUI(AltosUI.this);
222 /* Load a flight log CSV file and display a pretty graph.
225 private void GraphData() {
226 new AltosGraphUI(AltosUI.this);
229 /* Create the AltosUI menus
231 private void createMenu() {
232 JMenuBar menubar = new JMenuBar();
235 JRadioButtonMenuItem radioitem;
239 menu = new JMenu("File");
240 menu.setMnemonic(KeyEvent.VK_F);
243 item = new JMenuItem("Flash Image",KeyEvent.VK_I);
244 item.addActionListener(new ActionListener() {
245 public void actionPerformed(ActionEvent e) {
251 item = new JMenuItem("Export Data",KeyEvent.VK_E);
252 item.addActionListener(new ActionListener() {
253 public void actionPerformed(ActionEvent e) {
259 item = new JMenuItem("Graph Data",KeyEvent.VK_G);
260 item.addActionListener(new ActionListener() {
261 public void actionPerformed(ActionEvent e) {
267 item = new JMenuItem("Quit",KeyEvent.VK_Q);
268 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
269 ActionEvent.CTRL_MASK));
270 item.addActionListener(new ActionListener() {
271 public void actionPerformed(ActionEvent e) {
272 System.out.printf("exiting\n");
281 menu = new JMenu("Device");
282 menu.setMnemonic(KeyEvent.VK_D);
285 item = new JMenuItem("Connect to Device",KeyEvent.VK_C);
286 item.addActionListener(new ActionListener() {
287 public void actionPerformed(ActionEvent e) {
295 item = new JMenuItem("Set Callsign",KeyEvent.VK_S);
296 item.addActionListener(new ActionListener() {
297 public void actionPerformed(ActionEvent e) {
304 item = new JMenuItem("Configure TeleMetrum device",KeyEvent.VK_T);
305 item.addActionListener(new ActionListener() {
306 public void actionPerformed(ActionEvent e) {
307 ConfigureTeleMetrum();
315 menu = new JMenu("Log");
316 menu.setMnemonic(KeyEvent.VK_L);
319 item = new JMenuItem("New Log",KeyEvent.VK_N);
320 item.addActionListener(new ActionListener() {
321 public void actionPerformed(ActionEvent e) {
326 item = new JMenuItem("Configure Log",KeyEvent.VK_C);
327 item.addActionListener(new ActionListener() {
328 public void actionPerformed(ActionEvent e) {
329 AltosPreferences.ConfigureLog();
336 menu = new JMenu("Voice", true);
337 menu.setMnemonic(KeyEvent.VK_V);
340 radioitem = new JRadioButtonMenuItem("Enable Voice", AltosPreferences.voice());
341 radioitem.addActionListener(new ActionListener() {
342 public void actionPerformed(ActionEvent e) {
343 JRadioButtonMenuItem item = (JRadioButtonMenuItem) e.getSource();
344 boolean enabled = item.isSelected();
345 AltosPreferences.set_voice(enabled);
347 voice.speak_always("Enable voice.");
349 voice.speak_always("Disable voice.");
353 item = new JMenuItem("Test Voice",KeyEvent.VK_T);
354 item.addActionListener(new ActionListener() {
355 public void actionPerformed(ActionEvent e) {
356 voice.speak("That's one small step for man; one giant leap for mankind.");
361 this.setJMenuBar(menubar);
364 static AltosRecordIterable open_logfile(String filename) {
365 File file = new File (filename);
369 in = new FileInputStream(file);
370 if (filename.endsWith("eeprom"))
371 return new AltosEepromIterable(in);
373 return new AltosTelemetryIterable(in);
374 } catch (FileNotFoundException fe) {
375 System.out.printf("Cannot open '%s'\n", filename);
380 static AltosWriter open_csv(String filename) {
381 File file = new File (filename);
383 return new AltosCSV(file);
384 } catch (FileNotFoundException fe) {
385 System.out.printf("Cannot open '%s'\n", filename);
390 static AltosWriter open_kml(String filename) {
391 File file = new File (filename);
393 return new AltosKML(file);
394 } catch (FileNotFoundException fe) {
395 System.out.printf("Cannot open '%s'\n", filename);
400 static final int process_csv = 1;
401 static final int process_kml = 2;
403 static void process_file(String input, int process) {
404 AltosRecordIterable iterable = open_logfile(input);
405 if (iterable == null)
408 process = process_csv;
409 if ((process & process_csv) != 0) {
410 String output = Altos.replace_extension(input,".csv");
411 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
412 if (input.equals(output)) {
413 System.out.printf("Not processing '%s'\n", input);
415 AltosWriter writer = open_csv(output);
416 if (writer != null) {
417 writer.write(iterable);
422 if ((process & process_kml) != 0) {
423 String output = Altos.replace_extension(input,".kml");
424 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
425 if (input.equals(output)) {
426 System.out.printf("Not processing '%s'\n", input);
428 AltosWriter writer = open_kml(output);
431 writer.write(iterable);
437 public static void main(final String[] args) {
439 /* Handle batch-mode */
440 if (args.length == 2 && args[0].equals("--replay")) {
441 String filename = args[1];
444 in = new FileInputStream(filename);
445 } catch (Exception e) {
446 System.out.printf("Failed to open file '%s'\n", filename);
449 AltosRecordIterable recs;
450 AltosReplayReader reader;
451 if (filename.endsWith("eeprom")) {
452 recs = new AltosEepromIterable(in);
454 recs = new AltosTelemetryIterable(in);
456 reader = new AltosReplayReader(recs.iterator(), filename);
457 new AltosFlightUI(new AltosVoice(), reader);
459 } else if (args.length > 0) {
460 for (int i = 0; i < args.length; i++) {
461 if (args[i].equals("--kml"))
462 process |= process_kml;
463 else if (args[i].equals("--csv"))
464 process |= process_csv;
466 process_file(args[i], process);
469 AltosUI altosui = new AltosUI();
470 altosui.setVisible(true);
472 AltosDevice[] devices = AltosDevice.list(AltosDevice.product_basestation);
473 for (int i = 0; i < devices.length; i++)
474 altosui.telemetry_window(devices[i]);