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)
137 if (r.cmd != AltosLib.AO_LOG_INVALID)
140 } catch (ParseException pe) {
141 if (parse_exception == null)
142 parse_exception = pe;
155 void CaptureTiny (AltosEepromChunk eechunk) throws IOException {
156 boolean any_valid = false;
158 extension = "eeprom";
159 set_serial(flights.config_data.serial);
160 for (int i = 0; i < eechunk.data.length && !done; i += 2) {
161 int v = eechunk.data16(i);
164 if (i == 0 && start) {
168 r = new AltosEepromRecord(Altos.AO_LOG_FLIGHT, tiny_tick, 0, v);
173 if (Altos.ao_flight_startup <= s && s <= Altos.ao_flight_invalid) {
175 r = new AltosEepromRecord(Altos.AO_LOG_STATE, tiny_tick, state, 0);
176 if (state == Altos.ao_flight_landed)
184 r = new AltosEepromRecord(Altos.AO_LOG_PRESSURE, tiny_tick, 0, v);
187 * The flight software records ascent data every 100ms, and descent
190 if (state < Altos.ao_flight_drogue)
203 void LogTeleScience(AltosEepromTeleScience r) throws IOException {
204 if (r.type != Altos.AO_LOG_INVALID) {
205 String log_line = String.format("%c %4x %4x %d %5d %5d %5d %5d %5d %5d %5d %5d %5d %5d %5d %5d\n",
206 r.type, r.tick, r.tm_tick, r.tm_state,
207 r.data[0], r.data[1], r.data[2], r.data[3],
208 r.data[4], r.data[5], r.data[6], r.data[7],
209 r.data[8], r.data[9], r.data[10], r.data[11]);
210 if (eeprom_file != null)
211 eeprom_file.write(log_line);
213 eeprom_pending.add(log_line);
217 boolean telescience_start;
219 void CaptureTeleScience (AltosEepromChunk eechunk) throws IOException {
220 boolean any_valid = false;
222 extension = "science";
223 for (int i = 0; i < eechunk.chunk_size && !done; i += AltosEepromTeleScience.record_length) {
225 AltosEepromTeleScience r = new AltosEepromTeleScience(eechunk, i);
226 if (r.type == AltosEepromTeleScience.AO_LOG_TELESCIENCE_START) {
227 if (telescience_start) {
231 set_serial(r.data[0]);
232 set_flight(r.data[1]);
233 telescience_start = true;
235 if (!telescience_start)
242 } catch (ParseException pe) {
243 if (parse_exception == null)
244 parse_exception = pe;
253 void LogMega(AltosEepromMega r) throws IOException {
254 if (r.cmd != Altos.AO_LOG_INVALID) {
255 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",
257 r.data8[0], r.data8[1], r.data8[2], r.data8[3],
258 r.data8[4], r.data8[5], r.data8[6], r.data8[7],
259 r.data8[8], r.data8[9], r.data8[10], r.data8[11],
260 r.data8[12], r.data8[13], r.data8[14], r.data8[15],
261 r.data8[16], r.data8[17], r.data8[18], r.data8[19],
262 r.data8[20], r.data8[21], r.data8[22], r.data8[23],
263 r.data8[24], r.data8[25], r.data8[26], r.data8[27]);
264 if (eeprom_file != null)
265 eeprom_file.write(log_line);
267 eeprom_pending.add(log_line);
271 void CaptureMega(AltosEepromChunk eechunk) throws IOException {
272 boolean any_valid = false;
275 set_serial(flights.config_data.serial);
276 for (int i = 0; i < eechunk.chunk_size && !done; i += AltosEepromMega.record_length) {
278 AltosEepromMega r = new AltosEepromMega(eechunk, i);
279 if (r.cmd == Altos.AO_LOG_FLIGHT)
280 set_flight(r.data16(0));
282 /* Monitor state transitions to update display */
283 if (r.cmd == Altos.AO_LOG_STATE && r.data16(0) <= Altos.ao_flight_landed) {
285 if (state > Altos.ao_flight_pad)
289 if (r.cmd == Altos.AO_LOG_GPS_TIME) {
290 year = 2000 + r.data8(14);
296 if (r.cmd == Altos.AO_LOG_STATE && r.data16(0) == Altos.ao_flight_landed)
300 } catch (ParseException pe) {
301 if (parse_exception == null)
302 parse_exception = pe;
311 void CaptureTelemetry(AltosEepromChunk eechunk) throws IOException {
315 void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException {
316 int block, state_block = 0;
317 int log_format = flights.config_data.log_format;
323 if (flights.config_data.serial == 0)
324 throw new IOException("no serial number found");
326 /* Reset per-capture variables */
333 eeprom_pending = new LinkedList<String>();
335 /* Set serial number in the monitor dialog window */
336 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
338 state = 0; state_block = log.start_block;
339 for (block = log.start_block; !done && block < log.end_block; block++) {
340 monitor.set_value(AltosLib.state_name(state), state, block - state_block);
342 AltosEepromChunk eechunk = new AltosEepromChunk(serial_line, block, block == log.start_block);
345 * Guess what kind of data is there if the device
349 if (log_format == Altos.AO_LOG_FORMAT_UNKNOWN) {
350 if (block == log.start_block) {
351 if (eechunk.data(0) == Altos.AO_LOG_FLIGHT)
352 log_format = Altos.AO_LOG_FORMAT_FULL;
354 log_format = Altos.AO_LOG_FORMAT_TINY;
358 switch (log_format) {
359 case AltosLib.AO_LOG_FORMAT_FULL:
360 extension = "eeprom";
361 CaptureFull(eechunk);
363 case AltosLib.AO_LOG_FORMAT_TINY:
364 extension = "eeprom";
365 CaptureTiny(eechunk);
367 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
369 CaptureTelemetry(eechunk);
371 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
372 extension = "science";
373 CaptureTeleScience(eechunk);
375 case AltosLib.AO_LOG_FORMAT_MEGAMETRUM:
377 CaptureMega(eechunk);
381 if (eeprom_file != null) {
387 private void show_message_internal(String message, String title, int message_type) {
388 JOptionPane.showMessageDialog(frame,
394 private void show_message(String in_message, String in_title, int in_message_type) {
395 final String message = in_message;
396 final String title = in_title;
397 final int message_type = in_message_type;
398 Runnable r = new Runnable() {
401 show_message_internal(message, title, message_type);
402 } catch (Exception ex) {
406 SwingUtilities.invokeLater(r);
411 boolean failed = false;
413 serial_line.start_remote();
415 for (AltosEepromLog log : flights) {
416 parse_exception = null;
421 if (parse_exception != null) {
423 show_message(String.format("Flight %d download error\n%s\nValid log data saved",
425 parse_exception.getMessage()),
426 serial_line.device.toShortString(),
427 JOptionPane.WARNING_MESSAGE);
431 } catch (IOException ee) {
432 show_message(ee.getLocalizedMessage(),
433 serial_line.device.toShortString(),
434 JOptionPane.ERROR_MESSAGE);
435 } catch (InterruptedException ie) {
436 System.out.printf("download interrupted\n");
437 } catch (TimeoutException te) {
438 show_message(String.format("Connection to \"%s\" failed",
439 serial_line.device.toShortString()),
441 JOptionPane.ERROR_MESSAGE);
445 serial_line.stop_remote();
446 } catch (InterruptedException ie) {
449 serial_line.flush_output();
452 if (listener != null) {
453 Runnable r = new Runnable() {
456 listener.actionPerformed(new ActionEvent(this,
459 } catch (Exception ex) {
463 SwingUtilities.invokeLater(r);
467 public void start() {
468 eeprom_thread = new Thread(this);
469 eeprom_thread.start();
472 public void addActionListener(ActionListener l) {
476 public AltosEepromDownload(JFrame given_frame,
477 AltosSerial given_serial_line,
478 boolean given_remote,
479 AltosEepromList given_flights) {
482 serial_line = given_serial_line;
483 serial_line.set_frame(frame);
484 remote = given_remote;
485 flights = given_flights;
488 monitor = new AltosEepromMonitor(frame, Altos.ao_flight_boost, Altos.ao_flight_landed);
489 monitor.addActionListener(new ActionListener() {
490 public void actionPerformed(ActionEvent e) {
491 if (eeprom_thread != null)
492 eeprom_thread.interrupt();