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 CaptureTelemetry(AltosEepromChunk eechunk) throws IOException {
256 void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException {
257 int block, state_block = 0;
258 int log_format = flights.config_data.log_format;
264 if (flights.config_data.serial == 0)
265 throw new IOException("no serial number found");
267 /* Reset per-capture variables */
274 eeprom_pending = new LinkedList<String>();
276 /* Set serial number in the monitor dialog window */
277 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
279 state = 0; state_block = log.start_block;
280 for (block = log.start_block; !done && block < log.end_block; block++) {
281 monitor.set_value(Altos.state_to_string[state], state, block - state_block);
283 AltosEepromChunk eechunk = new AltosEepromChunk(serial_line, block, block == log.start_block);
286 * Guess what kind of data is there if the device
290 if (log_format == Altos.AO_LOG_FORMAT_UNKNOWN) {
291 if (block == log.start_block) {
292 if (eechunk.data(0) == Altos.AO_LOG_FLIGHT)
293 log_format = Altos.AO_LOG_FORMAT_FULL;
295 log_format = Altos.AO_LOG_FORMAT_TINY;
299 switch (log_format) {
300 case Altos.AO_LOG_FORMAT_FULL:
301 extension = "eeprom";
302 CaptureFull(eechunk);
304 case Altos.AO_LOG_FORMAT_TINY:
305 extension = "eeprom";
306 CaptureTiny(eechunk);
308 case Altos.AO_LOG_FORMAT_TELEMETRY:
310 CaptureTelemetry(eechunk);
312 case Altos.AO_LOG_FORMAT_TELESCIENCE:
313 extension = "science";
314 CaptureTeleScience(eechunk);
319 if (eeprom_file != null) {
325 private void show_message_internal(String message, String title, int message_type) {
326 JOptionPane.showMessageDialog(frame,
332 private void show_message(String in_message, String in_title, int in_message_type) {
333 final String message = in_message;
334 final String title = in_title;
335 final int message_type = in_message_type;
336 Runnable r = new Runnable() {
339 show_message_internal(message, title, message_type);
340 } catch (Exception ex) {
344 SwingUtilities.invokeLater(r);
349 boolean failed = false;
351 serial_line.start_remote();
353 for (AltosEepromLog log : flights) {
354 parse_exception = null;
359 if (parse_exception != null) {
361 show_message(String.format("Flight %d download error\n%s\nValid log data saved",
363 parse_exception.getMessage()),
364 serial_line.device.toShortString(),
365 JOptionPane.WARNING_MESSAGE);
369 } catch (IOException ee) {
370 show_message(ee.getLocalizedMessage(),
371 serial_line.device.toShortString(),
372 JOptionPane.ERROR_MESSAGE);
373 } catch (InterruptedException ie) {
374 System.out.printf("download interrupted\n");
375 } catch (TimeoutException te) {
376 show_message(String.format("Connection to \"%s\" failed",
377 serial_line.device.toShortString()),
379 JOptionPane.ERROR_MESSAGE);
383 serial_line.stop_remote();
384 } catch (InterruptedException ie) {
387 serial_line.flush_output();
390 if (listener != null) {
391 Runnable r = new Runnable() {
394 listener.actionPerformed(new ActionEvent(this,
397 } catch (Exception ex) {
401 SwingUtilities.invokeLater(r);
405 public void start() {
406 eeprom_thread = new Thread(this);
407 eeprom_thread.start();
410 public void addActionListener(ActionListener l) {
414 public AltosEepromDownload(JFrame given_frame,
415 AltosSerial given_serial_line,
416 boolean given_remote,
417 AltosEepromList given_flights) {
420 serial_line = given_serial_line;
421 serial_line.set_frame(frame);
422 remote = given_remote;
423 flights = given_flights;
426 monitor = new AltosEepromMonitor(frame, Altos.ao_flight_boost, Altos.ao_flight_landed);
427 monitor.addActionListener(new ActionListener() {
428 public void actionPerformed(ActionEvent e) {
429 if (eeprom_thread != null)
430 eeprom_thread.interrupt();