X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=altoslib%2FAltosTelemetry.java;h=a19e4226160a7e7d561a8c36a003191b26789419;hp=b84455d34d17236ad09dff2326166a63a4d727f4;hb=3b817a2b854065af23c9ec8e849150e6930f51e9;hpb=f07f6d55edf5b97020680b3ce1d9e00bb3df64a6 diff --git a/altoslib/AltosTelemetry.java b/altoslib/AltosTelemetry.java index b84455d3..a19e4226 100644 --- a/altoslib/AltosTelemetry.java +++ b/altoslib/AltosTelemetry.java @@ -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 @@ -15,7 +16,7 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -package org.altusmetrum.altoslib_1; +package org.altusmetrum.altoslib_13; import java.text.*; @@ -23,17 +24,26 @@ 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 tick; - public int serial; - public int rssi; - public int status; + 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(); + public abstract int tick(); /* 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++) @@ -42,7 +52,15 @@ public abstract class AltosTelemetry implements AltosStateUpdate { return sum == bytes[bytes.length - 1]; } - public void update_state(AltosState state) { + 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); @@ -56,10 +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_MM_sensor = 0x08; - final static int packet_type_MM_data = 0x09; - final static int packet_type_Mini = 0x10; - + final static int packet_type_mega_sensor = 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; + static AltosTelemetry parse_hex(String hex) throws ParseException, AltosCRCException { AltosTelemetry telem = null; @@ -78,46 +99,13 @@ public abstract class AltosTelemetry implements AltosStateUpdate { if (!cksum(bytes)) throw new ParseException(String.format("invalid line \"%s\"", hex), 0); - int rssi = AltosLib.int8(bytes, bytes.length - 3) / 2 - 74; - int status = AltosLib.uint8(bytes, bytes.length - 2); - - if ((status & PKT_APPEND_STATUS_1_CRC_OK) == 0) - throw new AltosCRCException(rssi); + 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: - int type = AltosLib.uint8(bytes, 4 + 1); -/* - switch (type) { - case packet_type_TM_sensor: - case packet_type_Tm_sensor: - case packet_type_Tn_sensor: - telem = new AltosTelemetrySensor(bytes); - break; - case packet_type_configuration: - telem = new AltosTelemetryConfiguration(bytes); - break; - case packet_type_location: - telem = new AltosTelemetryLocation(bytes); - break; - case packet_type_satellite: - telem = new AltosTelemetrySatellite(bytes); - break; - case packet_type_companion: - telem = new AltosTelemetryCompanion(bytes); - break; - case packet_type_MM_sensor: - telem = new AltosTelemetryMegaSensor(bytes); - break; - case packet_type_MM_data: - telem = new AltosTelemetryMegaData(bytes); - break; - default: - telem = new AltosTelemetryRaw(bytes); - break; - } -*/ + telem = AltosTelemetryStandard.parse_hex(bytes); break; case AltosLib.ao_telemetry_0_9_len + 4: telem = new AltosTelemetryLegacy(bytes); @@ -128,14 +116,24 @@ public abstract class AltosTelemetry implements AltosStateUpdate { default: throw new ParseException(String.format("Invalid packet length %d", bytes.length), 0); } - if (telem != null) { - telem.received_time = System.currentTimeMillis(); - telem.rssi = rssi; - telem.status = status; - } return telem; } + public void set_frequency(double frequency) { + this.frequency = frequency; + } + + public AltosTelemetry() { + this.received_time = System.currentTimeMillis(); + } + + public AltosTelemetry(int[] bytes) throws AltosCRCException { + this(); + this.bytes = bytes; + if ((status() & PKT_APPEND_STATUS_1_CRC_OK) == 0) + throw new AltosCRCException(rssi()); + } + public static AltosTelemetry parse(String line) throws ParseException, AltosCRCException { String[] word = line.split("\\s+"); int i =0; @@ -146,7 +144,7 @@ public abstract class AltosTelemetry implements AltosStateUpdate { throw new AltosCRCException(AltosParse.parse_int(word[i++])); } - AltosTelemetry telem; + AltosTelemetry telem = null; if (word[i].equals("TELEM")) { telem = parse_hex(word[i+1]);