2 * Copyright © 2011 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.*;
30 import org.altusmetrum.AltosLib.*;
34 public class AltosConfigData implements Iterable<String> {
36 /* Version information */
43 /* Strings returned */
44 LinkedList<String> lines;
46 /* Config information */
55 int accel_cal_plus, accel_cal_minus;
56 int radio_calibration;
61 int storage_erase_unit;
63 static String get_string(String line, String label) throws ParseException {
64 if (line.startsWith(label)) {
65 String quoted = line.substring(label.length()).trim();
67 if (quoted.startsWith("\""))
68 quoted = quoted.substring(1);
69 if (quoted.endsWith("\""))
70 quoted = quoted.substring(0,quoted.length()-1);
73 throw new ParseException("mismatch", 0);
76 static int get_int(String line, String label) throws NumberFormatException, ParseException {
77 if (line.startsWith(label)) {
78 String tail = line.substring(label.length()).trim();
79 String[] tokens = tail.split("\\s+");
80 if (tokens.length > 0)
81 return Integer.parseInt(tokens[0]);
83 throw new ParseException("mismatch", 0);
86 public Iterator<String> iterator() {
87 return lines.iterator();
90 public int log_available() {
92 case Altos.AO_LOG_FORMAT_TINY:
93 if (stored_flight == 0)
97 if (flight_log_max <= 0)
99 int log_space = storage_size - storage_erase_unit;
100 int log_used = stored_flight * flight_log_max;
102 if (log_used >= log_space)
104 return (log_space - log_used) / flight_log_max;
108 int[] parse_version(String v) {
109 String[] parts = v.split("\\.");
110 int r[] = new int[parts.length];
112 for (int i = 0; i < parts.length; i++) {
114 r[i] = Altos.fromdec(parts[i]);
115 } catch (NumberFormatException n) {
123 public int compare_version(String other) {
124 int[] me = parse_version(version);
125 int[] them = parse_version(other);
127 int l = Math.min(me.length, them.length);
129 for (int i = 0; i < l; i++) {
130 int d = me[i] - them[i];
141 public AltosConfigData(AltosSerial serial_line) throws InterruptedException, TimeoutException {
142 serial_line.printf("c s\np\nf\nl\nv\n");
143 lines = new LinkedList<String>();
148 String line = serial_line.get_reply();
150 throw new TimeoutException();
151 if (line.contains("Syntax error"))
154 try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
155 try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
156 try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
157 try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
158 try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
159 try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
161 radio_frequency = get_int(line, "Frequency:");
162 if (radio_frequency < 0)
163 radio_frequency = 434550;
164 } catch (Exception e) {}
166 if (line.startsWith("Accel cal")) {
167 String[] bits = line.split("\\s+");
168 if (bits.length >= 6) {
169 accel_cal_plus = Integer.parseInt(bits[3]);
170 accel_cal_minus = Integer.parseInt(bits[5]);
173 } catch (Exception e) {}
174 try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
175 try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
176 try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
177 try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
178 try { version = get_string(line,"software-version"); } catch (Exception e) {}
179 try { product = get_string(line,"product"); } catch (Exception e) {}
181 try { get_int(line, "flight"); stored_flight++; } catch (Exception e) {}
182 try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
183 try { storage_erase_unit = get_int(line, "Storage erase unit"); } catch (Exception e) {}
185 /* signals the end of the version info */
186 if (line.startsWith("software-version"))