altosui: Clean up eeprom parsing a bit
authorKeith Packard <keithp@keithp.com>
Sun, 14 Aug 2011 01:36:18 +0000 (18:36 -0700)
committerKeith Packard <keithp@keithp.com>
Sun, 14 Aug 2011 01:46:12 +0000 (18:46 -0700)
Export basic parsing and checksum functions for shared use.
Create 'erased' function to check a chunk of eeprom data for data.

Signed-off-by: Keith Packard <keithp@keithp.com>
altosui/AltosConvert.java
altosui/AltosEepromChunk.java
altosui/AltosEepromRecord.java

index c2ae9a5..207470a 100644 (file)
@@ -220,4 +220,24 @@ public class AltosConvert {
        static double radio_channel_to_frequency(int channel) {
                return 434.550 + channel * 0.100;
        }
+
+       static int[] ParseHex(String line) {
+               String[] tokens = line.split("\\s+");
+               int[] array = new int[tokens.length];
+
+               for (int i = 0; i < tokens.length; i++)
+                       try {
+                               array[i] = Integer.parseInt(tokens[i], 16);
+                       } catch (NumberFormatException ne) {
+                               return null;
+                       }
+               return array;
+       }
+
+       static int checksum(int[] data, int start, int length) {
+               int     csum = 0x5a;
+               for (int i = 0; i < length; i++)
+                       csum += data[i + start];
+               return csum & 0xff;
+       }
 }
index 1e42077..fb632a3 100644 (file)
@@ -52,6 +52,13 @@ public class AltosEepromChunk {
                return data[offset] | (data[offset + 1] << 8);
        }
 
+       boolean erased(int start, int len) {
+               for (int i = 0; i < len; i++)
+                       if (data[start+i] != 0xff)
+                               return false;
+               return true;
+       }
+
        public AltosEepromChunk(AltosSerial serial_line, int block)
                throws TimeoutException, InterruptedException {
 
index c0f9703..d8a0795 100644 (file)
@@ -40,42 +40,18 @@ public class AltosEepromRecord {
 
        static final int        record_length = 8;
 
-       int[] ParseHex(String line) {
-               String[] tokens = line.split("\\s+");
-               int[] array = new int[tokens.length];
-
-               for (int i = 0; i < tokens.length; i++)
-                       try {
-                               array[i] = Integer.parseInt(tokens[i], 16);
-                       } catch (NumberFormatException ne) {
-                               return null;
-                       }
-               return array;
-       }
-
-       int checksum(int[] data, int start) {
-               int     csum = 0x5a;
-               for (int i = 0; i < record_length; i++)
-                       csum += data[i + start];
-               return csum & 0xff;
-       }
-
        public AltosEepromRecord (AltosEepromChunk chunk, int start) throws ParseException {
 
                cmd = chunk.data(start);
                tick_valid = true;
 
-               int i;
-               for (i = 0; i < record_length; i++)
-                       if (chunk.data[start + i] != 0xff)
-                               break;
-               if (i != 8) {
-                       if (checksum(chunk.data, start) != 0)
+               tick_valid = !chunk.erased(start, record_length);
+               if (tick_valid) {
+                       if (AltosConvert.checksum(chunk.data, start, record_length) != 0)
                                throw new ParseException(String.format("invalid checksum at 0x%x",
                                                                       chunk.address + start), 0);
                } else {
                        cmd = Altos.AO_LOG_INVALID;
-                       tick_valid = false;
                }
 
                tick = chunk.data16(start + 2);