2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 import java.util.concurrent.*;
25 public class AltosEepromChunk {
27 static final int chunk_size = 256;
28 static final int per_line = 8;
32 public ParseException parse_exception = null;
34 int[] ParseHex(String line) {
35 String[] tokens = line.split("\\s+");
36 int[] array = new int[tokens.length];
38 for (int i = 0; i < tokens.length; i++)
40 array[i] = Integer.parseInt(tokens[i], 16);
41 } catch (NumberFormatException ne) {
47 int data(int offset) {
51 int data16(int offset) {
52 return data[offset] | (data[offset + 1] << 8);
55 public AltosEepromChunk(AltosSerial serial_line, int block)
56 throws TimeoutException, InterruptedException {
60 data = new int[chunk_size];
61 address = block * chunk_size;
62 serial_line.flush_input();
63 serial_line.printf("e %x\n", block);
65 for (offset = 0; offset < chunk_size; offset += per_line) {
67 String line = serial_line.get_reply(5000);
70 throw new TimeoutException();
72 int[] values = ParseHex(line);
74 if (values == null || values.length != per_line + 1)
75 throw new ParseException(String.format("invalid line %s", line), 0);
76 if (values[0] != offset)
77 throw new ParseException(String.format("data address out of sync at 0x%x",
78 address + offset), 0);
79 for (int i = 0; i < per_line; i++)
80 data[offset + i] = values[1 + i];
81 } catch (ParseException pe) {
82 for (int i = 0; i < per_line; i++)
83 data[offset + i] = 0xff;
84 if (parse_exception == null)