fc7ec32108a653978b689c30fbc9f90059bba036
[fw/altos] / altoslib / AltosEepromTM.java
1 /*
2  * Copyright © 2010 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 AltosEepromTM implements AltosStateUpdate {
23         public int      cmd;
24         public int      tick;
25         public int      a;
26         public int      b;
27         public String   data;
28         public boolean  tick_valid;
29
30         public static final int record_length = 8;
31
32         public void update_state(AltosState state) {
33                 state.set_tick(tick);
34                 switch (cmd) {
35                 case AltosLib.AO_LOG_FLIGHT:
36                         state.ground_accel = a;
37                         state.flight = b;
38                         state.set_boost_tick(tick);
39                         state.time = 0;
40                         break;
41                 case AltosLib.AO_LOG_SENSOR:
42                         state.set_telemetrum(a, b);
43                         break;
44                 case AltosLib.AO_LOG_PRESSURE:
45                         state.set_telemetrum(AltosState.MISSING, b);
46                         break;
47                 case AltosLib.AO_LOG_TEMP_VOLT:
48 /*
49                         
50                         record.temp = a;
51                         record.batt = b;
52                         eeprom_state.seen |= AltosRecord.seen_temp_volt;
53 */
54                         break;
55                 case AltosLib.AO_LOG_DEPLOY:
56 /*
57                         record.drogue = a;
58                         record.main = b;
59                         eeprom_state.seen |= AltosRecord.seen_deploy;
60                         has_ignite = true;
61 */
62                         break;
63                 case AltosLib.AO_LOG_STATE:
64                         state.state = a;
65                         break;
66 //              case AltosLib.AO_LOG_GPS_TIME:
67 //                      eeprom_state.gps_tick = record.tick;
68 //                      eeprom_state.seen |= AltosRecord.seen_gps_time;
69 //                      AltosGPS old = state.gps;
70 //                      AltosGPS gps = new AltosGPS();
71 //
72 //                      /* GPS date doesn't get repeated through the file */
73 //                      if (old != null) {
74 //                              gps.year = old.year;
75 //                              gps.month = old.month;
76 //                              gps.day = old.day;
77 //                      }
78 //                      gps.hour = (a & 0xff);
79 //                      gps.minute = (a >> 8);
80 //                      gps.second = (b & 0xff);
81 //
82 //                      int flags = (b >> 8);
83 //                      gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
84 //                      gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
85 //                      gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
86 //                              AltosLib.AO_GPS_NUM_SAT_SHIFT;
87 //                      state.temp_gps = gps;
88 //                      break;
89 //              case AltosLib.AO_LOG_GPS_LAT:
90 //                      int lat32 = a | (b << 16);
91 //                      if (state.temp_gps == null)
92 //                              state.temp_gps = new AltosGPS();
93 //                      state.temp_gps.lat = (double) lat32 / 1e7;
94 //                      break;
95 //              case AltosLib.AO_LOG_GPS_LON:
96 //                      int lon32 = a | (b << 16);
97 //                      if (state.temp_gps == null)
98 //                              state.temp_gps = new AltosGPS();
99 //                      state.temp_gps.lon = (double) lon32 / 1e7;
100 //                      break;
101 //              case AltosLib.AO_LOG_GPS_ALT:
102 //                      if (state.temp_gps == null)
103 //                              state.temp_gps = new AltosGPS();
104 //                      state.temp_gps.alt = a;
105 //                      break;
106 //              case AltosLib.AO_LOG_GPS_SAT:
107 //                      if (record.tick == eeprom_state.gps_tick) {
108 //                              int svid = a;
109 //                              int c_n0 = b >> 8;
110 //                              if (record.gps == null)
111 //                                      record.gps = new AltosGPS();
112 //                              record.gps.add_sat(svid, c_n0);
113 //                      }
114 //                      break;
115 //              case AltosLib.AO_LOG_GPS_DATE:
116 //                      if (record.gps == null)
117 //                              record.gps = new AltosGPS();
118 //                      record.gps.year = (a & 0xff) + 2000;
119 //                      record.gps.month = a >> 8;
120 //                      record.gps.day = b & 0xff;
121 //                      break;
122
123                 case AltosLib.AO_LOG_CONFIG_VERSION:
124                         break;
125                 case AltosLib.AO_LOG_MAIN_DEPLOY:
126                         break;
127                 case AltosLib.AO_LOG_APOGEE_DELAY:
128                         break;
129                 case AltosLib.AO_LOG_RADIO_CHANNEL:
130                         break;
131                 case AltosLib.AO_LOG_CALLSIGN:
132                         state.callsign = data;
133                         break;
134                 case AltosLib.AO_LOG_ACCEL_CAL:
135                         state.accel_plus_g = a;
136                         state.accel_minus_g = b;
137                         break;
138                 case AltosLib.AO_LOG_RADIO_CAL:
139                         break;
140                 case AltosLib.AO_LOG_MANUFACTURER:
141                         break;
142                 case AltosLib.AO_LOG_PRODUCT:
143                         break;
144                 case AltosLib.AO_LOG_SERIAL_NUMBER:
145                         state.serial = a;
146                         break;
147                 case AltosLib.AO_LOG_SOFTWARE_VERSION:
148                         break;
149                 }
150         }
151
152         public AltosEepromTM (AltosEepromChunk chunk, int start) throws ParseException {
153
154                 cmd = chunk.data(start);
155                 tick_valid = true;
156
157                 tick_valid = !chunk.erased(start, record_length);
158                 if (tick_valid) {
159                         if (AltosConvert.checksum(chunk.data, start, record_length) != 0)
160                                 throw new ParseException(String.format("invalid checksum at 0x%x",
161                                                                        chunk.address + start), 0);
162                 } else {
163                         cmd = AltosLib.AO_LOG_INVALID;
164                 }
165
166                 tick = chunk.data16(start + 2);
167                 a = chunk.data16(start + 4);
168                 b = chunk.data16(start + 6);
169
170                 data = null;
171         }
172
173         public AltosEepromTM (String line) {
174                 tick_valid = false;
175                 tick = 0;
176                 a = 0;
177                 b = 0;
178                 data = null;
179                 if (line == null) {
180                         cmd = AltosLib.AO_LOG_INVALID;
181                         data = "";
182                 } else {
183                         try {
184                                 String[] tokens = line.split("\\s+");
185
186                                 if (tokens[0].length() == 1) {
187                                         if (tokens.length != 4) {
188                                                 cmd = AltosLib.AO_LOG_INVALID;
189                                                 data = line;
190                                         } else {
191                                                 cmd = tokens[0].codePointAt(0);
192                                                 tick = Integer.parseInt(tokens[1],16);
193                                                 tick_valid = true;
194                                                 a = Integer.parseInt(tokens[2],16);
195                                                 b = Integer.parseInt(tokens[3],16);
196                                         }
197                                 } else if (tokens[0].equals("Config") && tokens[1].equals("version:")) {
198                                         cmd = AltosLib.AO_LOG_CONFIG_VERSION;
199                                         data = tokens[2];
200                                 } else if (tokens[0].equals("Main") && tokens[1].equals("deploy:")) {
201                                         cmd = AltosLib.AO_LOG_MAIN_DEPLOY;
202                                         a = Integer.parseInt(tokens[2]);
203                                 } else if (tokens[0].equals("Apogee") && tokens[1].equals("delay:")) {
204                                         cmd = AltosLib.AO_LOG_APOGEE_DELAY;
205                                         a = Integer.parseInt(tokens[2]);
206                                 } else if (tokens[0].equals("Radio") && tokens[1].equals("channel:")) {
207                                         cmd = AltosLib.AO_LOG_RADIO_CHANNEL;
208                                         a = Integer.parseInt(tokens[2]);
209                                 } else if (tokens[0].equals("Callsign:")) {
210                                         cmd = AltosLib.AO_LOG_CALLSIGN;
211                                         data = tokens[1].replaceAll("\"","");
212                                 } else if (tokens[0].equals("Accel") && tokens[1].equals("cal")) {
213                                         cmd = AltosLib.AO_LOG_ACCEL_CAL;
214                                         a = Integer.parseInt(tokens[3]);
215                                         b = Integer.parseInt(tokens[5]);
216                                 } else if (tokens[0].equals("Radio") && tokens[1].equals("cal:")) {
217                                         cmd = AltosLib.AO_LOG_RADIO_CAL;
218                                         a = Integer.parseInt(tokens[2]);
219                                 } else if (tokens[0].equals("Max") && tokens[1].equals("flight") && tokens[2].equals("log:")) {
220                                         cmd = AltosLib.AO_LOG_MAX_FLIGHT_LOG;
221                                         a = Integer.parseInt(tokens[3]);
222                                 } else if (tokens[0].equals("manufacturer")) {
223                                         cmd = AltosLib.AO_LOG_MANUFACTURER;
224                                         data = tokens[1];
225                                 } else if (tokens[0].equals("product")) {
226                                         cmd = AltosLib.AO_LOG_PRODUCT;
227                                         data = tokens[1];
228                                 } else if (tokens[0].equals("serial-number")) {
229                                         cmd = AltosLib.AO_LOG_SERIAL_NUMBER;
230                                         a = Integer.parseInt(tokens[1]);
231                                 } else if (tokens[0].equals("log-format")) {
232                                         cmd = AltosLib.AO_LOG_LOG_FORMAT;
233                                         a = Integer.parseInt(tokens[1]);
234                                 } else if (tokens[0].equals("software-version")) {
235                                         cmd = AltosLib.AO_LOG_SOFTWARE_VERSION;
236                                         data = tokens[1];
237                                 } else {
238                                         cmd = AltosLib.AO_LOG_INVALID;
239                                         data = line;
240                                 }
241                         } catch (NumberFormatException ne) {
242 v                               cmd = AltosLib.AO_LOG_INVALID;
243                                 data = line;
244                         }
245                 }
246         }
247
248         public AltosEepromTM(int in_cmd, int in_tick, int in_a, int in_b) {
249                 tick_valid = true;
250                 cmd = in_cmd;
251                 tick = in_tick;
252                 a = in_a;
253                 b = in_b;
254         }
255 }