Merge branch 'lpc'
[fw/altos] / altoslib / AltosEepromMini.java
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.altoslib_1;
19
20 import java.text.*;
21
22 public class AltosEepromMini {
23         public int      cmd;
24         public int      tick;
25         public boolean  valid;
26         public String   data;
27         public int      config_a, config_b;
28
29         public int      data8[];
30
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;
34
35         public int data8(int i) {
36                 return data8[i];
37         }
38
39         public int data16(int i) {
40                 return ((data8[i] | (data8[i+1] << 8)) << 16) >> 16;
41         }
42
43         public int data24(int i) {
44                 return data8[i] | (data8[i+1] << 8) | (data8[i+2] << 16);
45         }
46
47         public int data32(int i) {
48                 return data8[i] | (data8[i+1] << 8) | (data8[i+2] << 16) | (data8[i+3] << 24);
49         }
50
51         /* AO_LOG_FLIGHT elements */
52         public int flight() { return data16(0); }
53         public int ground_pres() { return data32(4); }
54
55         /* AO_LOG_STATE elements */
56         public int state() { return data16(0); }
57         public int reason() { return data16(2); }
58
59         /* AO_LOG_SENSOR elements */
60         public int pres() { return data24(0); }
61         public int temp() { return data24(3); }
62         public int sense_a() { return data16(6); }
63         public int sense_m() { return data16(8); }
64         public int v_batt() { return data16(10); }
65
66         public AltosEepromMini (AltosEepromChunk chunk, int start) throws ParseException {
67                 cmd = chunk.data(start);
68
69                 valid = !chunk.erased(start, record_length);
70                 if (valid) {
71                         if (AltosConvert.checksum(chunk.data, start, record_length) != 0)
72                                 throw new ParseException(String.format("invalid checksum at 0x%x",
73                                                                        chunk.address + start), 0);
74                 } else {
75                         cmd = AltosLib.AO_LOG_INVALID;
76                 }
77
78                 tick = chunk.data16(start+2);
79
80                 data8 = new int[data_length];
81                 for (int i = 0; i < data_length; i++)
82                         data8[i] = chunk.data(start + header_length + i);
83         }
84
85         public AltosEepromMini (String line) {
86                 valid = false;
87                 tick = 0;
88
89                 if (line == null) {
90                         cmd = AltosLib.AO_LOG_INVALID;
91                         line = "";
92                 } else {
93                         try {
94                                 String[] tokens = line.split("\\s+");
95
96                                 if (tokens[0].length() == 1) {
97                                         if (tokens.length != 2 + data_length) {
98                                                 cmd = AltosLib.AO_LOG_INVALID;
99                                                 data = line;
100                                         } else {
101                                                 cmd = tokens[0].codePointAt(0);
102                                                 tick = Integer.parseInt(tokens[1],16);
103                                                 valid = true;
104                                                 data8 = new int[data_length];
105                                                 for (int i = 0; i < data_length; i++)
106                                                         data8[i] = Integer.parseInt(tokens[2 + i],16);
107                                         }
108                                 } else if (tokens[0].equals("Config") && tokens[1].equals("version:")) {
109                                         cmd = AltosLib.AO_LOG_CONFIG_VERSION;
110                                         data = tokens[2];
111                                 } else if (tokens[0].equals("Main") && tokens[1].equals("deploy:")) {
112                                         cmd = AltosLib.AO_LOG_MAIN_DEPLOY;
113                                         config_a = Integer.parseInt(tokens[2]);
114                                 } else if (tokens[0].equals("Apogee") && tokens[1].equals("delay:")) {
115                                         cmd = AltosLib.AO_LOG_APOGEE_DELAY;
116                                         config_a = Integer.parseInt(tokens[2]);
117                                 } else if (tokens[0].equals("Radio") && tokens[1].equals("channel:")) {
118                                         cmd = AltosLib.AO_LOG_RADIO_CHANNEL;
119                                         config_a = Integer.parseInt(tokens[2]);
120                                 } else if (tokens[0].equals("Callsign:")) {
121                                         cmd = AltosLib.AO_LOG_CALLSIGN;
122                                         data = tokens[1].replaceAll("\"","");
123                                 } else if (tokens[0].equals("Accel") && tokens[1].equals("cal")) {
124                                         cmd = AltosLib.AO_LOG_ACCEL_CAL;
125                                         config_a = Integer.parseInt(tokens[3]);
126                                         config_b = Integer.parseInt(tokens[5]);
127                                 } else if (tokens[0].equals("Radio") && tokens[1].equals("cal:")) {
128                                         cmd = AltosLib.AO_LOG_RADIO_CAL;
129                                         config_a = Integer.parseInt(tokens[2]);
130                                 } else if (tokens[0].equals("Max") && tokens[1].equals("flight") && tokens[2].equals("log:")) {
131                                         cmd = AltosLib.AO_LOG_MAX_FLIGHT_LOG;
132                                         config_a = Integer.parseInt(tokens[3]);
133                                 } else if (tokens[0].equals("manufacturer")) {
134                                         cmd = AltosLib.AO_LOG_MANUFACTURER;
135                                         data = tokens[1];
136                                 } else if (tokens[0].equals("product")) {
137                                         cmd = AltosLib.AO_LOG_PRODUCT;
138                                         data = tokens[1];
139                                 } else if (tokens[0].equals("serial-number")) {
140                                         cmd = AltosLib.AO_LOG_SERIAL_NUMBER;
141                                         config_a = Integer.parseInt(tokens[1]);
142                                 } else if (tokens[0].equals("log-format")) {
143                                         cmd = AltosLib.AO_LOG_LOG_FORMAT;
144                                         config_a = Integer.parseInt(tokens[1]);
145                                 } else if (tokens[0].equals("software-version")) {
146                                         cmd = AltosLib.AO_LOG_SOFTWARE_VERSION;
147                                         data = tokens[1];
148                                 } else if (tokens[0].equals("ms5607")) {
149                                         if (tokens[1].equals("reserved:")) {
150                                                 cmd = AltosLib.AO_LOG_BARO_RESERVED;
151                                                 config_a = Integer.parseInt(tokens[2]);
152                                         } else if (tokens[1].equals("sens:")) {
153                                                 cmd = AltosLib.AO_LOG_BARO_SENS;
154                                                 config_a = Integer.parseInt(tokens[2]);
155                                         } else if (tokens[1].equals("off:")) {
156                                                 cmd = AltosLib.AO_LOG_BARO_OFF;
157                                                 config_a = Integer.parseInt(tokens[2]);
158                                         } else if (tokens[1].equals("tcs:")) {
159                                                 cmd = AltosLib.AO_LOG_BARO_TCS;
160                                                 config_a = Integer.parseInt(tokens[2]);
161                                         } else if (tokens[1].equals("tco:")) {
162                                                 cmd = AltosLib.AO_LOG_BARO_TCO;
163                                                 config_a = Integer.parseInt(tokens[2]);
164                                         } else if (tokens[1].equals("tref:")) {
165                                                 cmd = AltosLib.AO_LOG_BARO_TREF;
166                                                 config_a = Integer.parseInt(tokens[2]);
167                                         } else if (tokens[1].equals("tempsens:")) {
168                                                 cmd = AltosLib.AO_LOG_BARO_TEMPSENS;
169                                                 config_a = Integer.parseInt(tokens[2]);
170                                         } else if (tokens[1].equals("crc:")) {
171                                                 cmd = AltosLib.AO_LOG_BARO_CRC;
172                                                 config_a = Integer.parseInt(tokens[2]);
173                                         } else {
174                                                 cmd = AltosLib.AO_LOG_INVALID;
175                                                 data = line;
176                                         }
177                                 } else {
178                                         cmd = AltosLib.AO_LOG_INVALID;
179                                         data = line;
180                                 }
181                         } catch (NumberFormatException ne) {
182                                 cmd = AltosLib.AO_LOG_INVALID;
183                                 data = line;
184                         }
185                 }
186         }
187
188         public AltosEepromMini(int in_cmd, int in_tick) {
189                 cmd = in_cmd;
190                 tick = in_tick;
191                 valid = true;
192         }
193 }