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;
51 private void FlushPending() throws IOException {
52 for (String s : flights.config_data) {
54 eeprom_file.write('\n');
57 for (String s : eeprom_pending)
61 private void CheckFile(boolean force) throws IOException {
62 if (eeprom_file != null)
64 if (force || (flight != 0 && want_file)) {
65 AltosFile eeprom_name;
66 if (year != 0 && month != 0 && day != 0)
67 eeprom_name = new AltosFile(year, month, day, flights.config_data.serial, flight, "eeprom");
69 eeprom_name = new AltosFile(flights.config_data.serial, flight, "eeprom");
71 eeprom_file = new FileWriter(eeprom_name);
72 if (eeprom_file != null) {
73 monitor.set_file(eeprom_name.getName());
75 eeprom_pending = null;
80 void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException {
81 int block, state_block = 0;
86 if (flights.config_data.serial == 0)
87 throw new IOException("no serial number found");
89 /* Reset per-capture variables */
96 eeprom_pending = new LinkedList<String>();
98 /* Set serial number in the monitor dialog window */
99 monitor.set_serial(flights.config_data.serial);
100 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
102 state = 0; state_block = log.start_block;
103 for (block = log.start_block; !done && block < log.end_block; block++) {
104 monitor.set_value(Altos.state_to_string[state], state, block - state_block);
106 AltosEepromBlock eeblock = new AltosEepromBlock(serial_line, block);
108 if (eeblock.has_flight) {
109 flight = eeblock.flight;
110 monitor.set_flight(flight);
112 if (eeblock.has_date) {
114 month = eeblock.month;
119 if (eeblock.size() == 0 ||
120 eeblock.has_state && eeblock.state == Altos.ao_flight_landed)
123 /* Monitor state transitions to update display */
124 if (eeblock.has_state) {
125 if (eeblock.state > Altos.ao_flight_pad)
127 if (eeblock.state > state)
128 state = eeblock.state;
133 for (record = 0; record < eeblock.size(); record++) {
134 AltosEepromRecord r = eeblock.get(record);
136 String log_line = String.format("%c %4x %4x %4x\n",
137 r.cmd, r.tick, r.a, r.b);
138 if (eeprom_file != null)
139 eeprom_file.write(log_line);
141 eeprom_pending.add(log_line);
145 if (eeprom_file != null) {
151 private void show_error_internal(String message, String title) {
152 JOptionPane.showMessageDialog(frame,
155 JOptionPane.ERROR_MESSAGE);
158 private void show_error(String in_message, String in_title) {
159 final String message = in_message;
160 final String title = in_title;
161 Runnable r = new Runnable() {
164 show_error_internal(message, title);
165 } catch (Exception ex) {
169 SwingUtilities.invokeLater(r);
174 serial_line.start_remote();
177 for (AltosEepromLog log : flights) {
183 System.out.printf("All flights successfully downloaded\n");
185 } catch (IOException ee) {
186 show_error (serial_line.device.toShortString(),
187 ee.getLocalizedMessage());
188 } catch (InterruptedException ie) {
189 } catch (TimeoutException te) {
190 show_error (String.format("Connection to \"%s\" failed",
191 serial_line.device.toShortString()),
192 "Connection Failed");
195 serial_line.stop_remote();
197 serial_line.flush_output();
198 if (listener != null) {
199 Runnable r = new Runnable() {
202 listener.actionPerformed(new ActionEvent(this,
205 } catch (Exception ex) {
209 SwingUtilities.invokeLater(r);
213 public void start() {
214 eeprom_thread = new Thread(this);
215 eeprom_thread.start();
218 public void addActionListener(ActionListener l) {
222 public AltosEepromDownload(JFrame given_frame,
223 AltosSerial given_serial_line,
224 boolean given_remote,
225 AltosEepromList given_flights) {
228 serial_line = given_serial_line;
229 remote = given_remote;
230 flights = given_flights;
233 monitor = new AltosEepromMonitor(frame, Altos.ao_flight_boost, Altos.ao_flight_landed);
234 monitor.addActionListener(new ActionListener() {
235 public void actionPerformed(ActionEvent e) {
236 if (eeprom_thread != null)
237 eeprom_thread.interrupt();