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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altoslib_11;
25 public abstract class AltosEeprom implements AltosStateUpdate {
31 public int data8(int i) {
35 public int data16(int i) {
36 return ((data8[i] | (data8[i+1] << 8)) << 16) >> 16;
39 public int data24(int i) {
40 return data8[i] | (data8[i+1] << 8) | (data8[i+2] << 16);
43 public int data32(int i) {
44 return data8[i] | (data8[i+1] << 8) | (data8[i+2] << 16) | (data8[i+3] << 24);
47 public boolean has_seconds() { return false; }
49 public int seconds() { return 0; }
51 public final static int header_length = 4;
53 public abstract int record_length();
55 public void update_state(AltosState state) {
56 if (cmd == AltosLib.AO_LOG_FLIGHT)
57 state.set_boost_tick(tick);
62 public void write(PrintStream out) {
63 out.printf("%c %04x", cmd, tick);
65 for (int i = 0; i < data8.length; i++)
66 out.printf (" %02x", data8[i]);
71 public String string() {
74 s = String.format("%c %04x", cmd, tick);
76 for (int i = 0; i < data8.length; i++) {
77 String d = String.format(" %02x", data8[i]);
85 void parse_chunk(AltosEepromChunk chunk, int start) throws ParseException {
86 cmd = chunk.data(start);
88 int data_length = record_length() - header_length;
90 valid = !chunk.erased(start, record_length());
92 if (AltosConvert.checksum(chunk.data, start, record_length()) != 0)
93 throw new ParseException(String.format("invalid checksum at 0x%x",
94 chunk.address + start), 0);
96 cmd = AltosLib.AO_LOG_INVALID;
99 tick = chunk.data16(start+2);
101 data8 = new int[data_length];
102 for (int i = 0; i < data_length; i++)
103 data8[i] = chunk.data(start + header_length + i);
106 void parse_string(String line) {
109 cmd = AltosLib.AO_LOG_INVALID;
111 int data_length = record_length() - header_length;
116 String[] tokens = line.split("\\s+");
118 if (tokens[0].length() == 1) {
119 if (tokens.length == 2 + data_length) {
120 cmd = tokens[0].codePointAt(0);
121 tick = Integer.parseInt(tokens[1],16);
123 data8 = new int[data_length];
125 for (int i = 0; i < data_length; i++)
126 data8[i] = Integer.parseInt(tokens[2 + i],16);
129 } catch (NumberFormatException ne) {