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.
21 import java.awt.event.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.*;
33 public class AltosEepromDownload implements Runnable {
36 AltosSerial serial_line;
39 AltosEepromMonitor monitor;
45 FileWriter eeprom_file;
46 LinkedList<String> eeprom_pending;
48 AltosEepromList flights;
49 ActionListener listener;
51 ParseException parse_exception;
54 private void FlushPending() throws IOException {
55 for (String s : flights.config_data) {
57 eeprom_file.write('\n');
60 for (String s : eeprom_pending)
64 private void CheckFile(boolean force) throws IOException {
65 if (eeprom_file != null)
67 if (force || (flight != 0 && want_file)) {
68 AltosFile eeprom_name;
70 if (extension == null)
72 if (year != 0 && month != 0 && day != 0)
73 eeprom_name = new AltosFile(year, month, day, serial, flight, extension);
75 eeprom_name = new AltosFile(serial, flight, extension);
77 eeprom_file = new FileWriter(eeprom_name);
78 if (eeprom_file != null) {
79 monitor.set_file(eeprom_name.getName());
81 eeprom_pending = null;
86 void Log(AltosEepromRecord r) throws IOException {
87 if (r.cmd != Altos.AO_LOG_INVALID) {
88 String log_line = String.format("%c %4x %4x %4x\n",
89 r.cmd, r.tick, r.a, r.b);
90 if (eeprom_file != null)
91 eeprom_file.write(log_line);
93 eeprom_pending.add(log_line);
97 void set_serial(int in_serial) {
99 monitor.set_serial(serial);
102 void set_flight(int in_flight) {
104 monitor.set_flight(flight);
110 void CaptureFull(AltosEepromChunk eechunk) throws IOException {
111 boolean any_valid = false;
113 extension = "eeprom";
114 set_serial(flights.config_data.serial);
115 for (int i = 0; i < eechunk.chunk_size && !done; i += AltosEepromRecord.record_length) {
117 AltosEepromRecord r = new AltosEepromRecord(eechunk, i);
118 if (r.cmd == Altos.AO_LOG_FLIGHT)
121 /* Monitor state transitions to update display */
122 if (r.cmd == Altos.AO_LOG_STATE && r.a <= Altos.ao_flight_landed) {
124 if (state > Altos.ao_flight_pad)
128 if (r.cmd == Altos.AO_LOG_GPS_DATE) {
129 year = 2000 + (r.a & 0xff);
130 month = (r.a >> 8) & 0xff;
134 if (r.cmd == Altos.AO_LOG_STATE && r.a == Altos.ao_flight_landed)
138 } catch (ParseException pe) {
139 if (parse_exception == null)
140 parse_exception = pe;
153 void CaptureTiny (AltosEepromChunk eechunk) throws IOException {
154 boolean any_valid = false;
156 extension = "eeprom";
157 set_serial(flights.config_data.serial);
158 for (int i = 0; i < eechunk.data.length && !done; i += 2) {
159 int v = eechunk.data16(i);
162 if (i == 0 && start) {
166 r = new AltosEepromRecord(Altos.AO_LOG_FLIGHT, tiny_tick, 0, v);
171 if (Altos.ao_flight_startup <= s && s <= Altos.ao_flight_invalid) {
173 r = new AltosEepromRecord(Altos.AO_LOG_STATE, tiny_tick, state, 0);
174 if (state == Altos.ao_flight_landed)
182 r = new AltosEepromRecord(Altos.AO_LOG_PRESSURE, tiny_tick, 0, v);
185 * The flight software records ascent data every 100ms, and descent
188 if (state < Altos.ao_flight_drogue)
201 void LogTeleScience(AltosEepromTeleScience r) throws IOException {
202 if (r.type != Altos.AO_LOG_INVALID) {
203 String log_line = String.format("%c %4x %4x %d %5d %5d %5d %5d %5d %5d %5d %5d %5d %5d %5d %5d\n",
204 r.type, r.tick, r.tm_tick, r.tm_state,
205 r.data[0], r.data[1], r.data[2], r.data[3],
206 r.data[4], r.data[5], r.data[6], r.data[7],
207 r.data[8], r.data[9], r.data[10], r.data[11]);
208 if (eeprom_file != null)
209 eeprom_file.write(log_line);
211 eeprom_pending.add(log_line);
215 boolean telescience_start;
217 void CaptureTeleScience (AltosEepromChunk eechunk) throws IOException {
218 boolean any_valid = false;
220 extension = "science";
221 for (int i = 0; i < eechunk.chunk_size && !done; i += AltosEepromTeleScience.record_length) {
223 AltosEepromTeleScience r = new AltosEepromTeleScience(eechunk, i);
224 if (r.type == AltosEepromTeleScience.AO_LOG_TELESCIENCE_START) {
225 if (telescience_start) {
229 set_serial(r.data[0]);
230 set_flight(r.data[1]);
231 telescience_start = true;
233 if (!telescience_start)
240 } catch (ParseException pe) {
241 if (parse_exception == null)
242 parse_exception = pe;
251 void CaptureTelemetry(AltosEepromChunk eechunk) throws IOException {
255 void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException {
256 int block, state_block = 0;
257 int log_format = flights.config_data.log_format;
263 if (flights.config_data.serial == 0)
264 throw new IOException("no serial number found");
266 /* Reset per-capture variables */
273 eeprom_pending = new LinkedList<String>();
275 /* Set serial number in the monitor dialog window */
276 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
278 state = 0; state_block = log.start_block;
279 for (block = log.start_block; !done && block < log.end_block; block++) {
280 monitor.set_value(Altos.state_to_string[state], state, block - state_block);
282 AltosEepromChunk eechunk = new AltosEepromChunk(serial_line, block, block == log.start_block);
285 * Guess what kind of data is there if the device
289 if (log_format == Altos.AO_LOG_FORMAT_UNKNOWN) {
290 if (block == log.start_block) {
291 if (eechunk.data(0) == Altos.AO_LOG_FLIGHT)
292 log_format = Altos.AO_LOG_FORMAT_FULL;
294 log_format = Altos.AO_LOG_FORMAT_TINY;
298 switch (log_format) {
299 case Altos.AO_LOG_FORMAT_FULL:
300 extension = "eeprom";
301 CaptureFull(eechunk);
303 case Altos.AO_LOG_FORMAT_TINY:
304 extension = "eeprom";
305 CaptureTiny(eechunk);
307 case Altos.AO_LOG_FORMAT_TELEMETRY:
309 CaptureTelemetry(eechunk);
311 case Altos.AO_LOG_FORMAT_TELESCIENCE:
312 extension = "science";
313 CaptureTeleScience(eechunk);
318 if (eeprom_file != null) {
324 private void show_message_internal(String message, String title, int message_type) {
325 JOptionPane.showMessageDialog(frame,
331 private void show_message(String in_message, String in_title, int in_message_type) {
332 final String message = in_message;
333 final String title = in_title;
334 final int message_type = in_message_type;
335 Runnable r = new Runnable() {
338 show_message_internal(message, title, message_type);
339 } catch (Exception ex) {
343 SwingUtilities.invokeLater(r);
348 boolean failed = false;
350 serial_line.start_remote();
352 for (AltosEepromLog log : flights) {
353 parse_exception = null;
358 if (parse_exception != null) {
360 show_message(String.format("Flight %d download error\n%s\nValid log data saved",
362 parse_exception.getMessage()),
363 serial_line.device.toShortString(),
364 JOptionPane.WARNING_MESSAGE);
368 } catch (IOException ee) {
369 show_message(ee.getLocalizedMessage(),
370 serial_line.device.toShortString(),
371 JOptionPane.ERROR_MESSAGE);
372 } catch (InterruptedException ie) {
373 System.out.printf("download interrupted\n");
374 } catch (TimeoutException te) {
375 show_message(String.format("Connection to \"%s\" failed",
376 serial_line.device.toShortString()),
378 JOptionPane.ERROR_MESSAGE);
382 serial_line.stop_remote();
383 } catch (InterruptedException ie) {
386 serial_line.flush_output();
389 if (listener != null) {
390 Runnable r = new Runnable() {
393 listener.actionPerformed(new ActionEvent(this,
396 } catch (Exception ex) {
400 SwingUtilities.invokeLater(r);
404 public void start() {
405 eeprom_thread = new Thread(this);
406 eeprom_thread.start();
409 public void addActionListener(ActionListener l) {
413 public AltosEepromDownload(JFrame given_frame,
414 AltosSerial given_serial_line,
415 boolean given_remote,
416 AltosEepromList given_flights) {
419 serial_line = given_serial_line;
420 serial_line.set_frame(frame);
421 remote = given_remote;
422 flights = given_flights;
425 monitor = new AltosEepromMonitor(frame, Altos.ao_flight_boost, Altos.ao_flight_landed);
426 monitor.addActionListener(new ActionListener() {
427 public void actionPerformed(ActionEvent e) {
428 if (eeprom_thread != null)
429 eeprom_thread.interrupt();