add --replay command line argument
[fw/altos] / ao-tools / altosui / AltosUI.java
index de0673a209e396630fb3b166a2e1586bf2b7f7bb..2861444da7b5e9008e875d3285d7f225226ea18a 100644 (file)
@@ -39,7 +39,6 @@ import altosui.AltosPreferences;
 import altosui.AltosLog;
 import altosui.AltosVoice;
 import altosui.AltosFlightInfoTableModel;
-import altosui.AltosChannelMenu;
 import altosui.AltosFlashUI;
 import altosui.AltosLogfileChooser;
 import altosui.AltosCSVUI;
@@ -51,18 +50,6 @@ import altosui.AltosDisplayThread;
 import libaltosJNI.*;
 
 public class AltosUI extends JFrame {
-       private int channel = -1;
-
-       private AltosStatusTable flightStatus;
-       private AltosInfoTable flightInfo;
-       private AltosSerial serial_line;
-       private AltosLog altos_log;
-       private Box vbox;
-
-       private Font statusFont = new Font("SansSerif", Font.BOLD, 24);
-       private Font infoLabelFont = new Font("SansSerif", Font.PLAIN, 14);
-       private Font infoValueFont = new Font("Monospaced", Font.PLAIN, 14);
-
        public AltosVoice voice = new AltosVoice();
 
        public static boolean load_library(Frame frame) {
@@ -77,39 +64,117 @@ public class AltosUI extends JFrame {
                return true;
        }
 
+       void telemetry_window(AltosDevice device) {
+               try {
+                       AltosFlightReader reader = new AltosTelemetryReader(device);
+                       if (reader != null)
+                               new AltosFlightUI(voice, reader, device.getSerial());
+               } catch (FileNotFoundException ee) {
+                       JOptionPane.showMessageDialog(AltosUI.this,
+                                                     String.format("Cannot open device \"%s\"",
+                                                                   device.getPath()),
+                                                     "Cannot open target device",
+                                                     JOptionPane.ERROR_MESSAGE);
+               } catch (IOException ee) {
+                       JOptionPane.showMessageDialog(AltosUI.this,
+                                                     device.getPath(),
+                                                     "Unkonwn I/O error",
+                                                     JOptionPane.ERROR_MESSAGE);
+               }
+       }
+
+       Container       pane;
+       GridBagLayout   gridbag;
+
+       JButton addButton(int x, int y, String label) {
+               GridBagConstraints      c;
+               JButton                 b;
+
+               c = new GridBagConstraints();
+               c.gridx = x; c.gridy = y;
+               c.fill = GridBagConstraints.BOTH;
+               c.weightx = 1;
+               c.weighty = 1;
+               b = new JButton(label);
+
+               Dimension ps = b.getPreferredSize();
+
+               gridbag.setConstraints(b, c);
+               add(b, c);
+               return b;
+       }
+
        public AltosUI() {
 
                load_library(null);
 
-               String[] statusNames = { "Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
-               Object[][] statusData = { { "0", "pad", "-50", "0" } };
-
                java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
                if (imgURL != null)
                        setIconImage(new ImageIcon(imgURL).getImage());
 
                AltosPreferences.init(this);
 
-               vbox = Box.createVerticalBox();
-               this.add(vbox);
+               pane = getContentPane();
+               gridbag = new GridBagLayout();
+               pane.setLayout(gridbag);
 
-               flightStatus = new AltosStatusTable(this);
+               JButton b;
 
-               vbox.add(flightStatus);
-
-               flightInfo = new AltosInfoTable();
-               vbox.add(flightInfo.box());
+               b = addButton(0, 0, "Monitor Flight");
+               b.addActionListener(new ActionListener() {
+                                       public void actionPerformed(ActionEvent e) {
+                                               ConnectToDevice();
+                                       }
+                               });
+               b = addButton(1, 0, "Save Flight Data");
+               b.addActionListener(new ActionListener() {
+                                       public void actionPerformed(ActionEvent e) {
+                                               SaveFlightData();
+                                       }
+                               });
+               b = addButton(2, 0, "Replay Flight");
+               b.addActionListener(new ActionListener() {
+                                       public void actionPerformed(ActionEvent e) {
+                                               Replay();
+                                       }
+                               });
+               b = addButton(0, 1, "Graph Data");
+               b.addActionListener(new ActionListener() {
+                                       public void actionPerformed(ActionEvent e) {
+                                               GraphData();
+                                       }
+                               });
+               b = addButton(1, 1, "Export Data");
+               b.addActionListener(new ActionListener() {
+                                       public void actionPerformed(ActionEvent e) {
+                                               ExportData();
+                                       }
+                               });
+               b = addButton(2, 1, "Configure TeleMetrum");
+               b.addActionListener(new ActionListener() {
+                                       public void actionPerformed(ActionEvent e) {
+                                               ConfigureTeleMetrum();
+                                       }
+                               });
 
                setTitle("AltOS");
 
                createMenu();
 
-               serial_line = new AltosSerial();
-               altos_log = new AltosLog(serial_line);
-               int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
-               this.setSize(new Dimension (flightInfo.width(),
-                                           flightStatus.height() + flightInfo.height()));
-               this.validate();
+               pane.doLayout();
+               pane.validate();
+
+               doLayout();
+               validate();
+
+               setVisible(true);
+
+               Insets i = getInsets();
+               Dimension ps = rootPane.getPreferredSize();
+               ps.width += i.left + i.right;
+               ps.height += i.top + i.bottom;
+               setPreferredSize(ps);
+               setSize(ps);
                setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                addWindowListener(new WindowAdapter() {
                        @Override
@@ -117,63 +182,14 @@ public class AltosUI extends JFrame {
                                System.exit(0);
                        }
                });
-               voice.speak("Rocket flight monitor ready.");
-       }
-
-       class DeviceThread extends AltosDisplayThread {
-               AltosSerial     serial;
-               LinkedBlockingQueue<AltosLine> telem;
-
-               AltosRecord read() throws InterruptedException, ParseException, AltosCRCException, IOException {
-                       AltosLine l = telem.take();
-                       if (l.line == null)
-                               throw new IOException("IO error");
-                       return new AltosTelemetry(l.line);
-               }
-
-               void close(boolean interrupted) {
-                       serial.close();
-                       serial.remove_monitor(telem);
-               }
-
-               public DeviceThread(AltosSerial s, String in_name, AltosVoice voice, AltosStatusTable status, AltosInfoTable info) {
-                       super(AltosUI.this, voice, status, info);
-                       serial = s;
-                       telem = new LinkedBlockingQueue<AltosLine>();
-                       serial.add_monitor(telem);
-                       name = in_name;
-               }
        }
 
        private void ConnectToDevice() {
                AltosDevice     device = AltosDeviceDialog.show(AltosUI.this,
                                                                AltosDevice.product_basestation);
 
-               if (device != null) {
-                       try {
-                               stop_display();
-                               serial_line.open(device);
-                               DeviceThread thread = new DeviceThread(serial_line, device.getPath(), voice, flightStatus, flightInfo);
-                               serial_line.set_channel(AltosPreferences.channel());
-                               serial_line.set_callsign(AltosPreferences.callsign());
-                               run_display(thread);
-                       } catch (FileNotFoundException ee) {
-                               JOptionPane.showMessageDialog(AltosUI.this,
-                                                             String.format("Cannot open device \"%s\"",
-                                                                           device.getPath()),
-                                                             "Cannot open target device",
-                                                             JOptionPane.ERROR_MESSAGE);
-                       } catch (IOException ee) {
-                               JOptionPane.showMessageDialog(AltosUI.this,
-                                                             device.getPath(),
-                                                             "Unkonwn I/O error",
-                                                             JOptionPane.ERROR_MESSAGE);
-                       }
-               }
-       }
-
-       void DisconnectFromDevice () {
-               stop_display();
+               if (device != null)
+                       telemetry_window(device);
        }
 
        void ConfigureCallsign() {
@@ -181,11 +197,8 @@ public class AltosUI extends JFrame {
                result = JOptionPane.showInputDialog(AltosUI.this,
                                                     "Configure Callsign",
                                                     AltosPreferences.callsign());
-               if (result != null) {
+               if (result != null)
                        AltosPreferences.set_callsign(result);
-                       if (serial_line != null)
-                               serial_line.set_callsign(result);
-               }
        }
 
        void ConfigureTeleMetrum() {
@@ -196,25 +209,6 @@ public class AltosUI extends JFrame {
                new AltosFlashUI(AltosUI.this);
        }
 
-
-       Thread          display_thread;
-
-       private void stop_display() {
-               if (display_thread != null && display_thread.isAlive()) {
-                       display_thread.interrupt();
-                       try {
-                               display_thread.join();
-                       } catch (InterruptedException ie) {}
-               }
-               display_thread = null;
-       }
-
-       private void run_display(Thread thread) {
-               stop_display();
-               display_thread = thread;
-               display_thread.start();
-       }
-
        /*
         * Replay a flight from telemetry data
         */
@@ -222,12 +216,11 @@ public class AltosUI extends JFrame {
                AltosLogfileChooser chooser = new AltosLogfileChooser(
                        AltosUI.this);
                AltosRecordIterable iterable = chooser.runDialog();
-               if (iterable != null)
-                       run_display(new AltosReplayThread(this, iterable.iterator(),
-                                                         chooser.filename(),
-                                                         voice,
-                                                         flightStatus,
-                                                         flightInfo));
+               if (iterable != null) {
+                       AltosFlightReader reader = new AltosReplayReader(iterable.iterator(),
+                                                                        chooser.filename());
+                       new AltosFlightUI(voice, reader);
+               }
        }
 
        /* Connect to TeleMetrum, either directly or through
@@ -266,23 +259,7 @@ public class AltosUI extends JFrame {
                        menu.setMnemonic(KeyEvent.VK_F);
                        menubar.add(menu);
 
-                       item = new JMenuItem("Replay File",KeyEvent.VK_R);
-                       item.addActionListener(new ActionListener() {
-                                       public void actionPerformed(ActionEvent e) {
-                                               Replay();
-                                       }
-                               });
-                       menu.add(item);
-
-                       item = new JMenuItem("Save Flight Data",KeyEvent.VK_S);
-                       item.addActionListener(new ActionListener() {
-                                       public void actionPerformed(ActionEvent e) {
-                                               SaveFlightData();
-                                       }
-                               });
-                       menu.add(item);
-
-                       item = new JMenuItem("Flash Image",KeyEvent.VK_F);
+                       item = new JMenuItem("Flash Image",KeyEvent.VK_I);
                        item.addActionListener(new ActionListener() {
                                        public void actionPerformed(ActionEvent e) {
                                                FlashImage();
@@ -290,7 +267,7 @@ public class AltosUI extends JFrame {
                                });
                        menu.add(item);
 
-                       item = new JMenuItem("Export Data",KeyEvent.VK_F);
+                       item = new JMenuItem("Export Data",KeyEvent.VK_E);
                        item.addActionListener(new ActionListener() {
                                        public void actionPerformed(ActionEvent e) {
                                                ExportData();
@@ -298,7 +275,7 @@ public class AltosUI extends JFrame {
                                });
                        menu.add(item);
 
-                       item = new JMenuItem("Graph Data",KeyEvent.VK_F);
+                       item = new JMenuItem("Graph Data",KeyEvent.VK_G);
                        item.addActionListener(new ActionListener() {
                                        public void actionPerformed(ActionEvent e) {
                                                GraphData();
@@ -311,6 +288,7 @@ public class AltosUI extends JFrame {
                                                                   ActionEvent.CTRL_MASK));
                        item.addActionListener(new ActionListener() {
                                        public void actionPerformed(ActionEvent e) {
+                                               System.out.printf("exiting\n");
                                                System.exit(0);
                                        }
                                });
@@ -318,7 +296,7 @@ public class AltosUI extends JFrame {
                }
 
                // Device menu
-               {
+               if (false) {
                        menu = new JMenu("Device");
                        menu.setMnemonic(KeyEvent.VK_D);
                        menubar.add(menu);
@@ -331,14 +309,6 @@ public class AltosUI extends JFrame {
                                });
                        menu.add(item);
 
-                       item = new JMenuItem("Disconnect from Device",KeyEvent.VK_D);
-                       item.addActionListener(new ActionListener() {
-                                       public void actionPerformed(ActionEvent e) {
-                                               DisconnectFromDevice();
-                                       }
-                               });
-                       menu.add(item);
-
                        menu.addSeparator();
 
                        item = new JMenuItem("Set Callsign",KeyEvent.VK_S);
@@ -407,30 +377,7 @@ public class AltosUI extends JFrame {
                                });
                        menu.add(item);
                }
-
-               // Channel menu
-               {
-                       menu = new AltosChannelMenu(AltosPreferences.channel());
-                       menu.addActionListener(new ActionListener() {
-                                               public void actionPerformed(ActionEvent e) {
-                                                       int new_channel = Integer.parseInt(e.getActionCommand());
-                                                       AltosPreferences.set_channel(new_channel);
-                                                       serial_line.set_channel(new_channel);
-                                               }
-                               });
-                       menu.setMnemonic(KeyEvent.VK_C);
-                       menubar.add(menu);
-               }
-
                this.setJMenuBar(menubar);
-
-       }
-
-       static String replace_extension(String input, String extension) {
-               int dot = input.lastIndexOf(".");
-               if (dot > 0)
-                       input = input.substring(0,dot);
-               return input.concat(extension);
        }
 
        static AltosRecordIterable open_logfile(String filename) {
@@ -449,7 +396,7 @@ public class AltosUI extends JFrame {
                }
        }
 
-       static AltosCSV open_csv(String filename) {
+       static AltosWriter open_csv(String filename) {
                File file = new File (filename);
                try {
                        return new AltosCSV(file);
@@ -459,32 +406,91 @@ public class AltosUI extends JFrame {
                }
        }
 
-       static void process_file(String input) {
-               String output = replace_extension(input,".csv");
-               if (input.equals(output)) {
-                       System.out.printf("Not processing '%s'\n", input);
-                       return;
+       static AltosWriter open_kml(String filename) {
+               File file = new File (filename);
+               try {
+                       return new AltosKML(file);
+               } catch (FileNotFoundException fe) {
+                       System.out.printf("Cannot open '%s'\n", filename);
+                       return null;
                }
-               System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
+       }
+
+       static final int process_csv = 1;
+       static final int process_kml = 2;
+
+       static void process_file(String input, int process) {
                AltosRecordIterable iterable = open_logfile(input);
                if (iterable == null)
                        return;
-               AltosCSV writer = open_csv(output);
-               if (writer == null)
-                       return;
-               writer.write(iterable);
-               writer.close();
+               if (process == 0)
+                       process = process_csv;
+               if ((process & process_csv) != 0) {
+                       String output = Altos.replace_extension(input,".csv");
+                       System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
+                       if (input.equals(output)) {
+                               System.out.printf("Not processing '%s'\n", input);
+                       } else {
+                               AltosWriter writer = open_csv(output);
+                               if (writer != null) {
+                                       writer.write(iterable);
+                                       writer.close();
+                               }
+                       }
+               }
+               if ((process & process_kml) != 0) {
+                       String output = Altos.replace_extension(input,".kml");
+                       System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
+                       if (input.equals(output)) {
+                               System.out.printf("Not processing '%s'\n", input);
+                       } else {
+                               AltosWriter writer = open_kml(output);
+                               if (writer == null)
+                                       return;
+                               writer.write(iterable);
+                               writer.close();
+                       }
+               }
        }
 
        public static void main(final String[] args) {
-
+               int     process = 0;
                /* Handle batch-mode */
-               if (args.length > 0) {
-                       for (int i = 0; i < args.length; i++)
-                               process_file(args[i]);
+        if (args.length == 2 && args[0].equals("--replay")) {
+            String filename = args[1];
+            FileInputStream in;
+            try {
+                in = new FileInputStream(filename);
+            } catch (Exception e) {
+                System.out.printf("Failed to open file '%s'\n", filename);
+                return;
+            }
+            AltosRecordIterable recs;
+            AltosReplayReader reader;
+            if (filename.endsWith("eeprom")) {
+              recs = new AltosEepromIterable(in);
+            } else {
+              recs = new AltosTelemetryIterable(in); 
+            }
+            reader = new AltosReplayReader(recs.iterator(), filename);
+            new AltosFlightUI(new AltosVoice(), reader);
+            return;
+        } else if (args.length > 0) {
+                       for (int i = 0; i < args.length; i++) {
+                               if (args[i].equals("--kml"))
+                                       process |= process_kml;
+                               else if (args[i].equals("--csv"))
+                                       process |= process_csv;
+                               else
+                                       process_file(args[i], process);
+                       }
                } else {
                        AltosUI altosui = new AltosUI();
                        altosui.setVisible(true);
+
+                       AltosDevice[] devices = AltosDevice.list(AltosDevice.product_basestation);
+                       for (int i = 0; i < devices.length; i++)
+                               altosui.telemetry_window(devices[i]);
                }
        }
 }