altosui: Generalize and centralize telemetry constants, parse v0.8 telemetry
authorKeith Packard <keithp@keithp.com>
Sun, 17 Jul 2011 00:38:00 +0000 (17:38 -0700)
committerKeith Packard <keithp@keithp.com>
Sun, 17 Jul 2011 00:48:51 +0000 (17:48 -0700)
Move telemetry constants to Altos class, adding functions to compute
names and lengths. Generalize users of these values to use all of the
known values.

Add support for v0.8 TeleMetrum telemetry

Signed-off-by: Keith Packard <keithp@keithp.com>
altosui/Altos.java
altosui/AltosFlightUI.java
altosui/AltosPreferences.java
altosui/AltosScanUI.java
altosui/AltosSerial.java
altosui/AltosTelemetryRecordLegacy.java
altosui/AltosTelemetryRecordRaw.java

index 9626379..8d5916a 100644 (file)
@@ -70,11 +70,23 @@ public class Altos {
 
        /* Telemetry modes */
        static final int ao_telemetry_off = 0;
-       static final int ao_telemetry_legacy = 1;
-       static final int ao_telemetry_split = 2;
+       static final int ao_telemetry_min = 1;
+       static final int ao_telemetry_standard = 1;
+       static final int ao_telemetry_0_9 = 2;
+       static final int ao_telemetry_0_8 = 3;
+       static final int ao_telemetry_max = 3;
+
+       static final String[] ao_telemetry_name = {
+               "Off", "Standard Telemetry", "TeleMetrum v0.9", "TeleMetrum v0.8"
+       };
+
+       static final int ao_telemetry_standard_len = 32;
+       static final int ao_telemetry_0_9_len = 95;
+       static final int ao_telemetry_0_8_len = 94;
 
-       static final int ao_telemetry_split_len = 32;
-       static final int ao_telemetry_legacy_len = 95;
+       static final int[] ao_telemetry_len = {
+               0, 32, 95, 94
+       };
 
        static HashMap<String,Integer>  string_to_state = new HashMap<String,Integer>();
 
@@ -103,6 +115,20 @@ public class Altos {
                map_initialized = true;
        }
 
+       static int telemetry_len(int telemetry) {
+               if (telemetry <= ao_telemetry_max)
+                       return ao_telemetry_len[telemetry];
+               throw new IllegalArgumentException(String.format("Invalid telemetry %d",
+                                                                telemetry));
+       }
+
+       static String telemetry_name(int telemetry) {
+               if (telemetry <= ao_telemetry_max)
+                       return ao_telemetry_name[telemetry];
+               throw new IllegalArgumentException(String.format("Invalid telemetry %d",
+                                                                telemetry));
+       }
+       
        static String[] state_to_string = {
                "startup",
                "idle",
index 9536c4b..04bfc90 100644 (file)
@@ -156,14 +156,14 @@ public class AltosFlightUI extends JFrame implements AltosFlightDisplay {
 
                        // Telemetry format menu
                        telemetries = new JComboBox();
-                       telemetries.addItem("Original TeleMetrum Telemetry");
-                       telemetries.addItem("Standard AltOS Telemetry");
-                       int telemetry = 1;
-                       telemetry = AltosPreferences.telemetry(serial);
-                       if (telemetry > Altos.ao_telemetry_split)
-                               telemetry = Altos.ao_telemetry_split;
+                       for (int i = 1; i <= Altos.ao_telemetry_max; i++)
+                               telemetries.addItem(Altos.telemetry_name(i));
+                       int telemetry = AltosPreferences.telemetry(serial);
+                       if (telemetry <= Altos.ao_telemetry_off ||
+                           telemetry > Altos.ao_telemetry_max)
+                               telemetry = Altos.ao_telemetry_standard;
                        telemetries.setSelectedIndex(telemetry - 1);
-                       telemetries.setMaximumRowCount(2);
+                       telemetries.setMaximumRowCount(Altos.ao_telemetry_max);
                        telemetries.addActionListener(new ActionListener() {
                                        public void actionPerformed(ActionEvent e) {
                                                int telemetry = telemetries.getSelectedIndex() + 1;
index 5029aff..c8dee74 100644 (file)
@@ -216,7 +216,7 @@ class AltosPreferences {
                if (telemetries.containsKey(serial))
                        return telemetries.get(serial);
                int telemetry = preferences.getInt(String.format(telemetryPreferenceFormat, serial),
-                                                  Altos.ao_telemetry_split);
+                                                  Altos.ao_telemetry_standard);
                telemetries.put(serial, telemetry);
                return telemetry;
        }
index d94ac3a..54be4f5 100644 (file)
@@ -35,13 +35,12 @@ class AltosScanResult {
        int     flight;
        int     channel;
        int     telemetry;
-       static final String[] short_monitor_names = { "Standard", "Original" };
        
        boolean interrupted = false;
        
        public String toString() {
-               return String.format("%-9.9s serial %-4d flight %-4d (channel %-2d telemetry %s)",
-                                    callsign, serial, flight, channel, short_monitor_names[telemetry]);
+               return String.format("%-9.9s serial %-4d flight %-4d (channel %-2d %s)",
+                                    callsign, serial, flight, channel, Altos.telemetry_name(telemetry));
        }
 
        public String toShortString() {
@@ -116,9 +115,7 @@ public class AltosScanUI
        javax.swing.Timer               timer;
        AltosScanResults                results = new AltosScanResults();
 
-       static final String[]           monitor_names = { "Standard AltOS Telemetry", "Original TeleMetrum Telemetry" };
-       static final int[]              monitors = { 2, 1 };
-       int                             monitor;
+       int                             telemetry;
        int                             channel;
 
        final static int                timeout = 1200;
@@ -171,7 +168,7 @@ public class AltosScanUI
                                                                                                     record.serial,
                                                                                                     record.flight,
                                                                                                     channel,
-                                                                                                    monitor);
+                                                                                                    telemetry);
                                                        Runnable r = new Runnable() {
                                                                        public void run() {
                                                                                results.add(result);
@@ -195,7 +192,7 @@ public class AltosScanUI
        void set_label() {
                scanning_label.setText(String.format("Scanning: channel %d %s",
                                                     channel,
-                                                    monitor_names[monitor]));
+                                                    Altos.telemetry_name(telemetry)));
        }
 
        void next() {
@@ -207,10 +204,10 @@ public class AltosScanUI
                ++channel;
                if (channel > 9) {
                        channel = 0;
-                       ++monitor;
-                       if (monitor == monitors.length)
-                               monitor = 0;
-                       reader.serial.set_telemetry(monitors[monitor]);
+                       ++telemetry;
+                       if (telemetry > Altos.ao_telemetry_max)
+                               telemetry = Altos.ao_telemetry_min;
+                       reader.serial.set_telemetry(telemetry);
                }
                reader.serial.set_channel(channel);
                set_label();
@@ -251,7 +248,7 @@ public class AltosScanUI
                        if (r != null) {
                                if (device != null) {
                                        if (reader != null) {
-                                               reader.set_telemetry(monitors[r.telemetry]);
+                                               reader.set_telemetry(r.telemetry);
                                                reader.set_channel(r.channel);
                                                owner.telemetry_window(device);
                                        }
@@ -282,7 +279,7 @@ public class AltosScanUI
                try {
                        reader = new AltosTelemetryReader(device);
                        reader.serial.set_channel(channel);
-                       reader.serial.set_telemetry(monitors[monitor]);
+                       reader.serial.set_telemetry(telemetry);
                        handler = new TelemetryHandler();
                        thread = new Thread(handler);
                        thread.start();
@@ -329,6 +326,9 @@ public class AltosScanUI
 
                pane.setLayout(new GridBagLayout());
 
+               channel = 0;
+               telemetry = Altos.ao_telemetry_min;
+
                scanning_label = new JLabel("Scanning:");
                
                set_label();
index 3666cb4..2e8ce87 100644 (file)
@@ -326,13 +326,7 @@ public class AltosSerial implements Runnable {
        }
 
        private int telemetry_len() {
-               switch (telemetry) {
-               case 1:
-               default:
-                       return Altos.ao_telemetry_legacy_len;
-               case 2:
-                       return Altos.ao_telemetry_split_len;
-               }
+               return Altos.telemetry_len(telemetry);
        }
 
        public void set_channel(int in_channel) {
@@ -404,7 +398,7 @@ public class AltosSerial implements Runnable {
                line = "";
                monitor_mode = false;
                frame = null;
-               telemetry = Altos.ao_telemetry_split;
+               telemetry = Altos.ao_telemetry_standard;
                monitors = new LinkedList<LinkedBlockingQueue<AltosLine>> ();
                reply_queue = new LinkedBlockingQueue<AltosLine> ();
                open();
index e3751ee..756f3ec 100644 (file)
@@ -385,24 +385,25 @@ public class AltosTelemetryRecordLegacy extends AltosRecord implements AltosTele
         */
 
        int[]   bytes;
+       int     adjust;
 
        private int int8(int i) {
-               return Altos.int8(bytes, i + 1);
+               return Altos.int8(bytes, i + 1 + adjust);
        }
        private int uint8(int i) {
-               return Altos.uint8(bytes, i + 1);
+               return Altos.uint8(bytes, i + 1 + adjust);
        }
        private int int16(int i) {
-               return Altos.int16(bytes, i + 1);
+               return Altos.int16(bytes, i + 1 + adjust);
        }
        private int uint16(int i) {
-               return Altos.uint16(bytes, i + 1);
+               return Altos.uint16(bytes, i + 1 + adjust);
        }
        private int uint32(int i) {
-               return Altos.uint32(bytes, i + 1);
+               return Altos.uint32(bytes, i + 1 + adjust);
        }
        private String string(int i, int l) {
-               return Altos.string(bytes, i + 1, l);
+               return Altos.string(bytes, i + 1 + adjust, l);
        }
 
        static final int AO_GPS_NUM_SAT_MASK    = (0xf << 0);
@@ -428,8 +429,13 @@ public class AltosTelemetryRecordLegacy extends AltosRecord implements AltosTele
                }
                }
                version = 4;
-               callsign = string(62, 8);
+               adjust = 0;
                serial = uint16(0);
+
+               if (bytes.length == Altos.ao_telemetry_0_8_len + 4)
+                       adjust = -1;
+               
+               callsign = string(62, 8);
                flight = uint16(2);
                rssi = in_rssi;
                status = in_status;
index e6c4cfc..4b34f01 100644 (file)
@@ -72,7 +72,7 @@ public class AltosTelemetryRecordRaw implements AltosTelemetryRecord {
 
                /* length, data ..., rssi, status, checksum -- 4 bytes extra */
                switch (bytes.length) {
-               case Altos.ao_telemetry_split_len + 4:
+               case Altos.ao_telemetry_standard_len + 4:
                        int     type = Altos.uint8(bytes, 4 + 1);
                        switch (type) {
                        case packet_type_TM_sensor:
@@ -94,7 +94,10 @@ public class AltosTelemetryRecordRaw implements AltosTelemetryRecord {
                                break;
                        }
                        break;
-               case Altos.ao_telemetry_legacy_len + 4:
+               case Altos.ao_telemetry_0_9_len + 4:
+                       r = new AltosTelemetryRecordLegacy(bytes, rssi, status);
+                       break;
+               case Altos.ao_telemetry_0_8_len + 4:
                        r = new AltosTelemetryRecordLegacy(bytes, rssi, status);
                        break;
                default: