altosui: Add comments to Eeprom reader
[fw/altos] / ao-tools / altosui / AltosEepromReader.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.LinkedBlockingQueue;
30
31 import altosui.AltosRecord;
32 import altosui.AltosState;
33 import altosui.AltosDeviceDialog;
34 import altosui.AltosPreferences;
35 import altosui.AltosLog;
36 import altosui.AltosVoice;
37 import altosui.AltosEepromMonitor;
38
39 /*
40  * AltosRecords with an index field so they can be sorted by tick while preserving
41  * the original ordering for elements with matching ticks
42  */
43 class AltosOrderedRecord extends AltosEepromRecord implements Comparable<AltosOrderedRecord> {
44
45         int     index;
46
47         public AltosOrderedRecord(String line, int in_index, int prev_tick)
48                 throws ParseException {
49                 super(line);
50                 int new_tick = tick | (prev_tick & ~0xffff);
51                 if (new_tick < prev_tick) {
52                         if (prev_tick - new_tick > 0x8000)
53                                 new_tick += 0x10000;
54                 }
55                 tick = new_tick;
56                 index = in_index;
57         }
58
59         public int compareTo(AltosOrderedRecord o) {
60                 int     tick_diff = tick - o.tick;
61                 if (tick_diff != 0)
62                         return tick_diff;
63                 return index - o.index;
64         }
65 }
66
67 public class AltosEepromReader {
68
69         static final int        seen_flight = 1;
70         static final int        seen_sensor = 2;
71         static final int        seen_temp_volt = 4;
72         static final int        seen_deploy = 8;
73         static final int        seen_gps_time = 16;
74         static final int        seen_gps_lat = 32;
75         static final int        seen_gps_lon = 64;
76
77         static final int        seen_basic = seen_flight|seen_sensor|seen_temp_volt|seen_deploy;
78
79         AltosRecord             state;
80         AltosOrderedRecord      record;
81
82         TreeSet<AltosOrderedRecord>     records;
83
84         Iterator<AltosOrderedRecord>                    record_iterator;
85
86         int                     seen;
87
88         int                     index;
89
90         boolean                 last_reported;
91
92         double                  ground_pres;
93         double                  ground_accel;
94
95         int                     n_pad_samples;
96
97         int                     gps_tick;
98
99         FileInputStream input;
100
101         public AltosRecord read() throws IOException, ParseException {
102                 for (;;) {
103                         if (record == null) {
104                                 if (!record_iterator.hasNext()) {
105                                         if (last_reported)
106                                                 return null;
107                                         last_reported = true;
108                                         return state;
109                                 }
110                                 record = record_iterator.next();
111
112                                 if ((seen & seen_basic) == seen_basic && record.tick != state.tick)
113                                         return new AltosRecord(state);
114                         }
115
116                         state.tick = record.tick;
117                         switch (record.cmd) {
118                         case Altos.AO_LOG_FLIGHT:
119                                 state.ground_accel = record.a;
120                                 state.flight = record.b;
121                                 seen |= seen_flight;
122                                 break;
123                         case Altos.AO_LOG_SENSOR:
124                                 state.accel = record.a;
125                                 state.pres = record.b;
126                                 if (state.state < Altos.ao_flight_boost) {
127                                         n_pad_samples++;
128                                         ground_pres += state.pres;
129                                         state.ground_pres = (int) (ground_pres / n_pad_samples);
130                                         state.flight_pres = state.ground_pres;
131                                         System.out.printf("ground pressure %d altitude %f\n",
132                                                           record.b, state.altitude());
133                                         ground_accel += state.accel;
134                                         state.ground_accel = (int) (ground_accel / n_pad_samples);
135                                         state.flight_accel = state.ground_accel;
136                                 } else {
137                                         state.flight_pres = (state.flight_pres * 15 + state.pres) / 16;
138                                         state.flight_accel = (state.flight_accel * 15 + state.accel) / 16;
139                                         state.flight_vel += (state.accel_plus_g - state.accel);
140                                 }
141                                 seen |= seen_sensor;
142                                 break;
143                         case Altos.AO_LOG_TEMP_VOLT:
144                                 state.temp = record.a;
145                                 state.batt = record.b;
146                                 seen |= seen_temp_volt;
147                                 break;
148                         case Altos.AO_LOG_DEPLOY:
149                                 state.drogue = record.a;
150                                 state.main = record.b;
151                                 seen |= seen_deploy;
152                                 break;
153                         case Altos.AO_LOG_STATE:
154                                 System.out.printf("state %d\n", record.a);
155                                 state.state = record.a;
156                                 break;
157                         case Altos.AO_LOG_GPS_TIME:
158                                 gps_tick = state.tick;
159                                 state.gps = new AltosGPS();
160                                 state.gps.hour = (record.a & 0xff);
161                                 state.gps.minute = (record.a >> 8);
162                                 state.gps.second = (record.b & 0xff);
163                                 int flags = (record.b >> 8);
164                                 state.gps.connected = (flags & Altos.AO_GPS_RUNNING) != 0;
165                                 state.gps.locked = (flags & Altos.AO_GPS_VALID) != 0;
166                                 state.gps.date_valid = (flags & Altos.AO_GPS_DATE_VALID) != 0;
167                                 state.gps.nsat = (flags & Altos.AO_GPS_NUM_SAT_MASK) >>
168                                         Altos.AO_GPS_NUM_SAT_SHIFT;
169                                 break;
170                         case Altos.AO_LOG_GPS_LAT:
171                                 int lat32 = record.a | (record.b << 16);
172                                 state.gps.lat = (double) lat32 / 1e7;
173                                 break;
174                         case Altos.AO_LOG_GPS_LON:
175                                 int lon32 = record.a | (record.b << 16);
176                                 state.gps.lon = (double) lon32 / 1e7;
177                                 break;
178                         case Altos.AO_LOG_GPS_ALT:
179                                 state.gps.alt = record.a;
180                                 break;
181                         case Altos.AO_LOG_GPS_SAT:
182                                 if (state.tick == gps_tick) {
183                                         int svid = record.a;
184                                         int c_n0 = record.b >> 8;
185                                         state.gps.add_sat(svid, c_n0);
186                                 }
187                                 break;
188                         case Altos.AO_LOG_GPS_DATE:
189                                 state.gps.year = record.a & 0xff;
190                                 state.gps.month = record.a >> 8;
191                                 state.gps.day = record.b & 0xff;
192                                 break;
193
194                         case Altos.AO_LOG_CONFIG_VERSION:
195                                 break;
196                         case Altos.AO_LOG_MAIN_DEPLOY:
197                                 break;
198                         case Altos.AO_LOG_APOGEE_DELAY:
199                                 break;
200                         case Altos.AO_LOG_RADIO_CHANNEL:
201                                 break;
202                         case Altos.AO_LOG_CALLSIGN:
203                                 state.callsign = record.data;
204                                 break;
205                         case Altos.AO_LOG_ACCEL_CAL:
206                                 state.accel_plus_g = record.a;
207                                 state.accel_minus_g = record.b;
208                                 break;
209                         case Altos.AO_LOG_RADIO_CAL:
210                                 break;
211                         case Altos.AO_LOG_MANUFACTURER:
212                                 break;
213                         case Altos.AO_LOG_PRODUCT:
214                                 break;
215                         case Altos.AO_LOG_SERIAL_NUMBER:
216                                 break;
217                         case Altos.AO_LOG_SOFTWARE_VERSION:
218                                 break;
219                         }
220                         record = null;
221                 }
222         }
223
224         /*
225          * Read the whole file, dumping records into a RB tree so
226          * we can enumerate them in time order -- the eeprom data
227          * are sometimes out of order with GPS data getting timestamps
228          * matching the first packet out of the GPS unit but not
229          * written until the final GPS packet has been received.
230          */
231         public AltosEepromReader (FileInputStream in_input) {
232                 state = new AltosRecord();
233                 state.state = Altos.ao_flight_pad;
234                 state.accel_plus_g = 15758;
235                 state.accel_minus_g = 16294;
236                 input = in_input;
237                 seen = 0;
238                 records = new TreeSet<AltosOrderedRecord>();
239
240                 int index = 0;
241                 int tick = 0;
242
243                 try {
244                         for (;;) {
245                                 String line = AltosRecord.gets(input);
246                                 if (line == null)
247                                         break;
248                                 AltosOrderedRecord record = new AltosOrderedRecord(line, index++, tick);
249                                 if (record == null)
250                                         break;
251                                 tick = record.tick;
252                                 records.add(record);
253                         }
254                 } catch (IOException io) {
255                 } catch (ParseException pe) {
256                 }
257                 record_iterator = records.iterator();
258         }
259 }