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_14;
24 import java.util.concurrent.*;
26 class AltosEepromNameData extends AltosDataListener {
29 boolean avoid_duplicate_files = false;
31 public void set_rssi(int rssi, int status) { }
32 public void set_received_time(long received_time) { }
34 public void set_acceleration(double accel) { }
35 public void set_pressure(double pa) { }
36 public void set_thrust(double N) { }
38 public void set_temperature(double deg_c) { }
39 public void set_battery_voltage(double volts) { }
41 public void set_apogee_voltage(double volts) { }
42 public void set_main_voltage(double volts) { }
44 public void set_avoid_duplicate_files() {
45 avoid_duplicate_files = true;
48 public void set_gps(AltosGPS gps, boolean set_location, boolean set_sats) {
49 super.set_gps(gps, set_location, set_sats);
51 gps.year != AltosLib.MISSING &&
52 gps.month != AltosLib.MISSING &&
53 gps.day != AltosLib.MISSING) {
58 public boolean done() {
64 public void set_gyro(double roll, double pitch, double yaw) { }
65 public void set_accel_ground(double along, double across, double through) { }
66 public void set_accel(double along, double across, double through) { }
67 public void set_mag(double along, double across, double through) { }
68 public void set_pyro_voltage(double volts) { }
69 public void set_igniter_voltage(double[] voltage) { }
70 public void set_pyro_fired(int pyro_mask) { }
71 public void set_companion(AltosCompanion companion) { }
72 public void set_kalman(double height, double speed, double acceleration) { }
73 public void set_orient(double new_orient) { }
74 public void set_motor_pressure(double motor_pressure) { }
76 public AltosEepromNameData(AltosCalData cal_data) {
81 public class AltosEepromDownload implements Runnable {
86 AltosEepromMonitor monitor;
88 AltosEepromList flights;
91 private boolean has_gps_date(AltosState state) {
95 AltosGPS gps = state.gps;
98 gps.year != AltosLib.MISSING &&
99 gps.month != AltosLib.MISSING &&
100 gps.day != AltosLib.MISSING;
103 private AltosFile MakeFile(int serial, int flight, AltosEepromNameData name_data) throws IOException {
104 AltosFile eeprom_name;
107 if (name_data.gps != null) {
108 AltosGPS gps = name_data.gps;
109 eeprom_name = new AltosFile(gps.year, gps.month, gps.day,
110 serial, flight, "eeprom");
112 eeprom_name = new AltosFile(serial, flight, "eeprom");
113 if (!name_data.avoid_duplicate_files)
115 if (!eeprom_name.exists())
127 void LogError(String error) {
128 if (parse_errors != null)
129 parse_errors.concat(error.concat("\n"));
131 parse_errors = error;
134 class BlockCache extends Hashtable<Integer,AltosEepromChunk> {
137 AltosEepromChunk get(int start, boolean add) throws TimeoutException, InterruptedException {
139 return super.get(start);
140 AltosEepromChunk eechunk = new AltosEepromChunk(link, start, start == log.start_block);
146 public BlockCache(AltosEepromLog log) {
151 int FindLastLog(AltosEepromLog log, BlockCache cache) throws TimeoutException, InterruptedException {
152 int low = log.start_block;
153 int high = log.end_block - 1;
155 while (low <= high) {
156 int mid = (high + low) / 2;
158 if (!cache.get(mid, true).erased())
166 void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException, ParseException {
167 int block, state_block = 0;
168 int log_format = flights.config_data.log_format;
169 BlockCache cache = new BlockCache(log);
173 if (flights.config_data.serial < 0)
174 throw new IOException("no serial number found");
176 /* Set serial number in the monitor dialog window */
177 monitor.set_serial(log.serial);
178 monitor.set_flight(log.flight);
180 int start_block = log.start_block;
181 int end_block = FindLastLog(log, cache);
183 monitor.set_max(end_block - start_block - 1);
185 ArrayList<Byte> data = new ArrayList<Byte>();
187 /* Now scan the eeprom, reading blocks of data to create a byte array of data */
189 for (block = start_block; block < end_block; block++) {
190 monitor.set_block(block - start_block);
192 AltosEepromChunk eechunk = cache.get(block, false);
194 for (int i = 0; i < eechunk.data.length; i++)
195 data.add((byte) eechunk.data[i]);
198 /* Construct our internal representation of the eeprom data */
199 AltosEeprom eeprom = new AltosEeprom(flights.config_data, data);
201 /* Now see if we can't actually parse the resulting
202 * file to generate a better filename. Note that this
203 * doesn't need to work; we'll still save the data using
204 * a less accurate name.
206 AltosEepromRecordSet set = new AltosEepromRecordSet(eeprom);
207 AltosEepromNameData name_data = new AltosEepromNameData(set.cal_data());
209 for (AltosEepromRecord record : set.ordered) {
210 record.provide_data(name_data, set.cal_data());
211 if (name_data.done())
215 AltosFile f = MakeFile(flights.config_data.serial, log.flight, name_data);
219 boolean do_write = true;
222 do_write = monitor.check_overwrite(f);
225 FileWriter w = new FileWriter(f);
231 if (eeprom.errors != 0)
232 throw new ParseException(String.format("%d CRC Errors", eeprom.errors), 0);
235 static String label(int flight) {
242 static int flight(int flight) {
249 boolean success = false;
255 for (AltosEepromLog log : flights) {
257 if (log.download_selected) {
261 } catch (ParseException e) {
262 LogError(e.getMessage());
266 if (parse_errors != null) {
267 monitor.show_message(String.format("%s %d download error. Valid log data saved\n%s",
272 AltosEepromMonitor.WARNING_MESSAGE);
275 } catch (IOException ee) {
276 monitor.show_message(ee.getLocalizedMessage(),
278 AltosEepromMonitor.ERROR_MESSAGE);
279 } catch (InterruptedException ie) {
280 monitor.show_message(String.format("Connection to \"%s\" interrupted",
282 "Connection Interrupted",
283 AltosEepromMonitor.ERROR_MESSAGE);
284 } catch (TimeoutException te) {
285 monitor.show_message(String.format("Connection to \"%s\" failed",
288 AltosEepromMonitor.ERROR_MESSAGE);
293 } catch (InterruptedException ie) {
298 monitor.done(success);
301 public void start() {
302 eeprom_thread = new Thread(this);
303 monitor.set_thread(eeprom_thread);
304 eeprom_thread.start();
307 public AltosEepromDownload(AltosEepromMonitor given_monitor,
308 AltosLink given_link,
309 boolean given_remote,
310 AltosEepromList given_flights) {
312 monitor = given_monitor;
314 remote = given_remote;
315 flights = given_flights;