altosui: Add support for parsing list of flights from the 'l' command
authorKeith Packard <keithp@keithp.com>
Wed, 12 Jan 2011 05:28:07 +0000 (21:28 -0800)
committerKeith Packard <keithp@keithp.com>
Wed, 12 Jan 2011 05:28:07 +0000 (21:28 -0800)
This adds parsing support to enumerate the available flights, but does
not yet provide any UI to use it.

Signed-off-by: Keith Packard <keithp@keithp.com>
altosui/AltosEepromBlock.java
altosui/AltosEepromDownload.java
altosui/AltosEepromList.java [new file with mode: 0644]
altosui/AltosEepromLog.java [new file with mode: 0644]
altosui/AltosEepromRecord.java
altosui/AltosSerial.java
altosui/Makefile.am

index 0c1a4a9287b6ecd9beb373fc785b08fb6b36cc41..f223f3fb99edd3f14c9ae3a71b50c810f3f10c7e 100644 (file)
@@ -38,6 +38,12 @@ public class AltosEepromBlock extends ArrayList<AltosEepromRecord> {
        int     state;
        boolean has_date;
        int     year, month, day;
+       boolean has_lat;
+       double  lat;
+       boolean has_lon;
+       double  lon;
+       boolean has_time;
+       int     hour, minute, second;
 
        public AltosEepromBlock (AltosSerial serial_line, int block) throws TimeoutException, InterruptedException {
                int     addr;
@@ -46,6 +52,9 @@ public class AltosEepromBlock extends ArrayList<AltosEepromRecord> {
                has_flight = false;
                has_state = false;
                has_date = false;
+               has_lat = false;
+               has_lon = false;
+               has_time = false;
                serial_line.printf("e %x\n", block);
                for (addr = 0; !done && addr < 0x100;) {
                        try {
@@ -70,7 +79,20 @@ public class AltosEepromBlock extends ArrayList<AltosEepromRecord> {
                                        day = (r.b & 0xff);
                                        has_date = true;
                                }
-
+                               if (r.cmd == Altos.AO_LOG_GPS_TIME) {
+                                       hour = (r.a & 0xff);
+                                       minute = (r.a >> 8);
+                                       second = (r.b & 0xff);
+                                       has_time = true;
+                               }
+                               if (r.cmd == Altos.AO_LOG_GPS_LAT) {
+                                       lat = (double) (r.a | (r.b << 16)) / 1e7;
+                                       has_lat = true;
+                               }
+                               if (r.cmd == Altos.AO_LOG_GPS_LON) {
+                                       lon = (double) (r.a | (r.b << 16)) / 1e7;
+                                       has_lon = true;
+                               }
                                if (r.cmd == Altos.AO_LOG_STATE && r.a == Altos.ao_flight_landed)
                                        done = true;
                                add(addr / 8, r);
index 9a748710a3fa5063c91f4316544ee08c786c95cf..ca31fdcf9f6c5c7c614d069c7ba016ce8f1b9478 100644 (file)
@@ -161,11 +161,12 @@ public class AltosEepromDownload implements Runnable {
        int     start_block, end_block;
 
        public void run () {
-               if (remote) {
-                       serial_line.set_radio();
-                       serial_line.printf("p\nE 0\n");
-                       serial_line.flush_input();
-               }
+               try {
+                       new AltosEepromList(serial_line, remote);
+               } catch (Exception ee) { }
+
+               if (remote)
+                       serial_line.start_remote();
 
                try {
                        CaptureLog(start_block, end_block);
@@ -179,7 +180,7 @@ public class AltosEepromDownload implements Runnable {
                                    "Connection Failed");
                }
                if (remote)
-                       serial_line.printf("~");
+                       serial_line.stop_remote();
                monitor.done();
                serial_line.flush_output();
                serial_line.close();
diff --git a/altosui/AltosEepromList.java b/altosui/AltosEepromList.java
new file mode 100644 (file)
index 0000000..ac4a29d
--- /dev/null
@@ -0,0 +1,103 @@
+/*
+ * Copyright © 2011 Keith Packard <keithp@keithp.com>
+ *
+ * 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.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package altosui;
+
+import java.awt.*;
+import java.awt.event.*;
+import javax.swing.*;
+import javax.swing.filechooser.FileNameExtensionFilter;
+import javax.swing.table.*;
+import java.io.*;
+import java.util.*;
+import java.text.*;
+import java.util.prefs.*;
+import java.util.concurrent.*;
+
+import libaltosJNI.*;
+
+class AltosEepromFlight {
+       int     flight;
+       int     start;
+       int     end;
+
+       public AltosEepromFlight(int in_flight, int in_start, int in_end) {
+               flight = in_flight;
+               start = in_start;
+               end = in_end;
+       }
+}
+
+public class AltosEepromList extends ArrayList<AltosEepromLog> {
+       AltosConfigData config_data;
+
+       public AltosEepromList (AltosSerial serial_line, boolean remote) throws IOException, InterruptedException, TimeoutException {
+               try {
+                       if (remote)
+                               serial_line.start_remote();
+                       config_data = new AltosConfigData (serial_line);
+                       if (config_data.serial == 0)
+                               throw new IOException("no serial number found");
+
+                       ArrayList<AltosEepromFlight> flights = new ArrayList<AltosEepromFlight>();
+                       if (config_data.flight_log_max != 0) {
+                               serial_line.printf("l\n");
+                               for (;;) {
+                                       String line = serial_line.get_reply(5000);
+                                       if (line == null)
+                                               throw new TimeoutException();
+                                       if (line.contains("done"))
+                                               break;
+                                       if (line.contains("Syntax"))
+                                               continue;
+                                       String[] tokens = line.split("\\s+");
+                                       System.out.printf("got line %s (%d tokens)\n", line, tokens.length);
+                                       if (tokens.length < 6)
+                                               break;
+
+                                       int     flight = -1, start = -1, end = -1;
+                                       try {
+                                               if (tokens[0].equals("flight"))
+                                                       flight = AltosParse.parse_int(tokens[1]);
+                                               if (tokens[2].equals("start"))
+                                                       start = AltosParse.parse_hex(tokens[3]);
+                                               if (tokens[4].equals("end"))
+                                                       end = AltosParse.parse_hex(tokens[5]);
+                                               System.out.printf("parsed flight %d %x %x\n", flight, start, end);
+                                               if (flight > 0 && start >= 0 && end > 0)
+                                                       flights.add(new AltosEepromFlight(flight, start, end));
+                                       } catch (ParseException pe) { System.out.printf("Parse error %s\n", pe.toString()); }
+                               }
+                       } else {
+                               flights.add(new AltosEepromFlight(0, 0, 0xfff));
+                       }
+                       for (AltosEepromFlight flight : flights) {
+                               System.out.printf("Scanning flight %d %x %x\n", flight.flight, flight.start, flight.end);
+                               add(new AltosEepromLog(serial_line, config_data.serial,
+                                                      flight.start, flight.end));
+                       }
+               } finally {
+                       if (remote)
+                               serial_line.stop_remote();
+                       serial_line.flush_output();
+               }
+               for (int i = 0; i < size(); i++) {
+                       AltosEepromLog  l = get(i);
+                       System.out.printf("Found flight %d at %x - %x\n", l.flight, l.start_block, l.end_block);
+               }
+       }
+}
\ No newline at end of file
diff --git a/altosui/AltosEepromLog.java b/altosui/AltosEepromLog.java
new file mode 100644 (file)
index 0000000..36289b4
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * Copyright © 2011 Keith Packard <keithp@keithp.com>
+ *
+ * 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.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package altosui;
+
+import java.awt.*;
+import java.awt.event.*;
+import javax.swing.*;
+import javax.swing.filechooser.FileNameExtensionFilter;
+import javax.swing.table.*;
+import java.io.*;
+import java.util.*;
+import java.text.*;
+import java.util.prefs.*;
+import java.util.concurrent.*;
+
+import libaltosJNI.*;
+
+public class AltosEepromLog {
+       int             serial;
+       boolean         has_flight;
+       int             flight;
+       int             start_block;
+       int             end_block;
+
+       boolean         has_gps;
+       int             year, month, day;
+       int             hour, minute, second;
+       double          lat, lon;
+
+       public AltosEepromLog(AltosSerial serial_line, int in_serial,
+                             int in_start_block, int in_end_block)
+               throws InterruptedException, TimeoutException {
+
+               int             block;
+               boolean         has_date = false, has_time = false, has_lat = false, has_lon = false;
+
+               start_block = in_start_block;
+               end_block = in_end_block;
+               serial = in_serial;
+
+               if (in_end_block > in_start_block + 2)
+                       in_end_block = in_start_block + 2;
+
+               for (block = in_start_block; block < in_end_block; block++) {
+                       AltosEepromBlock eeblock = new AltosEepromBlock(serial_line, block);
+                       if (eeblock.has_flight) {
+                               flight = eeblock.flight;
+                               has_flight = true;
+                       }
+                       if (eeblock.has_date) {
+                               year = eeblock.year;
+                               month = eeblock.month;
+                               day = eeblock.day;
+                               has_date = true;
+                       }
+                       if (eeblock.has_time) {
+                               hour = eeblock.hour;
+                               minute = eeblock.minute;
+                               second = eeblock.second;
+                               has_time = true;
+                       }
+                       if (eeblock.has_lat) {
+                               lat = eeblock.lat;
+                               has_lat = true;
+                       }
+                       if (eeblock.has_lon) {
+                               lon = eeblock.lon;
+                               has_lon = true;
+                       }
+                       if (has_date && has_time && has_lat && has_lon)
+                               has_gps = true;
+                       if (has_gps && has_flight)
+                               break;
+               }
+               System.out.printf("Serial %d start block %d end block %d\n",
+                                 serial, start_block, end_block);
+               if (has_flight)
+                       System.out.printf("Flight %d\n", flight);
+               if (has_gps)
+                       System.out.printf("%d-%d-%d %d:%02d:%02d Lat %f Lon %f\n",
+                                         year, month, day, hour, minute, second, lat, lon);
+       }
+}
index e61a7159f19af1e4673b80ed560623ebeb7c3a47..584a04b7c6c053e9a261f1368a603bbb1790c844 100644 (file)
@@ -65,8 +65,10 @@ public class AltosEepromRecord {
                        throw new TimeoutException();
                int[] values = ParseHex(line);
 
-               if (values == null)
-                       throw new ParseException(String.format("invalid line %s", line), 0);
+               if (values == null || values.length < 9) {
+                       System.out.printf("invalid line %s", line);
+                       throw new ParseException(String.format("inalid line %s", line), 0);
+               }
                if (values[0] != (addr & 0xff))
                        throw new ParseException(String.format("data address out of sync at 0x%x",
                                                               addr), 0);
index b19143e58b7080070457d606a93e8798deaf9a71..6dce6f3d284c3b2326f6990a561dbe80c153a217 100644 (file)
@@ -242,6 +242,16 @@ public class AltosSerial implements Runnable {
                }
        }
 
+       public void start_remote() {
+               set_radio();
+               printf("p\nE 0\n");
+               flush_input();
+       }
+
+       public void stop_remote() {
+               printf ("~");
+       }
+
        public AltosSerial(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
                device = in_device;
                line = "";
index 8cdd64cc704d89e821df3584f504e17a7cae0f11..58d237872a9b0d6ee25d193f6b9e86cd77f2633a 100644 (file)
@@ -29,6 +29,8 @@ altosui_JAVA = \
        AltosDisplayThread.java \
        AltosEepromBlock.java \
        AltosEepromDownload.java \
+       AltosEepromList.java \
+       AltosEepromLog.java \
        AltosEepromMonitor.java \
        AltosEepromIterable.java \
        AltosEepromRecord.java \