2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
18 package org.altusmetrum.altoslib_4;
24 public class AltosEepromTM extends AltosEeprom {
28 public static final int record_length = 8;
30 public void write(PrintStream out) {
31 out.printf("%c %4x %4x %4x\n", cmd, tick, a, b);
34 public int record_length() { return record_length; }
36 public String string() {
37 return String.format("%c %4x %4x %4x\n", cmd, tick, a, b);
40 public void update_state(AltosState state) {
41 super.update_state(state);
45 /* Flush any pending GPS changes */
46 if (state.gps_pending) {
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:
61 case AltosLib.AO_LOG_FLIGHT:
62 state.set_state(AltosLib.ao_flight_pad);
63 state.set_ground_accel(a);
65 state.set_boost_tick(tick);
67 case AltosLib.AO_LOG_SENSOR:
69 state.set_pressure(AltosConvert.barometer_to_pressure(b));
71 case AltosLib.AO_LOG_PRESSURE:
72 state.set_pressure(AltosConvert.barometer_to_pressure(b));
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));
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));
82 case AltosLib.AO_LOG_STATE:
85 case AltosLib.AO_LOG_GPS_TIME:
86 gps = state.make_temp_gps(false);
88 gps.hour = (a & 0xff);
89 gps.minute = (a >> 8);
90 gps.second = (b & 0xff);
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;
99 case AltosLib.AO_LOG_GPS_LAT:
100 gps = state.make_temp_gps(false);
102 int lat32 = a | (b << 16);
103 gps.lat = (double) lat32 / 1e7;
105 case AltosLib.AO_LOG_GPS_LON:
106 gps = state.make_temp_gps(false);
108 int lon32 = a | (b << 16);
109 gps.lon = (double) lon32 / 1e7;
111 case AltosLib.AO_LOG_GPS_ALT:
112 gps = state.make_temp_gps(false);
115 case AltosLib.AO_LOG_GPS_SAT:
116 gps = state.make_temp_gps(true);
119 gps.add_sat(svid, c_n0);
121 case AltosLib.AO_LOG_GPS_DATE:
122 gps = state.make_temp_gps(false);
123 gps.year = (a & 0xff) + 2000;
130 public AltosEepromTM (AltosEepromChunk chunk, int start) throws ParseException {
132 cmd = chunk.data(start);
135 valid = !chunk.erased(start, record_length);
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);
141 cmd = AltosLib.AO_LOG_INVALID;
144 tick = chunk.data16(start + 2);
145 a = chunk.data16(start + 4);
146 b = chunk.data16(start + 6);
149 public AltosEepromTM (String line) {
155 cmd = AltosLib.AO_LOG_INVALID;
158 String[] tokens = line.split("\\s+");
160 if (tokens[0].length() == 1) {
161 if (tokens.length != 4) {
162 cmd = AltosLib.AO_LOG_INVALID;
164 cmd = tokens[0].codePointAt(0);
165 tick = Integer.parseInt(tokens[1],16);
167 a = Integer.parseInt(tokens[2],16);
168 b = Integer.parseInt(tokens[3],16);
171 cmd = AltosLib.AO_LOG_INVALID;
173 } catch (NumberFormatException ne) {
174 cmd = AltosLib.AO_LOG_INVALID;
179 public AltosEepromTM(int in_cmd, int in_tick, int in_a, int in_b) {
187 static public LinkedList<AltosEeprom> read(FileInputStream input) {
188 LinkedList<AltosEeprom> tms = new LinkedList<AltosEeprom>();
192 String line = AltosLib.gets(input);
195 AltosEepromTM tm = new AltosEepromTM(line);
197 } catch (IOException ie) {