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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altoslib_11;
23 import java.util.concurrent.*;
25 public class AltosConfigData implements Iterable<String> {
27 /* Version information */
28 public String manufacturer;
29 public String product;
32 public int log_format;
34 public String version;
35 public int altitude_32;
37 /* Strings returned */
38 public LinkedList<String> lines;
40 /* Config information */
42 public int main_deploy;
43 public int apogee_delay;
44 public int apogee_lockout;
47 public int radio_frequency;
48 public String callsign;
49 public int radio_enable;
50 public int radio_calibration;
51 public int telemetry_rate;
52 /* Old HAS_RADIO values */
53 public int radio_channel;
54 public int radio_setting;
57 public int accel_cal_plus, accel_cal_minus;
58 public int pad_orientation;
61 public int flight_log_max;
65 public int ignite_mode;
68 public String aes_key;
71 public AltosPyro[] pyros;
74 public double pyro_firing_time;
77 public int aprs_interval;
79 public int aprs_format;
84 /* Storage info replies */
85 public int storage_size;
86 public int storage_erase_unit;
88 /* Log listing replies */
89 public int stored_flight;
92 public int tracker_motion;
93 public int tracker_interval;
96 public int accel_zero_along, accel_zero_across, accel_zero_through;
99 public int ms5607_reserved;
100 public int ms5607_sens;
101 public int ms5607_off;
102 public int ms5607_tcs;
103 public int ms5607_tco;
104 public int ms5607_tref;
105 public int ms5607_tempsens;
106 public int ms5607_crc;
108 public static String get_string(String line, String label) throws ParseException {
109 if (line.startsWith(label)) {
110 String quoted = line.substring(label.length()).trim();
112 if (quoted.startsWith("\""))
113 quoted = quoted.substring(1);
114 if (quoted.endsWith("\""))
115 quoted = quoted.substring(0,quoted.length()-1);
118 throw new ParseException("mismatch", 0);
121 public static int get_int(String line, String label) throws NumberFormatException, ParseException {
122 if (line.startsWith(label)) {
123 String tail = line.substring(label.length()).trim();
124 String[] tokens = tail.split("\\s+");
125 if (tokens.length > 0)
126 return Integer.parseInt(tokens[0]);
128 throw new ParseException("mismatch", 0);
131 public static int[] get_values(String line, String label) throws NumberFormatException, ParseException {
132 if (line.startsWith(label)) {
133 String tail = line.substring(label.length()).trim();
134 String[] tokens = tail.split("\\s+");
135 if (tokens.length > 1) {
136 int[] values = new int[2];
137 values[0] = Integer.parseInt(tokens[0]);
138 values[1] = Integer.parseInt(tokens[1]);
142 throw new ParseException("mismatch", 0);
145 public Iterator<String> iterator() {
146 return lines.iterator();
149 public int log_space() {
153 if (storage_size > 0) {
154 int space = storage_size;
156 if (storage_erase_unit > 0 && use_flash_for_config())
157 space -= storage_erase_unit;
165 public int log_available() {
166 switch (log_format) {
167 case AltosLib.AO_LOG_FORMAT_TINY:
168 if (stored_flight == 0)
171 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
172 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
175 if (flight_log_max <= 0)
177 int log_max = flight_log_max * 1024;
178 int log_space = log_space();
181 if (stored_flight <= 0)
184 log_used = stored_flight * log_max;
187 if (log_used >= log_space)
190 log_avail = (log_space - log_used) / log_max;
196 public boolean has_monitor_battery() {
197 if (product.startsWith("TeleBT"))
202 int[] parse_version(String v) {
203 String[] parts = v.split("\\.");
204 int r[] = new int[parts.length];
206 for (int i = 0; i < parts.length; i++) {
208 r[i] = (int) AltosLib.fromdec(parts[i]);
209 } catch (NumberFormatException n) {
217 public int compare_version(String other) {
218 int[] me = parse_version(version);
219 int[] them = parse_version(other);
221 int l = Math.min(me.length, them.length);
223 for (int i = 0; i < l; i++) {
224 int d = me[i] - them[i];
235 public void reset() {
236 lines = new LinkedList<String>();
238 manufacturer = "unknown";
242 log_format = AltosLib.AO_LOG_FORMAT_UNKNOWN;
250 radio_frequency = -1;
253 radio_calibration = -1;
259 accel_cal_minus = -1;
260 pad_orientation = -1;
271 pyro_firing_time = -1;
280 tracker_interval = -1;
283 storage_erase_unit = -1;
286 accel_zero_along = -1;
287 accel_zero_across = -1;
288 accel_zero_through = -1;
291 public void parse_line(String line) {
293 /* Version replies */
294 try { manufacturer = get_string(line, "manufacturer"); } catch (Exception e) {}
295 try { product = get_string(line, "product"); } catch (Exception e) {}
296 try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
297 try { flight = get_int(line, "current-flight"); } catch (Exception e) {}
298 try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
299 try { log_space = get_int(line, "log-space"); } catch (Exception e) {}
300 try { altitude_32 = get_int(line, "altitude-32"); } catch (Exception e) {}
301 try { version = get_string(line, "software-version"); } catch (Exception e) {}
303 /* Version also contains MS5607 info, which we ignore here */
305 try { ms5607_reserved = get_int(line, "ms5607 reserved:"); } catch (Exception e) {}
306 try { ms5607_sens = get_int(line, "ms5607 sens:"); } catch (Exception e) {}
307 try { ms5607_off = get_int(line, "ms5607 off:"); } catch (Exception e) {}
308 try { ms5607_tcs = get_int(line, "ms5607 tcs:"); } catch (Exception e) {}
309 try { ms5607_tco = get_int(line, "ms5607 tco:"); } catch (Exception e) {}
310 try { ms5607_tref = get_int(line, "ms5607 tref:"); } catch (Exception e) {}
311 try { ms5607_tempsens = get_int(line, "ms5607 tempsens:"); } catch (Exception e) {}
312 try { ms5607_crc = get_int(line, "ms5607 crc:"); } catch (Exception e) {}
314 /* Config show replies */
317 try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
318 try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
319 try { apogee_lockout = get_int(line, "Apogee lockout:"); } catch (Exception e) {}
323 radio_frequency = get_int(line, "Frequency:");
324 if (radio_frequency < 0)
325 radio_frequency = 434550;
326 } catch (Exception e) {}
327 try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
328 try { radio_enable = get_int(line, "Radio enable:"); } catch (Exception e) {}
329 try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
330 try { telemetry_rate = get_int(line, "Telemetry rate:"); } catch (Exception e) {}
332 /* Old HAS_RADIO values */
333 try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
334 try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
338 if (line.startsWith("Accel cal")) {
339 String[] bits = line.split("\\s+");
340 if (bits.length >= 6) {
341 accel_cal_plus = Integer.parseInt(bits[3]);
342 accel_cal_minus = Integer.parseInt(bits[5]);
345 } catch (Exception e) {}
346 try { pad_orientation = get_int(line, "Pad orientation:"); } catch (Exception e) {}
349 try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
350 try { log_fixed = get_int(line, "Log fixed:"); } catch (Exception e) {}
353 try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
356 try { aes_key = get_string(line, "AES key:"); } catch (Exception e) {}
360 npyro = get_int(line, "Pyro-count:");
361 pyros = new AltosPyro[npyro];
363 } catch (Exception e) {}
366 AltosPyro p = new AltosPyro(pyro, line);
369 } catch (Exception e) {}
371 try { pyro_firing_time = get_int(line, "Pyro time:") / 100.0; } catch (Exception e) {}
374 try { aprs_interval = get_int(line, "APRS interval:"); } catch (Exception e) {}
375 try { aprs_ssid = get_int(line, "APRS SSID:"); } catch (Exception e) {}
376 try { aprs_format = get_int(line, "APRS format:"); } catch (Exception e) {}
379 try { beep = get_int(line, "Beeper setting:"); } catch (Exception e) {}
383 int[] values = get_values(line, "Tracker setting:");
384 tracker_motion = values[0];
385 tracker_interval = values[1];
386 } catch (Exception e) {}
388 /* Storage info replies */
389 try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
390 try { storage_erase_unit = get_int(line, "Storage erase unit:"); } catch (Exception e) {}
392 /* Log listing replies */
393 try { get_int(line, "flight"); stored_flight++; } catch (Exception e) {}
397 if (line.startsWith("IMU call along")) {
398 String[] bits = line.split("\\s+");
399 if (bits.length >= 8) {
400 accel_zero_along = Integer.parseInt(bits[3]);
401 accel_zero_across = Integer.parseInt(bits[5]);
402 accel_zero_through = Integer.parseInt(bits[7]);
405 } catch (Exception e) {}
408 public AltosConfigData() {
412 private void read_link(AltosLink link, String finished) throws InterruptedException, TimeoutException {
414 String line = link.get_reply();
416 throw new TimeoutException();
417 if (line.contains("Syntax error"))
419 this.parse_line(line);
421 /* signals the end of the version info */
422 if (line.startsWith(finished))
427 public boolean has_frequency() {
428 return radio_frequency >= 0 || radio_setting >= 0 || radio_channel >= 0;
431 public boolean has_telemetry_rate() {
432 return telemetry_rate >= 0;
435 public void set_frequency(double freq) {
436 int frequency = radio_frequency;
437 int setting = radio_setting;
440 radio_frequency = (int) Math.floor (freq * 1000 + 0.5);
442 } else if (setting > 0) {
443 radio_setting =AltosConvert.radio_frequency_to_setting(freq,
447 radio_channel = AltosConvert.radio_frequency_to_channel(freq);
451 public double frequency() {
452 int channel = radio_channel;
453 int setting = radio_setting;
455 if (radio_frequency < 0 && channel < 0 && setting < 0)
463 return AltosConvert.radio_to_frequency(radio_frequency,
469 boolean use_flash_for_config() {
470 if (product.startsWith("TeleMega"))
472 if (product.startsWith("TeleMetrum-v2"))
474 if (product.startsWith("EasyMega"))
480 public boolean mma655x_inverted() throws AltosUnknownProduct {
481 if (product.startsWith("EasyMega-v1"))
483 if (product.startsWith("TeleMetrum-v2"))
485 if (product.startsWith("TeleMega-v2"))
487 if (product.startsWith("TeleMega-v1"))
489 throw new AltosUnknownProduct(product);
492 public void get_values(AltosConfigValues source) throws AltosConfigDataException {
495 if (main_deploy >= 0)
496 main_deploy = source.main_deploy();
497 if (apogee_delay >= 0)
498 apogee_delay = source.apogee_delay();
499 if (apogee_lockout >= 0)
500 apogee_lockout = source.apogee_lockout();
504 set_frequency(source.radio_frequency());
505 if (radio_enable >= 0)
506 radio_enable = source.radio_enable();
507 if (callsign != null)
508 callsign = source.callsign();
509 if (telemetry_rate >= 0)
510 telemetry_rate = source.telemetry_rate();
513 if (pad_orientation >= 0)
514 pad_orientation = source.pad_orientation();
517 if (flight_log_max >= 0)
518 flight_log_max = source.flight_log_max();
521 if (ignite_mode >= 0)
522 ignite_mode = source.ignite_mode();
526 pyros = source.pyros();
527 if (pyro_firing_time >= 0)
528 pyro_firing_time = source.pyro_firing_time();
531 if (aprs_interval >= 0)
532 aprs_interval = source.aprs_interval();
534 aprs_ssid = source.aprs_ssid();
535 if (aprs_format >= 0)
536 aprs_format = source.aprs_format();
540 beep = source.beep();
542 if (tracker_motion >= 0)
543 tracker_motion = source.tracker_motion();
544 if (tracker_interval >= 0)
545 tracker_interval = source.tracker_interval();
548 public void set_values(AltosConfigValues dest) {
549 dest.set_serial(serial);
550 dest.set_product(product);
551 dest.set_version(version);
552 dest.set_altitude_32(altitude_32);
553 dest.set_main_deploy(main_deploy);
554 dest.set_apogee_delay(apogee_delay);
555 dest.set_apogee_lockout(apogee_lockout);
556 dest.set_radio_calibration(radio_calibration);
557 dest.set_radio_frequency(frequency());
558 dest.set_telemetry_rate(telemetry_rate);
559 boolean max_enabled = true;
561 if (log_space() == 0)
567 switch (log_format) {
568 case AltosLib.AO_LOG_FORMAT_TINY:
572 if (stored_flight > 0)
577 dest.set_flight_log_max_enabled(max_enabled);
578 dest.set_radio_enable(radio_enable);
579 dest.set_flight_log_max_limit(log_space() / 1024);
580 dest.set_flight_log_max(flight_log_max);
581 dest.set_ignite_mode(ignite_mode);
582 dest.set_pad_orientation(pad_orientation);
583 dest.set_callsign(callsign);
585 dest.set_pyros(pyros);
587 dest.set_pyros(null);
588 dest.set_pyro_firing_time(pyro_firing_time);
589 dest.set_aprs_interval(aprs_interval);
590 dest.set_aprs_ssid(aprs_ssid);
591 dest.set_aprs_format(aprs_format);
593 dest.set_tracker_motion(tracker_motion);
594 dest.set_tracker_interval(tracker_interval);
597 public boolean log_has_state() {
598 switch (log_format) {
599 case AltosLib.AO_LOG_FORMAT_TELEGPS:
605 public void save(AltosLink link, boolean remote) throws InterruptedException, TimeoutException {
608 if (main_deploy >= 0)
609 link.printf("c m %d\n", main_deploy);
610 if (apogee_delay >= 0)
611 link.printf("c d %d\n", apogee_delay);
612 if (apogee_lockout >= 0)
613 link.printf("c L %d\n", apogee_lockout);
616 if (has_frequency()) {
617 boolean has_frequency = radio_frequency >= 0;
618 boolean has_setting = radio_setting > 0;
619 double frequency = frequency();
620 link.set_radio_frequency(frequency,
624 /* When remote, reset the dongle frequency at the same time */
628 link.set_radio_frequency(frequency);
634 if (telemetry_rate >= 0) {
635 link.printf("c T %d\n", telemetry_rate);
639 link.set_telemetry_rate(telemetry_rate);
645 if (callsign != null) {
646 link.printf("c c %s\n", callsign);
650 link.set_callsign(callsign);
656 if (radio_enable >= 0)
657 link.printf("c e %d\n", radio_enable);
660 /* UI doesn't support accel cal */
661 if (pad_orientation >= 0)
662 link.printf("c o %d\n", pad_orientation);
665 if (flight_log_max != 0)
666 link.printf("c l %d\n", flight_log_max);
669 if (ignite_mode >= 0)
670 link.printf("c i %d\n", ignite_mode);
673 /* UI doesn't support AES key config */
677 for (int p = 0; p < pyros.length; p++) {
678 link.printf("c P %s\n",
679 pyros[p].toString());
682 if (pyro_firing_time >= 0)
683 link.printf("c I %d\n", (int) (pyro_firing_time * 100.0 + 0.5));
686 if (aprs_interval >= 0)
687 link.printf("c A %d\n", aprs_interval);
689 link.printf("c S %d\n", aprs_ssid);
690 if (aprs_format >= 0)
691 link.printf("c C %d\n", aprs_format);
695 link.printf("c b %d\n", beep);
698 if (tracker_motion >= 0 && tracker_interval >= 0)
699 link.printf("c t %d %d\n", tracker_motion, tracker_interval);
702 /* UI doesn't support accel cal */
704 link.printf("c w\n");
708 public AltosConfigData(AltosLink link) throws InterruptedException, TimeoutException {
710 link.printf("c s\nf\nv\n");
711 read_link(link, "software-version");
712 switch (log_format) {
713 case AltosLib.AO_LOG_FORMAT_UNKNOWN:
714 case AltosLib.AO_LOG_FORMAT_NONE:
718 read_link(link, "done");