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 public void set_rssi(int rssi, int status) { }
30 public void set_received_time(long received_time) { }
32 public void set_acceleration(double accel) { }
33 public void set_pressure(double pa) { }
34 public void set_thrust(double N) { }
36 public void set_temperature(double deg_c) { }
37 public void set_battery_voltage(double volts) { }
39 public void set_apogee_voltage(double volts) { }
40 public void set_main_voltage(double volts) { }
42 public void set_gps(AltosGPS gps) {
45 gps.year != AltosLib.MISSING &&
46 gps.month != AltosLib.MISSING &&
47 gps.day != AltosLib.MISSING) {
52 public boolean done() {
58 public void set_gyro(double roll, double pitch, double yaw) { }
59 public void set_accel_ground(double along, double across, double through) { }
60 public void set_accel(double along, double across, double through) { }
61 public void set_mag(double along, double across, double through) { }
62 public void set_pyro_voltage(double volts) { }
63 public void set_igniter_voltage(double[] voltage) { }
64 public void set_pyro_fired(int pyro_mask) { }
65 public void set_companion(AltosCompanion companion) { }
66 public void set_kalman(double height, double speed, double acceleration) { }
67 public void set_orient(double new_orient) { }
69 public AltosEepromNameData(AltosCalData cal_data) {
74 public class AltosEepromDownload implements Runnable {
79 AltosEepromMonitor monitor;
81 AltosEepromList flights;
84 private boolean has_gps_date(AltosState state) {
88 AltosGPS gps = state.gps;
91 gps.year != AltosLib.MISSING &&
92 gps.month != AltosLib.MISSING &&
93 gps.day != AltosLib.MISSING;
96 private AltosFile MakeFile(int serial, int flight, AltosEepromNameData name_data) throws IOException {
97 AltosFile eeprom_name;
99 if (name_data.gps != null) {
100 AltosGPS gps = name_data.gps;
101 eeprom_name = new AltosFile(gps.year, gps.month, gps.day,
102 serial, flight, "eeprom");
104 eeprom_name = new AltosFile(serial, flight, "eeprom");
113 void LogError(String error) {
114 if (parse_errors != null)
115 parse_errors.concat(error.concat("\n"));
117 parse_errors = error;
120 class BlockCache extends Hashtable<Integer,AltosEepromChunk> {
123 AltosEepromChunk get(int start, boolean add) throws TimeoutException, InterruptedException {
125 return super.get(start);
126 AltosEepromChunk eechunk = new AltosEepromChunk(link, start, start == log.start_block);
132 public BlockCache(AltosEepromLog log) {
137 int FindLastLog(AltosEepromLog log, BlockCache cache) throws TimeoutException, InterruptedException {
138 int low = log.start_block;
139 int high = log.end_block - 1;
141 while (low <= high) {
142 int mid = (high + low) / 2;
144 if (!cache.get(mid, true).erased())
152 void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException, ParseException {
153 int block, state_block = 0;
154 int log_format = flights.config_data.log_format;
155 BlockCache cache = new BlockCache(log);
159 if (flights.config_data.serial < 0)
160 throw new IOException("no serial number found");
162 /* Set serial number in the monitor dialog window */
163 monitor.set_serial(log.serial);
164 monitor.set_flight(log.flight);
166 int start_block = log.start_block;
167 int end_block = FindLastLog(log, cache);
169 monitor.set_max(end_block - start_block - 1);
171 ArrayList<Byte> data = new ArrayList<Byte>();
173 /* Now scan the eeprom, reading blocks of data to create a byte array of data */
175 for (block = start_block; block < end_block; block++) {
176 monitor.set_block(block - start_block);
178 AltosEepromChunk eechunk = cache.get(block, false);
180 for (int i = 0; i < eechunk.data.length; i++)
181 data.add((byte) eechunk.data[i]);
184 /* Construct our internal representation of the eeprom data */
185 AltosEeprom eeprom = new AltosEeprom(flights.config_data, data);
187 /* Now see if we can't actually parse the resulting
188 * file to generate a better filename. Note that this
189 * doesn't need to work; we'll still save the data using
190 * a less accurate name.
192 AltosEepromRecordSet set = new AltosEepromRecordSet(eeprom);
193 AltosEepromNameData name_data = new AltosEepromNameData(set.cal_data());
195 for (AltosEepromRecord record : set.ordered) {
196 record.provide_data(name_data, set.cal_data());
197 if (name_data.done())
201 AltosFile f = MakeFile(flights.config_data.serial, log.flight, name_data);
205 boolean do_write = true;
208 do_write = monitor.check_overwrite(f);
211 FileWriter w = new FileWriter(f);
217 if (eeprom.errors != 0)
218 throw new ParseException(String.format("%d CRC Errors", eeprom.errors), 0);
221 static String label(int flight) {
228 static int flight(int flight) {
235 boolean success = false;
241 for (AltosEepromLog log : flights) {
243 if (log.download_selected) {
247 } catch (ParseException e) {
248 LogError(e.getMessage());
252 if (parse_errors != null) {
253 monitor.show_message(String.format("%s %d download error. Valid log data saved\n%s",
258 AltosEepromMonitor.WARNING_MESSAGE);
261 } catch (IOException ee) {
262 monitor.show_message(ee.getLocalizedMessage(),
264 AltosEepromMonitor.ERROR_MESSAGE);
265 } catch (InterruptedException ie) {
266 monitor.show_message(String.format("Connection to \"%s\" interrupted",
268 "Connection Interrupted",
269 AltosEepromMonitor.ERROR_MESSAGE);
270 } catch (TimeoutException te) {
271 monitor.show_message(String.format("Connection to \"%s\" failed",
274 AltosEepromMonitor.ERROR_MESSAGE);
279 } catch (InterruptedException ie) {
284 monitor.done(success);
287 public void start() {
288 eeprom_thread = new Thread(this);
289 monitor.set_thread(eeprom_thread);
290 eeprom_thread.start();
293 public AltosEepromDownload(AltosEepromMonitor given_monitor,
294 AltosLink given_link,
295 boolean given_remote,
296 AltosEepromList given_flights) {
298 monitor = given_monitor;
300 remote = given_remote;
301 flights = given_flights;