]> git.gag.com Git - fw/altos/blob - altosui/AltosEepromDownload.java
altosui: Radio channel gets set to zero by altos when frequency is set
[fw/altos] / altosui / AltosEepromDownload.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.*;
30
31 import libaltosJNI.*;
32
33 public class AltosEepromDownload implements Runnable {
34
35         JFrame                  frame;
36         AltosSerial             serial_line;
37         boolean                 remote;
38         Thread                  eeprom_thread;
39         AltosEepromMonitor      monitor;
40
41         int                     flight;
42         int                     serial;
43         int                     year, month, day;
44         boolean                 want_file;
45         FileWriter              eeprom_file;
46         LinkedList<String>      eeprom_pending;
47
48         AltosEepromList         flights;
49         ActionListener          listener;
50         boolean                 success;
51         ParseException          parse_exception;
52         String                  extension;
53
54         private void FlushPending() throws IOException {
55                 for (String s : flights.config_data) {
56                         eeprom_file.write(s);
57                         eeprom_file.write('\n');
58                 }
59
60                 for (String s : eeprom_pending)
61                         eeprom_file.write(s);
62         }
63
64         private void CheckFile(boolean force) throws IOException {
65                 if (eeprom_file != null)
66                         return;
67                 if (force || (flight != 0 && want_file)) {
68                         AltosFile               eeprom_name;
69
70                         if (extension == null)
71                                 extension = "data";
72                         if (year != 0 && month != 0 && day != 0)
73                                 eeprom_name = new AltosFile(year, month, day, serial, flight, extension);
74                         else
75                                 eeprom_name = new AltosFile(serial, flight, extension);
76
77                         eeprom_file = new FileWriter(eeprom_name);
78                         if (eeprom_file != null) {
79                                 monitor.set_file(eeprom_name.getName());
80                                 FlushPending();
81                                 eeprom_pending = null;
82                         }
83                 }
84         }
85
86         void Log(AltosEepromRecord r) throws IOException {
87                 if (r.cmd != Altos.AO_LOG_INVALID) {
88                         String log_line = String.format("%c %4x %4x %4x\n",
89                                                         r.cmd, r.tick, r.a, r.b);
90                         if (eeprom_file != null)
91                                 eeprom_file.write(log_line);
92                         else
93                                 eeprom_pending.add(log_line);
94                 }
95         }
96
97         void set_serial(int in_serial) {
98                 serial = in_serial;
99                 monitor.set_serial(serial);
100         }
101
102         void set_flight(int in_flight) {
103                 flight = in_flight;
104                 monitor.set_flight(flight);
105         }
106                 
107         boolean                 done;
108         int                     state;
109
110         void CaptureFull(AltosEepromChunk eechunk) throws IOException {
111                 boolean any_valid = false;
112
113                 extension = "eeprom";
114                 set_serial(flights.config_data.serial);
115                 for (int i = 0; i < eechunk.chunk_size && !done; i += AltosEepromRecord.record_length) {
116                         try {
117                                 AltosEepromRecord r = new AltosEepromRecord(eechunk, i);
118                                 if (r.cmd == Altos.AO_LOG_FLIGHT)
119                                         set_flight(r.b);
120
121                                 /* Monitor state transitions to update display */
122                                 if (r.cmd == Altos.AO_LOG_STATE && r.a <= Altos.ao_flight_landed) {
123                                         state = r.a;
124                                         if (state > Altos.ao_flight_pad)
125                                                 want_file = true;
126                                 }
127
128                                 if (r.cmd == Altos.AO_LOG_GPS_DATE) {
129                                         year = 2000 + (r.a & 0xff);
130                                         month = (r.a >> 8) & 0xff;
131                                         day = (r.b & 0xff);
132                                         want_file = true;
133                                 }
134                                 if (r.cmd == Altos.AO_LOG_STATE && r.a == Altos.ao_flight_landed)
135                                         done = true;
136                                 any_valid = true;
137                                 Log(r);
138                         } catch (ParseException pe) {
139                                 if (parse_exception == null)
140                                         parse_exception = pe;
141                         }
142                 }
143
144                 if (!any_valid)
145                         done = true;
146
147                 CheckFile(false);
148         }
149
150         boolean start;
151         int     tiny_tick;
152
153         void CaptureTiny (AltosEepromChunk eechunk) throws IOException {
154                 boolean any_valid = false;
155
156                 extension = "eeprom";
157                 set_serial(flights.config_data.serial);
158                 for (int i = 0; i < eechunk.data.length && !done; i += 2) {
159                         int                     v = eechunk.data16(i);
160                         AltosEepromRecord       r;
161
162                         if (i == 0 && start) {
163                                 tiny_tick = 0;
164                                 start = false;
165                                 set_flight(v);
166                                 r = new AltosEepromRecord(Altos.AO_LOG_FLIGHT, tiny_tick, 0, v);
167                                 any_valid = true;
168                         } else {
169                                 int     s = v ^ 0x8000;
170
171                                 if (Altos.ao_flight_startup <= s && s <= Altos.ao_flight_invalid) {
172                                         state = s;
173                                         r = new AltosEepromRecord(Altos.AO_LOG_STATE, tiny_tick, state, 0);
174                                         if (state == Altos.ao_flight_landed)
175                                                 done = true;
176                                         state = s;
177                                         any_valid = true;
178                                 } else {
179                                         if (v != 0xffff)
180                                                 any_valid = true;
181
182                                         r = new AltosEepromRecord(Altos.AO_LOG_PRESSURE, tiny_tick, 0, v);
183
184                                         /*
185                                          * The flight software records ascent data every 100ms, and descent
186                                          * data every 1s.
187                                          */
188                                         if (state < Altos.ao_flight_drogue)
189                                                 tiny_tick += 10;
190                                         else
191                                                 tiny_tick += 100;
192                                 }
193                         }
194                         Log(r);
195                 }
196                 CheckFile(false);
197                 if (!any_valid)
198                         done = true;
199         }
200
201         void LogTeleScience(AltosEepromTeleScience r) throws IOException {
202                 if (r.type != Altos.AO_LOG_INVALID) {
203                         String log_line = String.format("%c %4x %4x %d %5d %5d %5d %5d %5d %5d %5d %5d %5d %5d %5d %5d\n",
204                                                         r.type, r.tick, r.tm_tick, r.tm_state,
205                                                         r.data[0], r.data[1], r.data[2], r.data[3], 
206                                                         r.data[4], r.data[5], r.data[6], r.data[7], 
207                                                         r.data[8], r.data[9], r.data[10], r.data[11]);
208                         if (eeprom_file != null)
209                                 eeprom_file.write(log_line);
210                         else
211                                 eeprom_pending.add(log_line);
212                 }
213         }
214         
215         boolean telescience_start;
216
217         void CaptureTeleScience (AltosEepromChunk eechunk) throws IOException {
218                 boolean any_valid = false;
219
220                 extension = "science";
221                 for (int i = 0; i < eechunk.chunk_size && !done; i += AltosEepromTeleScience.record_length) {
222                         try {
223                                 AltosEepromTeleScience r = new AltosEepromTeleScience(eechunk, i);
224                                 if (r.type == AltosEepromTeleScience.AO_LOG_TELESCIENCE_START) {
225                                         if (telescience_start) {
226                                                 done = true;
227                                                 break;
228                                         }
229                                         set_serial(r.data[0]);
230                                         set_flight(r.data[1]);
231                                         telescience_start = true;
232                                 } else {
233                                         if (!telescience_start)
234                                                 break;
235                                 }
236                                 state = r.tm_state;
237                                 want_file =true;
238                                 any_valid = true;
239                                 LogTeleScience(r);
240                         } catch (ParseException pe) {
241                                 if (parse_exception == null)
242                                         parse_exception = pe;
243                         }
244                 }
245
246                 CheckFile(false);
247                 if (!any_valid)
248                         done = true;
249         }
250
251         void CaptureTelemetry(AltosEepromChunk eechunk) throws IOException {
252                 
253         }
254
255         void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException {
256                 int                     block, state_block = 0;
257                 int                     log_format = flights.config_data.log_format;
258
259                 state = 0;
260                 done = false;
261                 start = true;
262
263                 if (flights.config_data.serial == 0)
264                         throw new IOException("no serial number found");
265
266                 /* Reset per-capture variables */
267                 flight = 0;
268                 year = 0;
269                 month = 0;
270                 day = 0;
271                 want_file = false;
272                 eeprom_file = null;
273                 eeprom_pending = new LinkedList<String>();
274
275                 /* Set serial number in the monitor dialog window */
276                 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
277
278                 state = 0; state_block = log.start_block;
279                 for (block = log.start_block; !done && block < log.end_block; block++) {
280                         monitor.set_value(Altos.state_to_string[state], state, block - state_block);
281
282                         AltosEepromChunk        eechunk = new AltosEepromChunk(serial_line, block, block == log.start_block);
283
284                         /*
285                          * Guess what kind of data is there if the device
286                          * didn't tell us
287                          */
288
289                         if (log_format == Altos.AO_LOG_FORMAT_UNKNOWN) {
290                                 if (block == log.start_block) {
291                                         if (eechunk.data(0) == Altos.AO_LOG_FLIGHT)
292                                                 log_format = Altos.AO_LOG_FORMAT_FULL;
293                                         else
294                                                 log_format = Altos.AO_LOG_FORMAT_TINY;
295                                 }
296                         }
297
298                         switch (log_format) {
299                         case Altos.AO_LOG_FORMAT_FULL:
300                                 extension = "eeprom";
301                                 CaptureFull(eechunk);
302                                 break;
303                         case Altos.AO_LOG_FORMAT_TINY:
304                                 extension = "eeprom";
305                                 CaptureTiny(eechunk);
306                                 break;
307                         case Altos.AO_LOG_FORMAT_TELEMETRY:
308                                 extension = "telem";
309                                 CaptureTelemetry(eechunk);
310                                 break;
311                         case Altos.AO_LOG_FORMAT_TELESCIENCE:
312                                 extension = "science";
313                                 CaptureTeleScience(eechunk);
314                                 break;
315                         }
316                 }
317                 CheckFile(true);
318                 if (eeprom_file != null) {
319                         eeprom_file.flush();
320                         eeprom_file.close();
321                 }
322         }
323
324         private void show_message_internal(String message, String title, int message_type) {
325                 JOptionPane.showMessageDialog(frame,
326                                               message,
327                                               title,
328                                               message_type);
329         }
330
331         private void show_message(String in_message, String in_title, int in_message_type) {
332                 final String message = in_message;
333                 final String title = in_title;
334                 final int message_type = in_message_type;
335                 Runnable r = new Runnable() {
336                                 public void run() {
337                                         try {
338                                                 show_message_internal(message, title, message_type);
339                                         } catch (Exception ex) {
340                                         }
341                                 }
342                         };
343                 SwingUtilities.invokeLater(r);
344         }
345
346         public void run () {
347                 try {
348                         boolean failed = false;
349                         if (remote)
350                                 serial_line.start_remote();
351
352                         for (AltosEepromLog log : flights) {
353                                 parse_exception = null;
354                                 if (log.selected) {
355                                         monitor.reset();
356                                         CaptureLog(log);
357                                 }
358                                 if (parse_exception != null) {
359                                         failed = true;
360                                         show_message(String.format("Flight %d download error\n%s\nValid log data saved",
361                                                                    log.flight,
362                                                                    parse_exception.getMessage()),
363                                                      serial_line.device.toShortString(),
364                                                      JOptionPane.WARNING_MESSAGE);
365                                 }
366                         }
367                         success = !failed;
368                 } catch (IOException ee) {
369                         show_message(ee.getLocalizedMessage(),
370                                      serial_line.device.toShortString(),
371                                      JOptionPane.ERROR_MESSAGE);
372                 } catch (InterruptedException ie) {
373                         System.out.printf("download interrupted\n");
374                 } catch (TimeoutException te) {
375                         show_message(String.format("Connection to \"%s\" failed",
376                                                    serial_line.device.toShortString()),
377                                      "Connection Failed",
378                                      JOptionPane.ERROR_MESSAGE);
379                 } finally {
380                         if (remote) {
381                                 try {
382                                         serial_line.stop_remote();
383                                 } catch (InterruptedException ie) {
384                                 }
385                         }
386                         serial_line.flush_output();
387                 }
388                 monitor.done();
389                 if (listener != null) {
390                         Runnable r = new Runnable() {
391                                         public void run() {
392                                                 try {
393                                                         listener.actionPerformed(new ActionEvent(this,
394                                                                                                  success ? 1 : 0,
395                                                                                                  "download"));
396                                                 } catch (Exception ex) {
397                                                 }
398                                         }
399                                 };
400                         SwingUtilities.invokeLater(r);
401                 }
402         }
403
404         public void start() {
405                 eeprom_thread = new Thread(this);
406                 eeprom_thread.start();
407         }
408
409         public void addActionListener(ActionListener l) {
410                 listener = l;
411         }
412
413         public AltosEepromDownload(JFrame given_frame,
414                                    AltosSerial given_serial_line,
415                                    boolean given_remote,
416                                    AltosEepromList given_flights) {
417
418                 frame = given_frame;
419                 serial_line = given_serial_line;
420                 serial_line.set_frame(frame);
421                 remote = given_remote;
422                 flights = given_flights;
423                 success = false;
424
425                 monitor = new AltosEepromMonitor(frame, Altos.ao_flight_boost, Altos.ao_flight_landed);
426                 monitor.addActionListener(new ActionListener() {
427                                 public void actionPerformed(ActionEvent e) {
428                                         if (eeprom_thread != null)
429                                                 eeprom_thread.interrupt();
430                                 }
431                         });
432         }
433 }