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