altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / altoslib / AltosEepromRecordSet.java
1 /*
2  * Copyright © 2017 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
15 package org.altusmetrum.altoslib_14;
16
17 import java.io.*;
18 import java.util.*;
19
20 public class AltosEepromRecordSet implements AltosRecordSet {
21         AltosEeprom                     eeprom;
22         TreeSet<AltosEepromRecord>      ordered;
23         AltosCalData                    cal_data;
24         boolean                         valid;
25
26         public AltosConfigData config_data() {
27                 return eeprom.config_data();
28         }
29
30         private void init_cal_data() {
31                 for (AltosEepromRecord record : ordered) {
32                         if (record.cmd() == AltosLib.AO_LOG_FLIGHT) {
33                                 cal_data.set_tick(record.tick());
34                                 cal_data.set_boost_tick();
35                                 cal_data.set_state(AltosLib.ao_flight_pad);
36                                 break;
37                         }
38                 }
39         }
40
41         public AltosCalData cal_data() {
42                 if (cal_data == null) {
43                         cal_data = new AltosCalData(config_data());
44                         init_cal_data();
45                 }
46                 return cal_data;
47         }
48
49         public void capture_series(AltosDataListener listener) {
50                 if (cal_data == null) {
51                         cal_data();
52                 } else {
53                         cal_data.reset();
54                         init_cal_data();
55                 }
56                 listener.set_log_format(config_data().log_format);
57
58                 for (AltosEepromRecord record : ordered) {
59                         record.provide_data(listener, cal_data);
60                 }
61                 listener.finish();
62         }
63
64         public boolean valid() {
65                 return valid;
66         }
67
68         public AltosEepromRecordSet(AltosEeprom eeprom) {
69                 this.eeprom = eeprom;
70
71                 AltosConfigData         config_data = eeprom.config_data();
72
73                 AltosEepromRecord       record = null;
74
75                 switch (config_data.log_format) {
76                 case AltosLib.AO_LOG_FORMAT_FULL:
77                         record = new AltosEepromRecordFull(eeprom);
78                         break;
79                 case AltosLib.AO_LOG_FORMAT_TINY:
80                         record = new AltosEepromRecordTiny(eeprom);
81                         break;
82                 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
83                 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
84                 case AltosLib.AO_LOG_FORMAT_TELEMEGA:
85                 case AltosLib.AO_LOG_FORMAT_TELEMEGA_3:
86                 case AltosLib.AO_LOG_FORMAT_TELEMEGA_OLD:
87                 case AltosLib.AO_LOG_FORMAT_EASYMEGA_2:
88                 case AltosLib.AO_LOG_FORMAT_TELEMEGA_4:
89                         record = new AltosEepromRecordMega(eeprom);
90                         break;
91                 case AltosLib.AO_LOG_FORMAT_TELEMETRUM:
92                         record = new AltosEepromRecordMetrum(eeprom);
93                         break;
94                 case AltosLib.AO_LOG_FORMAT_TELEMINI2:
95                 case AltosLib.AO_LOG_FORMAT_TELEMINI3:
96                 case AltosLib.AO_LOG_FORMAT_EASYMINI1:
97                 case AltosLib.AO_LOG_FORMAT_EASYMINI2:
98                         record = new AltosEepromRecordMini(eeprom);
99                         break;
100                 case AltosLib.AO_LOG_FORMAT_TELEGPS:
101                         record = new AltosEepromRecordGps(eeprom);
102                         break;
103                 case AltosLib.AO_LOG_FORMAT_TELEFIRETWO:
104                         record = new AltosEepromRecordFireTwo(eeprom);
105                         break;
106                 case AltosLib.AO_LOG_FORMAT_MICROPEAK2:
107                         record = new AltosEepromRecordMicroPeak2(eeprom);
108                         break;
109                 }
110
111                 ordered = new TreeSet<AltosEepromRecord>();
112
113                 if (record == null) {
114                         System.out.printf("failed to parse log format %d\n", config_data.log_format);
115                         valid = false;
116                         return;
117                 }
118                 valid = true;
119
120                 int     tick = 0;
121                 boolean first = true;
122
123                 do {
124                         int     t = record.tick();
125
126                         if (first) {
127                                 tick = t;
128                                 first = false;
129                         } else {
130                                 while (t < tick - 32767)
131                                         t += 65536;
132                                 tick = t;
133                         }
134                         record.wide_tick = tick;
135                         ordered.add(record);
136                         record = record.next();
137                 } while (record != null);
138         }
139
140         public AltosEepromRecordSet(InputStream input) throws IOException {
141                 this(new AltosEeprom(input));
142         }
143 }