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