altoslib: Handle eeprom tick wrapping
[fw/altos] / altoslib / AltosEepromRecord.java
index efcca857d9e17ba7a1429c9a48a82a64ad30de4a..75fe5381f51b1c9e57de5b85cc846969b55927d5 100644 (file)
@@ -14,7 +14,6 @@
 
 package org.altusmetrum.altoslib_11;
 
-
 public abstract class AltosEepromRecord implements Comparable<AltosEepromRecord> {
 
        AltosEepromNew          eeprom;
@@ -51,8 +50,12 @@ public abstract class AltosEepromRecord implements Comparable<AltosEepromRecord>
                return data8(i) | (data8(i+1) << 8) | (data8(i+2) << 16) | (data8(i+3) << 24);
        }
 
+       public boolean valid(int s) {
+               return AltosConvert.checksum(eeprom.data, s, length) == 0;
+       }
+
        public boolean valid() {
-               return AltosConvert.checksum(eeprom.data, start, length) == 0;
+               return valid(start);
        }
 
        private int cmdi() {
@@ -61,28 +64,53 @@ public abstract class AltosEepromRecord implements Comparable<AltosEepromRecord>
                return 1;
        }
 
+       public AltosConfigData config_data() {
+               return eeprom.config_data();
+       }
+
        public int compareTo(AltosEepromRecord o) {
                int     cmd_diff = cmdi() - o.cmdi();
 
                if (cmd_diff != 0)
                        return cmd_diff;
 
-               int     tick_diff = tick() - o.tick();
+               int     tick_diff = wide_tick - o.wide_tick;
 
                if (tick_diff != 0)
                        return tick_diff;
                return start - o.start;
        }
 
-       public void update_state(AltosState state) {
+       /* AltosDataProvider */
+       public void provide_data(AltosDataListener listener, AltosCalData cal_data) {
+               cal_data.set_tick(tick());
                if (cmd() == AltosLib.AO_LOG_FLIGHT)
-                       state.set_boost_tick(tick());
-               else
-                       state.set_tick(tick());
+                       cal_data.set_boost_tick();
+               listener.set_time(cal_data.time());
+
+               /* Flush any pending GPS changes */
+               if (!AltosLib.is_gps_cmd(cmd())) {
+                       AltosGPS gps = cal_data.temp_gps();
+                       if (gps != null) {
+                               listener.set_gps(gps);
+                               cal_data.reset_temp_gps();
+                       }
+               }
+       }
+
+       public int next_start() {
+               int     s = start + length;
+
+               while (s + length <= eeprom.data.size()) {
+                       if (valid(s))
+                               return s;
+                       s += length;
+               }
+               return -1;
        }
 
        public boolean hasNext() {
-               return start + length * 2 < eeprom.data.size();
+               return next_start() >= 0;
        }
 
        public abstract AltosEepromRecord next();
@@ -91,5 +119,8 @@ public abstract class AltosEepromRecord implements Comparable<AltosEepromRecord>
                this.eeprom = eeprom;
                this.start = start;
                this.length = length;
+
+               while (start + length < eeprom.data.size() && !valid())
+                       start += length;
        }
 }