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