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