Merge branch 'prefs_interface' into altosdroid
[fw/altos] / altoslib / AltosEepromMega.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;
19
20 import java.text.*;
21
22 public class AltosEepromMega {
23         public int      cmd;
24         public int      tick;
25         public boolean  valid;
26         public String   data;
27         public int      a, b;
28
29         public int      data8[];
30
31         public static final int record_length = 32;
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_x() { return data16(8); }
61         public int accel_y() { return data16(10); }
62         public int accel_z() { return data16(12); }
63         public int gyro_x() { return data16(14); }
64         public int gyro_y() { return data16(16); }
65         public int gyro_z() { return data16(18); }
66         public int mag_x() { return data16(20); }
67         public int mag_y() { return data16(22); }
68         public int mag_z() { return data16(24); }
69         public int accel() {
70                 int a = data16(26);
71                 if (a != 0xffff)
72                         return a;
73                 return accel_y();
74         }
75
76         /* AO_LOG_VOLT elements */
77         public int v_batt() { return data16(0); }
78         public int v_pbatt() { return data16(2); }
79         public int nsense() { return data16(4); }
80         public int sense(int i) { return data16(6 + i * 2); }
81
82         public AltosEepromMega (AltosEepromChunk chunk, int start) throws ParseException {
83                 cmd = chunk.data(start);
84
85                 valid = !chunk.erased(start, record_length);
86                 if (valid) {
87                         if (AltosConvert.checksum(chunk.data, start, record_length) != 0)
88                                 throw new ParseException(String.format("invalid checksum at 0x%x",
89                                                                        chunk.address + start), 0);
90                 } else {
91                         cmd = AltosLib.AO_LOG_INVALID;
92                 }
93
94                 tick = chunk.data16(start+2);
95
96                 data8 = new int[data_length];
97                 for (int i = 0; i < data_length; i++)
98                         data8[i] = chunk.data(start + header_length + i);
99         }
100
101         public AltosEepromMega (String line) {
102                 valid = false;
103                 tick = 0;
104
105                 if (line == null) {
106                         cmd = AltosLib.AO_LOG_INVALID;
107                         line = "";
108                 } else {
109                         try {
110                                 String[] tokens = line.split("\\s+");
111
112                                 if (tokens[0].length() == 1) {
113                                         if (tokens.length != 2 + data_length) {
114                                                 cmd = AltosLib.AO_LOG_INVALID;
115                                                 data = line;
116                                         } else {
117                                                 cmd = tokens[0].codePointAt(0);
118                                                 tick = Integer.parseInt(tokens[1],16);
119                                                 valid = true;
120                                                 data8 = new int[data_length];
121                                                 for (int i = 0; i < data_length; i++)
122                                                         data8[i] = Integer.parseInt(tokens[2 + i],16);
123                                         }
124                                 } else if (tokens[0].equals("Config") && tokens[1].equals("version:")) {
125                                         cmd = AltosLib.AO_LOG_CONFIG_VERSION;
126                                         data = tokens[2];
127                                 } else if (tokens[0].equals("Main") && tokens[1].equals("deploy:")) {
128                                         cmd = AltosLib.AO_LOG_MAIN_DEPLOY;
129                                         a = Integer.parseInt(tokens[2]);
130                                 } else if (tokens[0].equals("Apogee") && tokens[1].equals("delay:")) {
131                                         cmd = AltosLib.AO_LOG_APOGEE_DELAY;
132                                         a = Integer.parseInt(tokens[2]);
133                                 } else if (tokens[0].equals("Radio") && tokens[1].equals("channel:")) {
134                                         cmd = AltosLib.AO_LOG_RADIO_CHANNEL;
135                                         a = Integer.parseInt(tokens[2]);
136                                 } else if (tokens[0].equals("Callsign:")) {
137                                         cmd = AltosLib.AO_LOG_CALLSIGN;
138                                         data = tokens[1].replaceAll("\"","");
139                                 } else if (tokens[0].equals("Accel") && tokens[1].equals("cal")) {
140                                         cmd = AltosLib.AO_LOG_ACCEL_CAL;
141                                         a = Integer.parseInt(tokens[3]);
142                                         b = Integer.parseInt(tokens[5]);
143                                 } else if (tokens[0].equals("Radio") && tokens[1].equals("cal:")) {
144                                         cmd = AltosLib.AO_LOG_RADIO_CAL;
145                                         a = Integer.parseInt(tokens[2]);
146                                 } else if (tokens[0].equals("Max") && tokens[1].equals("flight") && tokens[2].equals("log:")) {
147                                         cmd = AltosLib.AO_LOG_MAX_FLIGHT_LOG;
148                                         a = Integer.parseInt(tokens[3]);
149                                 } else if (tokens[0].equals("manufacturer")) {
150                                         cmd = AltosLib.AO_LOG_MANUFACTURER;
151                                         data = tokens[1];
152                                 } else if (tokens[0].equals("product")) {
153                                         cmd = AltosLib.AO_LOG_PRODUCT;
154                                         data = tokens[1];
155                                 } else if (tokens[0].equals("serial-number")) {
156                                         cmd = AltosLib.AO_LOG_SERIAL_NUMBER;
157                                         a = Integer.parseInt(tokens[1]);
158                                 } else if (tokens[0].equals("log-format")) {
159                                         cmd = AltosLib.AO_LOG_LOG_FORMAT;
160                                         a = Integer.parseInt(tokens[1]);
161                                 } else if (tokens[0].equals("software-version")) {
162                                         cmd = AltosLib.AO_LOG_SOFTWARE_VERSION;
163                                         data = tokens[1];
164                                 } else if (tokens[0].equals("ms5607")) {
165                                         if (tokens[1].equals("reserved:")) {
166                                                 cmd = AltosLib.AO_LOG_BARO_RESERVED;
167                                                 a = Integer.parseInt(tokens[2]);
168                                         } else if (tokens[1].equals("sens:")) {
169                                                 cmd = AltosLib.AO_LOG_BARO_SENS;
170                                                 a = Integer.parseInt(tokens[2]);
171                                         } else if (tokens[1].equals("off:")) {
172                                                 cmd = AltosLib.AO_LOG_BARO_OFF;
173                                                 a = Integer.parseInt(tokens[2]);
174                                         } else if (tokens[1].equals("tcs:")) {
175                                                 cmd = AltosLib.AO_LOG_BARO_TCS;
176                                                 a = Integer.parseInt(tokens[2]);
177                                         } else if (tokens[1].equals("tco:")) {
178                                                 cmd = AltosLib.AO_LOG_BARO_TCO;
179                                                 a = Integer.parseInt(tokens[2]);
180                                         } else if (tokens[1].equals("tref:")) {
181                                                 cmd = AltosLib.AO_LOG_BARO_TREF;
182                                                 a = Integer.parseInt(tokens[2]);
183                                         } else if (tokens[1].equals("tempsens:")) {
184                                                 cmd = AltosLib.AO_LOG_BARO_TEMPSENS;
185                                                 a = Integer.parseInt(tokens[2]);
186                                         } else if (tokens[1].equals("crc:")) {
187                                                 cmd = AltosLib.AO_LOG_BARO_CRC;
188                                                 a = Integer.parseInt(tokens[2]);
189                                         } else {
190                                                 cmd = AltosLib.AO_LOG_INVALID;
191                                                 data = line;
192                                         }
193                                 } else {
194                                         cmd = AltosLib.AO_LOG_INVALID;
195                                         data = line;
196                                 }
197                         } catch (NumberFormatException ne) {
198                                 cmd = AltosLib.AO_LOG_INVALID;
199                                 data = line;
200                         }
201                 }
202         }
203
204         public AltosEepromMega(int in_cmd, int in_tick) {
205                 cmd = in_cmd;
206                 tick = in_tick;
207                 valid = true;
208         }
209 }