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.LinkedBlockingQueue;
32 import altosui.AltosSerial;
33 import altosui.AltosSerialMonitor;
34 import altosui.AltosTelemetry;
35 import altosui.AltosState;
36 import altosui.AltosDeviceDialog;
37 import altosui.AltosPreferences;
38 import altosui.AltosLog;
39 import altosui.AltosVoice;
40 import altosui.AltosEepromMonitor;
44 public class AltosEepromDownload implements Runnable {
46 static final String[] state_names = {
59 int[] ParseHex(String line) {
60 String[] tokens = line.split("\\s+");
61 int[] array = new int[tokens.length];
63 for (int i = 0; i < tokens.length; i++)
65 array[i] = Integer.parseInt(tokens[i], 16);
66 } catch (NumberFormatException ne) {
72 int checksum(int[] line) {
74 for (int i = 1; i < line.length; i++)
79 void FlushPending(FileWriter file, LinkedList<String> pending) throws IOException {
80 while (!pending.isEmpty()) {
81 file.write(pending.remove());
87 AltosSerial serial_line;
90 AltosEepromMonitor monitor;
92 void CaptureLog() throws IOException, InterruptedException {
94 int block, state_block = 0;
97 int year = 0, month = 0, day = 0;
100 boolean want_file = false;
102 FileWriter eeprom_file = null;
103 AltosFile eeprom_name;
104 LinkedList<String> eeprom_pending = new LinkedList<String>();
106 serial_line.printf("\nc s\nv\n");
108 /* Pull the serial number out of the version information */
111 String line = serial_line.get_reply();
113 if (line.startsWith("serial-number")) {
115 serial = Integer.parseInt(line.substring(13).trim());
116 } catch (NumberFormatException ne) {
121 eeprom_pending.add(String.format("%s\n", line));
123 /* signals the end of the version info */
124 if (line.startsWith("software-version"))
128 throw new IOException("no serial number found");
130 monitor.set_serial(serial);
131 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
133 state = 0; state_block = 0;
134 for (block = 0; !done && block < 511; block++) {
135 serial_line.printf("e %x\n", block);
137 monitor.set_value(state_names[state], state, block - state_block);
138 for (addr = 0; addr < 0x100;) {
139 String line = serial_line.get_reply();
140 int[] values = ParseHex(line);
142 if (values == null) {
143 System.out.printf("invalid line: %s\n", line);
144 } else if (values[0] != addr) {
145 System.out.printf("data address out of sync at 0x%x\n",
146 block * 256 + values[0]);
147 } else if (checksum(values) != 0) {
148 System.out.printf("invalid checksum at 0x%x\n",
149 block * 256 + values[0]);
153 int tick = values[3] + (values[4] << 8);
154 int a = values[5] + (values[6] << 8);
155 int b = values[7] + (values[8] << 8);
157 if (cmd == Altos.AO_LOG_FLIGHT) {
159 monitor.set_flight(flight);
162 /* Monitor state transitions to update display */
163 if (cmd == Altos.AO_LOG_STATE && a <= Altos.ao_flight_landed) {
164 if (a > Altos.ao_flight_pad)
171 if (cmd == Altos.AO_LOG_GPS_DATE) {
172 year = 2000 + (a & 0xff);
173 month = (a >> 8) & 0xff;
178 if (eeprom_file == null) {
179 if (serial != 0 && flight != 0 && want_file) {
180 if (year != 0 && month != 0 && day != 0)
181 eeprom_name = new AltosFile(year, month, day, serial, flight, "eeprom");
183 eeprom_name = new AltosFile(serial, flight, "eeprom");
185 monitor.set_file(eeprom_name.getName());
186 eeprom_file = new FileWriter(eeprom_name);
187 if (eeprom_file != null) {
188 FlushPending(eeprom_file, eeprom_pending);
189 eeprom_pending = null;
194 String log_line = String.format("%c %4x %4x %4x\n",
196 if (eeprom_file != null)
197 eeprom_file.write(log_line);
199 eeprom_pending.add(log_line);
201 if (cmd == Altos.AO_LOG_STATE && a == Altos.ao_flight_landed) {
210 if (eeprom_file == null) {
211 eeprom_name = new AltosFile(serial,flight,"eeprom");
212 eeprom_file = new FileWriter(eeprom_name);
213 if (eeprom_file != null) {
214 FlushPending(eeprom_file, eeprom_pending);
217 if (eeprom_file != null) {
225 serial_line.printf("m 0\n");
226 serial_line.set_channel(AltosPreferences.channel());
227 serial_line.printf("p\n");
230 monitor = new AltosEepromMonitor(frame, Altos.ao_flight_boost, Altos.ao_flight_landed);
231 monitor.addActionListener(new ActionListener() {
232 public void actionPerformed(ActionEvent e) {
233 eeprom_thread.interrupt();
238 } catch (IOException ee) {
239 JOptionPane.showMessageDialog(frame,
241 ee.getLocalizedMessage(),
242 JOptionPane.ERROR_MESSAGE);
243 } catch (InterruptedException ie) {
246 serial_line.printf("~");
251 public AltosEepromDownload(JFrame given_frame) {
253 device = AltosDeviceDialog.show(frame, null);
255 serial_line = new AltosSerial();
258 if (device != null) {
260 serial_line.open(device);
261 if (!device.getProduct().startsWith("TeleMetrum"))
263 eeprom_thread = new Thread(this);
264 eeprom_thread.start();
265 } catch (FileNotFoundException ee) {
266 JOptionPane.showMessageDialog(frame,
267 String.format("Cannot open device \"%s\"",
269 "Cannot open target device",
270 JOptionPane.ERROR_MESSAGE);
271 } catch (IOException ee) {
272 JOptionPane.showMessageDialog(frame,
274 ee.getLocalizedMessage(),
275 JOptionPane.ERROR_MESSAGE);