Enable telemetry monitoring
authorKeith Packard <keithp@keithp.com>
Tue, 6 Apr 2010 07:58:00 +0000 (00:58 -0700)
committerKeith Packard <keithp@keithp.com>
Tue, 6 Apr 2010 07:58:00 +0000 (00:58 -0700)
Signed-off-by: Keith Packard <keithp@keithp.com>
ao-tools/altosui/AltosDeviceDialog.java [new file with mode: 0644]
ao-tools/altosui/AltosDeviceLinux.java
ao-tools/altosui/AltosUI.java
ao-tools/altosui/Makefile

diff --git a/ao-tools/altosui/AltosDeviceDialog.java b/ao-tools/altosui/AltosDeviceDialog.java
new file mode 100644 (file)
index 0000000..cb1eef8
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Copyright © 2010 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package altosui;
+
+import java.lang.*;
+import java.util.*;
+import javax.swing.*;
+import altosui.AltosDevice;
+import altosui.AltosDeviceLinux;
+
+public class AltosDeviceDialog {
+
+       static AltosDevice show (JFrame frame, String product) {
+               AltosDevice[]   devices = null;
+               if (System.getProperty("os.name").startsWith("Linux"))
+                       devices = AltosDeviceLinux.list(product);
+               if (devices != null & devices.length > 0) {
+                       Object o = JOptionPane.showInputDialog(frame,
+                                                              "Select a device",
+                                                              "Device Selection",
+                                                              JOptionPane.PLAIN_MESSAGE,
+                                                              null,
+                                                              devices,
+                                                              devices[0]);
+                       return (AltosDevice) o;
+               } else {
+                       return null;
+               }
+       }
+}
index b777462780f0cf089d2b35eeda3375cae26ad3a5..ffc70aff7a2f70535b38319dc5032b266997b7c3 100644 (file)
@@ -19,8 +19,6 @@ package altosui;
 import java.lang.*;
 import java.io.*;
 import java.util.*;
-import altosui.AltosDeviceName;
-import altosui.AltosDeviceNameLinux;
 import altosui.AltosDevice;
 
 public class AltosDeviceLinux extends AltosDevice {
@@ -120,7 +118,7 @@ public class AltosDeviceLinux extends AltosDevice {
        }
 
        public String toString() {
-               return manufacturer + " " + product + " " + serial + " " + idProduct + " " + idVendor + " " + tty;
+               return String.format("%-20s %6d %-15s", product, serial, tty == null ? "" : tty);
        }
        static public AltosDeviceLinux[] list() {
                LinkedList<AltosDeviceLinux> devices = new LinkedList<AltosDeviceLinux>();
@@ -152,20 +150,23 @@ public class AltosDeviceLinux extends AltosDevice {
                        }
                }
                AltosDeviceLinux[] foo = new AltosDeviceLinux[devices.size()];
-               for (int e = 0; e < devices.size(); e++) {
+               for (int e = 0; e < devices.size(); e++)
                        foo[e] = devices.get(e);
-                       System.out.println("Device " + foo[e]);
-               }
                return foo;
        }
 
        static public AltosDeviceLinux[] list(String model) {
                AltosDeviceLinux[] devices = list();
-               LinkedList<AltosDeviceLinux> subset = new LinkedList<AltosDeviceLinux>();
-               for (int i = 0; i < devices.length; i++) {
-                       if (devices[i].product.startsWith(model))
-                               subset.add(devices[i]);
+               if (model != null) {
+                       LinkedList<AltosDeviceLinux> subset = new LinkedList<AltosDeviceLinux>();
+                       for (int i = 0; i < devices.length; i++) {
+                               if (devices[i].product.startsWith(model))
+                                       subset.add(devices[i]);
+                       }
+                       devices = new AltosDeviceLinux[subset.size()];
+                       for (int e = 0; e < subset.size(); e++)
+                               devices[e] = subset.get(e);
                }
-               return (AltosDeviceLinux[]) subset.toArray();
+               return devices;
        }
 }
index b2305a217568b5fe4d98fda3d9a567d85e32f310..7f008f3aadb7930e869a9916c3d56e2186fd88e5 100644 (file)
@@ -32,12 +32,7 @@ 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 +188,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));
@@ -405,38 +399,94 @@ public class AltosUI extends JFrame {
                }
        }
 
+       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;
+
+               String read() throws InterruptedException {
+                       System.out.println("Waiting for telemetry");
+                       String s = serial.get_telem();
+                       System.out.println("Got telemetry " + s);
+                       return s;
+               }
+
+               void close() {
+                       serial.close();
+                       System.out.println("DisplayThread done");
+               }
+
+               public DeviceThread(AltosSerial s) {
+                       serial = s;
+               }
+       }
+
        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 +505,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,48 +514,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) {
-                                                       break;
-                                               }
-                                       } 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();
        }
 
-       ReplayThread    replay_thread;
        /*
         * Replay a flight from telemetry data
         */
@@ -525,10 +573,7 @@ public class AltosUI extends JFrame {
                        try {
                                FileInputStream replay = new FileInputStream(file);
                                ReplayThread    thread = new ReplayThread(replay, filename);
-                               if (thread != null && replay_thread != null && replay_thread.isAlive())
-                                       replay_thread.interrupt();
-                               replay_thread = thread;
-                               replay_thread.start();
+                               run_display(thread);
                        } catch (FileNotFoundException ee) {
                                JOptionPane.showMessageDialog(AltosUI.this,
                                                              filename,
@@ -603,7 +648,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);
@@ -688,10 +733,6 @@ public class AltosUI extends JFrame {
 
        }
        public static void main(final String[] args) {
-               AltosDevice[] devices = AltosDeviceLinux.list();
-               for (int i = 0; i < devices.length; i++)
-                       System.out.printf("Model: %s Serial: %d Tty: %s\n",
-                                         devices[i].product, devices[i].serial, devices[i].tty);
                AltosUI altosui = new AltosUI();
                altosui.setVisible(true);
        }
index fcb259ebe9d0648cc5a2a598fa7fc9bf7ca17e64..fbe0a5eb41cb7ab2beafbcec03c3e2b2348e20f9 100644 (file)
@@ -13,6 +13,7 @@ CLASSFILES=\
        AltosUI.class \
        AltosDevice.class \
        AltosDeviceLinux.class \
+       AltosDeviceDialog.class
 
 JAVAFLAGS=-Xlint:unchecked