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.
20 import java.awt.event.*;
25 import java.util.concurrent.*;
26 import org.altusmetrum.AltosLib.*;
28 public class AltosEepromDownload implements Runnable {
31 AltosSerial serial_line;
34 AltosEepromMonitor monitor;
40 FileWriter eeprom_file;
41 LinkedList<String> eeprom_pending;
43 AltosEepromList flights;
44 ActionListener listener;
46 ParseException parse_exception;
49 private void FlushPending() throws IOException {
50 for (String s : flights.config_data) {
52 eeprom_file.write('\n');
55 for (String s : eeprom_pending)
59 private void CheckFile(boolean force) throws IOException {
60 if (eeprom_file != null)
62 if (force || (flight != 0 && want_file)) {
63 AltosFile eeprom_name;
65 if (extension == null)
67 if (year != 0 && month != 0 && day != 0)
68 eeprom_name = new AltosFile(year, month, day, serial, flight, extension);
70 eeprom_name = new AltosFile(serial, flight, extension);
72 eeprom_file = new FileWriter(eeprom_name);
73 if (eeprom_file != null) {
74 monitor.set_file(eeprom_name.getName());
76 eeprom_pending = null;
81 void Log(AltosEepromRecord r) throws IOException {
82 if (r.cmd != Altos.AO_LOG_INVALID) {
83 String log_line = String.format("%c %4x %4x %4x\n",
84 r.cmd, r.tick, r.a, r.b);
85 if (eeprom_file != null)
86 eeprom_file.write(log_line);
88 eeprom_pending.add(log_line);
92 void set_serial(int in_serial) {
94 monitor.set_serial(serial);
97 void set_flight(int in_flight) {
99 monitor.set_flight(flight);
105 void CaptureFull(AltosEepromChunk eechunk) throws IOException {
106 boolean any_valid = false;
108 extension = "eeprom";
109 set_serial(flights.config_data.serial);
110 for (int i = 0; i < AltosEepromChunk.chunk_size && !done; i += AltosEepromRecord.record_length) {
112 AltosEepromRecord r = new AltosEepromRecord(eechunk, i);
113 if (r.cmd == Altos.AO_LOG_FLIGHT)
116 /* Monitor state transitions to update display */
117 if (r.cmd == Altos.AO_LOG_STATE && r.a <= Altos.ao_flight_landed) {
119 if (state > Altos.ao_flight_pad)
123 if (r.cmd == Altos.AO_LOG_GPS_DATE) {
124 year = 2000 + (r.a & 0xff);
125 month = (r.a >> 8) & 0xff;
129 if (r.cmd == Altos.AO_LOG_STATE && r.a == Altos.ao_flight_landed)
131 if (r.cmd != AltosLib.AO_LOG_INVALID)
134 } catch (ParseException pe) {
135 if (parse_exception == null)
136 parse_exception = pe;
149 void CaptureTiny (AltosEepromChunk eechunk) throws IOException {
150 boolean any_valid = false;
152 extension = "eeprom";
153 set_serial(flights.config_data.serial);
154 for (int i = 0; i < eechunk.data.length && !done; i += 2) {
155 int v = eechunk.data16(i);
158 if (i == 0 && start) {
162 r = new AltosEepromRecord(Altos.AO_LOG_FLIGHT, tiny_tick, 0, v);
167 if (Altos.ao_flight_startup <= s && s <= Altos.ao_flight_invalid) {
169 r = new AltosEepromRecord(Altos.AO_LOG_STATE, tiny_tick, state, 0);
170 if (state == Altos.ao_flight_landed)
178 r = new AltosEepromRecord(Altos.AO_LOG_PRESSURE, tiny_tick, 0, v);
181 * The flight software records ascent data every 100ms, and descent
184 if (state < Altos.ao_flight_drogue)
197 void LogTeleScience(AltosEepromTeleScience r) throws IOException {
198 if (r.type != Altos.AO_LOG_INVALID) {
199 String log_line = String.format("%c %4x %4x %d %5d %5d %5d %5d %5d %5d %5d %5d %5d %5d %5d %5d\n",
200 r.type, r.tick, r.tm_tick, r.tm_state,
201 r.data[0], r.data[1], r.data[2], r.data[3],
202 r.data[4], r.data[5], r.data[6], r.data[7],
203 r.data[8], r.data[9], r.data[10], r.data[11]);
204 if (eeprom_file != null)
205 eeprom_file.write(log_line);
207 eeprom_pending.add(log_line);
211 boolean telescience_start;
213 void CaptureTeleScience (AltosEepromChunk eechunk) throws IOException {
214 boolean any_valid = false;
216 extension = "science";
217 for (int i = 0; i < AltosEepromChunk.chunk_size && !done; i += AltosEepromTeleScience.record_length) {
219 AltosEepromTeleScience r = new AltosEepromTeleScience(eechunk, i);
220 if (r.type == AltosEepromTeleScience.AO_LOG_TELESCIENCE_START) {
221 if (telescience_start) {
225 set_serial(r.data[0]);
226 set_flight(r.data[1]);
227 telescience_start = true;
229 if (!telescience_start)
236 } catch (ParseException pe) {
237 if (parse_exception == null)
238 parse_exception = pe;
247 void LogMega(AltosEepromMega r) throws IOException {
248 if (r.cmd != Altos.AO_LOG_INVALID) {
249 String log_line = String.format("%c %4x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x\n",
251 r.data8[0], r.data8[1], r.data8[2], r.data8[3],
252 r.data8[4], r.data8[5], r.data8[6], r.data8[7],
253 r.data8[8], r.data8[9], r.data8[10], r.data8[11],
254 r.data8[12], r.data8[13], r.data8[14], r.data8[15],
255 r.data8[16], r.data8[17], r.data8[18], r.data8[19],
256 r.data8[20], r.data8[21], r.data8[22], r.data8[23],
257 r.data8[24], r.data8[25], r.data8[26], r.data8[27]);
258 if (eeprom_file != null)
259 eeprom_file.write(log_line);
261 eeprom_pending.add(log_line);
265 void CaptureMega(AltosEepromChunk eechunk) throws IOException {
266 boolean any_valid = false;
269 set_serial(flights.config_data.serial);
270 for (int i = 0; i < AltosEepromChunk.chunk_size && !done; i += AltosEepromMega.record_length) {
272 AltosEepromMega r = new AltosEepromMega(eechunk, i);
273 if (r.cmd == Altos.AO_LOG_FLIGHT)
274 set_flight(r.data16(0));
276 /* Monitor state transitions to update display */
277 if (r.cmd == Altos.AO_LOG_STATE && r.data16(0) <= Altos.ao_flight_landed) {
279 if (state > Altos.ao_flight_pad)
283 if (r.cmd == Altos.AO_LOG_GPS_TIME) {
284 year = 2000 + r.data8(14);
290 if (r.cmd == Altos.AO_LOG_STATE && r.data16(0) == Altos.ao_flight_landed)
294 } catch (ParseException pe) {
295 if (parse_exception == null)
296 parse_exception = pe;
305 void CaptureTelemetry(AltosEepromChunk eechunk) throws IOException {
309 void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException {
310 int block, state_block = 0;
311 int log_format = flights.config_data.log_format;
317 if (flights.config_data.serial < 0)
318 throw new IOException("no serial number found");
320 /* Reset per-capture variables */
327 eeprom_pending = new LinkedList<String>();
329 /* Set serial number in the monitor dialog window */
330 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
332 state = 0; state_block = log.start_block;
333 for (block = log.start_block; !done && block < log.end_block; block++) {
334 monitor.set_value(AltosLib.state_name(state), state, block - state_block);
336 AltosEepromChunk eechunk = new AltosEepromChunk(serial_line, block, block == log.start_block);
339 * Guess what kind of data is there if the device
343 if (log_format == Altos.AO_LOG_FORMAT_UNKNOWN) {
344 if (block == log.start_block) {
345 if (eechunk.data(0) == Altos.AO_LOG_FLIGHT)
346 log_format = Altos.AO_LOG_FORMAT_FULL;
348 log_format = Altos.AO_LOG_FORMAT_TINY;
352 switch (log_format) {
353 case AltosLib.AO_LOG_FORMAT_FULL:
354 extension = "eeprom";
355 CaptureFull(eechunk);
357 case AltosLib.AO_LOG_FORMAT_TINY:
358 extension = "eeprom";
359 CaptureTiny(eechunk);
361 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
363 CaptureTelemetry(eechunk);
365 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
366 extension = "science";
367 CaptureTeleScience(eechunk);
369 case AltosLib.AO_LOG_FORMAT_MEGAMETRUM:
371 CaptureMega(eechunk);
375 if (eeprom_file != null) {
381 private void show_message_internal(String message, String title, int message_type) {
382 JOptionPane.showMessageDialog(frame,
388 private void show_message(String in_message, String in_title, int in_message_type) {
389 final String message = in_message;
390 final String title = in_title;
391 final int message_type = in_message_type;
392 Runnable r = new Runnable() {
395 show_message_internal(message, title, message_type);
396 } catch (Exception ex) {
400 SwingUtilities.invokeLater(r);
405 boolean failed = false;
407 serial_line.start_remote();
409 for (AltosEepromLog log : flights) {
410 parse_exception = null;
415 if (parse_exception != null) {
417 show_message(String.format("Flight %d download error\n%s\nValid log data saved",
419 parse_exception.getMessage()),
420 serial_line.device.toShortString(),
421 JOptionPane.WARNING_MESSAGE);
425 } catch (IOException ee) {
426 show_message(ee.getLocalizedMessage(),
427 serial_line.device.toShortString(),
428 JOptionPane.ERROR_MESSAGE);
429 } catch (InterruptedException ie) {
430 System.out.printf("download interrupted\n");
431 } catch (TimeoutException te) {
432 show_message(String.format("Connection to \"%s\" failed",
433 serial_line.device.toShortString()),
435 JOptionPane.ERROR_MESSAGE);
439 serial_line.stop_remote();
440 } catch (InterruptedException ie) {
443 serial_line.flush_output();
446 if (listener != null) {
447 Runnable r = new Runnable() {
450 listener.actionPerformed(new ActionEvent(this,
453 } catch (Exception ex) {
457 SwingUtilities.invokeLater(r);
461 public void start() {
462 eeprom_thread = new Thread(this);
463 eeprom_thread.start();
466 public void addActionListener(ActionListener l) {
470 public AltosEepromDownload(JFrame given_frame,
471 AltosSerial given_serial_line,
472 boolean given_remote,
473 AltosEepromList given_flights) {
476 serial_line = given_serial_line;
477 serial_line.set_frame(frame);
478 remote = given_remote;
479 flights = given_flights;
482 monitor = new AltosEepromMonitor(frame, Altos.ao_flight_boost, Altos.ao_flight_landed);
483 monitor.addActionListener(new ActionListener() {
484 public void actionPerformed(ActionEvent e) {
485 if (eeprom_thread != null)
486 eeprom_thread.interrupt();