micropeak: Keep reading until we get valid data
[fw/altos] / micropeak / MicroData.java
index 836d3c351983e13b5ecd299d4e67507945b026ee..e786ff1ed136de4e6800feb1263094f38af42a30 100644 (file)
@@ -20,7 +20,8 @@ package org.altusmetrum.micropeak;
 import java.lang.*;
 import java.io.*;
 import java.util.*;
-import org.altusmetrum.AltosLib.*;
+import org.altusmetrum.altoslib_3.*;
+import org.altusmetrum.altosuilib_1.*;
 
 class MicroIterator implements Iterator<MicroDataPoint> {
        int             i;
@@ -56,7 +57,40 @@ class MicroIterable implements Iterable<MicroDataPoint> {
        }
 }
 
-public class MicroData {
+class MicroUIIterator implements Iterator<AltosUIDataPoint> {
+       int             i;
+       MicroData       data;
+
+       public boolean hasNext() {
+               return i < data.pressures.length;
+       }
+
+       public AltosUIDataPoint next() {
+               return new MicroDataPoint(data, i++);
+       }
+
+       public MicroUIIterator (MicroData data) {
+               this.data = data;
+               i = 0;
+       }
+
+       public void remove() {
+       }
+}
+
+class MicroUIIterable implements Iterable<AltosUIDataPoint> {
+       MicroData       data;
+
+       public Iterator<AltosUIDataPoint> iterator() {
+               return new MicroUIIterator(data);
+       }
+
+       public MicroUIIterable(MicroData data) {
+               this.data = data;
+       }
+}
+
+public class MicroData implements AltosUIDataSet {
        public int              ground_pressure;
        public int              min_pressure;
        public int[]            pressures;
@@ -64,15 +98,15 @@ public class MicroData {
        private double          ground_altitude;
        private ArrayList<Integer>      bytes;
        String                  name;
+       MicroStats              stats;
        
-
-       class FileEndedException extends Exception {
+       public class FileEndedException extends Exception {
        }
 
-       class NonHexcharException extends Exception {
+       public class NonHexcharException extends Exception {
        }
 
-       class InvalidCrcException extends Exception {
+       public class InvalidCrcException extends Exception {
        }
 
        private int getc(InputStream f) throws IOException, FileEndedException {
@@ -133,14 +167,10 @@ public class MicroData {
                return h;
        }
 
-       private boolean find_header(InputStream f) throws IOException {
-               try {
-                       for (;;) {
-                               if (get_nonwhite(f) == 'M' && get_nonwhite(f) == 'P')
-                                       return true;
-                       }
-               } catch (FileEndedException fe) {
-                       return false;
+       private boolean find_header(InputStream f) throws IOException, FileEndedException {
+               for (;;) {
+                       if (get_nonwhite(f) == 'M' && get_nonwhite(f) == 'P')
+                               return true;
                }
        } 
 
@@ -178,6 +208,14 @@ public class MicroData {
                return AltosConvert.pressure_to_altitude(pressures[i]);
        }
 
+       public String name() {
+               return name;
+       }
+
+       public Iterable<AltosUIDataPoint> dataPoints() {
+               return new MicroUIIterable(this);
+       }
+
        public Iterable<MicroDataPoint> points() {
                return new MicroIterable(this);
        }
@@ -229,6 +267,18 @@ public class MicroData {
                return altitude(i) - ground_altitude;
        }
 
+       public double apogee_pressure() {
+               return min_pressure;
+       }
+
+       public double apogee_altitude() {
+               return AltosConvert.pressure_to_altitude(apogee_pressure());
+       }
+
+       public double apogee_height() {
+               return apogee_altitude() - ground_altitude;
+       }
+
        static final int speed_avg = 3;
        static final int accel_avg = 5;
 
@@ -263,11 +313,29 @@ public class MicroData {
                f.write('\n');
        }
 
+       public void export (Writer f) throws IOException {
+               PrintWriter     pw = new PrintWriter(f);
+               pw.printf("  Time, Press(Pa), Height(m), Height(f), Speed(m/s), Speed(mph), Speed(mach), Accel(m/s²), Accel(ft/s²),  Accel(g)\n");
+               for (MicroDataPoint point : points()) {
+                       pw.printf("%6.3f,%10.0f,%10.1f,%10.1f,%11.2f,%11.2f,%12.4f,%12.2f,%13.2f,%10.4f\n",
+                                 point.time,
+                                 point.pressure,
+                                 point.height,
+                                 AltosConvert.meters_to_feet(point.height),
+                                 point.speed,
+                                 AltosConvert.meters_to_mph(point.speed),
+                                 AltosConvert.meters_to_mach(point.speed),
+                                 point.accel,
+                                 AltosConvert.meters_to_feet(point.accel),
+                                 AltosConvert.meters_to_g(point.accel));
+               }
+       }
+
        public void set_name(String name) {
                this.name = name;
        }
 
-       public MicroData (InputStream f, String name) throws IOException, InterruptedException {
+       public MicroData (InputStream f, String name) throws IOException, InterruptedException, NonHexcharException, FileEndedException {
                this.name = name;
                bytes = new ArrayList<Integer>();
                if (!find_header(f))
@@ -309,10 +377,9 @@ public class MicroData {
                        crc_valid = crc == current_crc;
 
                        time_step = 0.192;
+                       stats = new MicroStats(this);
                } catch (FileEndedException fe) {
                        throw new IOException("File Ended Unexpectedly");
-               } catch (NonHexcharException ne) {
-                       throw new IOException("Non hexadecimal character found");
                }
        }