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