altoslib: Remove duplicate cmd/tick from TM eeprom file code
[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.io.*;
21 import java.util.*;
22 import java.text.*;
23
24 public class AltosEepromTM extends AltosEeprom {
25         public int      a;
26         public int      b;
27         public boolean  tick_valid;
28
29         public static final int record_length = 8;
30
31         public void write(PrintStream out) {
32                 out.printf("%c %4x %4x %4x\n", cmd, tick, a, b);
33         }
34
35         public int record_length() { return record_length; }
36
37         public String string() {
38                 return String.format("%c %4x %4x %4x\n", cmd, tick, a, b);
39         }
40
41         public void update_state(AltosState state) {
42                 super.update_state(state);
43
44                 AltosGPS        gps;
45
46                 /* Flush any pending GPS changes */
47                 if (state.gps_pending) {
48                         switch (cmd) {
49                         case AltosLib.AO_LOG_GPS_LAT:
50                         case AltosLib.AO_LOG_GPS_LON:
51                         case AltosLib.AO_LOG_GPS_ALT:
52                         case AltosLib.AO_LOG_GPS_SAT:
53                         case AltosLib.AO_LOG_GPS_DATE:
54                                 break;
55                         default:
56                                 state.set_temp_gps();
57                                 break;
58                         }
59                 }
60
61                 switch (cmd) {
62                 case AltosLib.AO_LOG_FLIGHT:
63                         state.set_state(AltosLib.ao_flight_pad);
64                         state.set_ground_accel(a);
65                         state.set_flight(b);
66                         state.set_boost_tick(tick);
67                         break;
68                 case AltosLib.AO_LOG_SENSOR:
69                         state.set_accel(a);
70                         state.set_pressure(AltosConvert.barometer_to_pressure(b));
71                         break;
72                 case AltosLib.AO_LOG_PRESSURE:
73                         state.set_pressure(AltosConvert.barometer_to_pressure(b));
74                         break;
75                 case AltosLib.AO_LOG_TEMP_VOLT:
76                         state.set_temperature(AltosConvert.thermometer_to_temperature(a));
77                         state.set_battery_voltage(AltosConvert.cc_battery_to_voltage(b));
78                         break;
79                 case AltosLib.AO_LOG_DEPLOY:
80                         state.set_apogee_voltage(AltosConvert.cc_ignitor_to_voltage(a));
81                         state.set_main_voltage(AltosConvert.cc_ignitor_to_voltage(b));
82                         break;
83                 case AltosLib.AO_LOG_STATE:
84                         state.set_state(a);
85                         break;
86                 case AltosLib.AO_LOG_GPS_TIME:
87                         gps = state.make_temp_gps(false);
88
89                         gps.hour = (a & 0xff);
90                         gps.minute = (a >> 8);
91                         gps.second = (b & 0xff);
92
93                         int flags = (b >> 8);
94
95                         gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
96                         gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
97                         gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
98                                 AltosLib.AO_GPS_NUM_SAT_SHIFT;
99                         break;
100                 case AltosLib.AO_LOG_GPS_LAT:
101                         gps = state.make_temp_gps(false);
102
103                         int lat32 = a | (b << 16);
104                         gps.lat = (double) lat32 / 1e7;
105                         break;
106                 case AltosLib.AO_LOG_GPS_LON:
107                         gps = state.make_temp_gps(false);
108
109                         int lon32 = a | (b << 16);
110                         gps.lon = (double) lon32 / 1e7;
111                         break;
112                 case AltosLib.AO_LOG_GPS_ALT:
113                         gps = state.make_temp_gps(false);
114                         gps.alt = a;
115                         break;
116                 case AltosLib.AO_LOG_GPS_SAT:
117                         gps = state.make_temp_gps(true);
118                         int svid = a;
119                         int c_n0 = b >> 8;
120                         gps.add_sat(svid, c_n0);
121                         break;
122                 case AltosLib.AO_LOG_GPS_DATE:
123                         gps = state.make_temp_gps(false);
124                         gps.year = (a & 0xff) + 2000;
125                         gps.month = a >> 8;
126                         gps.day = b & 0xff;
127                         break;
128                 }
129         }
130
131         public AltosEepromTM (AltosEepromChunk chunk, int start) throws ParseException {
132
133                 cmd = chunk.data(start);
134                 tick_valid = true;
135
136                 tick_valid = !chunk.erased(start, record_length);
137                 if (tick_valid) {
138                         if (AltosConvert.checksum(chunk.data, start, record_length) != 0)
139                                 throw new ParseException(String.format("invalid checksum at 0x%x",
140                                                                        chunk.address + start), 0);
141                 } else {
142                         cmd = AltosLib.AO_LOG_INVALID;
143                 }
144
145                 tick = chunk.data16(start + 2);
146                 a = chunk.data16(start + 4);
147                 b = chunk.data16(start + 6);
148         }
149
150         public AltosEepromTM (String line) {
151                 tick_valid = false;
152                 tick = 0;
153                 a = 0;
154                 b = 0;
155                 if (line == null) {
156                         cmd = AltosLib.AO_LOG_INVALID;
157                 } else {
158                         try {
159                                 String[] tokens = line.split("\\s+");
160
161                                 if (tokens[0].length() == 1) {
162                                         if (tokens.length != 4) {
163                                                 cmd = AltosLib.AO_LOG_INVALID;
164                                         } else {
165                                                 cmd = tokens[0].codePointAt(0);
166                                                 tick = Integer.parseInt(tokens[1],16);
167                                                 tick_valid = true;
168                                                 a = Integer.parseInt(tokens[2],16);
169                                                 b = Integer.parseInt(tokens[3],16);
170                                         }
171                                 } else {
172                                         cmd = AltosLib.AO_LOG_INVALID;
173                                 }
174                         } catch (NumberFormatException ne) {
175                                 cmd = AltosLib.AO_LOG_INVALID;
176                         }
177                 }
178         }
179
180         public AltosEepromTM(int in_cmd, int in_tick, int in_a, int in_b) {
181                 tick_valid = true;
182                 cmd = in_cmd;
183                 tick = in_tick;
184                 a = in_a;
185                 b = in_b;
186         }
187
188         static public LinkedList<AltosEeprom> read(FileInputStream input) {
189                 LinkedList<AltosEeprom> tms = new LinkedList<AltosEeprom>();
190
191                 for (;;) {
192                         try {
193                                 String line = AltosLib.gets(input);
194                                 if (line == null)
195                                         break;
196                                 AltosEepromTM tm = new AltosEepromTM(line);
197                                 tms.add(tm);
198                         } catch (IOException ie) {
199                                 break;
200                         }
201                 }
202
203                 return tms;
204         }
205
206 }