altoslib: Remove AltosRecord-based telemetry code
[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_2;
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 record_length() { return record_length; }
36
37         public int data8(int i) {
38                 return data8[i];
39         }
40
41         public int data16(int i) {
42                 return ((data8[i] | (data8[i+1] << 8)) << 16) >> 16;
43         }
44
45         public int data32(int i) {
46                 return data8[i] | (data8[i+1] << 8) | (data8[i+2] << 16) | (data8[i+3] << 24);
47         }
48
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); }
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 data32(0); }
61         public int temp() { return data32(4); }
62         public int accel() { return data16(8); }
63
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); }
68
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); }
73
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); }
82         
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); }
88
89         public AltosEepromMetrum (AltosEepromChunk chunk, int start) throws ParseException {
90                 cmd = chunk.data(start);
91
92                 valid = !chunk.erased(start, record_length);
93                 if (valid) {
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);
97                 } else {
98                         cmd = AltosLib.AO_LOG_INVALID;
99                 }
100
101                 tick = chunk.data16(start+2);
102
103                 data8 = new int[data_length];
104                 for (int i = 0; i < data_length; i++)
105                         data8[i] = chunk.data(start + header_length + i);
106         }
107
108         public AltosEepromMetrum (String line) {
109                 valid = false;
110                 tick = 0;
111
112                 if (line == null) {
113                         cmd = AltosLib.AO_LOG_INVALID;
114                         line = "";
115                 } else {
116                         try {
117                                 String[] tokens = line.split("\\s+");
118
119                                 if (tokens[0].length() == 1) {
120                                         if (tokens.length != 2 + data_length) {
121                                                 cmd = AltosLib.AO_LOG_INVALID;
122                                                 data = line;
123                                         } else {
124                                                 cmd = tokens[0].codePointAt(0);
125                                                 tick = Integer.parseInt(tokens[1],16);
126                                                 valid = true;
127                                                 data8 = new int[data_length];
128                                                 for (int i = 0; i < data_length; i++)
129                                                         data8[i] = Integer.parseInt(tokens[2 + i],16);
130                                         }
131                                 } else if (tokens[0].equals("Config") && tokens[1].equals("version:")) {
132                                         cmd = AltosLib.AO_LOG_CONFIG_VERSION;
133                                         data = tokens[2];
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;
158                                         data = tokens[1];
159                                 } else if (tokens[0].equals("product")) {
160                                         cmd = AltosLib.AO_LOG_PRODUCT;
161                                         data = tokens[1];
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;
170                                         data = tokens[1];
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]);
196                                         } else {
197                                                 cmd = AltosLib.AO_LOG_INVALID;
198                                                 data = line;
199                                         }
200                                 } else {
201                                         cmd = AltosLib.AO_LOG_INVALID;
202                                         data = line;
203                                 }
204                         } catch (NumberFormatException ne) {
205                                 cmd = AltosLib.AO_LOG_INVALID;
206                                 data = line;
207                         }
208                 }
209         }
210
211         public AltosEepromMetrum(int in_cmd, int in_tick) {
212                 cmd = in_cmd;
213                 tick = in_tick;
214                 valid = true;
215         }
216 }