a97b996fa89800ba1000791196d933817ff32bc1
[fw/altos] / altoslib / AltosConfigData.java
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 package org.altusmetrum.altoslib_13;
20
21 import java.util.*;
22 import java.text.*;
23 import java.util.concurrent.*;
24
25 /* Don't change the field names in this structure; they're part of all .eeprom files */
26 public class AltosConfigData {
27
28         /* Version information */
29         public String   manufacturer;
30         public String   product;
31         public int      serial;
32         public int      flight;
33         public int      log_format;
34         public int      log_space;
35         public String   version;
36         public int      altitude_32;
37         public int      config_major, config_minor;
38
39         /* Config information */
40         /* HAS_FLIGHT*/
41         public int      main_deploy;
42         public int      apogee_delay;
43         public int      apogee_lockout;
44
45         /* HAS_RADIO */
46         public int      radio_frequency;
47         public String   callsign;
48         public int      radio_enable;
49         public int      radio_calibration;
50         public int      telemetry_rate;
51         /* Old HAS_RADIO values */
52         public int      radio_channel;
53         public int      radio_setting;
54
55         /* HAS_ACCEL */
56         public int      accel_cal_plus, accel_cal_minus;
57         private int     accel_cal_plus_cooked, accel_cal_minus_cooked;
58         private boolean accel_cal_adjusted;
59         public int      pad_orientation;
60
61         /* HAS_LOG */
62         public int      flight_log_max;
63         public int      log_fixed;
64
65         /* HAS_IGNITE */
66         public int      ignite_mode;
67
68         /* HAS_AES */
69         public String   aes_key;
70
71         /* AO_PYRO_NUM */
72         public AltosPyro[]      pyros;
73         public int              npyro;
74         public int              pyro;
75         public double           pyro_firing_time;
76
77         /* HAS_APRS */
78         public int              aprs_interval;
79         public int              aprs_ssid;
80         public int              aprs_format;
81
82         /* HAS_BEEP */
83         public int              beep;
84
85         /* Storage info replies */
86         public int      storage_size;
87         public int      storage_erase_unit;
88
89         /* Log listing replies */
90         public int      stored_flight;
91
92         /* HAS_TRACKER */
93         public int      tracker_motion;
94         public int      tracker_interval;
95
96         /* HAS_GYRO */
97         public int      accel_zero_along, accel_zero_across, accel_zero_through;
98
99         /* ms5607 data */
100         AltosMs5607     ms5607;
101
102         public AltosMs5607 ms5607() {
103                 if (ms5607 == null)
104                         ms5607 = new AltosMs5607();
105                 return ms5607;
106         }
107
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();
111
112                         if (quoted.startsWith("\""))
113                                 quoted = quoted.substring(1);
114                         if (quoted.endsWith("\""))
115                                 quoted = quoted.substring(0,quoted.length()-1);
116                         return quoted;
117                 }
118                 throw new ParseException("mismatch", 0);
119         }
120
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]);
127                 }
128                 throw new ParseException("mismatch", 0);
129         }
130
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]);
139                                 return values;
140                         }
141                 }
142                 throw new ParseException("mismatch", 0);
143         }
144
145         public int log_space() {
146                 if (log_space != AltosLib.MISSING)
147                         return log_space;
148
149                 if (storage_size != AltosLib.MISSING) {
150                         int     space = storage_size;
151
152                         if (storage_erase_unit != AltosLib.MISSING && use_flash_for_config())
153                                 space -= storage_erase_unit;
154
155                         if (space != AltosLib.MISSING)
156                                 return space;
157                 }
158                 return 0;
159         }
160
161         public int log_available() {
162                 switch (log_format) {
163                 case AltosLib.AO_LOG_FORMAT_TINY:
164                         if (stored_flight == 0)
165                                 return 1;
166                         return 0;
167                 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
168                 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
169                         return 1;
170                 default:
171                         if (flight_log_max <= 0)
172                                 return 1;
173                         int     log_max = flight_log_max * 1024;
174                         int     log_space = log_space();
175                         int     log_used;
176
177                         if (stored_flight <= 0)
178                                 log_used = 0;
179                         else
180                                 log_used = stored_flight * log_max;
181                         int     log_avail;
182
183                         if (log_used >= log_space)
184                                 log_avail = 0;
185                         else
186                                 log_avail = (log_space - log_used) / log_max;
187
188                         return log_avail;
189                 }
190         }
191
192         public int invert_accel_value(int value) {
193                 if (value == AltosLib.MISSING)
194                         return AltosLib.MISSING;
195
196                 switch (log_format) {
197                 case AltosLib.AO_LOG_FORMAT_FULL:
198                         return 0x7fff - value;
199                 case AltosLib.AO_LOG_FORMAT_TELEMEGA_OLD:
200                 case AltosLib.AO_LOG_FORMAT_TELEMETRUM:
201                 case AltosLib.AO_LOG_FORMAT_TELEMEGA:
202                 case AltosLib.AO_LOG_FORMAT_TELEMEGA_3:
203                 case AltosLib.AO_LOG_FORMAT_TELEMEGA_4:
204                         return 4095 - value;
205                 case AltosLib.AO_LOG_FORMAT_EASYMEGA_2:
206                         return -value;
207                 default:
208                         return AltosLib.MISSING;
209                 }
210         }
211
212         public boolean has_monitor_battery() {
213                 if (product.startsWith("TeleBT"))
214                         return true;
215                 return false;
216         }
217
218         int[] parse_version(String v) {
219                 String[] parts = v.split("\\.");
220                 int r[] = new int[parts.length];
221
222                 for (int i = 0; i < parts.length; i++) {
223                         try {
224                                 r[i] = (int) AltosLib.fromdec(parts[i]);
225                         } catch (NumberFormatException n) {
226                                 r[i] = 0;
227                         }
228                 }
229
230                 return r;
231         }
232
233         public boolean altitude_32() {
234                 return altitude_32 == 1;
235         }
236
237         public int compare_version(String other) {
238                 int[]   me = parse_version(version);
239                 int[]   them = parse_version(other);
240
241                 int     l = Math.min(me.length, them.length);
242
243                 for (int i = 0; i < l; i++) {
244                         int     d = me[i] - them[i];
245                         if (d != 0)
246                                 return d;
247                 }
248                 if (me.length > l)
249                         return 1;
250                 if (them.length > l)
251                         return -1;
252                 return 0;
253         }
254
255         public void reset() {
256                 manufacturer = null;
257                 product = null;
258                 serial = AltosLib.MISSING;
259                 flight = AltosLib.MISSING;
260                 log_format = AltosLib.AO_LOG_FORMAT_UNKNOWN;
261                 log_space = AltosLib.MISSING;
262                 version = "unknown";
263                 config_major = AltosLib.MISSING;
264                 config_minor = AltosLib.MISSING;
265
266                 main_deploy = AltosLib.MISSING;
267                 apogee_delay = AltosLib.MISSING;
268                 apogee_lockout = AltosLib.MISSING;
269
270                 radio_frequency = AltosLib.MISSING;
271                 callsign = null;
272                 radio_enable = AltosLib.MISSING;
273                 radio_calibration = AltosLib.MISSING;
274                 radio_channel = AltosLib.MISSING;
275                 radio_setting = AltosLib.MISSING;
276                 telemetry_rate = AltosLib.MISSING;
277
278                 accel_cal_plus_cooked = AltosLib.MISSING;
279                 accel_cal_minus_cooked = AltosLib.MISSING;
280                 accel_cal_plus = AltosLib.MISSING;
281                 accel_cal_minus = AltosLib.MISSING;
282                 pad_orientation = AltosLib.MISSING;
283                 accel_cal_adjusted = false;
284
285                 flight_log_max = AltosLib.MISSING;
286                 log_fixed = AltosLib.MISSING;
287                 ignite_mode = AltosLib.MISSING;
288
289                 aes_key = null;
290
291                 pyro = AltosLib.MISSING;
292                 npyro = AltosLib.MISSING;
293                 pyros = null;
294                 pyro_firing_time = AltosLib.MISSING;
295
296                 aprs_interval = AltosLib.MISSING;
297                 aprs_ssid = AltosLib.MISSING;
298                 aprs_format = AltosLib.MISSING;
299
300                 beep = AltosLib.MISSING;
301
302                 tracker_motion = AltosLib.MISSING;
303                 tracker_interval = AltosLib.MISSING;
304
305                 storage_size = AltosLib.MISSING;
306                 storage_erase_unit = AltosLib.MISSING;
307                 stored_flight = AltosLib.MISSING;
308
309                 accel_zero_along = AltosLib.MISSING;
310                 accel_zero_across = AltosLib.MISSING;
311                 accel_zero_through = AltosLib.MISSING;
312         }
313
314         /* Return + accel calibration relative to a specific pad orientation */
315         public int accel_cal_plus(int pad_orientation) {
316                 adjust_accel_cal();
317                 switch (pad_orientation) {
318                 case AltosLib.AO_PAD_ORIENTATION_ANTENNA_UP:
319                         return accel_cal_plus_cooked;
320                 case AltosLib.AO_PAD_ORIENTATION_ANTENNA_DOWN:
321                         return invert_accel_value(accel_cal_minus_cooked);
322                 default:
323                         return AltosLib.MISSING;
324                 }
325         }
326
327         /* Return - accel calibration relative to a specific pad orientation */
328         public int accel_cal_minus(int pad_orientation) {
329                 adjust_accel_cal();
330                 switch (pad_orientation) {
331                 case AltosLib.AO_PAD_ORIENTATION_ANTENNA_UP:
332                         return accel_cal_minus_cooked;
333                 case AltosLib.AO_PAD_ORIENTATION_ANTENNA_DOWN:
334                         return invert_accel_value(accel_cal_plus_cooked);
335                 default:
336                         return AltosLib.MISSING;
337                 }
338         }
339
340         /* Once we have all of the values from the config data, compute the
341          * accel cal values relative to Antenna Up orientation.
342          */
343         private void adjust_accel_cal() {
344                 if (!accel_cal_adjusted &&
345                     pad_orientation != AltosLib.MISSING &&
346                     accel_cal_plus != AltosLib.MISSING &&
347                     accel_cal_minus != AltosLib.MISSING &&
348                     log_format != AltosLib.AO_LOG_FORMAT_UNKNOWN)
349                 {
350                         switch (pad_orientation) {
351                         case AltosLib.AO_PAD_ORIENTATION_ANTENNA_UP:
352                                 accel_cal_plus_cooked = accel_cal_plus;
353                                 accel_cal_minus_cooked = accel_cal_minus;
354                                 accel_cal_adjusted = true;
355                                 break;
356                         case AltosLib.AO_PAD_ORIENTATION_ANTENNA_DOWN:
357                                 accel_cal_plus_cooked = invert_accel_value(accel_cal_minus);
358                                 accel_cal_minus_cooked = invert_accel_value(accel_cal_plus);
359                                 accel_cal_adjusted = true;
360                                 break;
361                         default:
362                                 break;
363                         }
364                 }
365         }
366
367         public void parse_line(String line) {
368
369                 /* Version replies */
370                 try { manufacturer = get_string(line, "manufacturer"); } catch (Exception e) {}
371                 try { product = get_string(line, "product"); } catch (Exception e) {}
372                 try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
373                 try { flight = get_int(line, "current-flight"); } catch (Exception e) {}
374                 try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
375                 try { log_space = get_int(line, "log-space"); } catch (Exception e) {}
376                 try { altitude_32 = get_int(line, "altitude-32"); } catch (Exception e) {}
377                 try { version = get_string(line, "software-version"); } catch (Exception e) {}
378
379                 /* Version also contains MS5607 info, which we ignore here */
380
381                 try { ms5607().reserved = get_int(line, "ms5607 reserved:"); } catch (Exception e) {}
382                 try { ms5607().sens = get_int(line, "ms5607 sens:"); } catch (Exception e) {}
383                 try { ms5607().off = get_int(line, "ms5607 off:"); } catch (Exception e) {}
384                 try { ms5607().tcs = get_int(line, "ms5607 tcs:"); } catch (Exception e) {}
385                 try { ms5607().tco = get_int(line, "ms5607 tco:"); } catch (Exception e) {}
386                 try { ms5607().tref = get_int(line, "ms5607 tref:"); } catch (Exception e) {}
387                 try { ms5607().tempsens = get_int(line, "ms5607 tempsens:"); } catch (Exception e) {}
388                 try { ms5607().crc = get_int(line, "ms5607 crc:"); } catch (Exception e) {}
389
390                 /* Config show replies */
391
392                 try {
393                         if (line.startsWith("Config version")) {
394                                 String[] bits = line.split("\\s+");
395                                 if (bits.length >= 3) {
396                                         String[] cfg = bits[2].split("\\.");
397
398                                         if (cfg.length >= 2) {
399                                                 config_major = Integer.parseInt(cfg[0]);
400                                                 config_minor = Integer.parseInt(cfg[1]);
401                                         }
402                                 }
403                         }
404                 } catch (Exception e) {}
405
406                 /* HAS_FLIGHT */
407                 try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
408                 try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
409                 try { apogee_lockout = get_int(line, "Apogee lockout:"); } catch (Exception e) {}
410
411                 /* HAS_RADIO */
412                 try {
413                         radio_frequency = get_int(line, "Frequency:");
414                         if (radio_frequency < 0)
415                                 radio_frequency = 434550;
416                 } catch (Exception e) {}
417                 try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
418                 try { radio_enable = get_int(line, "Radio enable:"); } catch (Exception e) {}
419                 try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
420                 try { telemetry_rate = get_int(line, "Telemetry rate:"); } catch (Exception e) {}
421
422                 /* Old HAS_RADIO values */
423                 try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
424                 try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
425
426                 /* HAS_ACCEL */
427                 try {
428                         if (line.startsWith("Accel cal")) {
429                                 String[] bits = line.split("\\s+");
430                                 if (bits.length >= 6) {
431                                         accel_cal_plus = Integer.parseInt(bits[3]);
432                                         accel_cal_minus = Integer.parseInt(bits[5]);
433                                         accel_cal_adjusted = false;
434                                 }
435                         }
436                 } catch (Exception e) {}
437                 try { pad_orientation = get_int(line, "Pad orientation:"); } catch (Exception e) {}
438
439                 /* HAS_LOG */
440                 try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
441                 try { log_fixed = get_int(line, "Log fixed:"); } catch (Exception e) {}
442
443                 /* HAS_IGNITE */
444                 try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
445
446                 /* HAS_AES */
447                 try { aes_key = get_string(line, "AES key:"); } catch (Exception e) {}
448
449                 /* AO_PYRO_NUM */
450                 try {
451                         npyro = get_int(line, "Pyro-count:");
452                         pyros = new AltosPyro[npyro];
453                         pyro = 0;
454                 } catch (Exception e) {}
455                 if (npyro != AltosLib.MISSING) {
456                         try {
457                                 AltosPyro p = new AltosPyro(pyro, line);
458                                 if (pyro < npyro)
459                                         pyros[pyro++] = p;
460                         } catch (Exception e) {}
461                 }
462                 try { pyro_firing_time = get_int(line, "Pyro time:") / 100.0; } catch (Exception e) {}
463
464                 /* HAS_APRS */
465                 try { aprs_interval = get_int(line, "APRS interval:"); } catch (Exception e) {}
466                 try { aprs_ssid = get_int(line, "APRS SSID:"); } catch (Exception e) {}
467                 try { aprs_format = get_int(line, "APRS format:"); } catch (Exception e) {}
468
469                 /* HAS_BEEP */
470                 try { beep = get_int(line, "Beeper setting:"); } catch (Exception e) {}
471
472                 /* HAS_TRACKER */
473                 try {
474                         int[] values = get_values(line, "Tracker setting:");
475                         tracker_motion = values[0];
476                         tracker_interval = values[1];
477                 } catch (Exception e) {}
478
479                 /* Storage info replies */
480                 try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
481                 try { storage_erase_unit = get_int(line, "Storage erase unit:"); } catch (Exception e) {}
482
483                 /* Log listing replies */
484                 try { get_int(line, "flight"); stored_flight++; }  catch (Exception e) {}
485
486                 /* HAS_GYRO */
487                 try {
488                         if (line.startsWith("IMU cal along")) {
489                                 String[] bits = line.split("\\s+");
490                                 if (bits.length >= 8) {
491                                         accel_zero_along = Integer.parseInt(bits[3]);
492                                         accel_zero_across = Integer.parseInt(bits[5]);
493                                         accel_zero_through = Integer.parseInt(bits[7]);
494                                 }
495                         }
496                 } catch (Exception e) {}
497
498                 /* Fix accel cal as soon as all of the necessary values appear */
499                 adjust_accel_cal();
500         }
501
502         public AltosConfigData() {
503                 reset();
504         }
505
506         private void read_link(AltosLink link, String finished) throws InterruptedException, TimeoutException {
507                 for (;;) {
508                         String line = link.get_reply();
509                         if (line == null)
510                                 throw new TimeoutException();
511                         if (line.contains("Syntax error"))
512                                 continue;
513                         this.parse_line(line);
514
515                         /* signals the end of the version info */
516                         if (line.startsWith(finished))
517                                 break;
518                 }
519         }
520
521         public boolean has_frequency() {
522                 return radio_frequency != AltosLib.MISSING || radio_setting != AltosLib.MISSING || radio_channel != AltosLib.MISSING;
523         }
524
525         public boolean has_telemetry_rate() {
526                 return telemetry_rate != AltosLib.MISSING;
527         }
528
529         public void set_frequency(double freq) {
530                 int     frequency = radio_frequency;
531                 int     setting = radio_setting;
532
533                 if (frequency != AltosLib.MISSING) {
534                         radio_frequency = (int) Math.floor (freq * 1000 + 0.5);
535                         radio_channel = AltosLib.MISSING;
536                 } else if (setting != AltosLib.MISSING) {
537                         radio_setting =AltosConvert.radio_frequency_to_setting(freq, radio_calibration);
538                         radio_channel = AltosLib.MISSING;
539                 } else {
540                         radio_channel = AltosConvert.radio_frequency_to_channel(freq);
541                 }
542         }
543
544         public double frequency() {
545                 int     channel = radio_channel;
546                 int     setting = radio_setting;
547
548                 if (radio_frequency == AltosLib.MISSING && channel == AltosLib.MISSING && setting == AltosLib.MISSING)
549                         return AltosLib.MISSING;
550
551                 if (channel == AltosLib.MISSING)
552                         channel = 0;
553                 if (setting == AltosLib.MISSING)
554                         setting = 0;
555
556                 return AltosConvert.radio_to_frequency(radio_frequency,
557                                                        setting,
558                                                        radio_calibration,
559                                                        channel);
560         }
561
562         boolean use_flash_for_config() {
563                 if (product.startsWith("TeleMega"))
564                         return false;
565                 if (product.startsWith("TeleMetrum-v2"))
566                         return false;
567                 if (product.startsWith("TeleMetrum-v3"))
568                         return false;
569                 if (product.startsWith("EasyMega"))
570                         return false;
571                 return true;
572         }
573
574
575         public boolean mma655x_inverted() throws AltosUnknownProduct {
576                 if (product != null) {
577                         if (product.startsWith("EasyMega-v1"))
578                                 return false;
579                         if (product.startsWith("TeleMetrum-v2"))
580                                 return true;
581                         if (product.startsWith("TeleMega-v2"))
582                                 return false;
583                         if (product.startsWith("TeleMega-v1"))
584                                 return false;
585                 }
586                 throw new AltosUnknownProduct(product);
587         }
588
589         public boolean adxl375_inverted() throws AltosUnknownProduct {
590                 if (product != null) {
591                         if (product.startsWith("EasyMega-v2"))
592                                 return true;
593                         if (product.startsWith("TeleMetrum-v3"))
594                                 return true;
595                 }
596                 throw new AltosUnknownProduct(product);
597         }
598
599         public int adxl375_axis() throws AltosUnknownProduct {
600                 if (product != null) {
601                         if (product.startsWith("EasyMega-v2"))
602                                 return AltosAdxl375.X_AXIS;
603                         if (product.startsWith("TeleMetrum-v3"))
604                                 return AltosAdxl375.X_AXIS;
605                 }
606                 throw new AltosUnknownProduct(product);
607         }
608
609         public void get_values(AltosConfigValues source) throws AltosConfigDataException {
610
611                 /* HAS_FLIGHT */
612                 if (main_deploy != AltosLib.MISSING)
613                         main_deploy = source.main_deploy();
614                 if (apogee_delay != AltosLib.MISSING)
615                         apogee_delay = source.apogee_delay();
616                 if (apogee_lockout != AltosLib.MISSING)
617                         apogee_lockout = source.apogee_lockout();
618
619                 /* HAS_RADIO */
620                 if (has_frequency())
621                         set_frequency(source.radio_frequency());
622                 if (radio_enable != AltosLib.MISSING)
623                         radio_enable = source.radio_enable();
624                 if (callsign != null)
625                         callsign = source.callsign();
626                 if (telemetry_rate != AltosLib.MISSING)
627                         telemetry_rate = source.telemetry_rate();
628
629                 /* HAS_ACCEL */
630                 if (pad_orientation != AltosLib.MISSING)
631                         pad_orientation = source.pad_orientation();
632
633                 if (accel_cal_plus_cooked != AltosLib.MISSING)
634                         accel_cal_plus_cooked = source.accel_cal_plus();
635
636                 if (accel_cal_minus_cooked != AltosLib.MISSING)
637                         accel_cal_minus_cooked = source.accel_cal_minus();
638
639                 /* HAS_LOG */
640                 if (flight_log_max != AltosLib.MISSING)
641                         flight_log_max = source.flight_log_max();
642
643                 /* HAS_IGNITE */
644                 if (ignite_mode != AltosLib.MISSING)
645                         ignite_mode = source.ignite_mode();
646
647                 /* AO_PYRO_NUM */
648                 if (npyro != AltosLib.MISSING)
649                         pyros = source.pyros();
650                 if (pyro_firing_time != AltosLib.MISSING)
651                         pyro_firing_time = source.pyro_firing_time();
652
653                 /* HAS_APRS */
654                 if (aprs_interval != AltosLib.MISSING)
655                         aprs_interval = source.aprs_interval();
656                 if (aprs_ssid != AltosLib.MISSING)
657                         aprs_ssid = source.aprs_ssid();
658                 if (aprs_format != AltosLib.MISSING)
659                         aprs_format = source.aprs_format();
660
661                 /* HAS_BEEP */
662                 if (beep != AltosLib.MISSING)
663                         beep = source.beep();
664                 /* HAS_TRACKER */
665                 if (tracker_motion != AltosLib.MISSING)
666                         tracker_motion = source.tracker_motion();
667                 if (tracker_interval != AltosLib.MISSING)
668                         tracker_interval = source.tracker_interval();
669         }
670
671         public void set_values(AltosConfigValues dest) {
672                 dest.set_serial(serial);
673                 dest.set_product(product);
674                 dest.set_version(version);
675                 dest.set_altitude_32(altitude_32);
676                 dest.set_main_deploy(main_deploy);
677                 dest.set_apogee_delay(apogee_delay);
678                 dest.set_apogee_lockout(apogee_lockout);
679                 dest.set_radio_calibration(radio_calibration);
680                 dest.set_radio_frequency(frequency());
681                 dest.set_telemetry_rate(telemetry_rate);
682                 boolean max_enabled = true;
683
684                 if (log_space() == 0)
685                         max_enabled = false;
686
687                 if (log_fixed != AltosLib.MISSING)
688                         max_enabled = false;
689
690                 switch (log_format) {
691                 case AltosLib.AO_LOG_FORMAT_TINY:
692                         max_enabled = false;
693                         break;
694                 default:
695                         if (stored_flight != AltosLib.MISSING)
696                                 max_enabled = false;
697                         break;
698                 }
699
700                 dest.set_flight_log_max_enabled(max_enabled);
701                 dest.set_radio_enable(radio_enable);
702                 dest.set_flight_log_max_limit(log_space() / 1024);
703                 dest.set_flight_log_max(flight_log_max);
704                 dest.set_ignite_mode(ignite_mode);
705                 dest.set_pad_orientation(pad_orientation);
706                 dest.set_accel_cal(accel_cal_plus(AltosLib.AO_PAD_ORIENTATION_ANTENNA_UP),
707                                    accel_cal_minus(AltosLib.AO_PAD_ORIENTATION_ANTENNA_UP));
708                 dest.set_callsign(callsign);
709                 if (npyro != AltosLib.MISSING)
710                         dest.set_pyros(pyros);
711                 else
712                         dest.set_pyros(null);
713                 dest.set_pyro_firing_time(pyro_firing_time);
714                 dest.set_aprs_interval(aprs_interval);
715                 dest.set_aprs_ssid(aprs_ssid);
716                 dest.set_aprs_format(aprs_format);
717                 dest.set_beep(beep);
718                 dest.set_tracker_motion(tracker_motion);
719                 dest.set_tracker_interval(tracker_interval);
720         }
721
722         public boolean log_has_state() {
723                 switch (log_format) {
724                 case AltosLib.AO_LOG_FORMAT_TELEGPS:
725                         return false;
726                 }
727                 return true;
728         }
729
730         public void save(AltosLink link, boolean remote) throws InterruptedException, TimeoutException {
731
732                 /* HAS_FLIGHT */
733                 if (main_deploy != AltosLib.MISSING)
734                         link.printf("c m %d\n", main_deploy);
735                 if (apogee_delay != AltosLib.MISSING)
736                         link.printf("c d %d\n", apogee_delay);
737                 if (apogee_lockout != AltosLib.MISSING)
738                         link.printf("c L %d\n", apogee_lockout);
739
740                 /* HAS_RADIO */
741                 if (has_frequency()) {
742                         boolean has_frequency = radio_frequency != AltosLib.MISSING;
743                         boolean has_setting = radio_setting != AltosLib.MISSING;
744                         double frequency = frequency();
745                         link.set_radio_frequency(frequency,
746                                                         has_frequency,
747                                                         has_setting,
748                                                         radio_calibration);
749                         /* When remote, reset the dongle frequency at the same time */
750                         if (remote) {
751                                 link.flush_output();
752                                 link.stop_remote();
753                                 link.set_radio_frequency(frequency);
754                                 link.flush_output();
755                                 link.start_remote();
756                         }
757                 }
758
759                 if (telemetry_rate != AltosLib.MISSING) {
760                         link.printf("c T %d\n", telemetry_rate);
761                         if (remote) {
762                                 link.flush_output();
763                                 link.stop_remote();
764                                 link.set_telemetry_rate(telemetry_rate);
765                                 link.flush_output();
766                                 link.start_remote();
767                         }
768                 }
769
770                 if (callsign != null) {
771                         link.printf("c c %s\n", callsign);
772                         if (remote) {
773                                 link.flush_output();
774                                 link.stop_remote();
775                                 link.set_callsign(callsign);
776                                 link.flush_output();
777                                 link.start_remote();
778                         }
779                 }
780
781                 if (radio_enable != AltosLib.MISSING)
782                         link.printf("c e %d\n", radio_enable);
783
784                 /* HAS_ACCEL */
785                 /* set orientation first so that we know how to set the accel cal */
786                 if (pad_orientation != AltosLib.MISSING)
787                         link.printf("c o %d\n", pad_orientation);
788                 int plus = accel_cal_plus(pad_orientation);
789                 int minus = accel_cal_minus(pad_orientation);
790                 if (plus != AltosLib.MISSING && minus != AltosLib.MISSING)
791                         link.printf("c a %d %d\n", plus, minus);
792
793                 /* HAS_LOG */
794                 if (flight_log_max != 0)
795                         link.printf("c l %d\n", flight_log_max);
796
797                 /* HAS_IGNITE */
798                 if (ignite_mode != AltosLib.MISSING)
799                         link.printf("c i %d\n", ignite_mode);
800
801                 /* HAS_AES */
802                 /* UI doesn't support AES key config */
803
804                 /* AO_PYRO_NUM */
805                 if (npyro != AltosLib.MISSING) {
806                         for (int p = 0; p < pyros.length; p++) {
807                                 link.printf("c P %s\n",
808                                                    pyros[p].toString());
809                         }
810                 }
811                 if (pyro_firing_time != AltosLib.MISSING)
812                         link.printf("c I %d\n", (int) (pyro_firing_time * 100.0 + 0.5));
813
814                 /* HAS_APRS */
815                 if (aprs_interval != AltosLib.MISSING)
816                         link.printf("c A %d\n", aprs_interval);
817                 if (aprs_ssid != AltosLib.MISSING)
818                         link.printf("c S %d\n", aprs_ssid);
819                 if (aprs_format != AltosLib.MISSING)
820                         link.printf("c C %d\n", aprs_format);
821
822                 /* HAS_BEEP */
823                 if (beep != AltosLib.MISSING)
824                         link.printf("c b %d\n", beep);
825
826                 /* HAS_TRACKER */
827                 if (tracker_motion != AltosLib.MISSING && tracker_interval != AltosLib.MISSING)
828                         link.printf("c t %d %d\n", tracker_motion, tracker_interval);
829
830                 /* HAS_GYRO */
831                 /* UI doesn't support accel cal */
832
833                 link.printf("c w\n");
834                 link.flush_output();
835         }
836
837         public AltosConfigData(AltosLink link) throws InterruptedException, TimeoutException {
838                 reset();
839                 link.printf("c s\nf\nv\n");
840                 read_link(link, "software-version");
841                 switch (log_format) {
842                 case AltosLib.AO_LOG_FORMAT_UNKNOWN:
843                 case AltosLib.AO_LOG_FORMAT_NONE:
844                         break;
845                 default:
846                         link.printf("l\n");
847                         read_link(link, "done");
848                         break;
849                 }
850         }
851 }