X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=ao-tools%2Faltosui%2FAltosUI.java;h=2952fcc08c0021f4bdc508ac3cd8c72c57de9da6;hp=ab5f18288ca3deafba91c9dc7d745c55b8e327bf;hb=97f4874d19ec05c81a04a3ecd06abffcf7fbfafc;hpb=0e7abc9fedec568b431c983d3df1b0b29f4f10e3 diff --git a/ao-tools/altosui/AltosUI.java b/ao-tools/altosui/AltosUI.java index ab5f1828..2952fcc0 100644 --- a/ao-tools/altosui/AltosUI.java +++ b/ao-tools/altosui/AltosUI.java @@ -26,18 +26,14 @@ import java.io.*; import java.util.*; import java.text.*; import java.util.prefs.*; +import java.util.concurrent.LinkedBlockingQueue; import gnu.io.*; import altosui.AltosSerial; import altosui.AltosSerialMonitor; import altosui.AltosTelemetry; import altosui.AltosState; - -class AltosUIMonitor implements AltosSerialMonitor { - public void data(String data) { - System.out.println(data); - } -} +import altosui.AltosDeviceDialog; class AltosFlightStatusTableModel extends AbstractTableModel { private String[] columnNames = {"Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" }; @@ -193,7 +189,6 @@ public class AltosUI extends JFrame { createMenu(); serialLine = new AltosSerial(); - serialLine.monitor(new AltosUIMonitor()); int dpi = Toolkit.getDefaultToolkit().getScreenResolution(); this.setSize(new Dimension (infoValueMetrics.charWidth('0') * 6 * 20, statusHeight * 4 + infoHeight * 17)); @@ -397,46 +392,95 @@ public class AltosUI extends JFrame { return true; } - private void PickSerialDevice() { - java.util.Enumeration port_list = CommPortIdentifier.getPortIdentifiers(); - while (port_list.hasMoreElements()) { - CommPortIdentifier identifier = port_list.nextElement(); - System.out.println("Serial port " + identifier.getName()); + class DisplayThread extends Thread { + String read() throws InterruptedException { return null; } + + void close() { } + + void update(AltosState state) throws InterruptedException { } + + public void run() { + String line; + AltosState state = null; + + info_reset(); + info_finish(); + try { + while ((line = read()) != null) { + try { + AltosTelemetry t = new AltosTelemetry(line); + state = new AltosState(t, state); + update(state); + show(state); + } catch (ParseException pp) { + System.out.printf("Parse error on %s\n", line); + System.out.println("exception " + pp); + } + } + } catch (InterruptedException ee) { + } finally { + close(); + } + } + } + + class DeviceThread extends DisplayThread { + AltosSerial serial; + LinkedBlockingQueue telem; + + String read() throws InterruptedException { + return telem.take(); + } + + void close() { + serial.close(); + serial.remove_monitor(telem); + System.out.println("DisplayThread done"); + } + + public DeviceThread(AltosSerial s) { + serial = s; + telem = new LinkedBlockingQueue(); + serial.add_monitor(telem); } } private void ConnectToDevice() { - JFileChooser device_chooser = new JFileChooser(); - int returnVal = device_chooser.showOpenDialog(AltosUI.this); + AltosDevice device = AltosDeviceDialog.show(AltosUI.this, "TeleDongle"); - if (returnVal == JFileChooser.APPROVE_OPTION) { - File file = device_chooser.getSelectedFile(); + if (device != null) { try { - serialLine.connect(file.getCanonicalPath()); + serialLine.connect(device.tty); + DeviceThread thread = new DeviceThread(serialLine); + run_display(thread); } catch (FileNotFoundException ee) { JOptionPane.showMessageDialog(AltosUI.this, - file.getName(), + device.tty, "Cannot open serial port", JOptionPane.ERROR_MESSAGE); } catch (NoSuchPortException ee) { JOptionPane.showMessageDialog(AltosUI.this, - file.getName(), + device.tty, "No such serial port", JOptionPane.ERROR_MESSAGE); } catch (PortInUseException ee) { JOptionPane.showMessageDialog(AltosUI.this, - file.getName(), + device.tty, "Port in use", JOptionPane.ERROR_MESSAGE); } catch (IOException ee) { JOptionPane.showMessageDialog(AltosUI.this, - file.getName(), + device.tty, "Unkonwn I/O error", JOptionPane.ERROR_MESSAGE); } } } + void DisconnectFromDevice () { + stop_display(); + } + String readline(FileInputStream s) throws IOException { int c; String line = ""; @@ -455,7 +499,7 @@ public class AltosUI extends JFrame { * Open an existing telemetry file and replay it in realtime */ - class ReplayThread extends Thread { + class ReplayThread extends DisplayThread { FileInputStream replay; String filename; @@ -464,45 +508,46 @@ public class AltosUI extends JFrame { filename = name; } - /* Run the replay in a separate thread - * so that the UI can update - */ - public void run() { - String line; - AltosState state = null; + String read() { try { - while ((line = readline(replay)) != null) { - try { - AltosTelemetry t = new AltosTelemetry(line); - state = new AltosState(t, state); - show(state); - - /* Make it run in realtime after the rocket leaves the pad */ - try { - if (state.state > AltosTelemetry.ao_flight_pad) - Thread.sleep((int) (state.time_change * 1000)); - } catch (InterruptedException e) {} - } catch (ParseException pp) { - JOptionPane.showMessageDialog(AltosUI.this, - line, - "error parsing", - JOptionPane.ERROR_MESSAGE); - break; - } - } + return readline(replay); } catch (IOException ee) { JOptionPane.showMessageDialog(AltosUI.this, filename, "error reading", JOptionPane.ERROR_MESSAGE); - } finally { - try { - replay.close(); - } catch (IOException e) {} } + return null; + } + + void close () { + try { + replay.close(); + } catch (IOException ee) { + } + } + + void update(AltosState state) throws InterruptedException { + /* Make it run in realtime after the rocket leaves the pad */ + if (state.state > AltosTelemetry.ao_flight_pad) + Thread.sleep((int) (state.time_change * 1000)); } } + Thread display_thread; + + private void stop_display() { + if (display_thread != null && display_thread.isAlive()) + display_thread.interrupt(); + display_thread = null; + } + + private void run_display(Thread thread) { + stop_display(); + display_thread = thread; + display_thread.start(); + } + /* * Replay a flight from telemetry data */ @@ -522,7 +567,7 @@ public class AltosUI extends JFrame { try { FileInputStream replay = new FileInputStream(file); ReplayThread thread = new ReplayThread(replay, filename); - thread.start(); + run_display(thread); } catch (FileNotFoundException ee) { JOptionPane.showMessageDialog(AltosUI.this, filename, @@ -532,8 +577,7 @@ public class AltosUI extends JFrame { } } - /* - * Connect to TeleMetrum, either directly or through + /* Connect to TeleMetrum, either directly or through * a TeleDongle over the packet link */ private void SaveFlightData() { @@ -556,6 +600,8 @@ public class AltosUI extends JFrame { } } + /* Create the AltosUI menus + */ private void createMenu() { JMenuBar menubar = new JMenuBar(); JMenu menu; @@ -596,7 +642,7 @@ public class AltosUI extends JFrame { item = new JMenuItem("Disconnect from Device",KeyEvent.VK_D); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { - serialLine.close(); + DisconnectFromDevice(); } }); menu.add(item);