fcf224f35c2a948a3dda802ee5dafc135ff13848
[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_13;
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         public AltosCalData cal_data() {
31                 if (cal_data == null) {
32                         cal_data = new AltosCalData(config_data());
33                         for (AltosEepromRecord record : ordered) {
34                                 if (record.cmd() == AltosLib.AO_LOG_FLIGHT) {
35                                         cal_data.set_tick(record.tick());
36                                         cal_data.set_boost_tick();
37                                         break;
38                                 }
39                         }
40                 }
41                 return cal_data;
42         }
43
44         public void capture_series(AltosDataListener listener) {
45                 AltosCalData    cal_data = cal_data();
46
47                 cal_data.reset();
48                 listener.set_log_format(config_data().log_format);
49
50                 for (AltosEepromRecord record : ordered) {
51                         record.provide_data(listener, cal_data);
52                 }
53                 listener.finish();
54         }
55
56         public boolean valid() {
57                 return valid;
58         }
59
60         public AltosEepromRecordSet(AltosEeprom eeprom) {
61                 this.eeprom = eeprom;
62
63                 AltosConfigData         config_data = eeprom.config_data();
64
65                 AltosEepromRecord       record = null;
66
67                 switch (config_data.log_format) {
68                 case AltosLib.AO_LOG_FORMAT_FULL:
69                         record = new AltosEepromRecordFull(eeprom);
70                         break;
71                 case AltosLib.AO_LOG_FORMAT_TINY:
72                         record = new AltosEepromRecordTiny(eeprom);
73                         break;
74                 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
75                 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
76                 case AltosLib.AO_LOG_FORMAT_TELEMEGA:
77                 case AltosLib.AO_LOG_FORMAT_TELEMEGA_3:
78                 case AltosLib.AO_LOG_FORMAT_TELEMEGA_OLD:
79                 case AltosLib.AO_LOG_FORMAT_EASYMEGA_2:
80                 case AltosLib.AO_LOG_FORMAT_TELEMEGA_4:
81                         record = new AltosEepromRecordMega(eeprom);
82                         break;
83                 case AltosLib.AO_LOG_FORMAT_TELEMETRUM:
84                         record = new AltosEepromRecordMetrum(eeprom);
85                         break;
86                 case AltosLib.AO_LOG_FORMAT_TELEMINI2:
87                 case AltosLib.AO_LOG_FORMAT_TELEMINI3:
88                 case AltosLib.AO_LOG_FORMAT_EASYMINI1:
89                 case AltosLib.AO_LOG_FORMAT_EASYMINI2:
90                         record = new AltosEepromRecordMini(eeprom);
91                         break;
92                 case AltosLib.AO_LOG_FORMAT_TELEGPS:
93                         record = new AltosEepromRecordGps(eeprom);
94                         break;
95                 case AltosLib.AO_LOG_FORMAT_TELEFIRETWO:
96                         record = new AltosEepromRecordFireTwo(eeprom);
97                         break;
98                 case AltosLib.AO_LOG_FORMAT_MICROPEAK2:
99                         record = new AltosEepromRecordMicroPeak2(eeprom);
100                         break;
101                 }
102
103                 ordered = new TreeSet<AltosEepromRecord>();
104
105                 if (record == null) {
106                         System.out.printf("failed to parse log format %d\n", config_data.log_format);
107                         valid = false;
108                         return;
109                 }
110                 valid = true;
111
112                 int     tick = 0;
113                 boolean first = true;
114
115                 do {
116                         int     t = record.tick();
117
118                         if (first) {
119                                 tick = t;
120                                 first = false;
121                         } else {
122                                 while (t < tick - 32767)
123                                         t += 65536;
124                                 tick = t;
125                         }
126                         record.wide_tick = tick;
127                         ordered.add(record);
128                         record = record.next();
129                 } while (record != null);
130         }
131
132         public AltosEepromRecordSet(InputStream input) throws IOException {
133                 this(new AltosEeprom(input));
134         }
135 }