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 {
37 AltosSerial serial_line;
40 AltosEepromMonitor monitor;
43 int year = 0, month = 0, day = 0;
44 boolean want_file = false;
45 FileWriter eeprom_file = null;
46 LinkedList<String> eeprom_pending = new LinkedList<String>();
47 AltosConfigData config_data;
49 private void FlushPending() throws IOException {
50 for (String s : config_data) {
52 eeprom_file.write('\n');
55 for (String s : eeprom_pending)
59 private void CheckFile(boolean force) throws IOException {
60 if (eeprom_file != null)
62 if (force || (flight != 0 && want_file)) {
63 AltosFile eeprom_name;
64 if (year != 0 && month != 0 && day != 0)
65 eeprom_name = new AltosFile(year, month, day, serial, flight, "eeprom");
67 eeprom_name = new AltosFile(serial, flight, "eeprom");
69 eeprom_file = new FileWriter(eeprom_name);
70 if (eeprom_file != null) {
71 monitor.set_file(eeprom_name.getName());
73 eeprom_pending = null;
78 void CaptureLog(int start_block, int end_block) throws IOException, InterruptedException, TimeoutException {
79 int block, state_block = 0;
84 config_data = new AltosConfigData(serial_line);
85 serial = config_data.serial;
87 throw new IOException("no serial number found");
89 monitor.set_serial(serial);
90 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
92 state = 0; state_block = start_block;
93 for (block = start_block; !done && block < end_block; block++) {
94 monitor.set_value(Altos.state_to_string[state], state, block - state_block);
96 AltosEepromBlock eeblock = new AltosEepromBlock(serial_line, block);
97 if (eeblock.has_flight) {
98 flight = eeblock.flight;
99 monitor.set_flight(flight);
101 if (eeblock.has_date) {
103 month = eeblock.month;
108 if (eeblock.size() == 0 ||
109 eeblock.has_state && eeblock.state == Altos.ao_flight_landed)
112 /* Monitor state transitions to update display */
113 if (eeblock.has_state) {
114 if (eeblock.state > Altos.ao_flight_pad)
116 if (eeblock.state > state)
117 state = eeblock.state;
122 for (record = 0; record < eeblock.size(); record++) {
123 AltosEepromRecord r = eeblock.get(record);
125 String log_line = String.format("%c %4x %4x %4x\n",
126 r.cmd, r.tick, r.a, r.b);
127 if (eeprom_file != null)
128 eeprom_file.write(log_line);
130 eeprom_pending.add(log_line);
134 if (eeprom_file != null) {
140 private void show_error_internal(String message, String title) {
141 JOptionPane.showMessageDialog(frame,
144 JOptionPane.ERROR_MESSAGE);
147 private void show_error(String in_message, String in_title) {
148 final String message = in_message;
149 final String title = in_title;
150 Runnable r = new Runnable() {
153 show_error_internal(message, title);
154 } catch (Exception ex) {
158 SwingUtilities.invokeLater(r);
161 int start_block, end_block;
165 new AltosEepromList(serial_line, remote);
166 } catch (Exception ee) { }
169 serial_line.start_remote();
172 CaptureLog(start_block, end_block);
173 } catch (IOException ee) {
174 show_error (device.toShortString(),
175 ee.getLocalizedMessage());
176 } catch (InterruptedException ie) {
177 } catch (TimeoutException te) {
178 show_error (String.format("Connection to \"%s\" failed",
179 device.toShortString()),
180 "Connection Failed");
183 serial_line.stop_remote();
185 serial_line.flush_output();
189 public AltosEepromDownload(JFrame given_frame) {
191 device = AltosDeviceDialog.show(frame, AltosDevice.product_any);
195 if (device != null) {
197 serial_line = new AltosSerial(device);
198 if (!device.matchProduct(AltosDevice.product_telemetrum))
201 monitor = new AltosEepromMonitor(frame, Altos.ao_flight_boost, Altos.ao_flight_landed);
202 monitor.addActionListener(new ActionListener() {
203 public void actionPerformed(ActionEvent e) {
204 if (eeprom_thread != null)
205 eeprom_thread.interrupt();
209 eeprom_thread = new Thread(this);
212 eeprom_thread.start();
213 } catch (FileNotFoundException ee) {
214 JOptionPane.showMessageDialog(frame,
215 String.format("Cannot open device \"%s\"",
216 device.toShortString()),
217 "Cannot open target device",
218 JOptionPane.ERROR_MESSAGE);
219 } catch (AltosSerialInUseException si) {
220 JOptionPane.showMessageDialog(frame,
221 String.format("Device \"%s\" already in use",
222 device.toShortString()),
224 JOptionPane.ERROR_MESSAGE);
225 } catch (IOException ee) {
226 JOptionPane.showMessageDialog(frame,
227 device.toShortString(),
228 ee.getLocalizedMessage(),
229 JOptionPane.ERROR_MESSAGE);