From 5a3e96bef31959a287b8696778d7d8cf911a7dc4 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 13 Aug 2011 18:36:18 -0700 Subject: [PATCH] altosui: Clean up eeprom parsing a bit 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 --- altosui/AltosConvert.java | 20 ++++++++++++++++++++ altosui/AltosEepromChunk.java | 7 +++++++ altosui/AltosEepromRecord.java | 30 +++--------------------------- 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/altosui/AltosConvert.java b/altosui/AltosConvert.java index c2ae9a50..207470a5 100644 --- a/altosui/AltosConvert.java +++ b/altosui/AltosConvert.java @@ -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; + } } diff --git a/altosui/AltosEepromChunk.java b/altosui/AltosEepromChunk.java index 1e42077f..fb632a3f 100644 --- a/altosui/AltosEepromChunk.java +++ b/altosui/AltosEepromChunk.java @@ -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 { diff --git a/altosui/AltosEepromRecord.java b/altosui/AltosEepromRecord.java index c0f97035..d8a07951 100644 --- a/altosui/AltosEepromRecord.java +++ b/altosui/AltosEepromRecord.java @@ -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); -- 2.30.2