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