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