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.*;
33 public class AltosConfigData implements Iterable<String> {
35 /* Version information */
42 /* Strings returned */
43 LinkedList<String> lines;
45 /* Config information */
53 int accel_cal_plus, accel_cal_minus;
54 int radio_calibration;
59 int storage_erase_unit;
61 static String get_string(String line, String label) throws ParseException {
62 if (line.startsWith(label)) {
63 String quoted = line.substring(label.length()).trim();
65 if (quoted.startsWith("\""))
66 quoted = quoted.substring(1);
67 if (quoted.endsWith("\""))
68 quoted = quoted.substring(0,quoted.length()-1);
71 throw new ParseException("mismatch", 0);
74 static int get_int(String line, String label) throws NumberFormatException, ParseException {
75 if (line.startsWith(label)) {
76 String tail = line.substring(label.length()).trim();
77 String[] tokens = tail.split("\\s+");
78 if (tokens.length > 0)
79 return Integer.parseInt(tokens[0]);
81 throw new ParseException("mismatch", 0);
84 public Iterator<String> iterator() {
85 return lines.iterator();
88 public int log_available() {
90 case Altos.AO_LOG_FORMAT_TINY:
91 if (stored_flight == 0)
95 if (flight_log_max <= 0)
97 int log_space = storage_size - storage_erase_unit;
98 int log_used = stored_flight * flight_log_max;
100 if (log_used >= log_space)
102 return (log_space - log_used) / flight_log_max;
106 public AltosConfigData(AltosSerial serial_line) throws InterruptedException, TimeoutException {
107 serial_line.printf("c s\nf\nl\nv\n");
108 lines = new LinkedList<String>();
112 String line = serial_line.get_reply();
114 throw new TimeoutException();
115 if (line.contains("Syntax error"))
118 try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
119 try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
120 try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
121 try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
122 try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
123 try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
125 if (line.startsWith("Accel cal")) {
126 String[] bits = line.split("\\s+");
127 if (bits.length >= 6) {
128 accel_cal_plus = Integer.parseInt(bits[3]);
129 accel_cal_minus = Integer.parseInt(bits[5]);
132 } catch (Exception e) {}
133 try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
134 try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
135 try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
136 try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
137 try { version = get_string(line,"software-version"); } catch (Exception e) {}
138 try { product = get_string(line,"product"); } catch (Exception e) {}
140 try { get_int(line, "flight"); stored_flight++; } catch (Exception e) {}
141 try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
142 try { storage_erase_unit = get_int(line, "Storage erase unit"); } catch (Exception e) {}
144 /* signals the end of the version info */
145 if (line.startsWith("software-version"))