summaryrefslogtreecommitdiff
path: root/altoslib/AltosEeprom.java
blob: 57ee73adb87bfdb4b52dcf9402550c7b5c9611a6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright © 2013 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

package org.altusmetrum.altoslib_3;

import java.io.*;
import java.util.*;
import java.text.*;

public abstract class AltosEeprom implements AltosStateUpdate {
	public int	cmd;
	public int	tick;
	public int	data8[];
	public boolean	valid;

	public int data8(int i) {
		return data8[i];
	}

	public int data16(int i) {
		return ((data8[i] | (data8[i+1] << 8)) << 16) >> 16;
	}

	public int data24(int i) {
		return data8[i] | (data8[i+1] << 8) | (data8[i+2] << 16);
	}

	public int data32(int i) {
		return data8[i] | (data8[i+1] << 8) | (data8[i+2] << 16) | (data8[i+3] << 24);
	}

	public final static int header_length = 4;

	public abstract int record_length();

	public void update_state(AltosState state) {
		if (cmd == AltosLib.AO_LOG_FLIGHT)
			state.set_boost_tick(tick);
		else
			state.set_tick(tick);
	}

	public void write(PrintStream out) {
		out.printf("%c %04x", cmd, tick);
		if (data8 != null) {
			for (int i = 0; i < data8.length; i++)
				out.printf (" %02x", data8[i]);
		}
		out.printf ("\n");
	}

	public String string() {
		String	s;

		s = String.format("%c %04x", cmd, tick);
		if (data8 != null) {
			for (int i = 0; i < data8.length; i++) {
				String	d = String.format(" %02x", data8[i]);
				s = s.concat(d);
			}
		}
		s = s.concat("\n");
		return s;
	}

	void parse_chunk(AltosEepromChunk chunk, int start) throws ParseException {
		cmd = chunk.data(start);

		int data_length = record_length() - header_length;

		valid = !chunk.erased(start, record_length());
		if (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 = AltosLib.AO_LOG_INVALID;
		}

		tick = chunk.data16(start+2);

		data8 = new int[data_length];
		for (int i = 0; i < data_length; i++)
			data8[i] = chunk.data(start + header_length + i);
	}

	void parse_string(String line) {
		valid = false;
		tick = 0;
		cmd = AltosLib.AO_LOG_INVALID;

		int data_length = record_length() - header_length;

		if (line == null)
			return;
		try {
			String[] tokens = line.split("\\s+");

			if (tokens[0].length() == 1) {
				if (tokens.length == 2 + data_length) {
					cmd = tokens[0].codePointAt(0);
					tick = Integer.parseInt(tokens[1],16);
					valid = true;
					data8 = new int[data_length];

					for (int i = 0; i < data_length; i++)
						data8[i] = Integer.parseInt(tokens[2 + i],16);
				}
			}
		} catch (NumberFormatException ne) {
		}
	}
}