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.AltosRecord;
35 import altosui.AltosTelemetry;
36 import altosui.AltosState;
37 import altosui.AltosDeviceDialog;
38 import altosui.AltosPreferences;
39 import altosui.AltosLog;
40 import altosui.AltosVoice;
41 import altosui.AltosEepromMonitor;
45 public class AltosEepromDownload implements Runnable {
47 static final String[] state_names = {
60 int[] ParseHex(String line) {
61 String[] tokens = line.split("\\s+");
62 int[] array = new int[tokens.length];
64 for (int i = 0; i < tokens.length; i++)
66 array[i] = Integer.parseInt(tokens[i], 16);
67 } catch (NumberFormatException ne) {
73 int checksum(int[] line) {
75 for (int i = 1; i < line.length; i++)
80 void FlushPending(FileWriter file, LinkedList<String> pending) throws IOException {
81 while (!pending.isEmpty()) {
82 file.write(pending.remove());
88 AltosSerial serial_line;
91 AltosEepromMonitor monitor;
93 void CaptureLog() throws IOException, InterruptedException {
95 int block, state_block = 0;
98 int year = 0, month = 0, day = 0;
100 boolean done = false;
101 boolean want_file = false;
103 FileWriter eeprom_file = null;
104 AltosFile eeprom_name;
105 LinkedList<String> eeprom_pending = new LinkedList<String>();
107 serial_line.printf("\nc s\nv\n");
109 /* Pull the serial number out of the version information */
112 String line = serial_line.get_reply();
114 if (line.startsWith("serial-number")) {
116 serial = Integer.parseInt(line.substring(13).trim());
117 } catch (NumberFormatException ne) {
122 eeprom_pending.add(String.format("%s\n", line));
124 /* signals the end of the version info */
125 if (line.startsWith("software-version"))
129 throw new IOException("no serial number found");
131 monitor.set_serial(serial);
132 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
134 state = 0; state_block = 0;
135 for (block = 0; !done && block < 511; block++) {
136 serial_line.printf("e %x\n", block);
138 monitor.set_value(state_names[state], state, block - state_block);
139 for (addr = 0; addr < 0x100;) {
140 String line = serial_line.get_reply();
141 int[] values = ParseHex(line);
143 if (values == null) {
144 System.out.printf("invalid line: %s\n", line);
145 } else if (values[0] != addr) {
146 System.out.printf("data address out of sync at 0x%x\n",
147 block * 256 + values[0]);
148 } else if (checksum(values) != 0) {
149 System.out.printf("invalid checksum at 0x%x\n",
150 block * 256 + values[0]);
154 int tick = values[3] + (values[4] << 8);
155 int a = values[5] + (values[6] << 8);
156 int b = values[7] + (values[8] << 8);
158 if (cmd == Altos.AO_LOG_FLIGHT) {
160 monitor.set_flight(flight);
163 /* Monitor state transitions to update display */
164 if (cmd == Altos.AO_LOG_STATE && a <= Altos.ao_flight_landed) {
165 if (a > Altos.ao_flight_pad)
172 if (cmd == Altos.AO_LOG_GPS_DATE) {
173 year = 2000 + (a & 0xff);
174 month = (a >> 8) & 0xff;
179 if (eeprom_file == null) {
180 if (serial != 0 && flight != 0 && want_file) {
181 if (year != 0 && month != 0 && day != 0)
182 eeprom_name = new AltosFile(year, month, day, serial, flight, "eeprom");
184 eeprom_name = new AltosFile(serial, flight, "eeprom");
186 monitor.set_file(eeprom_name.getName());
187 eeprom_file = new FileWriter(eeprom_name);
188 if (eeprom_file != null) {
189 FlushPending(eeprom_file, eeprom_pending);
190 eeprom_pending = null;
195 String log_line = String.format("%c %4x %4x %4x\n",
197 if (eeprom_file != null)
198 eeprom_file.write(log_line);
200 eeprom_pending.add(log_line);
202 if (cmd == Altos.AO_LOG_STATE && a == Altos.ao_flight_landed) {
211 if (eeprom_file == null) {
212 eeprom_name = new AltosFile(serial,flight,"eeprom");
213 eeprom_file = new FileWriter(eeprom_name);
214 if (eeprom_file != null) {
215 FlushPending(eeprom_file, eeprom_pending);
218 if (eeprom_file != null) {
226 serial_line.printf("m 0\n");
227 serial_line.set_channel(AltosPreferences.channel());
228 serial_line.printf("p\n");
231 monitor = new AltosEepromMonitor(frame, Altos.ao_flight_boost, Altos.ao_flight_landed);
232 monitor.addActionListener(new ActionListener() {
233 public void actionPerformed(ActionEvent e) {
234 eeprom_thread.interrupt();
239 } catch (IOException ee) {
240 JOptionPane.showMessageDialog(frame,
242 ee.getLocalizedMessage(),
243 JOptionPane.ERROR_MESSAGE);
244 } catch (InterruptedException ie) {
247 serial_line.printf("~");
252 public AltosEepromDownload(JFrame given_frame) {
254 device = AltosDeviceDialog.show(frame, null);
256 serial_line = new AltosSerial();
259 if (device != null) {
261 serial_line.open(device);
262 if (!device.getProduct().startsWith("TeleMetrum"))
264 eeprom_thread = new Thread(this);
265 eeprom_thread.start();
266 } catch (FileNotFoundException ee) {
267 JOptionPane.showMessageDialog(frame,
268 String.format("Cannot open device \"%s\"",
270 "Cannot open target device",
271 JOptionPane.ERROR_MESSAGE);
272 } catch (IOException ee) {
273 JOptionPane.showMessageDialog(frame,
275 ee.getLocalizedMessage(),
276 JOptionPane.ERROR_MESSAGE);