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_12;
23 import java.util.concurrent.*;
25 public class AltosConfigData {
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;
36 public int config_major, config_minor;
38 /* Config information */
40 public int main_deploy;
41 public int apogee_delay;
42 public int apogee_lockout;
45 public int radio_frequency;
46 public String callsign;
47 public int radio_enable;
48 public int radio_calibration;
49 public int telemetry_rate;
50 /* Old HAS_RADIO values */
51 public int radio_channel;
52 public int radio_setting;
55 public int accel_cal_plus, accel_cal_minus;
56 public int pad_orientation;
59 public int flight_log_max;
63 public int ignite_mode;
66 public String aes_key;
69 public AltosPyro[] pyros;
72 public double pyro_firing_time;
75 public int aprs_interval;
77 public int aprs_format;
82 /* Storage info replies */
83 public int storage_size;
84 public int storage_erase_unit;
86 /* Log listing replies */
87 public int stored_flight;
90 public int tracker_motion;
91 public int tracker_interval;
94 public int accel_zero_along, accel_zero_across, accel_zero_through;
99 public AltosMs5607 ms5607() {
101 ms5607 = new AltosMs5607();
105 public static String get_string(String line, String label) throws ParseException {
106 if (line.startsWith(label)) {
107 String quoted = line.substring(label.length()).trim();
109 if (quoted.startsWith("\""))
110 quoted = quoted.substring(1);
111 if (quoted.endsWith("\""))
112 quoted = quoted.substring(0,quoted.length()-1);
115 throw new ParseException("mismatch", 0);
118 public static int get_int(String line, String label) throws NumberFormatException, ParseException {
119 if (line.startsWith(label)) {
120 String tail = line.substring(label.length()).trim();
121 String[] tokens = tail.split("\\s+");
122 if (tokens.length > 0)
123 return Integer.parseInt(tokens[0]);
125 throw new ParseException("mismatch", 0);
128 public static int[] get_values(String line, String label) throws NumberFormatException, ParseException {
129 if (line.startsWith(label)) {
130 String tail = line.substring(label.length()).trim();
131 String[] tokens = tail.split("\\s+");
132 if (tokens.length > 1) {
133 int[] values = new int[2];
134 values[0] = Integer.parseInt(tokens[0]);
135 values[1] = Integer.parseInt(tokens[1]);
139 throw new ParseException("mismatch", 0);
142 public int log_space() {
143 if (log_space != AltosLib.MISSING)
146 if (storage_size != AltosLib.MISSING) {
147 int space = storage_size;
149 if (storage_erase_unit != AltosLib.MISSING && use_flash_for_config())
150 space -= storage_erase_unit;
152 if (space != AltosLib.MISSING)
158 public int log_available() {
159 switch (log_format) {
160 case AltosLib.AO_LOG_FORMAT_TINY:
161 if (stored_flight == 0)
164 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
165 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
168 if (flight_log_max <= 0)
170 int log_max = flight_log_max * 1024;
171 int log_space = log_space();
174 if (stored_flight <= 0)
177 log_used = stored_flight * log_max;
180 if (log_used >= log_space)
183 log_avail = (log_space - log_used) / log_max;
189 public boolean has_monitor_battery() {
190 if (product.startsWith("TeleBT"))
195 int[] parse_version(String v) {
196 String[] parts = v.split("\\.");
197 int r[] = new int[parts.length];
199 for (int i = 0; i < parts.length; i++) {
201 r[i] = (int) AltosLib.fromdec(parts[i]);
202 } catch (NumberFormatException n) {
210 public boolean altitude_32() {
211 return altitude_32 == 1;
214 public int compare_version(String other) {
215 int[] me = parse_version(version);
216 int[] them = parse_version(other);
218 int l = Math.min(me.length, them.length);
220 for (int i = 0; i < l; i++) {
221 int d = me[i] - them[i];
232 public void reset() {
235 serial = AltosLib.MISSING;
236 flight = AltosLib.MISSING;
237 log_format = AltosLib.AO_LOG_FORMAT_UNKNOWN;
238 log_space = AltosLib.MISSING;
240 config_major = AltosLib.MISSING;
241 config_minor = AltosLib.MISSING;
243 main_deploy = AltosLib.MISSING;
244 apogee_delay = AltosLib.MISSING;
245 apogee_lockout = AltosLib.MISSING;
247 radio_frequency = AltosLib.MISSING;
249 radio_enable = AltosLib.MISSING;
250 radio_calibration = AltosLib.MISSING;
251 radio_channel = AltosLib.MISSING;
252 radio_setting = AltosLib.MISSING;
253 telemetry_rate = AltosLib.MISSING;
255 accel_cal_plus = AltosLib.MISSING;
256 accel_cal_minus = AltosLib.MISSING;
257 pad_orientation = AltosLib.MISSING;
259 flight_log_max = AltosLib.MISSING;
260 log_fixed = AltosLib.MISSING;
261 ignite_mode = AltosLib.MISSING;
265 pyro = AltosLib.MISSING;
266 npyro = AltosLib.MISSING;
268 pyro_firing_time = AltosLib.MISSING;
270 aprs_interval = AltosLib.MISSING;
271 aprs_ssid = AltosLib.MISSING;
272 aprs_format = AltosLib.MISSING;
274 beep = AltosLib.MISSING;
276 tracker_motion = AltosLib.MISSING;
277 tracker_interval = AltosLib.MISSING;
279 storage_size = AltosLib.MISSING;
280 storage_erase_unit = AltosLib.MISSING;
281 stored_flight = AltosLib.MISSING;
283 accel_zero_along = AltosLib.MISSING;
284 accel_zero_across = AltosLib.MISSING;
285 accel_zero_through = AltosLib.MISSING;
288 public void parse_line(String line) {
290 /* Version replies */
291 try { manufacturer = get_string(line, "manufacturer"); } catch (Exception e) {}
292 try { product = get_string(line, "product"); } catch (Exception e) {}
293 try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
294 try { flight = get_int(line, "current-flight"); } catch (Exception e) {}
295 try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
296 try { log_space = get_int(line, "log-space"); } catch (Exception e) {}
297 try { altitude_32 = get_int(line, "altitude-32"); } catch (Exception e) {}
298 try { version = get_string(line, "software-version"); } catch (Exception e) {}
300 /* Version also contains MS5607 info, which we ignore here */
302 try { ms5607().reserved = get_int(line, "ms5607 reserved:"); } catch (Exception e) {}
303 try { ms5607().sens = get_int(line, "ms5607 sens:"); } catch (Exception e) {}
304 try { ms5607().off = get_int(line, "ms5607 off:"); } catch (Exception e) {}
305 try { ms5607().tcs = get_int(line, "ms5607 tcs:"); } catch (Exception e) {}
306 try { ms5607().tco = get_int(line, "ms5607 tco:"); } catch (Exception e) {}
307 try { ms5607().tref = get_int(line, "ms5607 tref:"); } catch (Exception e) {}
308 try { ms5607().tempsens = get_int(line, "ms5607 tempsens:"); } catch (Exception e) {}
309 try { ms5607().crc = get_int(line, "ms5607 crc:"); } catch (Exception e) {}
311 /* Config show replies */
314 if (line.startsWith("Config version")) {
315 String[] bits = line.split("\\s+");
316 if (bits.length >= 3) {
317 String[] cfg = bits[2].split("\\.");
319 if (cfg.length >= 2) {
320 config_major = Integer.parseInt(cfg[0]);
321 config_minor = Integer.parseInt(cfg[1]);
325 } catch (Exception e) {}
328 try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
329 try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
330 try { apogee_lockout = get_int(line, "Apogee lockout:"); } catch (Exception e) {}
334 radio_frequency = get_int(line, "Frequency:");
335 if (radio_frequency < 0)
336 radio_frequency = 434550;
337 } catch (Exception e) {}
338 try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
339 try { radio_enable = get_int(line, "Radio enable:"); } catch (Exception e) {}
340 try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
341 try { telemetry_rate = get_int(line, "Telemetry rate:"); } catch (Exception e) {}
343 /* Old HAS_RADIO values */
344 try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
345 try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
349 if (line.startsWith("Accel cal")) {
350 String[] bits = line.split("\\s+");
351 if (bits.length >= 6) {
352 accel_cal_plus = Integer.parseInt(bits[3]);
353 accel_cal_minus = Integer.parseInt(bits[5]);
356 } catch (Exception e) {}
357 try { pad_orientation = get_int(line, "Pad orientation:"); } catch (Exception e) {}
360 try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
361 try { log_fixed = get_int(line, "Log fixed:"); } catch (Exception e) {}
364 try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
367 try { aes_key = get_string(line, "AES key:"); } catch (Exception e) {}
371 npyro = get_int(line, "Pyro-count:");
372 pyros = new AltosPyro[npyro];
374 } catch (Exception e) {}
375 if (npyro != AltosLib.MISSING) {
377 AltosPyro p = new AltosPyro(pyro, line);
380 } catch (Exception e) {}
382 try { pyro_firing_time = get_int(line, "Pyro time:") / 100.0; } catch (Exception e) {}
385 try { aprs_interval = get_int(line, "APRS interval:"); } catch (Exception e) {}
386 try { aprs_ssid = get_int(line, "APRS SSID:"); } catch (Exception e) {}
387 try { aprs_format = get_int(line, "APRS format:"); } catch (Exception e) {}
390 try { beep = get_int(line, "Beeper setting:"); } catch (Exception e) {}
394 int[] values = get_values(line, "Tracker setting:");
395 tracker_motion = values[0];
396 tracker_interval = values[1];
397 } catch (Exception e) {}
399 /* Storage info replies */
400 try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
401 try { storage_erase_unit = get_int(line, "Storage erase unit:"); } catch (Exception e) {}
403 /* Log listing replies */
404 try { get_int(line, "flight"); stored_flight++; } catch (Exception e) {}
408 if (line.startsWith("IMU cal along")) {
409 String[] bits = line.split("\\s+");
410 if (bits.length >= 8) {
411 accel_zero_along = Integer.parseInt(bits[3]);
412 accel_zero_across = Integer.parseInt(bits[5]);
413 accel_zero_through = Integer.parseInt(bits[7]);
416 } catch (Exception e) {}
419 public AltosConfigData() {
423 private void read_link(AltosLink link, String finished) throws InterruptedException, TimeoutException {
425 String line = link.get_reply();
427 throw new TimeoutException();
428 if (line.contains("Syntax error"))
430 this.parse_line(line);
432 /* signals the end of the version info */
433 if (line.startsWith(finished))
438 public boolean has_frequency() {
439 return radio_frequency != AltosLib.MISSING || radio_setting != AltosLib.MISSING || radio_channel != AltosLib.MISSING;
442 public boolean has_telemetry_rate() {
443 return telemetry_rate != AltosLib.MISSING;
446 public void set_frequency(double freq) {
447 int frequency = radio_frequency;
448 int setting = radio_setting;
450 if (frequency != AltosLib.MISSING) {
451 radio_frequency = (int) Math.floor (freq * 1000 + 0.5);
452 radio_channel = AltosLib.MISSING;
453 } else if (setting != AltosLib.MISSING) {
454 radio_setting =AltosConvert.radio_frequency_to_setting(freq, radio_calibration);
455 radio_channel = AltosLib.MISSING;
457 radio_channel = AltosConvert.radio_frequency_to_channel(freq);
461 public double frequency() {
462 int channel = radio_channel;
463 int setting = radio_setting;
465 if (radio_frequency == AltosLib.MISSING && channel == AltosLib.MISSING && setting == AltosLib.MISSING)
466 return AltosLib.MISSING;
468 if (channel == AltosLib.MISSING)
470 if (setting == AltosLib.MISSING)
473 return AltosConvert.radio_to_frequency(radio_frequency,
479 boolean use_flash_for_config() {
480 if (product.startsWith("TeleMega"))
482 if (product.startsWith("TeleMetrum-v2"))
484 if (product.startsWith("EasyMega"))
490 public boolean mma655x_inverted() throws AltosUnknownProduct {
491 if (product != null) {
492 if (product.startsWith("EasyMega-v1"))
494 if (product.startsWith("TeleMetrum-v2"))
496 if (product.startsWith("TeleMega-v2"))
498 if (product.startsWith("TeleMega-v1"))
501 throw new AltosUnknownProduct(product);
504 public void get_values(AltosConfigValues source) throws AltosConfigDataException {
507 if (main_deploy != AltosLib.MISSING)
508 main_deploy = source.main_deploy();
509 if (apogee_delay != AltosLib.MISSING)
510 apogee_delay = source.apogee_delay();
511 if (apogee_lockout != AltosLib.MISSING)
512 apogee_lockout = source.apogee_lockout();
516 set_frequency(source.radio_frequency());
517 if (radio_enable != AltosLib.MISSING)
518 radio_enable = source.radio_enable();
519 if (callsign != null)
520 callsign = source.callsign();
521 if (telemetry_rate != AltosLib.MISSING)
522 telemetry_rate = source.telemetry_rate();
525 if (pad_orientation != AltosLib.MISSING)
526 pad_orientation = source.pad_orientation();
528 if (accel_cal_plus != AltosLib.MISSING)
529 accel_cal_plus = source.accel_cal_plus();
531 if (accel_cal_minus != AltosLib.MISSING)
532 accel_cal_minus = source.accel_cal_minus();
535 if (flight_log_max != AltosLib.MISSING)
536 flight_log_max = source.flight_log_max();
539 if (ignite_mode != AltosLib.MISSING)
540 ignite_mode = source.ignite_mode();
543 if (npyro != AltosLib.MISSING)
544 pyros = source.pyros();
545 if (pyro_firing_time != AltosLib.MISSING)
546 pyro_firing_time = source.pyro_firing_time();
549 if (aprs_interval != AltosLib.MISSING)
550 aprs_interval = source.aprs_interval();
551 if (aprs_ssid != AltosLib.MISSING)
552 aprs_ssid = source.aprs_ssid();
553 if (aprs_format != AltosLib.MISSING)
554 aprs_format = source.aprs_format();
557 if (beep != AltosLib.MISSING)
558 beep = source.beep();
560 if (tracker_motion != AltosLib.MISSING)
561 tracker_motion = source.tracker_motion();
562 if (tracker_interval != AltosLib.MISSING)
563 tracker_interval = source.tracker_interval();
566 public void set_values(AltosConfigValues dest) {
567 dest.set_serial(serial);
568 dest.set_product(product);
569 dest.set_version(version);
570 dest.set_altitude_32(altitude_32);
571 dest.set_main_deploy(main_deploy);
572 dest.set_apogee_delay(apogee_delay);
573 dest.set_apogee_lockout(apogee_lockout);
574 dest.set_radio_calibration(radio_calibration);
575 dest.set_radio_frequency(frequency());
576 dest.set_telemetry_rate(telemetry_rate);
577 boolean max_enabled = true;
579 if (log_space() == 0)
582 if (log_fixed != AltosLib.MISSING)
585 switch (log_format) {
586 case AltosLib.AO_LOG_FORMAT_TINY:
590 if (stored_flight != AltosLib.MISSING)
595 dest.set_flight_log_max_enabled(max_enabled);
596 dest.set_radio_enable(radio_enable);
597 dest.set_flight_log_max_limit(log_space() / 1024);
598 dest.set_flight_log_max(flight_log_max);
599 dest.set_ignite_mode(ignite_mode);
600 dest.set_pad_orientation(pad_orientation);
601 dest.set_accel_cal(accel_cal_plus, accel_cal_minus);
602 dest.set_callsign(callsign);
603 if (npyro != AltosLib.MISSING)
604 dest.set_pyros(pyros);
606 dest.set_pyros(null);
607 dest.set_pyro_firing_time(pyro_firing_time);
608 dest.set_aprs_interval(aprs_interval);
609 dest.set_aprs_ssid(aprs_ssid);
610 dest.set_aprs_format(aprs_format);
612 dest.set_tracker_motion(tracker_motion);
613 dest.set_tracker_interval(tracker_interval);
616 public boolean log_has_state() {
617 switch (log_format) {
618 case AltosLib.AO_LOG_FORMAT_TELEGPS:
624 public void save(AltosLink link, boolean remote) throws InterruptedException, TimeoutException {
627 if (main_deploy != AltosLib.MISSING)
628 link.printf("c m %d\n", main_deploy);
629 if (apogee_delay != AltosLib.MISSING)
630 link.printf("c d %d\n", apogee_delay);
631 if (apogee_lockout != AltosLib.MISSING)
632 link.printf("c L %d\n", apogee_lockout);
635 if (has_frequency()) {
636 boolean has_frequency = radio_frequency != AltosLib.MISSING;
637 boolean has_setting = radio_setting != AltosLib.MISSING;
638 double frequency = frequency();
639 link.set_radio_frequency(frequency,
643 /* When remote, reset the dongle frequency at the same time */
647 link.set_radio_frequency(frequency);
653 if (telemetry_rate != AltosLib.MISSING) {
654 link.printf("c T %d\n", telemetry_rate);
658 link.set_telemetry_rate(telemetry_rate);
664 if (callsign != null) {
665 link.printf("c c %s\n", callsign);
669 link.set_callsign(callsign);
675 if (radio_enable != AltosLib.MISSING)
676 link.printf("c e %d\n", radio_enable);
679 if (pad_orientation != AltosLib.MISSING)
680 link.printf("c o %d\n", pad_orientation);
681 if (accel_cal_plus != AltosLib.MISSING && accel_cal_minus != AltosLib.MISSING)
682 link.printf("c a %d %d\n", accel_cal_plus, accel_cal_minus);
685 if (flight_log_max != 0)
686 link.printf("c l %d\n", flight_log_max);
689 if (ignite_mode != AltosLib.MISSING)
690 link.printf("c i %d\n", ignite_mode);
693 /* UI doesn't support AES key config */
696 if (npyro != AltosLib.MISSING) {
697 for (int p = 0; p < pyros.length; p++) {
698 link.printf("c P %s\n",
699 pyros[p].toString());
702 if (pyro_firing_time != AltosLib.MISSING)
703 link.printf("c I %d\n", (int) (pyro_firing_time * 100.0 + 0.5));
706 if (aprs_interval != AltosLib.MISSING)
707 link.printf("c A %d\n", aprs_interval);
708 if (aprs_ssid != AltosLib.MISSING)
709 link.printf("c S %d\n", aprs_ssid);
710 if (aprs_format != AltosLib.MISSING)
711 link.printf("c C %d\n", aprs_format);
714 if (beep != AltosLib.MISSING)
715 link.printf("c b %d\n", beep);
718 if (tracker_motion != AltosLib.MISSING && tracker_interval != AltosLib.MISSING)
719 link.printf("c t %d %d\n", tracker_motion, tracker_interval);
722 /* UI doesn't support accel cal */
724 link.printf("c w\n");
728 public AltosConfigData(AltosLink link) throws InterruptedException, TimeoutException {
730 link.printf("c s\nf\nv\n");
731 read_link(link, "software-version");
732 switch (log_format) {
733 case AltosLib.AO_LOG_FORMAT_UNKNOWN:
734 case AltosLib.AO_LOG_FORMAT_NONE:
738 read_link(link, "done");