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