2 * Copyright © 2013 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.
18 package org.altusmetrum.altoslib_2;
24 public abstract class AltosEeprom implements AltosStateUpdate {
30 public int data8(int i) {
34 public int data16(int i) {
35 return ((data8[i] | (data8[i+1] << 8)) << 16) >> 16;
38 public int data24(int i) {
39 return data8[i] | (data8[i+1] << 8) | (data8[i+2] << 16);
42 public int data32(int i) {
43 return data8[i] | (data8[i+1] << 8) | (data8[i+2] << 16) | (data8[i+3] << 24);
46 public final static int header_length = 4;
48 public abstract int record_length();
50 public void update_state(AltosState state) {
51 if (cmd == AltosLib.AO_LOG_FLIGHT)
52 state.set_boost_tick(tick);
57 public void write(PrintStream out) {
58 out.printf("%c %04x", cmd, tick);
60 for (int i = 0; i < data8.length; i++)
61 out.printf (" %02x", data8[i]);
66 public String string() {
69 s = String.format("%c %04x", cmd, tick);
71 for (int i = 0; i < data8.length; i++) {
72 String d = String.format(" %02x", data8[i]);
80 void parse_chunk(AltosEepromChunk chunk, int start) throws ParseException {
81 cmd = chunk.data(start);
83 int data_length = record_length() - header_length;
85 valid = !chunk.erased(start, record_length());
87 if (AltosConvert.checksum(chunk.data, start, record_length()) != 0)
88 throw new ParseException(String.format("invalid checksum at 0x%x",
89 chunk.address + start), 0);
91 cmd = AltosLib.AO_LOG_INVALID;
94 tick = chunk.data16(start+2);
96 data8 = new int[data_length];
97 for (int i = 0; i < data_length; i++)
98 data8[i] = chunk.data(start + header_length + i);
101 void parse_string(String line) {
104 cmd = AltosLib.AO_LOG_INVALID;
106 int data_length = record_length() - header_length;
111 String[] tokens = line.split("\\s+");
113 if (tokens[0].length() == 1) {
114 if (tokens.length == 2 + data_length) {
115 cmd = tokens[0].codePointAt(0);
116 tick = Integer.parseInt(tokens[1],16);
118 data8 = new int[data_length];
120 for (int i = 0; i < data_length; i++)
121 data8[i] = Integer.parseInt(tokens[2 + i],16);
124 } catch (NumberFormatException ne) {