summaryrefslogtreecommitdiff
path: root/altoslib/AltosEepromHeader.java
blob: 6ce7ddd38516ecd3de0db752801e77b76b1202dc (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 * 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 class AltosEepromHeader extends AltosEeprom {

	public int	cmd;
	public String	data;
	public int	config_a, config_b;
	public boolean	last;
	public boolean	valid;

	public int record_length () { return 0; }

	/* XXX pull rest of config data to state */
	public void update_state(AltosState state) {
		switch (cmd) {
		case AltosLib.AO_LOG_CONFIG_VERSION:
			break;
		case AltosLib.AO_LOG_MAIN_DEPLOY:
			break;
		case AltosLib.AO_LOG_APOGEE_DELAY:
			break;
		case AltosLib.AO_LOG_RADIO_CHANNEL:
			break;
		case AltosLib.AO_LOG_CALLSIGN:
			state.set_callsign(data);
			break;
		case AltosLib.AO_LOG_ACCEL_CAL:
			state.set_accel_g(config_a, config_b);
			break;
		case AltosLib.AO_LOG_RADIO_CAL:
			break;
		case AltosLib.AO_LOG_MANUFACTURER:
			break;
		case AltosLib.AO_LOG_PRODUCT:
			break;
		case AltosLib.AO_LOG_LOG_FORMAT:
			state.log_format = config_a;
			break;
		case AltosLib.AO_LOG_SERIAL_NUMBER:
			state.set_serial(config_a);
			break;
		case AltosLib.AO_LOG_BARO_RESERVED:
			state.make_baro();
			state.baro.reserved = config_a;
			break;
		case AltosLib.AO_LOG_BARO_SENS:
			state.make_baro();
			state.baro.sens = config_a;
			break;
		case AltosLib.AO_LOG_BARO_OFF:
			state.make_baro();
			state.baro.off = config_a;
			break;
		case AltosLib.AO_LOG_BARO_TCS:
			state.make_baro();
			state.baro.tcs = config_a;
			break;
		case AltosLib.AO_LOG_BARO_TCO:
			state.make_baro();
			state.baro.tco = config_a;
			break;
		case AltosLib.AO_LOG_BARO_TREF:
			state.make_baro();
			state.baro.tref = config_a;
			break;
		case AltosLib.AO_LOG_BARO_TEMPSENS:
			state.make_baro();
			state.baro.tempsens = config_a;
			break;
		case AltosLib.AO_LOG_BARO_CRC:
			state.make_baro();
			state.baro.crc = config_a;
			break;
		case AltosLib.AO_LOG_SOFTWARE_VERSION:
			state.set_firmware_version(data);
			break;
		}
	}

	public void write(PrintStream out) {
		switch (cmd) {
		case AltosLib.AO_LOG_CONFIG_VERSION:
			out.printf("# Config version: %s\n", data);
			break;
		case AltosLib.AO_LOG_MAIN_DEPLOY:
			out.printf("# Main deploy: %s\n", config_a);
			break;
		case AltosLib.AO_LOG_APOGEE_DELAY:
			out.printf("# Apogee delay: %s\n", config_a);
			break;
		case AltosLib.AO_LOG_RADIO_CHANNEL:
			out.printf("# Radio channel: %s\n", config_a);
			break;
		case AltosLib.AO_LOG_CALLSIGN:
			out.printf("# Callsign: %s\n", data);
			break;
		case AltosLib.AO_LOG_ACCEL_CAL:
			out.printf ("# Accel cal: %d %d\n", config_a, config_b);
			break;
		case AltosLib.AO_LOG_RADIO_CAL:
			out.printf ("# Radio cal: %d\n", config_a);
			break;
		case AltosLib.AO_LOG_MAX_FLIGHT_LOG:
			out.printf ("# Max flight log: %d\n", config_a);
			break;
		case AltosLib.AO_LOG_MANUFACTURER:
			out.printf ("# Manufacturer: %s\n", data);
			break;
		case AltosLib.AO_LOG_PRODUCT:
			out.printf ("# Product: %s\n", data);
			break;
		case AltosLib.AO_LOG_SERIAL_NUMBER:
			out.printf ("# Serial number: %d\n", config_a);
			break;
		case AltosLib.AO_LOG_SOFTWARE_VERSION:
			out.printf ("# Software version: %s\n", data);
			break;
		case AltosLib.AO_LOG_BARO_RESERVED:
			out.printf ("# Baro reserved: %d\n", config_a);
			break;
		case AltosLib.AO_LOG_BARO_SENS:
			out.printf ("# Baro sens: %d\n", config_a);
			break;
		case AltosLib.AO_LOG_BARO_OFF:
			out.printf ("# Baro off: %d\n", config_a);
			break;
		case AltosLib.AO_LOG_BARO_TCS:
			out.printf ("# Baro tcs: %d\n", config_a);
			break;
		case AltosLib.AO_LOG_BARO_TCO:
			out.printf ("# Baro tco: %d\n", config_a);
			break;
		case AltosLib.AO_LOG_BARO_TREF:
			out.printf ("# Baro tref: %d\n", config_a);
			break;
		case AltosLib.AO_LOG_BARO_TEMPSENS:
			out.printf ("# Baro tempsens: %d\n", config_a);
			break;
		case AltosLib.AO_LOG_BARO_CRC:
			out.printf ("# Baro crc: %d\n", config_a);
			break;
		}
	}
	
	public AltosEepromHeader (String[] tokens) {
		last = false;
		valid = true;
		try {
			if (tokens[0].equals("Config") && tokens[1].equals("version:")) {
				cmd = AltosLib.AO_LOG_CONFIG_VERSION;
				data = tokens[2];
			} else if (tokens[0].equals("Main") && tokens[1].equals("deploy:")) {
				cmd = AltosLib.AO_LOG_MAIN_DEPLOY;
				config_a = Integer.parseInt(tokens[2]);
			} else if (tokens[0].equals("Apogee") && tokens[1].equals("delay:")) {
				cmd = AltosLib.AO_LOG_APOGEE_DELAY;
				config_a = Integer.parseInt(tokens[2]);
			} else if (tokens[0].equals("Radio") && tokens[1].equals("channel:")) {
				cmd = AltosLib.AO_LOG_RADIO_CHANNEL;
				config_a = Integer.parseInt(tokens[2]);
			} else if (tokens[0].equals("Callsign:")) {
				cmd = AltosLib.AO_LOG_CALLSIGN;
				data = tokens[1].replaceAll("\"","");
			} else if (tokens[0].equals("Accel") && tokens[1].equals("cal")) {
				cmd = AltosLib.AO_LOG_ACCEL_CAL;
				config_a = Integer.parseInt(tokens[3]);
				config_b = Integer.parseInt(tokens[5]);
			} else if (tokens[0].equals("Radio") && tokens[1].equals("cal:")) {
				cmd = AltosLib.AO_LOG_RADIO_CAL;
				config_a = Integer.parseInt(tokens[2]);
			} else if (tokens[0].equals("Max") && tokens[1].equals("flight") && tokens[2].equals("log:")) {
				cmd = AltosLib.AO_LOG_MAX_FLIGHT_LOG;
				config_a = Integer.parseInt(tokens[3]);
			} else if (tokens[0].equals("manufacturer")) {
				cmd = AltosLib.AO_LOG_MANUFACTURER;
				data = tokens[1];
			} else if (tokens[0].equals("product")) {
				cmd = AltosLib.AO_LOG_PRODUCT;
				data = tokens[1];
			} else if (tokens[0].equals("serial-number")) {
				cmd = AltosLib.AO_LOG_SERIAL_NUMBER;
				config_a = Integer.parseInt(tokens[1]);
			} else if (tokens[0].equals("log-format")) {
				cmd = AltosLib.AO_LOG_LOG_FORMAT;
				config_a = Integer.parseInt(tokens[1]);
			} else if (tokens[0].equals("software-version")) {
				cmd = AltosLib.AO_LOG_SOFTWARE_VERSION;
				data = tokens[1];
				last = true;
			} else if (tokens[0].equals("ms5607")) {
				if (tokens[1].equals("reserved:")) {
					cmd = AltosLib.AO_LOG_BARO_RESERVED;
					config_a = Integer.parseInt(tokens[2]);
				} else if (tokens[1].equals("sens:")) {
					cmd = AltosLib.AO_LOG_BARO_SENS;
					config_a = Integer.parseInt(tokens[2]);
				} else if (tokens[1].equals("off:")) {
					cmd = AltosLib.AO_LOG_BARO_OFF;
					config_a = Integer.parseInt(tokens[2]);
				} else if (tokens[1].equals("tcs:")) {
					cmd = AltosLib.AO_LOG_BARO_TCS;
					config_a = Integer.parseInt(tokens[2]);
				} else if (tokens[1].equals("tco:")) {
					cmd = AltosLib.AO_LOG_BARO_TCO;
					config_a = Integer.parseInt(tokens[2]);
				} else if (tokens[1].equals("tref:")) {
					cmd = AltosLib.AO_LOG_BARO_TREF;
					config_a = Integer.parseInt(tokens[2]);
				} else if (tokens[1].equals("tempsens:")) {
					cmd = AltosLib.AO_LOG_BARO_TEMPSENS;
					config_a = Integer.parseInt(tokens[2]);
				} else if (tokens[1].equals("crc:")) {
					cmd = AltosLib.AO_LOG_BARO_CRC;
					config_a = Integer.parseInt(tokens[2]);
				} else {
					cmd = AltosLib.AO_LOG_INVALID;
					data = tokens[2];
				}
			} else
				valid = false;
		} catch (Exception e) {
			valid = false;
		}
	}

	static public LinkedList<AltosEeprom> read(FileInputStream input) {
		LinkedList<AltosEeprom> headers = new LinkedList<AltosEeprom>();

		for (;;) {
			try {
				String line = AltosLib.gets(input);
				if (line == null)
					break;
				AltosEepromHeader header = new AltosEepromHeader(line);
				headers.add(header);
				if (header.last)
					break;
			} catch (IOException ie) {
				break;
			}
		}

		return headers;
	}

	static public void write (PrintStream out, LinkedList<AltosEepromHeader> headers) {
		out.printf("# Comments\n");
		for (AltosEepromHeader header : headers) {
			header.write(out);
		}
		
	}

	public AltosEepromHeader (String line) {
		this(line.split("\\s+"));
	}
}