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.
18 package org.altusmetrum.AltosLib;
23 import java.util.prefs.*;
24 import java.util.concurrent.*;
26 public class AltosEepromMega {
35 public static final int record_length = 32;
36 static final int header_length = 4;
37 static final int data_length = record_length - header_length;
39 public int data8(int i) {
43 public int data16(int i) {
44 return ((data8[i] | (data8[i+1] << 8)) << 16) >> 16;
47 public int data32(int i) {
48 return data8[i] | (data8[i+1] << 8) | (data8[i+2] << 16) | (data8[i+3] << 24);
51 /* AO_LOG_FLIGHT elements */
52 public int flight() { return data16(0); }
53 public int ground_accel() { return data16(2); }
54 public int ground_pres() { return data32(4); }
55 public int ground_temp() { return data32(8); }
57 /* AO_LOG_STATE elements */
58 public int state() { return data16(0); }
59 public int reason() { return data16(2); }
61 /* AO_LOG_SENSOR elements */
62 public int pres() { return data32(0); }
63 public int temp() { return data32(4); }
64 public int accel_x() { return data16(8); }
65 public int accel_y() { return data16(10); }
66 public int accel_z() { return data16(12); }
67 public int gyro_x() { return data16(14); }
68 public int gyro_y() { return data16(16); }
69 public int gyro_z() { return data16(18); }
70 public int mag_x() { return data16(20); }
71 public int mag_y() { return data16(22); }
72 public int mag_z() { return data16(24); }
80 /* AO_LOG_VOLT elements */
81 public int v_batt() { return data16(0); }
82 public int v_pbatt() { return data16(2); }
83 public int nsense() { return data16(4); }
84 public int sense(int i) { return data16(6 + i * 2); }
86 public AltosEepromMega (AltosEepromChunk chunk, int start) throws ParseException {
87 cmd = chunk.data(start);
89 valid = !chunk.erased(start, record_length);
91 if (AltosConvert.checksum(chunk.data, start, record_length) != 0)
92 throw new ParseException(String.format("invalid checksum at 0x%x",
93 chunk.address + start), 0);
95 cmd = AltosLib.AO_LOG_INVALID;
98 tick = chunk.data16(start+2);
100 data8 = new int[data_length];
101 for (int i = 0; i < data_length; i++)
102 data8[i] = chunk.data(start + header_length + i);
105 public AltosEepromMega (String line) {
110 cmd = AltosLib.AO_LOG_INVALID;
114 String[] tokens = line.split("\\s+");
116 if (tokens[0].length() == 1) {
117 if (tokens.length != 2 + data_length) {
118 cmd = AltosLib.AO_LOG_INVALID;
121 cmd = tokens[0].codePointAt(0);
122 tick = Integer.parseInt(tokens[1],16);
124 data8 = new int[data_length];
125 for (int i = 0; i < data_length; i++)
126 data8[i] = Integer.parseInt(tokens[2 + i],16);
128 } else if (tokens[0].equals("Config") && tokens[1].equals("version:")) {
129 cmd = AltosLib.AO_LOG_CONFIG_VERSION;
131 } else if (tokens[0].equals("Main") && tokens[1].equals("deploy:")) {
132 cmd = AltosLib.AO_LOG_MAIN_DEPLOY;
133 a = Integer.parseInt(tokens[2]);
134 } else if (tokens[0].equals("Apogee") && tokens[1].equals("delay:")) {
135 cmd = AltosLib.AO_LOG_APOGEE_DELAY;
136 a = Integer.parseInt(tokens[2]);
137 } else if (tokens[0].equals("Radio") && tokens[1].equals("channel:")) {
138 cmd = AltosLib.AO_LOG_RADIO_CHANNEL;
139 a = Integer.parseInt(tokens[2]);
140 } else if (tokens[0].equals("Callsign:")) {
141 cmd = AltosLib.AO_LOG_CALLSIGN;
142 data = tokens[1].replaceAll("\"","");
143 } else if (tokens[0].equals("Accel") && tokens[1].equals("cal")) {
144 cmd = AltosLib.AO_LOG_ACCEL_CAL;
145 a = Integer.parseInt(tokens[3]);
146 b = Integer.parseInt(tokens[5]);
147 } else if (tokens[0].equals("Radio") && tokens[1].equals("cal:")) {
148 cmd = AltosLib.AO_LOG_RADIO_CAL;
149 a = Integer.parseInt(tokens[2]);
150 } else if (tokens[0].equals("Max") && tokens[1].equals("flight") && tokens[2].equals("log:")) {
151 cmd = AltosLib.AO_LOG_MAX_FLIGHT_LOG;
152 a = Integer.parseInt(tokens[3]);
153 } else if (tokens[0].equals("manufacturer")) {
154 cmd = AltosLib.AO_LOG_MANUFACTURER;
156 } else if (tokens[0].equals("product")) {
157 cmd = AltosLib.AO_LOG_PRODUCT;
159 } else if (tokens[0].equals("serial-number")) {
160 cmd = AltosLib.AO_LOG_SERIAL_NUMBER;
161 a = Integer.parseInt(tokens[1]);
162 } else if (tokens[0].equals("log-format")) {
163 cmd = AltosLib.AO_LOG_LOG_FORMAT;
164 a = Integer.parseInt(tokens[1]);
165 } else if (tokens[0].equals("software-version")) {
166 cmd = AltosLib.AO_LOG_SOFTWARE_VERSION;
168 } else if (tokens[0].equals("ms5607")) {
169 if (tokens[1].equals("reserved:")) {
170 cmd = AltosLib.AO_LOG_BARO_RESERVED;
171 a = Integer.parseInt(tokens[2]);
172 } else if (tokens[1].equals("sens:")) {
173 cmd = AltosLib.AO_LOG_BARO_SENS;
174 a = Integer.parseInt(tokens[2]);
175 } else if (tokens[1].equals("off:")) {
176 cmd = AltosLib.AO_LOG_BARO_OFF;
177 a = Integer.parseInt(tokens[2]);
178 } else if (tokens[1].equals("tcs:")) {
179 cmd = AltosLib.AO_LOG_BARO_TCS;
180 a = Integer.parseInt(tokens[2]);
181 } else if (tokens[1].equals("tco:")) {
182 cmd = AltosLib.AO_LOG_BARO_TCO;
183 a = Integer.parseInt(tokens[2]);
184 } else if (tokens[1].equals("tref:")) {
185 cmd = AltosLib.AO_LOG_BARO_TREF;
186 a = Integer.parseInt(tokens[2]);
187 } else if (tokens[1].equals("tempsens:")) {
188 cmd = AltosLib.AO_LOG_BARO_TEMPSENS;
189 a = Integer.parseInt(tokens[2]);
190 } else if (tokens[1].equals("crc:")) {
191 cmd = AltosLib.AO_LOG_BARO_CRC;
192 a = Integer.parseInt(tokens[2]);
194 cmd = AltosLib.AO_LOG_INVALID;
198 cmd = AltosLib.AO_LOG_INVALID;
201 } catch (NumberFormatException ne) {
202 cmd = AltosLib.AO_LOG_INVALID;
208 public AltosEepromMega(int in_cmd, int in_tick) {