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