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 c2ae9a507897079970036ebef8cca8505b306e66..207470a5eb5ef6016341dc0fd0d8986db65c51ab 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 1e42077ff01f5168593ca3df415cedf34b12540a..fb632a3f50fd6f8b9b8964ae3cefb33221d47eef 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 c0f970354dad3d7f1dcb6d31d6cc2754820b0c2f..d8a07951a72369f7a73fa7cecd45565bb3c51476 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);