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.*;
30 import org.altusmetrum.AltosLib.*;
34 public class AltosEepromDownload implements Runnable {
37 AltosSerial serial_line;
40 AltosEepromMonitor monitor;
46 FileWriter eeprom_file;
47 LinkedList<String> eeprom_pending;
49 AltosEepromList flights;
50 ActionListener listener;
52 ParseException parse_exception;
55 private void FlushPending() throws IOException {
56 for (String s : flights.config_data) {
58 eeprom_file.write('\n');
61 for (String s : eeprom_pending)
65 private void CheckFile(boolean force) throws IOException {
66 if (eeprom_file != null)
68 if (force || (flight != 0 && want_file)) {
69 AltosFile eeprom_name;
71 if (extension == null)
73 if (year != 0 && month != 0 && day != 0)
74 eeprom_name = new AltosFile(year, month, day, serial, flight, extension);
76 eeprom_name = new AltosFile(serial, flight, extension);
78 eeprom_file = new FileWriter(eeprom_name);
79 if (eeprom_file != null) {
80 monitor.set_file(eeprom_name.getName());
82 eeprom_pending = null;
87 void Log(AltosEepromRecord r) throws IOException {
88 if (r.cmd != Altos.AO_LOG_INVALID) {
89 String log_line = String.format("%c %4x %4x %4x\n",
90 r.cmd, r.tick, r.a, r.b);
91 if (eeprom_file != null)
92 eeprom_file.write(log_line);
94 eeprom_pending.add(log_line);
98 void set_serial(int in_serial) {
100 monitor.set_serial(serial);
103 void set_flight(int in_flight) {
105 monitor.set_flight(flight);
111 void CaptureFull(AltosEepromChunk eechunk) throws IOException {
112 boolean any_valid = false;
114 extension = "eeprom";
115 set_serial(flights.config_data.serial);
116 for (int i = 0; i < eechunk.chunk_size && !done; i += AltosEepromRecord.record_length) {
118 AltosEepromRecord r = new AltosEepromRecord(eechunk, i);
119 if (r.cmd == Altos.AO_LOG_FLIGHT)
122 /* Monitor state transitions to update display */
123 if (r.cmd == Altos.AO_LOG_STATE && r.a <= Altos.ao_flight_landed) {
125 if (state > Altos.ao_flight_pad)
129 if (r.cmd == Altos.AO_LOG_GPS_DATE) {
130 year = 2000 + (r.a & 0xff);
131 month = (r.a >> 8) & 0xff;
135 if (r.cmd == Altos.AO_LOG_STATE && r.a == Altos.ao_flight_landed)
139 } catch (ParseException pe) {
140 if (parse_exception == null)
141 parse_exception = pe;
154 void CaptureTiny (AltosEepromChunk eechunk) throws IOException {
155 boolean any_valid = false;
157 extension = "eeprom";
158 set_serial(flights.config_data.serial);
159 for (int i = 0; i < eechunk.data.length && !done; i += 2) {
160 int v = eechunk.data16(i);
163 if (i == 0 && start) {
167 r = new AltosEepromRecord(Altos.AO_LOG_FLIGHT, tiny_tick, 0, v);
172 if (Altos.ao_flight_startup <= s && s <= Altos.ao_flight_invalid) {
174 r = new AltosEepromRecord(Altos.AO_LOG_STATE, tiny_tick, state, 0);
175 if (state == Altos.ao_flight_landed)
183 r = new AltosEepromRecord(Altos.AO_LOG_PRESSURE, tiny_tick, 0, v);
186 * The flight software records ascent data every 100ms, and descent
189 if (state < Altos.ao_flight_drogue)
202 void LogTeleScience(AltosEepromTeleScience r) throws IOException {
203 if (r.type != Altos.AO_LOG_INVALID) {
204 String log_line = String.format("%c %4x %4x %d %5d %5d %5d %5d %5d %5d %5d %5d %5d %5d %5d %5d\n",
205 r.type, r.tick, r.tm_tick, r.tm_state,
206 r.data[0], r.data[1], r.data[2], r.data[3],
207 r.data[4], r.data[5], r.data[6], r.data[7],
208 r.data[8], r.data[9], r.data[10], r.data[11]);
209 if (eeprom_file != null)
210 eeprom_file.write(log_line);
212 eeprom_pending.add(log_line);
216 boolean telescience_start;
218 void CaptureTeleScience (AltosEepromChunk eechunk) throws IOException {
219 boolean any_valid = false;
221 extension = "science";
222 for (int i = 0; i < eechunk.chunk_size && !done; i += AltosEepromTeleScience.record_length) {
224 AltosEepromTeleScience r = new AltosEepromTeleScience(eechunk, i);
225 if (r.type == AltosEepromTeleScience.AO_LOG_TELESCIENCE_START) {
226 if (telescience_start) {
230 set_serial(r.data[0]);
231 set_flight(r.data[1]);
232 telescience_start = true;
234 if (!telescience_start)
241 } catch (ParseException pe) {
242 if (parse_exception == null)
243 parse_exception = pe;
252 void LogMega(AltosEepromMega r) throws IOException {
253 if (r.cmd != Altos.AO_LOG_INVALID) {
254 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",
256 r.data8[0], r.data8[1], r.data8[2], r.data8[3],
257 r.data8[4], r.data8[5], r.data8[6], r.data8[7],
258 r.data8[8], r.data8[9], r.data8[10], r.data8[11],
259 r.data8[12], r.data8[13], r.data8[14], r.data8[15],
260 r.data8[16], r.data8[17], r.data8[18], r.data8[19],
261 r.data8[20], r.data8[21], r.data8[22], r.data8[23],
262 r.data8[24], r.data8[25], r.data8[26], r.data8[27]);
263 if (eeprom_file != null)
264 eeprom_file.write(log_line);
266 eeprom_pending.add(log_line);
270 void CaptureMega(AltosEepromChunk eechunk) throws IOException {
271 boolean any_valid = false;
274 set_serial(flights.config_data.serial);
275 for (int i = 0; i < eechunk.chunk_size && !done; i += AltosEepromMega.record_length) {
277 AltosEepromMega r = new AltosEepromMega(eechunk, i);
278 if (r.cmd == Altos.AO_LOG_FLIGHT)
279 set_flight(r.data16(0));
281 /* Monitor state transitions to update display */
282 if (r.cmd == Altos.AO_LOG_STATE && r.data16(0) <= Altos.ao_flight_landed) {
284 if (state > Altos.ao_flight_pad)
288 if (r.cmd == Altos.AO_LOG_GPS_TIME) {
289 year = 2000 + r.data8(14);
295 if (r.cmd == Altos.AO_LOG_STATE && r.data16(0) == Altos.ao_flight_landed)
299 } catch (ParseException pe) {
300 if (parse_exception == null)
301 parse_exception = pe;
310 void CaptureTelemetry(AltosEepromChunk eechunk) throws IOException {
314 void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException {
315 int block, state_block = 0;
316 int log_format = flights.config_data.log_format;
322 if (flights.config_data.serial == 0)
323 throw new IOException("no serial number found");
325 /* Reset per-capture variables */
332 eeprom_pending = new LinkedList<String>();
334 /* Set serial number in the monitor dialog window */
335 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
337 state = 0; state_block = log.start_block;
338 for (block = log.start_block; !done && block < log.end_block; block++) {
339 monitor.set_value(Altos.state_to_string[state], state, block - state_block);
341 AltosEepromChunk eechunk = new AltosEepromChunk(serial_line, block, block == log.start_block);
344 * Guess what kind of data is there if the device
348 if (log_format == Altos.AO_LOG_FORMAT_UNKNOWN) {
349 if (block == log.start_block) {
350 if (eechunk.data(0) == Altos.AO_LOG_FLIGHT)
351 log_format = Altos.AO_LOG_FORMAT_FULL;
353 log_format = Altos.AO_LOG_FORMAT_TINY;
357 switch (log_format) {
358 case AltosLib.AO_LOG_FORMAT_FULL:
359 extension = "eeprom";
360 CaptureFull(eechunk);
362 case AltosLib.AO_LOG_FORMAT_TINY:
363 extension = "eeprom";
364 CaptureTiny(eechunk);
366 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
368 CaptureTelemetry(eechunk);
370 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
371 extension = "science";
372 CaptureTeleScience(eechunk);
374 case AltosLib.AO_LOG_FORMAT_MEGAMETRUM:
376 CaptureMega(eechunk);
380 if (eeprom_file != null) {
386 private void show_message_internal(String message, String title, int message_type) {
387 JOptionPane.showMessageDialog(frame,
393 private void show_message(String in_message, String in_title, int in_message_type) {
394 final String message = in_message;
395 final String title = in_title;
396 final int message_type = in_message_type;
397 Runnable r = new Runnable() {
400 show_message_internal(message, title, message_type);
401 } catch (Exception ex) {
405 SwingUtilities.invokeLater(r);
410 boolean failed = false;
412 serial_line.start_remote();
414 for (AltosEepromLog log : flights) {
415 parse_exception = null;
420 if (parse_exception != null) {
422 show_message(String.format("Flight %d download error\n%s\nValid log data saved",
424 parse_exception.getMessage()),
425 serial_line.device.toShortString(),
426 JOptionPane.WARNING_MESSAGE);
430 } catch (IOException ee) {
431 show_message(ee.getLocalizedMessage(),
432 serial_line.device.toShortString(),
433 JOptionPane.ERROR_MESSAGE);
434 } catch (InterruptedException ie) {
435 System.out.printf("download interrupted\n");
436 } catch (TimeoutException te) {
437 show_message(String.format("Connection to \"%s\" failed",
438 serial_line.device.toShortString()),
440 JOptionPane.ERROR_MESSAGE);
444 serial_line.stop_remote();
445 } catch (InterruptedException ie) {
448 serial_line.flush_output();
451 if (listener != null) {
452 Runnable r = new Runnable() {
455 listener.actionPerformed(new ActionEvent(this,
458 } catch (Exception ex) {
462 SwingUtilities.invokeLater(r);
466 public void start() {
467 eeprom_thread = new Thread(this);
468 eeprom_thread.start();
471 public void addActionListener(ActionListener l) {
475 public AltosEepromDownload(JFrame given_frame,
476 AltosSerial given_serial_line,
477 boolean given_remote,
478 AltosEepromList given_flights) {
481 serial_line = given_serial_line;
482 serial_line.set_frame(frame);
483 remote = given_remote;
484 flights = given_flights;
487 monitor = new AltosEepromMonitor(frame, Altos.ao_flight_boost, Altos.ao_flight_landed);
488 monitor.addActionListener(new ActionListener() {
489 public void actionPerformed(ActionEvent e) {
490 if (eeprom_thread != null)
491 eeprom_thread.interrupt();