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;
32 import altosui.AltosSerial;
33 import altosui.AltosSerialMonitor;
34 import altosui.AltosRecord;
35 import altosui.AltosTelemetry;
36 import altosui.AltosState;
37 import altosui.AltosDeviceDialog;
38 import altosui.AltosPreferences;
39 import altosui.AltosLog;
40 import altosui.AltosVoice;
41 import altosui.AltosFlightInfoTableModel;
42 import altosui.AltosChannelMenu;
43 import altosui.AltosFlashUI;
44 import altosui.AltosLogfileChooser;
45 import altosui.AltosCSVUI;
46 import altosui.AltosLine;
47 import altosui.AltosStatusTable;
48 import altosui.AltosInfoTable;
52 public class AltosUI extends JFrame {
53 private int channel = -1;
55 private AltosStatusTable flightStatus;
56 private AltosInfoTable flightInfo;
57 private AltosSerial serial_line;
58 private AltosLog altos_log;
61 private Font statusFont = new Font("SansSerif", Font.BOLD, 24);
62 private Font infoLabelFont = new Font("SansSerif", Font.PLAIN, 14);
63 private Font infoValueFont = new Font("Monospaced", Font.PLAIN, 14);
65 public AltosVoice voice = new AltosVoice();
67 public static boolean load_library(Frame frame) {
68 if (!AltosDevice.load_library()) {
69 JOptionPane.showMessageDialog(frame,
70 String.format("No AltOS library in \"%s\"",
71 System.getProperty("java.library.path","<undefined>")),
72 "Cannot load device access library",
73 JOptionPane.ERROR_MESSAGE);
83 String[] statusNames = { "Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
84 Object[][] statusData = { { "0", "pad", "-50", "0" } };
86 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
88 setIconImage(new ImageIcon(imgURL).getImage());
90 AltosPreferences.init(this);
92 vbox = Box.createVerticalBox();
95 flightStatus = new AltosStatusTable(this);
97 vbox.add(flightStatus);
99 flightInfo = new AltosInfoTable();
100 vbox.add(flightInfo.box());
106 serial_line = new AltosSerial();
107 altos_log = new AltosLog(serial_line);
108 int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
109 this.setSize(new Dimension (flightInfo.width(),
110 flightStatus.height() + flightInfo.height()));
112 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
113 addWindowListener(new WindowAdapter() {
115 public void windowClosing(WindowEvent e) {
119 voice.speak("Rocket flight monitor ready.");
122 void show(AltosState state, int crc_errors) {
124 flightStatus.set(state);
125 flightInfo.show(state, crc_errors);
129 class IdleThread extends Thread {
132 private AltosState state;
133 int reported_landing;
137 public synchronized void report(boolean last) {
141 /* reset the landing count once we hear about a new flight */
142 if (state.state < Altos.ao_flight_drogue)
143 reported_landing = 0;
145 /* Shut up once the rocket is on the ground */
146 if (reported_landing > 2) {
150 /* If the rocket isn't on the pad, then report height */
151 if (Altos.ao_flight_drogue <= state.state &&
152 state.state < Altos.ao_flight_landed &&
155 voice.speak("Height %d, bearing %d, elevation %d, range %d.\n",
156 (int) (state.height + 0.5),
157 (int) (state.from_pad.bearing + 0.5),
158 (int) (state.elevation + 0.5),
159 (int) (state.range + 0.5));
160 } else if (state.state > Altos.ao_flight_pad) {
161 voice.speak("%d meters", (int) (state.height + 0.5));
163 reported_landing = 0;
166 /* If the rocket is coming down, check to see if it has landed;
167 * either we've got a landed report or we haven't heard from it in
170 if (state.state >= Altos.ao_flight_drogue &&
172 System.currentTimeMillis() - state.report_time >= 15000 ||
173 state.state == Altos.ao_flight_landed))
175 if (Math.abs(state.baro_speed) < 20 && state.height < 100)
176 voice.speak("rocket landed safely");
178 voice.speak("rocket may have crashed");
179 if (state.from_pad != null)
180 voice.speak("Bearing %d degrees, range %d meters.",
181 (int) (state.from_pad.bearing + 0.5),
182 (int) (state.from_pad.distance + 0.5));
188 return System.currentTimeMillis();
191 void set_report_time() {
192 report_time = now() + report_interval;
197 reported_landing = 0;
199 report_interval = 10000;
205 synchronized (this) {
206 long sleep_time = report_time - now();
214 } catch (InterruptedException ie) {
217 } catch (InterruptedException iie) { }
221 public synchronized void notice(AltosState new_state, boolean spoken) {
222 AltosState old_state = state;
224 if (!started && state.state > Altos.ao_flight_pad) {
229 if (state.state < Altos.ao_flight_drogue)
230 report_interval = 10000;
232 report_interval = 20000;
233 if (old_state != null && old_state.state != state.state) {
241 private boolean tell(AltosState state, AltosState old_state) {
243 if (old_state == null || old_state.state != state.state) {
244 voice.speak(state.data.state());
245 if ((old_state == null || old_state.state <= Altos.ao_flight_boost) &&
246 state.state > Altos.ao_flight_boost) {
247 voice.speak("max speed: %d meters per second.",
248 (int) (state.max_speed + 0.5));
250 } else if ((old_state == null || old_state.state < Altos.ao_flight_drogue) &&
251 state.state >= Altos.ao_flight_drogue) {
252 voice.speak("max height: %d meters.",
253 (int) (state.max_height + 0.5));
257 if (old_state == null || old_state.gps_ready != state.gps_ready) {
258 if (state.gps_ready) {
259 voice.speak("GPS ready");
262 else if (old_state != null) {
263 voice.speak("GPS lost");
271 class DisplayThread extends Thread {
272 IdleThread idle_thread;
280 AltosRecord read() throws InterruptedException, ParseException, AltosCRCException, IOException { return null; }
282 void close(boolean interrupted) { }
284 void update(AltosState state) throws InterruptedException { }
287 boolean interrupted = false;
289 AltosState state = null;
290 AltosState old_state = null;
293 idle_thread = new IdleThread();
299 AltosRecord record = read();
303 state = new AltosState(record, state);
305 show(state, crc_errors);
306 told = tell(state, old_state);
307 idle_thread.notice(state, told);
308 } catch (ParseException pp) {
309 System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
310 } catch (AltosCRCException ce) {
312 show(state, crc_errors);
315 } catch (InterruptedException ee) {
317 } catch (IOException ie) {
318 JOptionPane.showMessageDialog(AltosUI.this,
319 String.format("Error reading from \"%s\"", name),
320 "Telemetry Read Error",
321 JOptionPane.ERROR_MESSAGE);
324 idle_thread.interrupt();
327 } catch (InterruptedException ie) {}
331 public void report() {
332 if (idle_thread != null)
333 idle_thread.report(true);
337 class DeviceThread extends DisplayThread {
339 LinkedBlockingQueue<AltosLine> telem;
341 AltosRecord read() throws InterruptedException, ParseException, AltosCRCException, IOException {
342 AltosLine l = telem.take();
344 throw new IOException("IO error");
345 return new AltosTelemetry(l.line);
348 void close(boolean interrupted) {
350 serial.remove_monitor(telem);
353 public DeviceThread(AltosSerial s, String in_name) {
355 telem = new LinkedBlockingQueue<AltosLine>();
356 serial.add_monitor(telem);
361 private void ConnectToDevice() {
362 AltosDevice device = AltosDeviceDialog.show(AltosUI.this,
363 AltosDevice.product_basestation);
365 if (device != null) {
368 serial_line.open(device);
369 DeviceThread thread = new DeviceThread(serial_line, device.getPath());
370 serial_line.set_channel(AltosPreferences.channel());
371 serial_line.set_callsign(AltosPreferences.callsign());
373 } catch (FileNotFoundException ee) {
374 JOptionPane.showMessageDialog(AltosUI.this,
375 String.format("Cannot open device \"%s\"",
377 "Cannot open target device",
378 JOptionPane.ERROR_MESSAGE);
379 } catch (IOException ee) {
380 JOptionPane.showMessageDialog(AltosUI.this,
383 JOptionPane.ERROR_MESSAGE);
388 void DisconnectFromDevice () {
392 void ConfigureCallsign() {
394 result = JOptionPane.showInputDialog(AltosUI.this,
395 "Configure Callsign",
396 AltosPreferences.callsign());
397 if (result != null) {
398 AltosPreferences.set_callsign(result);
399 if (serial_line != null)
400 serial_line.set_callsign(result);
404 void ConfigureTeleMetrum() {
405 new AltosConfig(AltosUI.this);
409 new AltosFlashUI(AltosUI.this);
413 * Open an existing telemetry file and replay it in realtime
416 class ReplayThread extends DisplayThread {
420 public AltosRecord read() {
422 return reader.read();
423 } catch (IOException ie) {
424 JOptionPane.showMessageDialog(AltosUI.this,
427 JOptionPane.ERROR_MESSAGE);
428 } catch (ParseException pe) {
433 public void close (boolean interrupted) {
438 public ReplayThread(AltosReader in_reader, String in_name) {
441 void update(AltosState state) throws InterruptedException {
442 /* Make it run in realtime after the rocket leaves the pad */
443 if (state.state > Altos.ao_flight_pad)
444 Thread.sleep((int) (Math.min(state.time_change,10) * 1000));
448 Thread display_thread;
450 private void stop_display() {
451 if (display_thread != null && display_thread.isAlive()) {
452 display_thread.interrupt();
454 display_thread.join();
455 } catch (InterruptedException ie) {}
457 display_thread = null;
460 private void run_display(Thread thread) {
462 display_thread = thread;
463 display_thread.start();
467 * Replay a flight from telemetry data
469 private void Replay() {
470 AltosLogfileChooser chooser = new AltosLogfileChooser(
472 AltosReader reader = chooser.runDialog();
474 run_display(new ReplayThread(reader,
475 chooser.filename()));
478 /* Connect to TeleMetrum, either directly or through
479 * a TeleDongle over the packet link
481 private void SaveFlightData() {
482 new AltosEepromDownload(AltosUI.this);
485 /* Load a flight log file and write out a CSV file containing
486 * all of the data in standard units
489 private void ExportData() {
490 new AltosCSVUI(AltosUI.this);
493 /* Create the AltosUI menus
495 private void createMenu() {
496 JMenuBar menubar = new JMenuBar();
499 JRadioButtonMenuItem radioitem;
503 menu = new JMenu("File");
504 menu.setMnemonic(KeyEvent.VK_F);
507 item = new JMenuItem("Replay File",KeyEvent.VK_R);
508 item.addActionListener(new ActionListener() {
509 public void actionPerformed(ActionEvent e) {
515 item = new JMenuItem("Save Flight Data",KeyEvent.VK_S);
516 item.addActionListener(new ActionListener() {
517 public void actionPerformed(ActionEvent e) {
523 item = new JMenuItem("Flash Image",KeyEvent.VK_F);
524 item.addActionListener(new ActionListener() {
525 public void actionPerformed(ActionEvent e) {
531 item = new JMenuItem("Export Data",KeyEvent.VK_F);
532 item.addActionListener(new ActionListener() {
533 public void actionPerformed(ActionEvent e) {
539 item = new JMenuItem("Quit",KeyEvent.VK_Q);
540 item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
541 ActionEvent.CTRL_MASK));
542 item.addActionListener(new ActionListener() {
543 public void actionPerformed(ActionEvent e) {
552 menu = new JMenu("Device");
553 menu.setMnemonic(KeyEvent.VK_D);
556 item = new JMenuItem("Connect to Device",KeyEvent.VK_C);
557 item.addActionListener(new ActionListener() {
558 public void actionPerformed(ActionEvent e) {
564 item = new JMenuItem("Disconnect from Device",KeyEvent.VK_D);
565 item.addActionListener(new ActionListener() {
566 public void actionPerformed(ActionEvent e) {
567 DisconnectFromDevice();
574 item = new JMenuItem("Set Callsign",KeyEvent.VK_S);
575 item.addActionListener(new ActionListener() {
576 public void actionPerformed(ActionEvent e) {
583 item = new JMenuItem("Configure TeleMetrum device",KeyEvent.VK_T);
584 item.addActionListener(new ActionListener() {
585 public void actionPerformed(ActionEvent e) {
586 ConfigureTeleMetrum();
594 menu = new JMenu("Log");
595 menu.setMnemonic(KeyEvent.VK_L);
598 item = new JMenuItem("New Log",KeyEvent.VK_N);
599 item.addActionListener(new ActionListener() {
600 public void actionPerformed(ActionEvent e) {
605 item = new JMenuItem("Configure Log",KeyEvent.VK_C);
606 item.addActionListener(new ActionListener() {
607 public void actionPerformed(ActionEvent e) {
608 AltosPreferences.ConfigureLog();
615 menu = new JMenu("Voice", true);
616 menu.setMnemonic(KeyEvent.VK_V);
619 radioitem = new JRadioButtonMenuItem("Enable Voice", AltosPreferences.voice());
620 radioitem.addActionListener(new ActionListener() {
621 public void actionPerformed(ActionEvent e) {
622 JRadioButtonMenuItem item = (JRadioButtonMenuItem) e.getSource();
623 boolean enabled = item.isSelected();
624 AltosPreferences.set_voice(enabled);
626 voice.speak_always("Enable voice.");
628 voice.speak_always("Disable voice.");
632 item = new JMenuItem("Test Voice",KeyEvent.VK_T);
633 item.addActionListener(new ActionListener() {
634 public void actionPerformed(ActionEvent e) {
635 voice.speak("That's one small step for man; one giant leap for mankind.");
643 menu = new AltosChannelMenu(AltosPreferences.channel());
644 menu.addActionListener(new ActionListener() {
645 public void actionPerformed(ActionEvent e) {
646 int new_channel = Integer.parseInt(e.getActionCommand());
647 AltosPreferences.set_channel(new_channel);
648 serial_line.set_channel(new_channel);
651 menu.setMnemonic(KeyEvent.VK_C);
655 this.setJMenuBar(menubar);
659 static String replace_extension(String input, String extension) {
660 int dot = input.lastIndexOf(".");
662 input = input.substring(0,dot);
663 return input.concat(extension);
666 static AltosReader open_logfile(String filename) {
667 File file = new File (filename);
671 in = new FileInputStream(file);
672 if (filename.endsWith("eeprom"))
673 return new AltosEepromReader(in);
675 return new AltosTelemetryReader(in);
676 } catch (FileNotFoundException fe) {
677 System.out.printf("Cannot open '%s'\n", filename);
682 static AltosCSV open_csv(String filename) {
683 File file = new File (filename);
685 return new AltosCSV(file);
686 } catch (FileNotFoundException fe) {
687 System.out.printf("Cannot open '%s'\n", filename);
692 static void process_file(String input) {
693 String output = replace_extension(input,".csv");
694 if (input.equals(output)) {
695 System.out.printf("Not processing '%s'\n", input);
698 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
699 AltosReader reader = open_logfile(input);
702 AltosCSV writer = open_csv(output);
705 writer.write(reader);
710 public static void main(final String[] args) {
712 /* Handle batch-mode */
713 if (args.length > 0) {
714 for (int i = 0; i < args.length; i++)
715 process_file(args[i]);
717 AltosUI altosui = new AltosUI();
718 altosui.setVisible(true);