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;
42 public class AltosEeprom {
44 static final int AO_LOG_FLIGHT = 'F';
45 static final int AO_LOG_SENSOR = 'A';
46 static final int AO_LOG_TEMP_VOLT = 'T';
47 static final int AO_LOG_DEPLOY = 'D';
48 static final int AO_LOG_STATE = 'S';
49 static final int AO_LOG_GPS_TIME = 'G';
50 static final int AO_LOG_GPS_LAT = 'N';
51 static final int AO_LOG_GPS_LON = 'W';
52 static final int AO_LOG_GPS_ALT = 'H';
53 static final int AO_LOG_GPS_SAT = 'V';
54 static final int AO_LOG_GPS_DATE = 'Y';
56 static final int ao_flight_startup = 0;
57 static final int ao_flight_idle = 1;
58 static final int ao_flight_pad = 2;
59 static final int ao_flight_boost = 3;
60 static final int ao_flight_fast = 4;
61 static final int ao_flight_coast = 5;
62 static final int ao_flight_drogue = 6;
63 static final int ao_flight_main = 7;
64 static final int ao_flight_landed = 8;
65 static final int ao_flight_invalid = 9;
67 static String[] state_names = {
80 static int[] ParseHex(String line) {
81 String[] tokens = line.split("\\s+");
82 int[] array = new int[tokens.length];
84 for (int i = 0; i < tokens.length; i++)
86 array[i] = Integer.parseInt(tokens[i], 16);
87 } catch (NumberFormatException ne) {
93 static int checksum(int[] line) {
95 for (int i = 1; i < line.length; i++)
100 static void FlushPending(FileWriter file, LinkedList<String> pending) throws IOException {
101 while (!pending.isEmpty()) {
102 file.write(pending.remove());
106 static void CaptureLog(AltosSerial serial_line) throws IOException, InterruptedException {
111 int year = 0, month = 0, day = 0;
113 boolean done = false;
114 boolean want_file = false;
116 FileWriter eeprom_file = null;
117 AltosFile eeprom_name;
118 LinkedList<String> eeprom_pending = new LinkedList<String>();
120 serial_line.printf("v\n");
122 /* Pull the serial number out of the version information */
125 String line = serial_line.get_reply();
127 if (line.startsWith("serial-number")) {
129 serial = Integer.parseInt(line.substring(13).trim());
130 eeprom_pending.add(String.format("%s\n", line));
131 } catch (NumberFormatException ne) {
136 /* signals the end of the version info */
137 if (line.startsWith("software-version"))
141 throw new IOException("no serial number found");
143 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
145 for (block = 0; !done && block < 511; block++) {
146 serial_line.printf("e %x\n", block);
148 for (addr = 0; addr < 0x100;) {
149 String line = serial_line.get_reply();
150 int[] values = ParseHex(line);
152 if (values == null) {
153 System.out.printf("invalid line: %s\n", line);
154 } else if (values[0] != addr) {
155 System.out.printf("data address out of sync at 0x%x\n",
156 block * 256 + values[0]);
157 } else if (checksum(values) != 0) {
158 System.out.printf("invalid checksum at 0x%x\n",
159 block * 256 + values[0]);
163 int tick = values[3] + (values[4] << 8);
164 int a = values[5] + (values[6] << 8);
165 int b = values[7] + (values[8] << 8);
167 if (cmd == AO_LOG_FLIGHT)
170 /* Monitor state transitions to update display */
171 if (cmd == AO_LOG_STATE && a <= ao_flight_landed) {
172 System.out.printf ("%s\n", state_names[a]);
173 if (a > ao_flight_pad)
178 if (cmd == AO_LOG_GPS_DATE) {
179 year = 2000 + (a & 0xff);
180 month = (a >> 8) & 0xff;
185 if (eeprom_file == null) {
186 if (serial != 0 && flight != 0 && want_file) {
187 if (year != 0 && month != 0 && day != 0)
188 eeprom_name = new AltosFile(year, month, day, serial, flight, "eeprom-new");
190 eeprom_name = new AltosFile(serial, flight, "eeprom-new");
192 eeprom_file = new FileWriter(eeprom_name);
193 if (eeprom_file != null) {
194 FlushPending(eeprom_file, eeprom_pending);
195 eeprom_pending = null;
200 String log_line = String.format("%c %4x %4x %4x\n",
202 if (eeprom_file != null)
203 eeprom_file.write(log_line);
205 eeprom_pending.add(log_line);
207 if (cmd == AO_LOG_STATE && a == ao_flight_landed) {
216 if (eeprom_file == null) {
217 eeprom_name = new AltosFile(serial,flight,"eeprom-new");
218 eeprom_file = new FileWriter(eeprom_name);
219 if (eeprom_file != null) {
220 FlushPending(eeprom_file, eeprom_pending);
223 if (eeprom_file != null) {
229 public static void SaveFlightData (JFrame frame) {
230 altos_device device = AltosDeviceDialog.show(frame, null);
231 boolean remote = false;
232 AltosSerial serial_line = new AltosSerial();
237 serial_line.open(device);
238 if (!device.getProduct().startsWith("TeleMetrum"))
242 serial_line.printf("m 0\n");
243 serial_line.set_channel(AltosPreferences.channel());
244 serial_line.printf("p\n");
246 CaptureLog(serial_line);
248 serial_line.printf("~");
250 } catch (FileNotFoundException ee) {
251 JOptionPane.showMessageDialog(frame,
252 String.format("Cannot open device \"%s\"",
254 "Cannot open target device",
255 JOptionPane.ERROR_MESSAGE);
256 } catch (IOException ee) {
257 JOptionPane.showMessageDialog(frame,
259 ee.getLocalizedMessage(),
260 JOptionPane.ERROR_MESSAGE);
261 } catch (InterruptedException ie) {