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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altoslib_11;
25 public class AltosEepromTM extends AltosEeprom {
29 public static final int record_length = 8;
31 public void write(PrintStream out) {
32 out.printf("%c %4x %4x %4x\n", cmd, tick, a, b);
35 public int record_length() { return record_length; }
37 public String string() {
38 return String.format("%c %4x %4x %4x\n", cmd, tick, a, b);
41 public void update_state(AltosState state) {
42 super.update_state(state);
46 /* Flush any pending GPS changes */
47 if (state.gps_pending) {
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:
62 case AltosLib.AO_LOG_FLIGHT:
63 state.set_state(AltosLib.ao_flight_pad);
64 state.set_ground_accel(a);
66 state.set_boost_tick(tick);
68 case AltosLib.AO_LOG_SENSOR:
70 state.set_pressure(AltosConvert.barometer_to_pressure(b));
72 case AltosLib.AO_LOG_PRESSURE:
73 state.set_pressure(AltosConvert.barometer_to_pressure(b));
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));
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));
83 case AltosLib.AO_LOG_STATE:
86 case AltosLib.AO_LOG_GPS_TIME:
87 gps = state.make_temp_gps(false);
89 gps.hour = (a & 0xff);
90 gps.minute = (a >> 8);
91 gps.second = (b & 0xff);
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;
100 case AltosLib.AO_LOG_GPS_LAT:
101 gps = state.make_temp_gps(false);
103 int lat32 = a | (b << 16);
104 gps.lat = (double) lat32 / 1e7;
106 case AltosLib.AO_LOG_GPS_LON:
107 gps = state.make_temp_gps(false);
109 int lon32 = a | (b << 16);
110 gps.lon = (double) lon32 / 1e7;
112 case AltosLib.AO_LOG_GPS_ALT:
113 gps = state.make_temp_gps(false);
116 case AltosLib.AO_LOG_GPS_SAT:
117 gps = state.make_temp_gps(true);
120 gps.add_sat(svid, c_n0);
122 case AltosLib.AO_LOG_GPS_DATE:
123 gps = state.make_temp_gps(false);
124 gps.year = (a & 0xff) + 2000;
131 public AltosEepromTM (AltosEepromChunk chunk, int start) throws ParseException {
133 cmd = chunk.data(start);
136 valid = !chunk.erased(start, record_length);
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);
142 cmd = AltosLib.AO_LOG_INVALID;
145 tick = chunk.data16(start + 2);
146 a = chunk.data16(start + 4);
147 b = chunk.data16(start + 6);
150 public AltosEepromTM (String line) {
156 cmd = AltosLib.AO_LOG_INVALID;
159 String[] tokens = line.split("\\s+");
161 if (tokens[0].length() == 1) {
162 if (tokens.length != 4) {
163 cmd = AltosLib.AO_LOG_INVALID;
165 cmd = tokens[0].codePointAt(0);
166 tick = Integer.parseInt(tokens[1],16);
168 a = Integer.parseInt(tokens[2],16);
169 b = Integer.parseInt(tokens[3],16);
172 cmd = AltosLib.AO_LOG_INVALID;
174 } catch (NumberFormatException ne) {
175 cmd = AltosLib.AO_LOG_INVALID;
180 public AltosEepromTM(int in_cmd, int in_tick, int in_a, int in_b) {
188 static public LinkedList<AltosEeprom> read(FileInputStream input) {
189 LinkedList<AltosEeprom> tms = new LinkedList<AltosEeprom>();
193 String line = AltosLib.gets(input);
196 AltosEepromTM tm = new AltosEepromTM(line);
198 } catch (IOException ie) {