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