Merge branch 'master' into new-state
[fw/altos] / altoslib / AltosEepromMetrum.java
1 /*
2  * Copyright © 2013 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 AltosEepromMetrum {
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 data32(int i) {
44                 return data8[i] | (data8[i+1] << 8) | (data8[i+2] << 16) | (data8[i+3] << 24);
45         }
46
47         /* AO_LOG_FLIGHT elements */
48         public int flight() { return data16(0); }
49         public int ground_accel() { return data16(2); }
50         public int ground_pres() { return data32(4); }
51         public int ground_temp() { return data32(8); }
52
53         /* AO_LOG_STATE elements */
54         public int state() { return data16(0); }
55         public int reason() { return data16(2); }
56
57         /* AO_LOG_SENSOR elements */
58         public int pres() { return data32(0); }
59         public int temp() { return data32(4); }
60         public int accel() { return data16(8); }
61
62         /* AO_LOG_TEMP_VOLT elements */
63         public int v_batt() { return data16(0); }
64         public int sense_a() { return data16(2); }
65         public int sense_m() { return data16(4); }
66
67         /* AO_LOG_GPS_POS elements */
68         public int latitude() { return data32(0); }
69         public int longitude() { return data32(4); }
70         public int altitude() { return data16(8); }
71
72         /* AO_LOG_GPS_TIME elements */
73         public int hour() { return data8(0); }
74         public int minute() { return data8(1); }
75         public int second() { return data8(2); }
76         public int flags() { return data8(3); }
77         public int year() { return data8(4); }
78         public int month() { return data8(5); }
79         public int day() { return data8(6); }
80         
81         /* AO_LOG_GPS_SAT elements */
82         public int channels() { return data8(0); }
83         public int more() { return data8(1); }
84         public int svid(int n) { return data8(2 + n * 2); }
85         public int c_n(int n) { return data8(2 + n * 2 + 1); }
86
87         public AltosEepromMetrum (AltosEepromChunk chunk, int start) throws ParseException {
88                 cmd = chunk.data(start);
89
90                 valid = !chunk.erased(start, record_length);
91                 if (valid) {
92                         if (AltosConvert.checksum(chunk.data, start, record_length) != 0)
93                                 throw new ParseException(String.format("invalid checksum at 0x%x",
94                                                                        chunk.address + start), 0);
95                 } else {
96                         cmd = AltosLib.AO_LOG_INVALID;
97                 }
98
99                 tick = chunk.data16(start+2);
100
101                 data8 = new int[data_length];
102                 for (int i = 0; i < data_length; i++)
103                         data8[i] = chunk.data(start + header_length + i);
104         }
105
106         public AltosEepromMetrum (String line) {
107                 valid = false;
108                 tick = 0;
109
110                 if (line == null) {
111                         cmd = AltosLib.AO_LOG_INVALID;
112                         line = "";
113                 } else {
114                         try {
115                                 String[] tokens = line.split("\\s+");
116
117                                 if (tokens[0].length() == 1) {
118                                         if (tokens.length != 2 + data_length) {
119                                                 cmd = AltosLib.AO_LOG_INVALID;
120                                                 data = line;
121                                         } else {
122                                                 cmd = tokens[0].codePointAt(0);
123                                                 tick = Integer.parseInt(tokens[1],16);
124                                                 valid = true;
125                                                 data8 = new int[data_length];
126                                                 for (int i = 0; i < data_length; i++)
127                                                         data8[i] = Integer.parseInt(tokens[2 + i],16);
128                                         }
129                                 } else if (tokens[0].equals("Config") && tokens[1].equals("version:")) {
130                                         cmd = AltosLib.AO_LOG_CONFIG_VERSION;
131                                         data = tokens[2];
132                                 } else if (tokens[0].equals("Main") && tokens[1].equals("deploy:")) {
133                                         cmd = AltosLib.AO_LOG_MAIN_DEPLOY;
134                                         config_a = Integer.parseInt(tokens[2]);
135                                 } else if (tokens[0].equals("Apogee") && tokens[1].equals("delay:")) {
136                                         cmd = AltosLib.AO_LOG_APOGEE_DELAY;
137                                         config_a = Integer.parseInt(tokens[2]);
138                                 } else if (tokens[0].equals("Radio") && tokens[1].equals("channel:")) {
139                                         cmd = AltosLib.AO_LOG_RADIO_CHANNEL;
140                                         config_a = Integer.parseInt(tokens[2]);
141                                 } else if (tokens[0].equals("Callsign:")) {
142                                         cmd = AltosLib.AO_LOG_CALLSIGN;
143                                         data = tokens[1].replaceAll("\"","");
144                                 } else if (tokens[0].equals("Accel") && tokens[1].equals("cal")) {
145                                         cmd = AltosLib.AO_LOG_ACCEL_CAL;
146                                         config_a = Integer.parseInt(tokens[3]);
147                                         config_b = Integer.parseInt(tokens[5]);
148                                 } else if (tokens[0].equals("Radio") && tokens[1].equals("cal:")) {
149                                         cmd = AltosLib.AO_LOG_RADIO_CAL;
150                                         config_a = Integer.parseInt(tokens[2]);
151                                 } else if (tokens[0].equals("Max") && tokens[1].equals("flight") && tokens[2].equals("log:")) {
152                                         cmd = AltosLib.AO_LOG_MAX_FLIGHT_LOG;
153                                         config_a = Integer.parseInt(tokens[3]);
154                                 } else if (tokens[0].equals("manufacturer")) {
155                                         cmd = AltosLib.AO_LOG_MANUFACTURER;
156                                         data = tokens[1];
157                                 } else if (tokens[0].equals("product")) {
158                                         cmd = AltosLib.AO_LOG_PRODUCT;
159                                         data = tokens[1];
160                                 } else if (tokens[0].equals("serial-number")) {
161                                         cmd = AltosLib.AO_LOG_SERIAL_NUMBER;
162                                         config_a = Integer.parseInt(tokens[1]);
163                                 } else if (tokens[0].equals("log-format")) {
164                                         cmd = AltosLib.AO_LOG_LOG_FORMAT;
165                                         config_a = Integer.parseInt(tokens[1]);
166                                 } else if (tokens[0].equals("software-version")) {
167                                         cmd = AltosLib.AO_LOG_SOFTWARE_VERSION;
168                                         data = tokens[1];
169                                 } else if (tokens[0].equals("ms5607")) {
170                                         if (tokens[1].equals("reserved:")) {
171                                                 cmd = AltosLib.AO_LOG_BARO_RESERVED;
172                                                 config_a = Integer.parseInt(tokens[2]);
173                                         } else if (tokens[1].equals("sens:")) {
174                                                 cmd = AltosLib.AO_LOG_BARO_SENS;
175                                                 config_a = Integer.parseInt(tokens[2]);
176                                         } else if (tokens[1].equals("off:")) {
177                                                 cmd = AltosLib.AO_LOG_BARO_OFF;
178                                                 config_a = Integer.parseInt(tokens[2]);
179                                         } else if (tokens[1].equals("tcs:")) {
180                                                 cmd = AltosLib.AO_LOG_BARO_TCS;
181                                                 config_a = Integer.parseInt(tokens[2]);
182                                         } else if (tokens[1].equals("tco:")) {
183                                                 cmd = AltosLib.AO_LOG_BARO_TCO;
184                                                 config_a = Integer.parseInt(tokens[2]);
185                                         } else if (tokens[1].equals("tref:")) {
186                                                 cmd = AltosLib.AO_LOG_BARO_TREF;
187                                                 config_a = Integer.parseInt(tokens[2]);
188                                         } else if (tokens[1].equals("tempsens:")) {
189                                                 cmd = AltosLib.AO_LOG_BARO_TEMPSENS;
190                                                 config_a = Integer.parseInt(tokens[2]);
191                                         } else if (tokens[1].equals("crc:")) {
192                                                 cmd = AltosLib.AO_LOG_BARO_CRC;
193                                                 config_a = Integer.parseInt(tokens[2]);
194                                         } else {
195                                                 cmd = AltosLib.AO_LOG_INVALID;
196                                                 data = line;
197                                         }
198                                 } else {
199                                         cmd = AltosLib.AO_LOG_INVALID;
200                                         data = line;
201                                 }
202                         } catch (NumberFormatException ne) {
203                                 cmd = AltosLib.AO_LOG_INVALID;
204                                 data = line;
205                         }
206                 }
207         }
208
209         public AltosEepromMetrum(int in_cmd, int in_tick) {
210                 cmd = in_cmd;
211                 tick = in_tick;
212                 valid = true;
213         }
214 }