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