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;
44 FileWriter eeprom_file;
45 LinkedList<String> eeprom_pending;
47 AltosEepromList flights;
48 ActionListener listener;
50 ParseException parse_exception;
52 private void FlushPending() throws IOException {
53 for (String s : flights.config_data) {
55 eeprom_file.write('\n');
58 for (String s : eeprom_pending)
62 private void CheckFile(boolean force) throws IOException {
63 if (eeprom_file != null)
65 if (force || (flight != 0 && want_file)) {
66 AltosFile eeprom_name;
67 if (year != 0 && month != 0 && day != 0)
68 eeprom_name = new AltosFile(year, month, day, flights.config_data.serial, flight, "eeprom");
70 eeprom_name = new AltosFile(flights.config_data.serial, flight, "eeprom");
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 static final int log_full = 1;
93 static final int log_tiny = 2;
98 void CaptureFull(AltosEepromChunk eechunk) throws IOException {
99 boolean any_valid = false;
100 for (int i = 0; i < eechunk.chunk_size && !done; i += AltosEepromRecord.record_length) {
102 AltosEepromRecord r = new AltosEepromRecord(eechunk, i);
103 if (r.cmd == Altos.AO_LOG_FLIGHT) {
105 monitor.set_flight(flight);
108 /* Monitor state transitions to update display */
109 if (r.cmd == Altos.AO_LOG_STATE && r.a <= Altos.ao_flight_landed) {
111 if (state > Altos.ao_flight_pad)
115 if (r.cmd == Altos.AO_LOG_GPS_DATE) {
116 year = 2000 + (r.a & 0xff);
117 month = (r.a >> 8) & 0xff;
121 if (r.cmd == Altos.AO_LOG_STATE && r.a == Altos.ao_flight_landed)
125 } catch (ParseException pe) {
126 if (parse_exception == null)
127 parse_exception = pe;
140 void CaptureTiny (AltosEepromChunk eechunk) throws IOException {
141 boolean any_valid = false;
143 for (int i = 0; i < eechunk.data.length && !done; i += 2) {
144 int v = eechunk.data16(i);
147 if (i == 0 && start) {
151 monitor.set_flight(flight);
152 r = new AltosEepromRecord(Altos.AO_LOG_FLIGHT, tiny_tick, 0, v);
157 if (Altos.ao_flight_startup <= s && s <= Altos.ao_flight_invalid) {
158 r = new AltosEepromRecord(Altos.AO_LOG_STATE, tiny_tick, s, 0);
159 if (s == Altos.ao_flight_landed)
166 r = new AltosEepromRecord(Altos.AO_LOG_PRESSURE, tiny_tick, 0, v);
169 * The flight software records ascent data every 100ms, and descent
172 if (state < Altos.ao_flight_drogue)
185 void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException {
186 int block, state_block = 0;
193 if (flights.config_data.serial == 0)
194 throw new IOException("no serial number found");
196 /* Reset per-capture variables */
203 eeprom_pending = new LinkedList<String>();
205 /* Set serial number in the monitor dialog window */
206 monitor.set_serial(flights.config_data.serial);
207 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
209 state = 0; state_block = log.start_block;
210 for (block = log.start_block; !done && block < log.end_block; block++) {
211 monitor.set_value(Altos.state_to_string[state], state, block - state_block);
213 AltosEepromChunk eechunk = new AltosEepromChunk(serial_line, block);
216 * Figure out what kind of data is there
219 if (block == log.start_block) {
220 if (eechunk.data(0) == Altos.AO_LOG_FLIGHT)
221 log_style = log_full;
223 log_style = log_tiny;
228 CaptureFull(eechunk);
231 CaptureTiny(eechunk);
236 if (eeprom_file != null) {
242 private void show_message_internal(String message, String title, int message_type) {
243 JOptionPane.showMessageDialog(frame,
249 private void show_message(String in_message, String in_title, int in_message_type) {
250 final String message = in_message;
251 final String title = in_title;
252 final int message_type = in_message_type;
253 Runnable r = new Runnable() {
256 show_message_internal(message, title, message_type);
257 } catch (Exception ex) {
261 SwingUtilities.invokeLater(r);
266 serial_line.start_remote();
269 boolean failed = false;
270 for (AltosEepromLog log : flights) {
271 parse_exception = null;
276 if (parse_exception != null) {
278 show_message(String.format("Flight %d download error\n%s\nValid log data saved",
280 parse_exception.getMessage()),
281 serial_line.device.toShortString(),
282 JOptionPane.WARNING_MESSAGE);
286 } catch (IOException ee) {
287 show_message(ee.getLocalizedMessage(),
288 serial_line.device.toShortString(),
289 JOptionPane.ERROR_MESSAGE);
290 } catch (InterruptedException ie) {
291 } catch (TimeoutException te) {
292 show_message(String.format("Connection to \"%s\" failed",
293 serial_line.device.toShortString()),
295 JOptionPane.ERROR_MESSAGE);
298 serial_line.stop_remote();
300 serial_line.flush_output();
301 if (listener != null) {
302 Runnable r = new Runnable() {
305 listener.actionPerformed(new ActionEvent(this,
308 } catch (Exception ex) {
312 SwingUtilities.invokeLater(r);
316 public void start() {
317 eeprom_thread = new Thread(this);
318 eeprom_thread.start();
321 public void addActionListener(ActionListener l) {
325 public AltosEepromDownload(JFrame given_frame,
326 AltosSerial given_serial_line,
327 boolean given_remote,
328 AltosEepromList given_flights) {
331 serial_line = given_serial_line;
332 serial_line.set_frame(frame);
333 remote = given_remote;
334 flights = given_flights;
337 monitor = new AltosEepromMonitor(frame, Altos.ao_flight_boost, Altos.ao_flight_landed);
338 monitor.addActionListener(new ActionListener() {
339 public void actionPerformed(ActionEvent e) {
340 if (eeprom_thread != null)
341 eeprom_thread.interrupt();