Bump java lib versions in preparation for 1.9.2
[fw/altos] / altoslib / AltosTelemetry.java
index 5600e7beb3f19b31620d8217d9e239d8d0eda054..ec3d8495dc1efff7fe65071e42aefecec471936e 100644 (file)
@@ -16,7 +16,7 @@
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  */
 
-package org.altusmetrum.altoslib_11;
+package org.altusmetrum.altoslib_14;
 
 import java.text.*;
 
@@ -24,12 +24,15 @@ import java.text.*;
  * Telemetry data contents
  */
 
-public abstract class AltosTelemetry implements AltosStateUpdate {
+public abstract class AltosTelemetry implements AltosDataProvider {
        int[]   bytes;
 
        /* All telemetry packets have these fields */
-       public int rssi() { return AltosConvert.telem_to_rssi(AltosLib.int8(bytes, bytes.length - 3)); }
-       public int status() { return AltosLib.uint8(bytes, bytes.length - 2); }
+       static public int rssi(int[] bytes) { return AltosConvert.telem_to_rssi(AltosLib.int8(bytes, bytes.length - 3)); }
+       static public int status(int[] bytes) { return AltosLib.uint8(bytes, bytes.length - 2); }
+
+       public int rssi() { return rssi(bytes); }
+       public int status() { return status(bytes); }
 
        /* All telemetry packets report these fields in some form */
        public abstract int serial();
@@ -38,6 +41,9 @@ public abstract class AltosTelemetry implements AltosStateUpdate {
        /* Mark when we received the packet */
        long            received_time;
 
+       /* Mark frequency packet was received on */
+       public double           frequency = AltosLib.MISSING;
+
        static boolean cksum(int[] bytes) {
                int     sum = 0x5a;
                for (int i = 1; i < bytes.length - 1; i++)
@@ -46,13 +52,15 @@ public abstract class AltosTelemetry implements AltosStateUpdate {
                return sum == bytes[bytes.length - 1];
        }
 
-       public void update_state(AltosState state) {
-               state.set_serial(serial());
-               if (state.state() == AltosLib.ao_flight_invalid)
-                       state.set_state(AltosLib.ao_flight_startup);
-               state.set_tick(tick());
-               state.set_rssi(rssi(), status());
-               state.set_received_time(received_time);
+       public void provide_data(AltosDataListener listener) {
+               listener.set_serial(serial());
+               if (listener.state() == AltosLib.ao_flight_invalid)
+                       listener.set_state(AltosLib.ao_flight_startup);
+               if (frequency != AltosLib.MISSING)
+                       listener.set_frequency(frequency);
+               listener.set_tick(tick());
+               listener.set_rssi(rssi(), status());
+               listener.set_received_time(received_time);
        }
 
        final static int PKT_APPEND_STATUS_1_CRC_OK             = (1 << 7);
@@ -66,12 +74,13 @@ public abstract class AltosTelemetry implements AltosStateUpdate {
        final static int packet_type_location = 0x05;
        final static int packet_type_satellite = 0x06;
        final static int packet_type_companion = 0x07;
-       final static int packet_type_mega_sensor = 0x08;
+       final static int packet_type_mega_sensor_mpu = 0x08;
        final static int packet_type_mega_data = 0x09;
        final static int packet_type_metrum_sensor = 0x0a;
        final static int packet_type_metrum_data = 0x0b;
        final static int packet_type_mini2 = 0x10;
        final static int packet_type_mini3 = 0x11;
+       final static int packet_type_mega_sensor_bmx160 = 0x12;
 
        static AltosTelemetry parse_hex(String hex)  throws ParseException, AltosCRCException {
                AltosTelemetry  telem = null;
@@ -91,6 +100,9 @@ public abstract class AltosTelemetry implements AltosStateUpdate {
                if (!cksum(bytes))
                        throw new ParseException(String.format("invalid line \"%s\"", hex), 0);
 
+               if ((status(bytes) & PKT_APPEND_STATUS_1_CRC_OK) == 0)
+                       throw new AltosCRCException(rssi(bytes));
+
                /* length, data ..., rssi, status, checksum -- 4 bytes extra */
                switch (bytes.length) {
                case AltosLib.ao_telemetry_standard_len + 4:
@@ -108,27 +120,8 @@ public abstract class AltosTelemetry implements AltosStateUpdate {
                return telem;
        }
 
-       public static int extend_height(AltosState state, int height_16) {
-               double  compare_height;
-               int     height = height_16;
-
-               if (state.gps != null && state.gps.alt != AltosLib.MISSING) {
-                       compare_height = state.gps_height();
-               } else {
-                       compare_height = state.height();
-               }
-
-               if (compare_height != AltosLib.MISSING) {
-                       int     high_bits = (int) Math.floor (compare_height / 65536.0);
-
-                       height = (high_bits << 16) | (height_16 & 0xffff);
-
-                       if (Math.abs(height + 65536 - compare_height) < Math.abs(height - compare_height))
-                               height += 65536;
-                       else if (Math.abs(height - 65536 - compare_height) < Math.abs(height - compare_height))
-                               height -= 65536;
-               }
-               return height;
+       public void set_frequency(double frequency) {
+               this.frequency = frequency;
        }
 
        public AltosTelemetry() {