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;
31 import altosui.AltosSerial;
32 import altosui.AltosSerialMonitor;
33 import altosui.AltosTelemetry;
34 import altosui.AltosState;
35 import altosui.AltosDeviceDialog;
36 import altosui.AltosPreferences;
37 import altosui.AltosLog;
38 import altosui.AltosVoice;
39 import altosui.AltosEepromMonitor;
43 public class AltosEeprom implements Runnable {
45 static final int AO_LOG_FLIGHT = 'F';
46 static final int AO_LOG_SENSOR = 'A';
47 static final int AO_LOG_TEMP_VOLT = 'T';
48 static final int AO_LOG_DEPLOY = 'D';
49 static final int AO_LOG_STATE = 'S';
50 static final int AO_LOG_GPS_TIME = 'G';
51 static final int AO_LOG_GPS_LAT = 'N';
52 static final int AO_LOG_GPS_LON = 'W';
53 static final int AO_LOG_GPS_ALT = 'H';
54 static final int AO_LOG_GPS_SAT = 'V';
55 static final int AO_LOG_GPS_DATE = 'Y';
57 static final int ao_flight_startup = 0;
58 static final int ao_flight_idle = 1;
59 static final int ao_flight_pad = 2;
60 static final int ao_flight_boost = 3;
61 static final int ao_flight_fast = 4;
62 static final int ao_flight_coast = 5;
63 static final int ao_flight_drogue = 6;
64 static final int ao_flight_main = 7;
65 static final int ao_flight_landed = 8;
66 static final int ao_flight_invalid = 9;
68 static final String[] state_names = {
81 int[] ParseHex(String line) {
82 String[] tokens = line.split("\\s+");
83 int[] array = new int[tokens.length];
85 for (int i = 0; i < tokens.length; i++)
87 array[i] = Integer.parseInt(tokens[i], 16);
88 } catch (NumberFormatException ne) {
94 int checksum(int[] line) {
96 for (int i = 1; i < line.length; i++)
101 void FlushPending(FileWriter file, LinkedList<String> pending) throws IOException {
102 while (!pending.isEmpty()) {
103 file.write(pending.remove());
109 AltosSerial serial_line;
111 Thread eeprom_thread;
112 AltosEepromMonitor monitor;
114 void CaptureLog() throws IOException, InterruptedException {
116 int block, state_block = 0;
119 int year = 0, month = 0, day = 0;
121 boolean done = false;
122 boolean want_file = false;
124 FileWriter eeprom_file = null;
125 AltosFile eeprom_name;
126 LinkedList<String> eeprom_pending = new LinkedList<String>();
128 serial_line.printf("v\n");
130 /* Pull the serial number out of the version information */
133 String line = serial_line.get_reply();
135 if (line.startsWith("serial-number")) {
137 serial = Integer.parseInt(line.substring(13).trim());
138 eeprom_pending.add(String.format("%s\n", line));
139 } catch (NumberFormatException ne) {
144 /* signals the end of the version info */
145 if (line.startsWith("software-version"))
149 throw new IOException("no serial number found");
151 monitor.set_serial(serial);
152 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
154 state = 0; state_block = 0;
155 for (block = 0; !done && block < 511; block++) {
156 serial_line.printf("e %x\n", block);
158 monitor.set_value(state_names[state], state, block - state_block);
159 for (addr = 0; addr < 0x100;) {
160 String line = serial_line.get_reply();
161 int[] values = ParseHex(line);
163 if (values == null) {
164 System.out.printf("invalid line: %s\n", line);
165 } else if (values[0] != addr) {
166 System.out.printf("data address out of sync at 0x%x\n",
167 block * 256 + values[0]);
168 } else if (checksum(values) != 0) {
169 System.out.printf("invalid checksum at 0x%x\n",
170 block * 256 + values[0]);
174 int tick = values[3] + (values[4] << 8);
175 int a = values[5] + (values[6] << 8);
176 int b = values[7] + (values[8] << 8);
178 if (cmd == AO_LOG_FLIGHT) {
180 monitor.set_flight(flight);
183 /* Monitor state transitions to update display */
184 if (cmd == AO_LOG_STATE && a <= ao_flight_landed) {
185 if (a > ao_flight_pad)
192 if (cmd == AO_LOG_GPS_DATE) {
193 year = 2000 + (a & 0xff);
194 month = (a >> 8) & 0xff;
199 if (eeprom_file == null) {
200 if (serial != 0 && flight != 0 && want_file) {
201 if (year != 0 && month != 0 && day != 0)
202 eeprom_name = new AltosFile(year, month, day, serial, flight, "eeprom");
204 eeprom_name = new AltosFile(serial, flight, "eeprom");
206 monitor.set_file(eeprom_name.getName());
207 eeprom_file = new FileWriter(eeprom_name);
208 if (eeprom_file != null) {
209 FlushPending(eeprom_file, eeprom_pending);
210 eeprom_pending = null;
215 String log_line = String.format("%c %4x %4x %4x\n",
217 if (eeprom_file != null)
218 eeprom_file.write(log_line);
220 eeprom_pending.add(log_line);
222 if (cmd == AO_LOG_STATE && a == ao_flight_landed) {
231 if (eeprom_file == null) {
232 eeprom_name = new AltosFile(serial,flight,"eeprom");
233 eeprom_file = new FileWriter(eeprom_name);
234 if (eeprom_file != null) {
235 FlushPending(eeprom_file, eeprom_pending);
238 if (eeprom_file != null) {
246 serial_line.printf("m 0\n");
247 serial_line.set_channel(AltosPreferences.channel());
248 serial_line.printf("p\n");
251 monitor = new AltosEepromMonitor(frame, ao_flight_boost, ao_flight_landed);
252 monitor.addActionListener(new ActionListener() {
253 public void actionPerformed(ActionEvent e) {
254 eeprom_thread.interrupt();
259 } catch (IOException ee) {
260 JOptionPane.showMessageDialog(frame,
262 ee.getLocalizedMessage(),
263 JOptionPane.ERROR_MESSAGE);
264 } catch (InterruptedException ie) {
267 serial_line.printf("~");
272 public AltosEeprom(JFrame given_frame) {
274 device = AltosDeviceDialog.show(frame, null);
276 serial_line = new AltosSerial();
279 if (device != null) {
281 serial_line.open(device);
282 if (!device.getProduct().startsWith("TeleMetrum"))
284 eeprom_thread = new Thread(this);
285 eeprom_thread.start();
286 } catch (FileNotFoundException ee) {
287 JOptionPane.showMessageDialog(frame,
288 String.format("Cannot open device \"%s\"",
290 "Cannot open target device",
291 JOptionPane.ERROR_MESSAGE);
292 } catch (IOException ee) {
293 JOptionPane.showMessageDialog(frame,
295 ee.getLocalizedMessage(),
296 JOptionPane.ERROR_MESSAGE);