altoslib: Compute tilt angle from eeprom data
[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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altoslib_5;
19
20 import java.util.*;
21 import java.text.*;
22 import java.util.concurrent.*;
23
24 public class AltosConfigData implements Iterable<String> {
25
26         /* Version information */
27         public String   manufacturer;
28         public String   product;
29         public int      serial;
30         public int      flight;
31         public int      log_format;
32         public int      log_space;
33         public String   version;
34         public int      altitude_32;
35
36         /* Strings returned */
37         public LinkedList<String>       lines;
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         public int      pad_orientation;
58
59         /* HAS_LOG */
60         public int      flight_log_max;
61         public int      log_fixed;
62
63         /* HAS_IGNITE */
64         public int      ignite_mode;
65
66         /* HAS_AES */
67         public String   aes_key;
68
69         /* AO_PYRO_NUM */
70         public AltosPyro[]      pyros;
71         public int              npyro;
72         public int              pyro;
73         public double           pyro_firing_time;
74
75         /* HAS_APRS */
76         public int              aprs_interval;
77         public int              aprs_ssid;
78
79         /* HAS_BEEP */
80         public int              beep;
81
82         /* Storage info replies */
83         public int      storage_size;
84         public int      storage_erase_unit;
85
86         /* Log listing replies */
87         public int      stored_flight;
88
89         /* HAS_TRACKER */
90         public int      tracker_motion;
91         public int      tracker_interval;
92
93         /* HAS_GYRO */
94         public int      accel_zero_along, accel_zero_across, accel_zero_through;
95
96         public static String get_string(String line, String label) throws  ParseException {
97                 if (line.startsWith(label)) {
98                         String  quoted = line.substring(label.length()).trim();
99
100                         if (quoted.startsWith("\""))
101                                 quoted = quoted.substring(1);
102                         if (quoted.endsWith("\""))
103                                 quoted = quoted.substring(0,quoted.length()-1);
104                         return quoted;
105                 }
106                 throw new ParseException("mismatch", 0);
107         }
108
109         public static int get_int(String line, String label) throws NumberFormatException, ParseException {
110                 if (line.startsWith(label)) {
111                         String tail = line.substring(label.length()).trim();
112                         String[] tokens = tail.split("\\s+");
113                         if (tokens.length > 0)
114                                 return  Integer.parseInt(tokens[0]);
115                 }
116                 throw new ParseException("mismatch", 0);
117         }
118
119         public static int[] get_values(String line, String label) throws NumberFormatException, ParseException {
120                 if (line.startsWith(label)) {
121                         String tail = line.substring(label.length()).trim();
122                         String[] tokens = tail.split("\\s+");
123                         if (tokens.length > 1) {
124                                 int[]   values = new int[2];
125                                 values[0] = Integer.parseInt(tokens[0]);
126                                 values[1] = Integer.parseInt(tokens[1]);
127                                 return values;
128                         }
129                 }
130                 throw new ParseException("mismatch", 0);
131         }
132
133         public Iterator<String> iterator() {
134                 return lines.iterator();
135         }
136
137         public int log_space() {
138                 if (log_space > 0)
139                         return log_space;
140
141                 if (storage_size > 0) {
142                         int     space = storage_size;
143
144                         if (storage_erase_unit > 0 && use_flash_for_config())
145                                 space -= storage_erase_unit;
146
147                         if (space > 0)
148                                 return space;
149                 }
150                 return 0;
151         }
152
153         public int log_available() {
154                 switch (log_format) {
155                 case AltosLib.AO_LOG_FORMAT_TINY:
156                         if (stored_flight == 0)
157                                 return 1;
158                         return 0;
159                 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
160                 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
161                         return 1;
162                 default:
163                         if (flight_log_max <= 0)
164                                 return 1;
165                         int     log_max = flight_log_max * 1024;
166                         int     log_space = log_space();
167                         int     log_used;
168
169                         if (stored_flight <= 0)
170                                 log_used = 0;
171                         else
172                                 log_used = stored_flight * log_max;
173                         int     log_avail;
174
175                         if (log_used >= log_space)
176                                 log_avail = 0;
177                         else
178                                 log_avail = (log_space - log_used) / log_max;
179
180                         return log_avail;
181                 }
182         }
183
184         public boolean has_monitor_battery() {
185                 if (product.startsWith("TeleBT"))
186                         return true;
187                 return false;
188         }
189
190         int[] parse_version(String v) {
191                 String[] parts = v.split("\\.");
192                 int r[] = new int[parts.length];
193
194                 for (int i = 0; i < parts.length; i++) {
195                         try {
196                                 r[i] = AltosLib.fromdec(parts[i]);
197                         } catch (NumberFormatException n) {
198                                 r[i] = 0;
199                         }
200                 }
201
202                 return r;
203         }
204
205         public int compare_version(String other) {
206                 int[]   me = parse_version(version);
207                 int[]   them = parse_version(other);
208
209                 int     l = Math.min(me.length, them.length);
210
211                 for (int i = 0; i < l; i++) {
212                         int     d = me[i] - them[i];
213                         if (d != 0)
214                                 return d;
215                 }
216                 if (me.length > l)
217                         return 1;
218                 if (them.length > l)
219                         return -1;
220                 return 0;
221         }
222
223         public void reset() {
224                 lines = new LinkedList<String>();
225
226                 manufacturer = "unknown";
227                 product = "unknown";
228                 serial = 0;
229                 flight = 0;
230                 log_format = AltosLib.AO_LOG_FORMAT_UNKNOWN;
231                 log_space = -1;
232                 version = "unknown";
233
234                 main_deploy = -1;
235                 apogee_delay = -1;
236                 apogee_lockout = -1;
237
238                 radio_frequency = -1;
239                 callsign = null;
240                 radio_enable = -1;
241                 radio_calibration = -1;
242                 radio_channel = -1;
243                 radio_setting = -1;
244                 telemetry_rate = -1;
245
246                 accel_cal_plus = -1;
247                 accel_cal_minus = -1;
248                 pad_orientation = -1;
249
250                 flight_log_max = -1;
251                 log_fixed = -1;
252                 ignite_mode = -1;
253
254                 aes_key = "";
255
256                 pyro = 0;
257                 npyro = 0;
258                 pyros = null;
259                 pyro_firing_time = -1;
260
261                 aprs_interval = -1;
262                 aprs_ssid = -1;
263
264                 beep = -1;
265
266                 tracker_motion = -1;
267                 tracker_interval = -1;
268
269                 storage_size = -1;
270                 storage_erase_unit = -1;
271                 stored_flight = 0;
272
273                 accel_zero_along = -1;
274                 accel_zero_across = -1;
275                 accel_zero_through = -1;
276         }
277
278         public void parse_line(String line) {
279                 lines.add(line);
280                 /* Version replies */
281                 try { manufacturer = get_string(line, "manufacturer"); } catch (Exception e) {}
282                 try { product = get_string(line, "product"); } catch (Exception e) {}
283                 try { serial = get_int(line, "serial-number"); } catch (Exception e) {}
284                 try { flight = get_int(line, "current-flight"); } catch (Exception e) {}
285                 try { log_format = get_int(line, "log-format"); } catch (Exception e) {}
286                 try { log_space = get_int(line, "log-space"); } catch (Exception e) {}
287                 try { altitude_32 = get_int(line, "altitude-32"); } catch (Exception e) {}
288                 try { version = get_string(line, "software-version"); } catch (Exception e) {}
289
290                 /* Version also contains MS5607 info, which we ignore here */
291
292                 /* Config show replies */
293
294                 /* HAS_FLIGHT */
295                 try { main_deploy = get_int(line, "Main deploy:"); } catch (Exception e) {}
296                 try { apogee_delay = get_int(line, "Apogee delay:"); } catch (Exception e) {}
297                 try { apogee_lockout = get_int(line, "Apogee lockout:"); } catch (Exception e) {}
298
299                 /* HAS_RADIO */
300                 try {
301                         radio_frequency = get_int(line, "Frequency:");
302                         if (radio_frequency < 0)
303                                 radio_frequency = 434550;
304                 } catch (Exception e) {}
305                 try { callsign = get_string(line, "Callsign:"); } catch (Exception e) {}
306                 try { radio_enable = get_int(line, "Radio enable:"); } catch (Exception e) {}
307                 try { radio_calibration = get_int(line, "Radio cal:"); } catch (Exception e) {}
308                 try { telemetry_rate = get_int(line, "Telemetry rate:"); } catch (Exception e) {}
309
310                 /* Old HAS_RADIO values */
311                 try { radio_channel = get_int(line, "Radio channel:"); } catch (Exception e) {}
312                 try { radio_setting = get_int(line, "Radio setting:"); } catch (Exception e) {}
313
314                 /* HAS_ACCEL */
315                 try {
316                         if (line.startsWith("Accel cal")) {
317                                 String[] bits = line.split("\\s+");
318                                 if (bits.length >= 6) {
319                                         accel_cal_plus = Integer.parseInt(bits[3]);
320                                         accel_cal_minus = Integer.parseInt(bits[5]);
321                                 }
322                         }
323                 } catch (Exception e) {}
324                 try { pad_orientation = get_int(line, "Pad orientation:"); } catch (Exception e) {}
325
326                 /* HAS_LOG */
327                 try { flight_log_max = get_int(line, "Max flight log:"); } catch (Exception e) {}
328                 try { log_fixed = get_int(line, "Log fixed:"); } catch (Exception e) {}
329
330                 /* HAS_IGNITE */
331                 try { ignite_mode = get_int(line, "Ignite mode:"); } catch (Exception e) {}
332
333                 /* HAS_AES */
334                 try { aes_key = get_string(line, "AES key:"); } catch (Exception e) {}
335
336                 /* AO_PYRO_NUM */
337                 try {
338                         npyro = get_int(line, "Pyro-count:");
339                         pyros = new AltosPyro[npyro];
340                         pyro = 0;
341                 } catch (Exception e) {}
342                 if (npyro > 0) {
343                         try {
344                                 AltosPyro p = new AltosPyro(pyro, line);
345                                 if (pyro < npyro)
346                                         pyros[pyro++] = p;
347                         } catch (Exception e) {}
348                 }
349                 try { pyro_firing_time = get_int(line, "Pyro time:") / 100.0; } catch (Exception e) {}
350
351                 /* HAS_APRS */
352                 try { aprs_interval = get_int(line, "APRS interval:"); } catch (Exception e) {}
353                 try { aprs_ssid = get_int(line, "APRS SSID:"); } catch (Exception e) {}
354
355                 /* HAS_BEEP */
356                 try { beep = get_int(line, "Beeper setting:"); } catch (Exception e) {}
357
358                 /* HAS_TRACKER */
359                 try {
360                         int[] values = get_values(line, "Tracker setting:");
361                         tracker_motion = values[0];
362                         tracker_interval = values[1];
363                 } catch (Exception e) {}
364
365                 /* Storage info replies */
366                 try { storage_size = get_int(line, "Storage size:"); } catch (Exception e) {}
367                 try { storage_erase_unit = get_int(line, "Storage erase unit:"); } catch (Exception e) {}
368
369                 /* Log listing replies */
370                 try { get_int(line, "flight"); stored_flight++; }  catch (Exception e) {}
371
372                 /* HAS_GYRO */
373                 try {
374                         if (line.startsWith("IMU call along")) {
375                                 String[] bits = line.split("\\s+");
376                                 if (bits.length >= 8) {
377                                         accel_zero_along = Integer.parseInt(bits[3]);
378                                         accel_zero_across = Integer.parseInt(bits[5]);
379                                         accel_zero_through = Integer.parseInt(bits[7]);
380                                 }
381                         }
382                 } catch (Exception e) {}
383         }
384
385         public AltosConfigData() {
386                 reset();
387         }
388
389         private void read_link(AltosLink link, String finished) throws InterruptedException, TimeoutException {
390                 for (;;) {
391                         String line = link.get_reply();
392                         if (line == null)
393                                 throw new TimeoutException();
394                         if (line.contains("Syntax error"))
395                                 continue;
396                         this.parse_line(line);
397
398                         /* signals the end of the version info */
399                         if (line.startsWith(finished))
400                                 break;
401                 }
402         }
403
404         public boolean has_frequency() {
405                 return radio_frequency >= 0 || radio_setting >= 0 || radio_channel >= 0;
406         }
407
408         public boolean has_telemetry_rate() {
409                 return telemetry_rate >= 0;
410         }
411
412         public void set_frequency(double freq) {
413                 int     frequency = radio_frequency;
414                 int     setting = radio_setting;
415
416                 if (frequency > 0) {
417                         radio_frequency = (int) Math.floor (freq * 1000 + 0.5);
418                         radio_channel = -1;
419                 } else if (setting > 0) {
420                         radio_setting =AltosConvert.radio_frequency_to_setting(freq,
421                                                                                     radio_calibration);
422                         radio_channel = -1;
423                 } else {
424                         radio_channel = AltosConvert.radio_frequency_to_channel(freq);
425                 }
426         }
427
428         public double frequency() {
429                 int     channel = radio_channel;
430                 int     setting = radio_setting;
431
432                 if (radio_frequency < 0 && channel < 0 && setting < 0)
433                         return -1;
434
435                 if (channel < 0)
436                         channel = 0;
437                 if (setting < 0)
438                         setting = 0;
439
440                 return AltosConvert.radio_to_frequency(radio_frequency,
441                                                        setting,
442                                                        radio_calibration,
443                                                        channel);
444         }
445
446         boolean use_flash_for_config() {
447                 if (product.startsWith("TeleMega"))
448                         return false;
449                 if (product.startsWith("TeleMetrum-v2"))
450                         return false;
451                 if (product.startsWith("EasyMega"))
452                         return false;
453                 return true;
454         }
455
456
457         public void get_values(AltosConfigValues source) throws AltosConfigDataException {
458
459                 /* HAS_FLIGHT */
460                 if (main_deploy >= 0)
461                         main_deploy = source.main_deploy();
462                 if (apogee_delay >= 0)
463                         apogee_delay = source.apogee_delay();
464                 if (apogee_lockout >= 0)
465                         apogee_lockout = source.apogee_lockout();
466
467                 /* HAS_RADIO */
468                 if (has_frequency())
469                         set_frequency(source.radio_frequency());
470                 if (radio_enable >= 0)
471                         radio_enable = source.radio_enable();
472                 if (callsign != null)
473                         callsign = source.callsign();
474                 if (radio_calibration >= 0)
475                         radio_calibration = source.radio_calibration();
476                 if (telemetry_rate >= 0)
477                         telemetry_rate = source.telemetry_rate();
478
479                 /* HAS_ACCEL */
480                 if (pad_orientation >= 0)
481                         pad_orientation = source.pad_orientation();
482
483                 /* HAS_LOG */
484                 if (flight_log_max >= 0)
485                         flight_log_max = source.flight_log_max();
486
487                 /* HAS_IGNITE */
488                 if (ignite_mode >= 0)
489                         ignite_mode = source.ignite_mode();
490
491                 /* AO_PYRO_NUM */
492                 if (npyro > 0)
493                         pyros = source.pyros();
494                 if (pyro_firing_time >= 0)
495                         pyro_firing_time = source.pyro_firing_time();
496
497                 /* HAS_APRS */
498                 if (aprs_interval >= 0)
499                         aprs_interval = source.aprs_interval();
500                 if (aprs_ssid >= 0)
501                         aprs_ssid = source.aprs_ssid();
502
503                 /* HAS_BEEP */
504                 if (beep >= 0)
505                         beep = source.beep();
506                 /* HAS_TRACKER */
507                 if (tracker_motion >= 0)
508                         tracker_motion = source.tracker_motion();
509                 if (tracker_interval >= 0)
510                         tracker_interval = source.tracker_interval();
511         }
512
513         public void set_values(AltosConfigValues dest) {
514                 dest.set_serial(serial);
515                 dest.set_product(product);
516                 dest.set_version(version);
517                 dest.set_altitude_32(altitude_32);
518                 dest.set_main_deploy(main_deploy);
519                 dest.set_apogee_delay(apogee_delay);
520                 dest.set_apogee_lockout(apogee_lockout);
521                 dest.set_radio_calibration(radio_calibration);
522                 dest.set_radio_frequency(frequency());
523                 dest.set_telemetry_rate(telemetry_rate);
524                 boolean max_enabled = true;
525
526                 if (log_space() == 0)
527                         max_enabled = false;
528
529                 if (log_fixed > 0)
530                         max_enabled = false;
531
532                 switch (log_format) {
533                 case AltosLib.AO_LOG_FORMAT_TINY:
534                         max_enabled = false;
535                         break;
536                 default:
537                         if (stored_flight > 0)
538                                 max_enabled = false;
539                         break;
540                 }
541
542                 dest.set_flight_log_max_enabled(max_enabled);
543                 dest.set_radio_enable(radio_enable);
544                 dest.set_flight_log_max_limit(log_space() / 1024);
545                 dest.set_flight_log_max(flight_log_max);
546                 dest.set_ignite_mode(ignite_mode);
547                 dest.set_pad_orientation(pad_orientation);
548                 dest.set_callsign(callsign);
549                 if (npyro > 0)
550                         dest.set_pyros(pyros);
551                 else
552                         dest.set_pyros(null);
553                 dest.set_pyro_firing_time(pyro_firing_time);
554                 dest.set_aprs_interval(aprs_interval);
555                 dest.set_aprs_ssid(aprs_ssid);
556                 dest.set_beep(beep);
557                 dest.set_tracker_motion(tracker_motion);
558                 dest.set_tracker_interval(tracker_interval);
559         }
560
561         public void save(AltosLink link, boolean remote) throws InterruptedException, TimeoutException {
562
563                 /* HAS_FLIGHT */
564                 if (main_deploy >= 0)
565                         link.printf("c m %d\n", main_deploy);
566                 if (apogee_delay >= 0)
567                         link.printf("c d %d\n", apogee_delay);
568                 if (apogee_lockout >= 0)
569                         link.printf("c L %d\n", apogee_lockout);
570
571                 /* Don't mess with radio calibration when remote */
572                 if (radio_calibration > 0 && !remote)
573                         link.printf("c f %d\n", radio_calibration);
574
575                 /* HAS_RADIO */
576                 if (has_frequency()) {
577                         boolean has_frequency = radio_frequency >= 0;
578                         boolean has_setting = radio_setting > 0;
579                         double frequency = frequency();
580                         link.set_radio_frequency(frequency,
581                                                         has_frequency,
582                                                         has_setting,
583                                                         radio_calibration);
584                         /* When remote, reset the dongle frequency at the same time */
585                         if (remote) {
586                                 link.flush_output();
587                                 link.stop_remote();
588                                 link.set_radio_frequency(frequency);
589                                 link.flush_output();
590                                 link.start_remote();
591                         }
592                 }
593
594                 if (telemetry_rate >= 0) {
595                         link.printf("c T %d\n", telemetry_rate);
596                         if (remote) {
597                                 link.flush_output();
598                                 link.stop_remote();
599                                 link.set_telemetry_rate(telemetry_rate);
600                                 link.flush_output();
601                                 link.start_remote();
602                         }
603                 }
604
605                 if (callsign != null) {
606                         link.printf("c c %s\n", callsign);
607                         if (remote) {
608                                 link.flush_output();
609                                 link.stop_remote();
610                                 link.set_callsign(callsign);
611                                 link.flush_output();
612                                 link.start_remote();
613                         }
614                 }
615
616                 if (radio_enable >= 0)
617                         link.printf("c e %d\n", radio_enable);
618
619                 /* HAS_ACCEL */
620                 /* UI doesn't support accel cal */
621                 if (pad_orientation >= 0)
622                         link.printf("c o %d\n", pad_orientation);
623
624                 /* HAS_LOG */
625                 if (flight_log_max != 0)
626                         link.printf("c l %d\n", flight_log_max);
627
628                 /* HAS_IGNITE */
629                 if (ignite_mode >= 0)
630                         link.printf("c i %d\n", ignite_mode);
631
632                 /* HAS_AES */
633                 /* UI doesn't support AES key config */
634
635                 /* AO_PYRO_NUM */
636                 if (npyro > 0) {
637                         for (int p = 0; p < pyros.length; p++) {
638                                 link.printf("c P %s\n",
639                                                    pyros[p].toString());
640                         }
641                 }
642                 if (pyro_firing_time >= 0)
643                         link.printf("c I %d\n", (int) (pyro_firing_time * 100.0 + 0.5));
644
645                 /* HAS_APRS */
646                 if (aprs_interval >= 0)
647                         link.printf("c A %d\n", aprs_interval);
648                 if (aprs_ssid >= 0)
649                         link.printf("c S %d\n", aprs_ssid);
650
651                 /* HAS_BEEP */
652                 if (beep >= 0)
653                         link.printf("c b %d\n", beep);
654
655                 /* HAS_TRACKER */
656                 if (tracker_motion >= 0 && tracker_interval >= 0)
657                         link.printf("c t %d %d\n", tracker_motion, tracker_interval);
658
659                 /* HAS_GYRO */
660                 /* UI doesn't support accel cal */
661
662                 link.printf("c w\n");
663                 link.flush_output();
664         }
665
666         public AltosConfigData(AltosLink link) throws InterruptedException, TimeoutException {
667                 reset();
668                 link.printf("c s\nf\nv\n");
669                 read_link(link, "software-version");
670                 switch (log_format) {
671                 case AltosLib.AO_LOG_FORMAT_UNKNOWN:
672                 case AltosLib.AO_LOG_FORMAT_NONE:
673                         break;
674                 default:
675                         link.printf("l\n");
676                         read_link(link, "done");
677                         break;
678                 }
679         }
680
681 }