altosui: Output recorded clock tick in CSV files
[fw/altos] / altosui / AltosConfigData.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
31 import libaltosJNI.*;
32
33 public class AltosConfigData implements Iterable<String> {
34
35         /* Version information */
36         String  manufacturer;
37         String  product;
38         String  version;
39         int     log_format;
40         int     serial;
41
42         /* Strings returned */
43         LinkedList<String>      lines;
44
45         /* Config information */
46         int     config_major;
47         int     config_minor;
48         int     main_deploy;
49         int     apogee_delay;
50         int     radio_channel;
51         int     radio_setting;
52         int     radio_frequency;
53         String  callsign;
54         int     accel_cal_plus, accel_cal_minus;
55         int     radio_calibration;
56         int     flight_log_max;
57         int     ignite_mode;
58         int     stored_flight;
59         int     storage_size;
60         int     storage_erase_unit;
61
62         static String get_string(String line, String label) throws  ParseException {
63                 if (line.startsWith(label)) {
64                         String  quoted = line.substring(label.length()).trim();
65
66                         if (quoted.startsWith("\""))
67                                 quoted = quoted.substring(1);
68                         if (quoted.endsWith("\""))
69                                 quoted = quoted.substring(0,quoted.length()-1);
70                         return quoted;
71                 }
72                 throw new ParseException("mismatch", 0);
73         }
74
75         static int get_int(String line, String label) throws NumberFormatException, ParseException {
76                 if (line.startsWith(label)) {
77                         String tail = line.substring(label.length()).trim();
78                         String[] tokens = tail.split("\\s+");
79                         if (tokens.length > 0)
80                                 return  Integer.parseInt(tokens[0]);
81                 }
82                 throw new ParseException("mismatch", 0);
83         }
84
85         public Iterator<String> iterator() {
86                 return lines.iterator();
87         }
88
89         public int log_available() {
90                 switch (log_format) {
91                 case Altos.AO_LOG_FORMAT_TINY:
92                         if (stored_flight == 0)
93                                 return 1;
94                         return 0;
95                 default:
96                         if (flight_log_max <= 0)
97                                 return 1;
98                         int     log_space = storage_size - storage_erase_unit;
99                         int     log_used = stored_flight * flight_log_max;
100
101                         if (log_used >= log_space)
102                                 return 0;
103                         return (log_space - log_used) / flight_log_max;
104                 }
105         }
106
107         int[] parse_version(String v) {
108                 String[] parts = v.split("\\.");
109                 int r[] = new int[parts.length];
110
111                 for (int i = 0; i < parts.length; i++) {
112                         try {
113                                 r[i] = Altos.fromdec(parts[i]);
114                         } catch (NumberFormatException n) {
115                                 r[i] = 0;
116                         }
117                 }
118
119                 return r;
120         }
121         
122         public int compare_version(String other) {
123                 int[]   me = parse_version(version);
124                 int[]   them = parse_version(other);
125
126                 int     l = Math.min(me.length, them.length);
127
128                 for (int i = 0; i < l; i++) {
129                         int     d = me[i] - them[i];
130                         if (d != 0)
131                                 return d;
132                 }
133                 if (me.length > l)
134                         return 1;
135                 if (them.length > l)
136                         return -1;
137                 return 0;
138         }
139
140         public AltosConfigData(AltosSerial serial_line) throws InterruptedException, TimeoutException {
141                 serial_line.printf("c s\nf\nl\nv\n");
142                 lines = new LinkedList<String>();
143                 radio_setting = 0;
144                 radio_frequency = 0;
145                 stored_flight = 0;
146                 for (;;) {
147                         String line = serial_line.get_reply();
148                         if (line == null)
149                                 throw new TimeoutException();
150                         if (line.contains("Syntax error"))
151                                 continue;
152                         lines.add(line);
153                         try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
154                         try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
155                         try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
156                         try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
157                         try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
158                         try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
159                         try {
160                                 radio_frequency = get_int(line, "Frequency:");
161                                 if (radio_frequency < 0)
162                                         radio_frequency = 434550;
163                         } catch (Exception e) {}
164                         try {
165                                 if (line.startsWith("Accel cal")) {
166                                         String[] bits = line.split("\\s+");
167                                         if (bits.length >= 6) {
168                                                 accel_cal_plus = Integer.parseInt(bits[3]);
169                                                 accel_cal_minus = Integer.parseInt(bits[5]);
170                                         }
171                                 }
172                         } catch (Exception e) {}
173                         try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
174                         try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
175                         try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
176                         try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
177                         try { version = get_string(line,"software-version"); } catch (Exception e) {}
178                         try { product = get_string(line,"product"); } catch (Exception e) {}
179
180                         try { get_int(line, "flight"); stored_flight++; }  catch (Exception e) {}
181                         try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
182                         try { storage_erase_unit = get_int(line, "Storage erase unit"); } catch (Exception e) {}
183
184                         /* signals the end of the version info */
185                         if (line.startsWith("software-version"))
186                                 break;
187                 }
188         }
189
190 }