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