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_13;
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) {
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) { }
75 public AltosEepromNameData(AltosCalData cal_data) {
80 public class AltosEepromDownload implements Runnable {
85 AltosEepromMonitor monitor;
87 AltosEepromList flights;
90 private boolean has_gps_date(AltosState state) {
94 AltosGPS gps = state.gps;
97 gps.year != AltosLib.MISSING &&
98 gps.month != AltosLib.MISSING &&
99 gps.day != AltosLib.MISSING;
102 private AltosFile MakeFile(int serial, int flight, AltosEepromNameData name_data) throws IOException {
103 AltosFile eeprom_name;
106 if (name_data.gps != null) {
107 AltosGPS gps = name_data.gps;
108 eeprom_name = new AltosFile(gps.year, gps.month, gps.day,
109 serial, flight, "eeprom");
111 eeprom_name = new AltosFile(serial, flight, "eeprom");
112 if (!name_data.avoid_duplicate_files)
114 if (!eeprom_name.exists())
126 void LogError(String error) {
127 if (parse_errors != null)
128 parse_errors.concat(error.concat("\n"));
130 parse_errors = error;
133 class BlockCache extends Hashtable<Integer,AltosEepromChunk> {
136 AltosEepromChunk get(int start, boolean add) throws TimeoutException, InterruptedException {
138 return super.get(start);
139 AltosEepromChunk eechunk = new AltosEepromChunk(link, start, start == log.start_block);
145 public BlockCache(AltosEepromLog log) {
150 int FindLastLog(AltosEepromLog log, BlockCache cache) throws TimeoutException, InterruptedException {
151 int low = log.start_block;
152 int high = log.end_block - 1;
154 while (low <= high) {
155 int mid = (high + low) / 2;
157 if (!cache.get(mid, true).erased())
165 void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException, ParseException {
166 int block, state_block = 0;
167 int log_format = flights.config_data.log_format;
168 BlockCache cache = new BlockCache(log);
172 if (flights.config_data.serial < 0)
173 throw new IOException("no serial number found");
175 /* Set serial number in the monitor dialog window */
176 monitor.set_serial(log.serial);
177 monitor.set_flight(log.flight);
179 int start_block = log.start_block;
180 int end_block = FindLastLog(log, cache);
182 monitor.set_max(end_block - start_block - 1);
184 ArrayList<Byte> data = new ArrayList<Byte>();
186 /* Now scan the eeprom, reading blocks of data to create a byte array of data */
188 for (block = start_block; block < end_block; block++) {
189 monitor.set_block(block - start_block);
191 AltosEepromChunk eechunk = cache.get(block, false);
193 for (int i = 0; i < eechunk.data.length; i++)
194 data.add((byte) eechunk.data[i]);
197 /* Construct our internal representation of the eeprom data */
198 AltosEeprom eeprom = new AltosEeprom(flights.config_data, data);
200 /* Now see if we can't actually parse the resulting
201 * file to generate a better filename. Note that this
202 * doesn't need to work; we'll still save the data using
203 * a less accurate name.
205 AltosEepromRecordSet set = new AltosEepromRecordSet(eeprom);
206 AltosEepromNameData name_data = new AltosEepromNameData(set.cal_data());
208 for (AltosEepromRecord record : set.ordered) {
209 record.provide_data(name_data, set.cal_data());
210 if (name_data.done())
214 AltosFile f = MakeFile(flights.config_data.serial, log.flight, name_data);
218 boolean do_write = true;
221 do_write = monitor.check_overwrite(f);
224 FileWriter w = new FileWriter(f);
230 if (eeprom.errors != 0)
231 throw new ParseException(String.format("%d CRC Errors", eeprom.errors), 0);
234 static String label(int flight) {
241 static int flight(int flight) {
248 boolean success = false;
254 for (AltosEepromLog log : flights) {
256 if (log.download_selected) {
260 } catch (ParseException e) {
261 LogError(e.getMessage());
265 if (parse_errors != null) {
266 monitor.show_message(String.format("%s %d download error. Valid log data saved\n%s",
271 AltosEepromMonitor.WARNING_MESSAGE);
274 } catch (IOException ee) {
275 monitor.show_message(ee.getLocalizedMessage(),
277 AltosEepromMonitor.ERROR_MESSAGE);
278 } catch (InterruptedException ie) {
279 monitor.show_message(String.format("Connection to \"%s\" interrupted",
281 "Connection Interrupted",
282 AltosEepromMonitor.ERROR_MESSAGE);
283 } catch (TimeoutException te) {
284 monitor.show_message(String.format("Connection to \"%s\" failed",
287 AltosEepromMonitor.ERROR_MESSAGE);
292 } catch (InterruptedException ie) {
297 monitor.done(success);
300 public void start() {
301 eeprom_thread = new Thread(this);
302 monitor.set_thread(eeprom_thread);
303 eeprom_thread.start();
306 public AltosEepromDownload(AltosEepromMonitor given_monitor,
307 AltosLink given_link,
308 boolean given_remote,
309 AltosEepromList given_flights) {
311 monitor = given_monitor;
313 remote = given_remote;
314 flights = given_flights;