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 String[] state_names = {
81 static 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 static int checksum(int[] line) {
96 for (int i = 1; i < line.length; i++)
101 static void FlushPending(FileWriter file, LinkedList<String> pending) throws IOException {
102 while (!pending.isEmpty()) {
103 file.write(pending.remove());
107 static void CaptureLog(JFrame frame, AltosSerial serial_line) throws IOException, InterruptedException {
109 int block, state_block = 0;
112 int year = 0, month = 0, day = 0;
114 boolean done = false;
115 boolean want_file = false;
117 FileWriter eeprom_file = null;
118 AltosFile eeprom_name;
119 LinkedList<String> eeprom_pending = new LinkedList<String>();
120 AltosEepromMonitor monitor = new AltosEepromMonitor(frame, ao_flight_boost, ao_flight_landed);
122 serial_line.printf("v\n");
124 /* Pull the serial number out of the version information */
127 String line = serial_line.get_reply();
129 if (line.startsWith("serial-number")) {
131 serial = Integer.parseInt(line.substring(13).trim());
132 eeprom_pending.add(String.format("%s\n", line));
133 } catch (NumberFormatException ne) {
138 /* signals the end of the version info */
139 if (line.startsWith("software-version"))
143 throw new IOException("no serial number found");
145 monitor.set_serial(serial);
146 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
148 state = 0; state_block = 0;
149 for (block = 0; !done && block < 511; block++) {
150 serial_line.printf("e %x\n", block);
152 monitor.set_value(state_names[state], state, block - state_block);
153 for (addr = 0; addr < 0x100;) {
154 String line = serial_line.get_reply();
155 int[] values = ParseHex(line);
157 if (values == null) {
158 System.out.printf("invalid line: %s\n", line);
159 } else if (values[0] != addr) {
160 System.out.printf("data address out of sync at 0x%x\n",
161 block * 256 + values[0]);
162 } else if (checksum(values) != 0) {
163 System.out.printf("invalid checksum at 0x%x\n",
164 block * 256 + values[0]);
168 int tick = values[3] + (values[4] << 8);
169 int a = values[5] + (values[6] << 8);
170 int b = values[7] + (values[8] << 8);
172 if (cmd == AO_LOG_FLIGHT) {
174 monitor.set_flight(flight);
177 /* Monitor state transitions to update display */
178 if (cmd == AO_LOG_STATE && a <= ao_flight_landed) {
179 System.out.printf ("%s\n", state_names[a]);
180 if (a > ao_flight_pad)
187 if (cmd == AO_LOG_GPS_DATE) {
188 year = 2000 + (a & 0xff);
189 month = (a >> 8) & 0xff;
194 if (eeprom_file == null) {
195 if (serial != 0 && flight != 0 && want_file) {
196 if (year != 0 && month != 0 && day != 0)
197 eeprom_name = new AltosFile(year, month, day, serial, flight, "eeprom-new");
199 eeprom_name = new AltosFile(serial, flight, "eeprom-new");
201 monitor.set_file(eeprom_name.getName());
202 eeprom_file = new FileWriter(eeprom_name);
203 if (eeprom_file != null) {
204 FlushPending(eeprom_file, eeprom_pending);
205 eeprom_pending = null;
210 String log_line = String.format("%c %4x %4x %4x\n",
212 if (eeprom_file != null)
213 eeprom_file.write(log_line);
215 eeprom_pending.add(log_line);
217 if (cmd == AO_LOG_STATE && a == ao_flight_landed) {
226 if (eeprom_file == null) {
227 eeprom_name = new AltosFile(serial,flight,"eeprom-new");
228 eeprom_file = new FileWriter(eeprom_name);
229 if (eeprom_file != null) {
230 FlushPending(eeprom_file, eeprom_pending);
233 if (eeprom_file != null) {
242 AltosSerial serial_line;
244 Thread eeprom_thread;
248 serial_line.printf("m 0\n");
249 serial_line.set_channel(AltosPreferences.channel());
250 serial_line.printf("p\n");
253 CaptureLog(frame, serial_line);
254 } catch (IOException ee) {
255 JOptionPane.showMessageDialog(frame,
257 ee.getLocalizedMessage(),
258 JOptionPane.ERROR_MESSAGE);
259 } catch (InterruptedException ie) {
262 serial_line.printf("~");
266 public AltosEeprom(JFrame given_frame) {
268 device = AltosDeviceDialog.show(frame, null);
269 serial_line = new AltosSerial();
272 if (device != null) {
274 serial_line.open(device);
275 if (!device.getProduct().startsWith("TeleMetrum"))
277 eeprom_thread = new Thread(this);
278 eeprom_thread.start();
279 } catch (FileNotFoundException ee) {
280 JOptionPane.showMessageDialog(frame,
281 String.format("Cannot open device \"%s\"",
283 "Cannot open target device",
284 JOptionPane.ERROR_MESSAGE);
285 } catch (IOException ee) {
286 JOptionPane.showMessageDialog(frame,
288 ee.getLocalizedMessage(),
289 JOptionPane.ERROR_MESSAGE);