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 {
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 /* Config information */
39 public int main_deploy;
40 public int apogee_delay;
41 public int apogee_lockout;
44 public int radio_frequency;
45 public String callsign;
46 public int radio_enable;
47 public int radio_calibration;
48 public int telemetry_rate;
49 /* Old HAS_RADIO values */
50 public int radio_channel;
51 public int radio_setting;
54 public int accel_cal_plus, accel_cal_minus;
55 public int pad_orientation;
58 public int flight_log_max;
62 public int ignite_mode;
65 public String aes_key;
68 public AltosPyro[] pyros;
71 public double pyro_firing_time;
74 public int aprs_interval;
76 public int aprs_format;
81 /* Storage info replies */
82 public int storage_size;
83 public int storage_erase_unit;
85 /* Log listing replies */
86 public int stored_flight;
89 public int tracker_motion;
90 public int tracker_interval;
93 public int accel_zero_along, accel_zero_across, accel_zero_through;
98 public AltosMs5607 ms5607() {
100 ms5607 = new AltosMs5607();
104 public static String get_string(String line, String label) throws ParseException {
105 if (line.startsWith(label)) {
106 String quoted = line.substring(label.length()).trim();
108 if (quoted.startsWith("\""))
109 quoted = quoted.substring(1);
110 if (quoted.endsWith("\""))
111 quoted = quoted.substring(0,quoted.length()-1);
114 throw new ParseException("mismatch", 0);
117 public static int get_int(String line, String label) throws NumberFormatException, ParseException {
118 if (line.startsWith(label)) {
119 String tail = line.substring(label.length()).trim();
120 String[] tokens = tail.split("\\s+");
121 if (tokens.length > 0)
122 return Integer.parseInt(tokens[0]);
124 throw new ParseException("mismatch", 0);
127 public static int[] get_values(String line, String label) throws NumberFormatException, ParseException {
128 if (line.startsWith(label)) {
129 String tail = line.substring(label.length()).trim();
130 String[] tokens = tail.split("\\s+");
131 if (tokens.length > 1) {
132 int[] values = new int[2];
133 values[0] = Integer.parseInt(tokens[0]);
134 values[1] = Integer.parseInt(tokens[1]);
138 throw new ParseException("mismatch", 0);
141 public int log_space() {
145 if (storage_size > 0) {
146 int space = storage_size;
148 if (storage_erase_unit > 0 && use_flash_for_config())
149 space -= storage_erase_unit;
157 public int log_available() {
158 switch (log_format) {
159 case AltosLib.AO_LOG_FORMAT_TINY:
160 if (stored_flight == 0)
163 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
164 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
167 if (flight_log_max <= 0)
169 int log_max = flight_log_max * 1024;
170 int log_space = log_space();
173 if (stored_flight <= 0)
176 log_used = stored_flight * log_max;
179 if (log_used >= log_space)
182 log_avail = (log_space - log_used) / log_max;
188 public boolean has_monitor_battery() {
189 if (product.startsWith("TeleBT"))
194 int[] parse_version(String v) {
195 String[] parts = v.split("\\.");
196 int r[] = new int[parts.length];
198 for (int i = 0; i < parts.length; i++) {
200 r[i] = (int) AltosLib.fromdec(parts[i]);
201 } catch (NumberFormatException n) {
209 public boolean altitude_32() {
210 return altitude_32 == 1;
213 public int compare_version(String other) {
214 int[] me = parse_version(version);
215 int[] them = parse_version(other);
217 int l = Math.min(me.length, them.length);
219 for (int i = 0; i < l; i++) {
220 int d = me[i] - them[i];
231 public void reset() {
234 serial = AltosLib.MISSING;
235 flight = AltosLib.MISSING;
236 log_format = AltosLib.AO_LOG_FORMAT_UNKNOWN;
237 log_space = AltosLib.MISSING;
240 main_deploy = AltosLib.MISSING;
241 apogee_delay = AltosLib.MISSING;
242 apogee_lockout = AltosLib.MISSING;
244 radio_frequency = AltosLib.MISSING;
246 radio_enable = AltosLib.MISSING;
247 radio_calibration = AltosLib.MISSING;
248 radio_channel = AltosLib.MISSING;
249 radio_setting = AltosLib.MISSING;
250 telemetry_rate = AltosLib.MISSING;
252 accel_cal_plus = AltosLib.MISSING;
253 accel_cal_minus = AltosLib.MISSING;
254 pad_orientation = AltosLib.MISSING;
256 flight_log_max = AltosLib.MISSING;
257 log_fixed = AltosLib.MISSING;
258 ignite_mode = AltosLib.MISSING;
265 pyro_firing_time = AltosLib.MISSING;
267 aprs_interval = AltosLib.MISSING;
268 aprs_ssid = AltosLib.MISSING;
269 aprs_format = AltosLib.MISSING;
271 beep = AltosLib.MISSING;
273 tracker_motion = AltosLib.MISSING;
274 tracker_interval = AltosLib.MISSING;
276 storage_size = AltosLib.MISSING;
277 storage_erase_unit = AltosLib.MISSING;
278 stored_flight = AltosLib.MISSING;
280 accel_zero_along = AltosLib.MISSING;
281 accel_zero_across = AltosLib.MISSING;
282 accel_zero_through = AltosLib.MISSING;
285 public void parse_line(String line) {
286 /* Version replies */
287 try { manufacturer = get_string(line, "manufacturer"); } catch (Exception e) {}
288 try { product = get_string(line, "product"); } catch (Exception e) {}
289 try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
290 try { flight = get_int(line, "current-flight"); } catch (Exception e) {}
291 try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
292 try { log_space = get_int(line, "log-space"); } catch (Exception e) {}
293 try { altitude_32 = get_int(line, "altitude-32"); } catch (Exception e) {}
294 try { version = get_string(line, "software-version"); } catch (Exception e) {}
296 /* Version also contains MS5607 info, which we ignore here */
298 try { ms5607().reserved = get_int(line, "ms5607 reserved:"); } catch (Exception e) {}
299 try { ms5607().sens = get_int(line, "ms5607 sens:"); } catch (Exception e) {}
300 try { ms5607().off = get_int(line, "ms5607 off:"); } catch (Exception e) {}
301 try { ms5607().tcs = get_int(line, "ms5607 tcs:"); } catch (Exception e) {}
302 try { ms5607().tco = get_int(line, "ms5607 tco:"); } catch (Exception e) {}
303 try { ms5607().tref = get_int(line, "ms5607 tref:"); } catch (Exception e) {}
304 try { ms5607().tempsens = get_int(line, "ms5607 tempsens:"); } catch (Exception e) {}
305 try { ms5607().crc = get_int(line, "ms5607 crc:"); } catch (Exception e) {}
307 /* Config show replies */
310 try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
311 try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
312 try { apogee_lockout = get_int(line, "Apogee lockout:"); } catch (Exception e) {}
316 radio_frequency = get_int(line, "Frequency:");
317 if (radio_frequency < 0)
318 radio_frequency = 434550;
319 } catch (Exception e) {}
320 try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
321 try { radio_enable = get_int(line, "Radio enable:"); } catch (Exception e) {}
322 try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
323 try { telemetry_rate = get_int(line, "Telemetry rate:"); } catch (Exception e) {}
325 /* Old HAS_RADIO values */
326 try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
327 try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
331 if (line.startsWith("Accel cal")) {
332 String[] bits = line.split("\\s+");
333 if (bits.length >= 6) {
334 accel_cal_plus = Integer.parseInt(bits[3]);
335 accel_cal_minus = Integer.parseInt(bits[5]);
338 } catch (Exception e) {}
339 try { pad_orientation = get_int(line, "Pad orientation:"); } catch (Exception e) {}
342 try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
343 try { log_fixed = get_int(line, "Log fixed:"); } catch (Exception e) {}
346 try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
349 try { aes_key = get_string(line, "AES key:"); } catch (Exception e) {}
353 npyro = get_int(line, "Pyro-count:");
354 pyros = new AltosPyro[npyro];
356 } catch (Exception e) {}
359 AltosPyro p = new AltosPyro(pyro, line);
362 } catch (Exception e) {}
364 try { pyro_firing_time = get_int(line, "Pyro time:") / 100.0; } catch (Exception e) {}
367 try { aprs_interval = get_int(line, "APRS interval:"); } catch (Exception e) {}
368 try { aprs_ssid = get_int(line, "APRS SSID:"); } catch (Exception e) {}
369 try { aprs_format = get_int(line, "APRS format:"); } catch (Exception e) {}
372 try { beep = get_int(line, "Beeper setting:"); } catch (Exception e) {}
376 int[] values = get_values(line, "Tracker setting:");
377 tracker_motion = values[0];
378 tracker_interval = values[1];
379 } catch (Exception e) {}
381 /* Storage info replies */
382 try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
383 try { storage_erase_unit = get_int(line, "Storage erase unit:"); } catch (Exception e) {}
385 /* Log listing replies */
386 try { get_int(line, "flight"); stored_flight++; } catch (Exception e) {}
390 if (line.startsWith("IMU call along")) {
391 String[] bits = line.split("\\s+");
392 if (bits.length >= 8) {
393 accel_zero_along = Integer.parseInt(bits[3]);
394 accel_zero_across = Integer.parseInt(bits[5]);
395 accel_zero_through = Integer.parseInt(bits[7]);
398 } catch (Exception e) {}
401 public AltosConfigData() {
405 private void read_link(AltosLink link, String finished) throws InterruptedException, TimeoutException {
407 String line = link.get_reply();
409 throw new TimeoutException();
410 if (line.contains("Syntax error"))
412 this.parse_line(line);
414 /* signals the end of the version info */
415 if (line.startsWith(finished))
420 public boolean has_frequency() {
421 return radio_frequency >= 0 || radio_setting >= 0 || radio_channel >= 0;
424 public boolean has_telemetry_rate() {
425 return telemetry_rate >= 0;
428 public void set_frequency(double freq) {
429 int frequency = radio_frequency;
430 int setting = radio_setting;
433 radio_frequency = (int) Math.floor (freq * 1000 + 0.5);
434 radio_channel = AltosLib.MISSING;
435 } else if (setting > 0) {
436 radio_setting =AltosConvert.radio_frequency_to_setting(freq,
438 radio_channel = AltosLib.MISSING;
440 radio_channel = AltosConvert.radio_frequency_to_channel(freq);
444 public double frequency() {
445 int channel = radio_channel;
446 int setting = radio_setting;
448 if (radio_frequency < 0 && channel < 0 && setting < 0)
449 return AltosLib.MISSING;
456 return AltosConvert.radio_to_frequency(radio_frequency,
462 boolean use_flash_for_config() {
463 if (product.startsWith("TeleMega"))
465 if (product.startsWith("TeleMetrum-v2"))
467 if (product.startsWith("EasyMega"))
473 public boolean mma655x_inverted() throws AltosUnknownProduct {
474 if (product.startsWith("EasyMega-v1"))
476 if (product.startsWith("TeleMetrum-v2"))
478 if (product.startsWith("TeleMega-v2"))
480 if (product.startsWith("TeleMega-v1"))
482 throw new AltosUnknownProduct(product);
485 public void get_values(AltosConfigValues source) throws AltosConfigDataException {
488 if (main_deploy >= 0)
489 main_deploy = source.main_deploy();
490 if (apogee_delay >= 0)
491 apogee_delay = source.apogee_delay();
492 if (apogee_lockout >= 0)
493 apogee_lockout = source.apogee_lockout();
497 set_frequency(source.radio_frequency());
498 if (radio_enable >= 0)
499 radio_enable = source.radio_enable();
500 if (callsign != null)
501 callsign = source.callsign();
502 if (telemetry_rate >= 0)
503 telemetry_rate = source.telemetry_rate();
506 if (pad_orientation >= 0)
507 pad_orientation = source.pad_orientation();
510 if (flight_log_max >= 0)
511 flight_log_max = source.flight_log_max();
514 if (ignite_mode >= 0)
515 ignite_mode = source.ignite_mode();
519 pyros = source.pyros();
520 if (pyro_firing_time >= 0)
521 pyro_firing_time = source.pyro_firing_time();
524 if (aprs_interval >= 0)
525 aprs_interval = source.aprs_interval();
527 aprs_ssid = source.aprs_ssid();
528 if (aprs_format >= 0)
529 aprs_format = source.aprs_format();
533 beep = source.beep();
535 if (tracker_motion >= 0)
536 tracker_motion = source.tracker_motion();
537 if (tracker_interval >= 0)
538 tracker_interval = source.tracker_interval();
541 public void set_values(AltosConfigValues dest) {
542 dest.set_serial(serial);
543 dest.set_product(product);
544 dest.set_version(version);
545 dest.set_altitude_32(altitude_32);
546 dest.set_main_deploy(main_deploy);
547 dest.set_apogee_delay(apogee_delay);
548 dest.set_apogee_lockout(apogee_lockout);
549 dest.set_radio_calibration(radio_calibration);
550 dest.set_radio_frequency(frequency());
551 dest.set_telemetry_rate(telemetry_rate);
552 boolean max_enabled = true;
554 if (log_space() == 0)
560 switch (log_format) {
561 case AltosLib.AO_LOG_FORMAT_TINY:
565 if (stored_flight > 0)
570 dest.set_flight_log_max_enabled(max_enabled);
571 dest.set_radio_enable(radio_enable);
572 dest.set_flight_log_max_limit(log_space() / 1024);
573 dest.set_flight_log_max(flight_log_max);
574 dest.set_ignite_mode(ignite_mode);
575 dest.set_pad_orientation(pad_orientation);
576 dest.set_callsign(callsign);
578 dest.set_pyros(pyros);
580 dest.set_pyros(null);
581 dest.set_pyro_firing_time(pyro_firing_time);
582 dest.set_aprs_interval(aprs_interval);
583 dest.set_aprs_ssid(aprs_ssid);
584 dest.set_aprs_format(aprs_format);
586 dest.set_tracker_motion(tracker_motion);
587 dest.set_tracker_interval(tracker_interval);
590 public boolean log_has_state() {
591 switch (log_format) {
592 case AltosLib.AO_LOG_FORMAT_TELEGPS:
598 public void save(AltosLink link, boolean remote) throws InterruptedException, TimeoutException {
601 if (main_deploy >= 0)
602 link.printf("c m %d\n", main_deploy);
603 if (apogee_delay >= 0)
604 link.printf("c d %d\n", apogee_delay);
605 if (apogee_lockout >= 0)
606 link.printf("c L %d\n", apogee_lockout);
609 if (has_frequency()) {
610 boolean has_frequency = radio_frequency >= 0;
611 boolean has_setting = radio_setting > 0;
612 double frequency = frequency();
613 link.set_radio_frequency(frequency,
617 /* When remote, reset the dongle frequency at the same time */
621 link.set_radio_frequency(frequency);
627 if (telemetry_rate >= 0) {
628 link.printf("c T %d\n", telemetry_rate);
632 link.set_telemetry_rate(telemetry_rate);
638 if (callsign != null) {
639 link.printf("c c %s\n", callsign);
643 link.set_callsign(callsign);
649 if (radio_enable >= 0)
650 link.printf("c e %d\n", radio_enable);
653 /* UI doesn't support accel cal */
654 if (pad_orientation >= 0)
655 link.printf("c o %d\n", pad_orientation);
658 if (flight_log_max != 0)
659 link.printf("c l %d\n", flight_log_max);
662 if (ignite_mode >= 0)
663 link.printf("c i %d\n", ignite_mode);
666 /* UI doesn't support AES key config */
670 for (int p = 0; p < pyros.length; p++) {
671 link.printf("c P %s\n",
672 pyros[p].toString());
675 if (pyro_firing_time >= 0)
676 link.printf("c I %d\n", (int) (pyro_firing_time * 100.0 + 0.5));
679 if (aprs_interval >= 0)
680 link.printf("c A %d\n", aprs_interval);
682 link.printf("c S %d\n", aprs_ssid);
683 if (aprs_format >= 0)
684 link.printf("c C %d\n", aprs_format);
688 link.printf("c b %d\n", beep);
691 if (tracker_motion >= 0 && tracker_interval >= 0)
692 link.printf("c t %d %d\n", tracker_motion, tracker_interval);
695 /* UI doesn't support accel cal */
697 link.printf("c w\n");
701 public AltosConfigData(AltosLink link) throws InterruptedException, TimeoutException {
703 link.printf("c s\nf\nv\n");
704 read_link(link, "software-version");
705 switch (log_format) {
706 case AltosLib.AO_LOG_FORMAT_UNKNOWN:
707 case AltosLib.AO_LOG_FORMAT_NONE:
711 read_link(link, "done");