micropeak: Add support for MicroPeak v2.0
authorKeith Packard <keithp@keithp.com>
Sat, 8 Aug 2020 03:20:59 +0000 (20:20 -0700)
committerKeith Packard <keithp@keithp.com>
Sat, 8 Aug 2020 03:30:11 +0000 (20:30 -0700)
MicroPeak v2.0 sends a 'unique id' with the log data; capture that and
included it in the file name.

MicroPeak v2.0 can be directly connected to the host using the µP I/O
board. Handle that case by sending the 'l' command which will cause
the device to dump the log.

Signed-off-by: Keith Packard <keithp@keithp.com>
micropeak/MicroData.java
micropeak/MicroDownload.java
micropeak/MicroFile.java
micropeak/MicroSave.java
micropeak/MicroSerial.java

index 4a795dffa2337edd97c2dc8a8c3f9bd0b0671a55..359ccd7aea592bced582a440e87d8f3f6d66a02c 100644 (file)
@@ -34,8 +34,10 @@ public class MicroData {
 
        private double          time_step;
        private ArrayList<Integer>      bytes;
+       public int              nsamples;
        public int              log_id;
        String                  name;
+       String                  unique_id;
 
        public static final int LOG_ID_MICROPEAK = 0;
        public static final int LOG_ID_MICROKITE = 1;
@@ -134,6 +136,21 @@ public class MicroData {
                return v;
        }
 
+       private String get_line(InputStream f) throws IOException, FileEndedException, NonHexcharException {
+               int c;
+               StringBuffer line = new StringBuffer();
+
+               do {
+                       c = f.read();
+               } while (Character.isWhitespace(c));
+
+               do {
+                       line.append((char) c);
+                       c = f.read();
+               } while (!Character.isWhitespace(c));
+               return new String(line);
+       }
+
        private int swap16(int i) {
                return ((i << 8) & 0xff00) | ((i >> 8) & 0xff);
        }
@@ -277,10 +294,14 @@ public class MicroData {
                        file_crc = 0xffff;
                        ground_pressure = get_32(f);
                        min_pressure = get_32(f);
-                       int nsamples = get_16(f);
+                       nsamples = get_16(f);
 
                        log_id = nsamples >> 12;
                        nsamples &= 0xfff;
+                       if (log_id == LOG_ID_MICROPEAK2) {
+                               int nsamples_high = get_16(f);
+                               nsamples |= (nsamples_high << 12);
+                       }
 
                        cal_data.set_ground_pressure(ground_pressure);
 
@@ -328,6 +349,15 @@ public class MicroData {
                                flight_series.set_pressure(cur);
                        }
 
+                       int current_crc = swap16(~file_crc & 0xffff);
+                       int crc = get_16(f);
+
+                       crc_valid = crc == current_crc;
+
+                       if (log_id == LOG_ID_MICROPEAK2) {
+                               unique_id = get_line(f);
+                       }
+
                        flight_series.finish();
 
                        /* Build states */
@@ -335,8 +365,10 @@ public class MicroData {
                        flight_series.set_time(0);
                        flight_series.set_state(AltosLib.ao_flight_boost);
 
-                       flight_series.set_time(flight_series.speed_series.max().time);
-                       flight_series.set_state(AltosLib.ao_flight_coast);
+                       if (flight_series.speed_series != null && flight_series.speed_series.max() != null) {
+                               flight_series.set_time(flight_series.speed_series.max().time);
+                               flight_series.set_state(AltosLib.ao_flight_coast);
+                       }
 
                        flight_series.set_time(flight_series.height_series.max().time);
                        flight_series.set_state(AltosLib.ao_flight_drogue);
@@ -351,10 +383,6 @@ public class MicroData {
 
                        flight_stats = new AltosFlightStats(flight_series);
 
-                       int current_crc = swap16(~file_crc & 0xffff);
-                       int crc = get_16(f);
-
-                       crc_valid = crc == current_crc;
 
                } catch (FileEndedException fe) {
                        throw new IOException("File Ended Unexpectedly");
index be5a8568c0b23947d6aa6f4f28c80a6d160482ac..33c5587b9c2217c45657fae4bc924efaaab93159 100644 (file)
@@ -158,6 +158,7 @@ public class MicroDownload extends AltosUIDialog implements Runnable, ActionList
                                } catch (MicroData.NonHexcharException nhe) {
                                }
                        }
+                       write_thread.join();
                } catch (FileNotFoundException fe) {
                } catch (IOException ioe) {
                } catch (InterruptedException ie) {
@@ -168,6 +169,25 @@ public class MicroDownload extends AltosUIDialog implements Runnable, ActionList
        }
 
        Thread  serial_thread;
+       Thread  write_thread;
+
+       public class SerialWriter implements Runnable {
+               MicroSerial serial;
+
+               public void run () {
+                       try {
+                               Thread.sleep(100);
+                               serial.write('l');
+                               serial.write('\n');
+                               serial.flush();
+                       } catch (InterruptedException ie) {
+                       }
+               }
+
+               public SerialWriter(MicroSerial serial) {
+                       this.serial = serial;
+               }
+       }
 
        public void start() {
                try {
@@ -178,6 +198,10 @@ public class MicroDownload extends AltosUIDialog implements Runnable, ActionList
                }
                serial_thread = new Thread(this);
                serial_thread.start();
+
+               SerialWriter writer = new SerialWriter(serial);
+               write_thread = new Thread(writer);
+               write_thread.start();
        }
 
        public void actionPerformed(ActionEvent ae) {
index cea41c41374d8690af1649b8fcbc3678850f6cd3..f75cd1c3cf30156e05e00f04ac5ec6c9139d9d4b 100644 (file)
@@ -25,22 +25,29 @@ import org.altusmetrum.altosuilib_14.*;
 
 public class MicroFile {
 
-       public static File make(File directory, int year, int month, int day) {
+       public static File make(MicroData data, File directory, int year, int month, int day) {
+               String unique = "";
+               if (data != null && data.unique_id != null)
+                       unique = String.format("-%s", data.unique_id);
                for (int sequence = 1;; sequence++) {
-                       String s = String.format("%04d-%02d-%02d-flight-%03d.mpd",
-                                                year, month, day, sequence);
+                       String s = String.format("%04d-%02d-%02d%s-flight-%03d.mpd",
+                                                year, month, day, unique, sequence);
                        File file = new File(directory, s);
                        if (!file.exists())
                                return file;
                }
        }
 
-       public static File make(File directory) {
+       public static File make(MicroData data, File directory) {
                Calendar        cal = Calendar.getInstance();
-               return make(directory, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH));
+               return make(data, directory, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH));
+       }
+
+       public static File make(MicroData data) {
+               return make(data, AltosUIPreferences.logdir());
        }
 
        public static File make() {
-               return make(AltosUIPreferences.logdir());
+               return make(null);
        }
-}
\ No newline at end of file
+}
index b45cd834d3a2c1c1ae557323b8aced57bc9b9020..f6e2ed2a2e1743e67eb38cfcb9b7a9e2ed2ad4fb 100644 (file)
@@ -72,7 +72,7 @@ public class MicroSave extends JFileChooser {
                                                                      JOptionPane.YES_NO_OPTION);
                                if (r != JOptionPane.YES_OPTION)
                                        continue;
-                                                             
+
                                if (!file.canWrite()) {
                                        JOptionPane.showMessageDialog(frame,
                                                                      String.format("\"%s\" is not writable",
@@ -104,6 +104,6 @@ public class MicroSave extends JFileChooser {
                setFileFilter(new FileNameExtensionFilter("MicroPeak data file",
                                                          "mpd"));
                setCurrentDirectory(AltosUIPreferences.last_logdir());
-               setSelectedFile(MicroFile.make());
+               setSelectedFile(MicroFile.make(data));
        }
 }
index 57fb30db90766fbb06d85e08b382ebaee4846dc3..c688242874837c2c60c9093191e3d847e920867a 100644 (file)
@@ -28,6 +28,8 @@ public class MicroSerial extends InputStream {
        private MicroSerialLog  log;
 
        public int read() {
+               if (file == null)
+                       return -1;
                int     c = libaltos.altos_getchar(file, 0);
                if (Thread.interrupted())
                        return -1;
@@ -40,6 +42,14 @@ public class MicroSerial extends InputStream {
                return c;
        }
 
+       public void write(char c) {
+               libaltos.altos_putchar(file, c);
+       }
+
+       public void flush() {
+               libaltos.altos_flush(file);
+       }
+
        public void close() {
                if (file != null) {
                        libaltos.altos_close(file);