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;
22 public class AltosEepromMetrum {
27 public int config_a, config_b;
31 public static final int record_length = 16;
32 static final int header_length = 4;
33 static final int data_length = record_length - header_length;
35 public int record_length() { return record_length; }
37 public int data8(int i) {
41 public int data16(int i) {
42 return ((data8[i] | (data8[i+1] << 8)) << 16) >> 16;
45 public int data32(int i) {
46 return data8[i] | (data8[i+1] << 8) | (data8[i+2] << 16) | (data8[i+3] << 24);
49 /* AO_LOG_FLIGHT elements */
50 public int flight() { return data16(0); }
51 public int ground_accel() { return data16(2); }
52 public int ground_pres() { return data32(4); }
53 public int ground_temp() { return data32(8); }
55 /* AO_LOG_STATE elements */
56 public int state() { return data16(0); }
57 public int reason() { return data16(2); }
59 /* AO_LOG_SENSOR elements */
60 public int pres() { return data32(0); }
61 public int temp() { return data32(4); }
62 public int accel() { return data16(8); }
64 /* AO_LOG_TEMP_VOLT elements */
65 public int v_batt() { return data16(0); }
66 public int sense_a() { return data16(2); }
67 public int sense_m() { return data16(4); }
69 /* AO_LOG_GPS_POS elements */
70 public int latitude() { return data32(0); }
71 public int longitude() { return data32(4); }
72 public int altitude() { return data16(8); }
74 /* AO_LOG_GPS_TIME elements */
75 public int hour() { return data8(0); }
76 public int minute() { return data8(1); }
77 public int second() { return data8(2); }
78 public int flags() { return data8(3); }
79 public int year() { return data8(4); }
80 public int month() { return data8(5); }
81 public int day() { return data8(6); }
83 /* AO_LOG_GPS_SAT elements */
84 public int channels() { return data8(0); }
85 public int more() { return data8(1); }
86 public int svid(int n) { return data8(2 + n * 2); }
87 public int c_n(int n) { return data8(2 + n * 2 + 1); }
89 public AltosEepromMetrum (AltosEepromChunk chunk, int start) throws ParseException {
90 cmd = chunk.data(start);
92 valid = !chunk.erased(start, record_length);
94 if (AltosConvert.checksum(chunk.data, start, record_length) != 0)
95 throw new ParseException(String.format("invalid checksum at 0x%x",
96 chunk.address + start), 0);
98 cmd = AltosLib.AO_LOG_INVALID;
101 tick = chunk.data16(start+2);
103 data8 = new int[data_length];
104 for (int i = 0; i < data_length; i++)
105 data8[i] = chunk.data(start + header_length + i);
108 public AltosEepromMetrum (String line) {
113 cmd = AltosLib.AO_LOG_INVALID;
117 String[] tokens = line.split("\\s+");
119 if (tokens[0].length() == 1) {
120 if (tokens.length != 2 + data_length) {
121 cmd = AltosLib.AO_LOG_INVALID;
124 cmd = tokens[0].codePointAt(0);
125 tick = Integer.parseInt(tokens[1],16);
127 data8 = new int[data_length];
128 for (int i = 0; i < data_length; i++)
129 data8[i] = Integer.parseInt(tokens[2 + i],16);
131 } else if (tokens[0].equals("Config") && tokens[1].equals("version:")) {
132 cmd = AltosLib.AO_LOG_CONFIG_VERSION;
134 } else if (tokens[0].equals("Main") && tokens[1].equals("deploy:")) {
135 cmd = AltosLib.AO_LOG_MAIN_DEPLOY;
136 config_a = Integer.parseInt(tokens[2]);
137 } else if (tokens[0].equals("Apogee") && tokens[1].equals("delay:")) {
138 cmd = AltosLib.AO_LOG_APOGEE_DELAY;
139 config_a = Integer.parseInt(tokens[2]);
140 } else if (tokens[0].equals("Radio") && tokens[1].equals("channel:")) {
141 cmd = AltosLib.AO_LOG_RADIO_CHANNEL;
142 config_a = Integer.parseInt(tokens[2]);
143 } else if (tokens[0].equals("Callsign:")) {
144 cmd = AltosLib.AO_LOG_CALLSIGN;
145 data = tokens[1].replaceAll("\"","");
146 } else if (tokens[0].equals("Accel") && tokens[1].equals("cal")) {
147 cmd = AltosLib.AO_LOG_ACCEL_CAL;
148 config_a = Integer.parseInt(tokens[3]);
149 config_b = Integer.parseInt(tokens[5]);
150 } else if (tokens[0].equals("Radio") && tokens[1].equals("cal:")) {
151 cmd = AltosLib.AO_LOG_RADIO_CAL;
152 config_a = Integer.parseInt(tokens[2]);
153 } else if (tokens[0].equals("Max") && tokens[1].equals("flight") && tokens[2].equals("log:")) {
154 cmd = AltosLib.AO_LOG_MAX_FLIGHT_LOG;
155 config_a = Integer.parseInt(tokens[3]);
156 } else if (tokens[0].equals("manufacturer")) {
157 cmd = AltosLib.AO_LOG_MANUFACTURER;
159 } else if (tokens[0].equals("product")) {
160 cmd = AltosLib.AO_LOG_PRODUCT;
162 } else if (tokens[0].equals("serial-number")) {
163 cmd = AltosLib.AO_LOG_SERIAL_NUMBER;
164 config_a = Integer.parseInt(tokens[1]);
165 } else if (tokens[0].equals("log-format")) {
166 cmd = AltosLib.AO_LOG_LOG_FORMAT;
167 config_a = Integer.parseInt(tokens[1]);
168 } else if (tokens[0].equals("software-version")) {
169 cmd = AltosLib.AO_LOG_SOFTWARE_VERSION;
171 } else if (tokens[0].equals("ms5607")) {
172 if (tokens[1].equals("reserved:")) {
173 cmd = AltosLib.AO_LOG_BARO_RESERVED;
174 config_a = Integer.parseInt(tokens[2]);
175 } else if (tokens[1].equals("sens:")) {
176 cmd = AltosLib.AO_LOG_BARO_SENS;
177 config_a = Integer.parseInt(tokens[2]);
178 } else if (tokens[1].equals("off:")) {
179 cmd = AltosLib.AO_LOG_BARO_OFF;
180 config_a = Integer.parseInt(tokens[2]);
181 } else if (tokens[1].equals("tcs:")) {
182 cmd = AltosLib.AO_LOG_BARO_TCS;
183 config_a = Integer.parseInt(tokens[2]);
184 } else if (tokens[1].equals("tco:")) {
185 cmd = AltosLib.AO_LOG_BARO_TCO;
186 config_a = Integer.parseInt(tokens[2]);
187 } else if (tokens[1].equals("tref:")) {
188 cmd = AltosLib.AO_LOG_BARO_TREF;
189 config_a = Integer.parseInt(tokens[2]);
190 } else if (tokens[1].equals("tempsens:")) {
191 cmd = AltosLib.AO_LOG_BARO_TEMPSENS;
192 config_a = Integer.parseInt(tokens[2]);
193 } else if (tokens[1].equals("crc:")) {
194 cmd = AltosLib.AO_LOG_BARO_CRC;
195 config_a = Integer.parseInt(tokens[2]);
197 cmd = AltosLib.AO_LOG_INVALID;
201 cmd = AltosLib.AO_LOG_INVALID;
204 } catch (NumberFormatException ne) {
205 cmd = AltosLib.AO_LOG_INVALID;
211 public AltosEepromMetrum(int in_cmd, int in_tick) {