telegps: use new eeprom reading code
[fw/altos] / telegps / TeleGPS.java
index e78c212..cf2cbd4 100644 (file)
@@ -3,7 +3,8 @@
  *
  * 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.
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -23,8 +24,9 @@ import javax.swing.*;
 import java.io.*;
 import java.util.concurrent.*;
 import java.util.*;
-import org.altusmetrum.altoslib_5.*;
-import org.altusmetrum.altosuilib_3.*;
+import java.text.*;
+import org.altusmetrum.altoslib_11.*;
+import org.altusmetrum.altosuilib_11.*;
 
 public class TeleGPS
        extends AltosUIFrame
@@ -32,12 +34,12 @@ public class TeleGPS
 {
 
        static String[] telegps_icon_names = {
-               "/telegps-16.png",
-               "/telegps-32.png",
-               "/telegps-48.png",
-               "/telegps-64.png",
-               "/telegps-128.png",
-               "/telegps-256.png"
+               "/altusmetrum-telegps-16.png",
+               "/altusmetrum-telegps-32.png",
+               "/altusmetrum-telegps-48.png",
+               "/altusmetrum-telegps-64.png",
+               "/altusmetrum-telegps-128.png",
+               "/altusmetrum-telegps-256.png"
        };
 
        static { set_icon_names(telegps_icon_names); }
@@ -52,24 +54,26 @@ public class TeleGPS
 
        AltosFlightReader       reader;
        TeleGPSDisplayThread    thread;
+       boolean                 idle_mode;
 
        JMenuBar                menu_bar;
 
        JMenu                   file_menu;
        JMenu                   monitor_menu;
        JMenu                   device_menu;
-       AltosFreqList           frequencies;
+       AltosUIFreqList         frequencies;
        ActionListener          frequency_listener;
+       AltosUIRateList         rates;
+       ActionListener          rate_listener;
 
        Container               bag;
 
        TeleGPSStatus           telegps_status;
        TeleGPSStatusUpdate     status_update;
-       javax.swing.Timer       status_timer;
 
        JTabbedPane             pane;
 
-       AltosUIMap              map;
+       AltosUIMap              map;
        TeleGPSInfo             gps_info;
        TeleGPSState            gps_state;
        AltosInfoTable          info_table;
@@ -145,6 +149,7 @@ public class TeleGPS
        public void show(AltosState state, AltosListenerState listener_state) {
                try {
                        status_update.saved_state = state;
+                       status_update.saved_listener_state = listener_state;
 
                        if (state == null)
                                state = new AltosState();
@@ -176,22 +181,50 @@ public class TeleGPS
        void disconnect() {
                setTitle("TeleGPS");
                stop_display();
-               if (status_timer != null) {
-                       status_timer.stop();
-                       status_timer = null;
-                       status_update = null;
-               }
+               telegps_status.stop();
 
                telegps_status.disable_receive();
                disable_frequency_menu();
+               disable_rate_menu();
        }
 
-       void connect(AltosDevice device) {
-               if (reader != null)
-                       disconnect();
+       void connect_flight(AltosDevice device) {
                try {
                        AltosFlightReader       reader = new AltosTelemetryReader(new AltosSerial(device));
-                       set_reader(reader, device);
+                       set_reader(reader, device, false);
+               } catch (FileNotFoundException ee) {
+                       JOptionPane.showMessageDialog(this,
+                                                     ee.getMessage(),
+                                                     String.format ("Cannot open %s", device.toShortString()),
+                                                     JOptionPane.ERROR_MESSAGE);
+               } catch (AltosSerialInUseException si) {
+                       JOptionPane.showMessageDialog(this,
+                                                     String.format("Device \"%s\" already in use",
+                                                                   device.toShortString()),
+                                                     "Device in use",
+                                                     JOptionPane.ERROR_MESSAGE);
+               } catch (IOException ee) {
+                       JOptionPane.showMessageDialog(this,
+                                                     String.format ("Unknown I/O error on %s", device.toShortString()),
+                                                     "Unknown I/O error",
+                                                     JOptionPane.ERROR_MESSAGE);
+               } catch (TimeoutException te) {
+                       JOptionPane.showMessageDialog(this,
+                                                     String.format ("Timeout on %s", device.toShortString()),
+                                                     "Timeout error",
+                                                     JOptionPane.ERROR_MESSAGE);
+               } catch (InterruptedException ie) {
+                       JOptionPane.showMessageDialog(this,
+                                                     String.format("Interrupted %s", device.toShortString()),
+                                                     "Interrupted exception",
+                                                     JOptionPane.ERROR_MESSAGE);
+               }
+       }
+
+       void connect_idle(AltosDevice device) {
+               try {
+                               AltosFlightReader       reader = new AltosIdleReader(new AltosSerial(device), false);
+                       set_reader(reader, device, true);
                } catch (FileNotFoundException ee) {
                        JOptionPane.showMessageDialog(this,
                                                      ee.getMessage(),
@@ -221,9 +254,18 @@ public class TeleGPS
                }
        }
 
+       void connect(AltosDevice device) {
+               if (reader != null)
+                       disconnect();
+               if (device.matchProduct(AltosLib.product_basestation))
+                       connect_flight(device);
+               else
+                       connect_idle(device);
+       }
+
        void connect() {
                AltosDevice     device = AltosDeviceUIDialog.show(this,
-                                                                 AltosLib.product_basestation);
+                                                                 AltosLib.product_any);
                if (device == null)
                        return;
                connect(device);
@@ -351,32 +393,71 @@ public class TeleGPS
                frequencies.set_product("Monitor");
                frequencies.set_serial(serial);
                frequencies.set_frequency(AltosUIPreferences.frequency(serial));
+               frequencies.setEnabled(true);
 
-               menu_bar.add(frequencies);
-               menu_bar.repaint();
        }
 
        void disable_frequency_menu() {
-               if (frequency_listener == null)
-                       return;
-               frequencies.removeActionListener(frequency_listener);
-               menu_bar.remove(frequencies);
-               menu_bar.repaint();
-               frequency_listener = null;
+               if (frequency_listener != null) {
+                       frequencies.removeActionListener(frequency_listener);
+                       frequencies.setEnabled(false);
+                       frequency_listener = null;
+               }
+
        }
 
-       public void set_reader(AltosFlightReader reader, AltosDevice device) {
+       void enable_rate_menu(int serial, final AltosFlightReader reader) {
+
+               if (rate_listener != null)
+                       disable_rate_menu();
+
+               rate_listener = new ActionListener() {
+                               public void actionPerformed(ActionEvent e) {
+                                       int rate = rates.rate();
+                                       try {
+                                               reader.set_telemetry_rate(rate);
+                                       } catch (TimeoutException te) {
+                                       } catch (InterruptedException ie) {
+                                       }
+                                       reader.save_telemetry_rate();
+                               }
+                       };
+
+               rates.addActionListener(rate_listener);
+               rates.set_product("Monitor");
+               rates.set_serial(serial);
+               rates.set_rate(AltosUIPreferences.telemetry_rate(serial));
+               rates.setEnabled(reader.supports_telemetry_rate(AltosLib.ao_telemetry_rate_2400));
+       }
+
+       void disable_rate_menu() {
+               if (rate_listener != null) {
+                       rates.removeActionListener(rate_listener);
+                       rates.setEnabled(false);
+                       rate_listener = null;
+               }
+
+       }
+
+       public void set_reader(AltosFlightReader reader, AltosDevice device, boolean idle_mode) {
+               this.idle_mode = idle_mode;
                status_update = new TeleGPSStatusUpdate(telegps_status);
 
-               status_timer = new javax.swing.Timer(100, status_update);
-               status_timer.start();
+               telegps_status.start(status_update);
 
                setTitle(String.format("TeleGPS %s", reader.name));
                thread = new TeleGPSDisplayThread(this, voice(), this, reader);
                thread.start();
 
-               if (device != null)
-                       enable_frequency_menu(device.getSerial(), reader);
+               if (device != null) {
+                       if (idle_mode) {
+                               disable_frequency_menu();
+                               disable_rate_menu();
+                       } else {
+                               enable_frequency_menu(device.getSerial(), reader);
+                               enable_rate_menu(device.getSerial(), reader);
+                       }
+               }
        }
 
        static int      number_of_windows;
@@ -447,8 +528,6 @@ public class TeleGPS
                bag = getContentPane();
                bag.setLayout(new GridBagLayout());
 
-               GridBagConstraints c = new GridBagConstraints();
-
                setTitle("TeleGPS");
 
                menu_bar = new JMenuBar();
@@ -457,7 +536,17 @@ public class TeleGPS
                file_menu = make_menu("File", file_menu_entries);
                monitor_menu = make_menu("Monitor", monitor_menu_entries);
                device_menu = make_menu("Device", device_menu_entries);
-               frequencies = new AltosFreqList();
+
+               set_inset(3);
+               frequencies = new AltosUIFreqList();
+               frequencies.setEnabled(false);
+               bag.add(frequencies, constraints (0, 1));
+
+               rates = new AltosUIRateList();
+               rates.setEnabled(false);
+               bag.add(rates, constraints(1, 1));
+               next_row();
+               set_inset(0);
 
                displays = new LinkedList<AltosFlightDisplay>();
 
@@ -465,13 +554,9 @@ public class TeleGPS
 
                /* TeleGPS status is always visible */
                telegps_status = new TeleGPSStatus();
-               c.gridx = 0;
-               c.gridy = 1;
-               c.fill = GridBagConstraints.HORIZONTAL;
-               c.weightx = 1;
-               c.gridwidth = 2;
-               bag.add(telegps_status, c);
-               c.gridwidth = 1;
+               bag.add(telegps_status, constraints(0, 3, GridBagConstraints.HORIZONTAL));
+               next_row();
+
                displays.add(telegps_status);
 
 
@@ -481,13 +566,7 @@ public class TeleGPS
                pane = new JTabbedPane();
 
                /* Make the tabbed pane use the rest of the window space */
-               c.gridx = 0;
-               c.gridy = 2;
-               c.fill = GridBagConstraints.BOTH;
-               c.weightx = 1;
-               c.weighty = 1;
-               c.gridwidth = 2;
-               bag.add(pane, c);
+               bag.add(pane, constraints(0, 3, GridBagConstraints.BOTH));
 
                map = new AltosUIMap();
                pane.add(map.getName(), map);
@@ -523,9 +602,9 @@ public class TeleGPS
                add_window();
        }
 
-       public TeleGPS(AltosFlightReader reader) {
+       public TeleGPS(AltosFlightReader reader, boolean idle_mode) {
                this();
-               set_reader(reader, null);
+               set_reader(reader, null, idle_mode);
        }
 
        public TeleGPS(AltosDevice device) {
@@ -535,16 +614,23 @@ public class TeleGPS
 
        static AltosStateIterable record_iterable(File file) {
                FileInputStream in;
-               try {
-                       in = new FileInputStream(file);
-               } catch (Exception e) {
-                       System.out.printf("Failed to open file '%s'\n", file);
-                       return null;
-               }
-               if (file.getName().endsWith("telem"))
-                       return new AltosTelemetryFile(in);
-               else
-                       return new AltosEepromFile(in);
+                if (file.getName().endsWith("telem")) {
+                        try {
+                                in = new FileInputStream(file);
+                                return new AltosTelemetryFile(in);
+                        } catch (Exception e) {
+                                System.out.printf("Failed to open file '%s'\n", file);
+                        }
+                } else {
+
+                        try {
+                                AltosEepromFile f = new AltosEepromFile(new FileReader(file));
+                                return f;
+                        } catch (Exception e) {
+                                System.out.printf("Failed to open file '%s'\n", file);
+                        }
+                }
+                return null;
        }
 
        static AltosReplayReader replay_file(File file) {
@@ -571,7 +657,7 @@ public class TeleGPS
                if (new_reader == null)
                        return false;
 
-               new TeleGPS(new_reader);
+               new TeleGPS(new_reader, true);
                return true;
        }
 
@@ -598,7 +684,6 @@ public class TeleGPS
        public static void help(int code) {
                System.out.printf("Usage: altosui [OPTION]... [FILE]...\n");
                System.out.printf("  Options:\n");
-               System.out.printf("    --fetchmaps <lat> <lon>\tpre-fetch maps for site map view\n");
                System.out.printf("    --replay <filename>\t\trelive the glory of past flights \n");
                System.out.printf("    --graph <filename>\t\tgraph a flight\n");
                System.out.printf("    --csv\tgenerate comma separated output for spreadsheets, etc\n");
@@ -623,16 +708,7 @@ public class TeleGPS
                for (int i = 0; i < args.length; i++) {
                        if (args[i].equals("--help"))
                                help(0);
-                       else if (args[i].equals("--fetchmaps")) {
-                               if (args.length < i + 3) {
-                                       help(1);
-                               } else {
-                                       double lat = Double.parseDouble(args[i+1]);
-                                       double lon = Double.parseDouble(args[i+2]);
-                                       AltosUIMap.prefetch_maps(lat, lon);
-                                       i += 2;
-                               }
-                       } else if (args[i].equals("--replay"))
+                       else if (args[i].equals("--replay"))
                                process = process_replay;
                        else if (args[i].equals("--kml"))
                                process = process_kml;