altosui: Complete split out of separate java library
[fw/altos] / altosui / AltosEepromLog.java
1 /*
2  * Copyright © 2011 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 import org.altusmetrum.AltosLib.*;
31
32 import libaltosJNI.*;
33
34 /*
35  * Extract a bit of information from an eeprom-stored flight log.
36  */
37
38 public class AltosEepromLog {
39         int             serial;
40         boolean         has_flight;
41         int             flight;
42         int             start_block;
43         int             end_block;
44
45         int             year, month, day;
46
47         boolean         download;
48         boolean         delete;
49
50         public AltosEepromLog(AltosConfigData config_data,
51                               AltosSerial serial_line,
52                               int in_flight, int in_start_block,
53                               int in_end_block)
54                 throws InterruptedException, TimeoutException {
55
56                 int             block;
57                 boolean         has_date = false;
58
59                 flight = in_flight;
60                 if (flight != 0)
61                         has_flight = true;
62                 start_block = in_start_block;
63                 end_block = in_end_block;
64                 serial = config_data.serial;
65
66                 /*
67                  * By default, request that every log be downloaded but not deleted
68                  */
69                 download = true;
70                 delete = false;
71
72                 /*
73                  * Look in TeleMetrum log data for date
74                  */
75                 if (config_data.log_format == Altos.AO_LOG_FORMAT_UNKNOWN ||
76                     config_data.log_format == Altos.AO_LOG_FORMAT_FULL)
77                 {
78                         /*
79                          * Only look in the first two blocks so that this
80                          * process doesn't take a long time
81                          */
82                         if (in_end_block > in_start_block + 2)
83                                 in_end_block = in_start_block + 2;
84
85                         for (block = in_start_block; block < in_end_block; block++) {
86                                 AltosEepromChunk eechunk = new AltosEepromChunk(serial_line, block, block == in_start_block);
87
88                                 for (int i = 0; i < eechunk.chunk_size; i += AltosEepromRecord.record_length) {
89                                         try {
90                                                 AltosEepromRecord r = new AltosEepromRecord(eechunk, i);
91
92                                                 if (r.cmd == Altos.AO_LOG_FLIGHT) {
93                                                         flight = r.b;
94                                                         has_flight = true;
95                                                 }
96                                                 if (r.cmd == Altos.AO_LOG_GPS_DATE) {
97                                                         year = 2000 + (r.a & 0xff);
98                                                         month = (r.a >> 8) & 0xff;
99                                                         day = (r.b & 0xff);
100                                                         has_date = true;
101                                                 }
102                                         } catch (ParseException pe) {
103                                         }
104                                 }
105                                 if (has_date && has_flight)
106                                         break;
107                         }
108                 }
109         }
110 }