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_1;
24 public class AltosEepromTM extends AltosEeprom {
29 public boolean tick_valid;
31 public static final int record_length = 8;
33 public void write(PrintStream out) {
34 out.printf("%c %4x %4x %4x\n", cmd, tick, a, b);
37 public int record_length() { return record_length; }
39 public String string() {
40 return String.format("%c %4x %4x %4x\n", cmd, tick, a, b);
43 public void update_state(AltosState 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:
71 double pressure = AltosConvert.barometer_to_pressure(b);
72 state.set_pressure(pressure);
74 case AltosLib.AO_LOG_PRESSURE:
76 state.set_pressure(AltosConvert.barometer_to_pressure(b));
78 case AltosLib.AO_LOG_TEMP_VOLT:
80 state.set_temperature(AltosConvert.thermometer_to_temperature(a));
81 state.set_battery_voltage(AltosConvert.cc_battery_to_voltage(b));
83 case AltosLib.AO_LOG_DEPLOY:
85 state.set_apogee_voltage(AltosConvert.cc_ignitor_to_voltage(a));
86 state.set_main_voltage(AltosConvert.cc_ignitor_to_voltage(b));
88 case AltosLib.AO_LOG_STATE:
92 case AltosLib.AO_LOG_GPS_TIME:
93 gps = state.make_temp_gps(false);
95 gps.hour = (a & 0xff);
96 gps.minute = (a >> 8);
97 gps.second = (b & 0xff);
101 gps.connected = (flags & AltosLib.AO_GPS_RUNNING) != 0;
102 gps.locked = (flags & AltosLib.AO_GPS_VALID) != 0;
103 gps.nsat = (flags & AltosLib.AO_GPS_NUM_SAT_MASK) >>
104 AltosLib.AO_GPS_NUM_SAT_SHIFT;
106 case AltosLib.AO_LOG_GPS_LAT:
107 gps = state.make_temp_gps(false);
109 int lat32 = a | (b << 16);
110 gps.lat = (double) lat32 / 1e7;
112 case AltosLib.AO_LOG_GPS_LON:
113 gps = state.make_temp_gps(false);
115 int lon32 = a | (b << 16);
116 gps.lon = (double) lon32 / 1e7;
118 case AltosLib.AO_LOG_GPS_ALT:
119 gps = state.make_temp_gps(false);
122 case AltosLib.AO_LOG_GPS_SAT:
123 gps = state.make_temp_gps(true);
126 gps.add_sat(svid, c_n0);
128 case AltosLib.AO_LOG_GPS_DATE:
129 gps = state.make_temp_gps(false);
130 gps.year = (a & 0xff) + 2000;
137 public AltosEepromTM (AltosEepromChunk chunk, int start) throws ParseException {
139 cmd = chunk.data(start);
142 tick_valid = !chunk.erased(start, record_length);
144 if (AltosConvert.checksum(chunk.data, start, record_length) != 0)
145 throw new ParseException(String.format("invalid checksum at 0x%x",
146 chunk.address + start), 0);
148 cmd = AltosLib.AO_LOG_INVALID;
151 tick = chunk.data16(start + 2);
152 a = chunk.data16(start + 4);
153 b = chunk.data16(start + 6);
156 public AltosEepromTM (String line) {
162 cmd = AltosLib.AO_LOG_INVALID;
165 String[] tokens = line.split("\\s+");
167 if (tokens[0].length() == 1) {
168 if (tokens.length != 4) {
169 cmd = AltosLib.AO_LOG_INVALID;
171 cmd = tokens[0].codePointAt(0);
172 tick = Integer.parseInt(tokens[1],16);
174 a = Integer.parseInt(tokens[2],16);
175 b = Integer.parseInt(tokens[3],16);
178 cmd = AltosLib.AO_LOG_INVALID;
180 } catch (NumberFormatException ne) {
181 cmd = AltosLib.AO_LOG_INVALID;
186 public AltosEepromTM(int in_cmd, int in_tick, int in_a, int in_b) {
194 static public LinkedList<AltosEeprom> read(FileInputStream input) {
195 LinkedList<AltosEeprom> tms = new LinkedList<AltosEeprom>();
199 String line = AltosLib.gets(input);
202 AltosEepromTM tm = new AltosEepromTM(line);
204 } catch (IOException ie) {