c019a092bbeb0c44d3652e1768e8fbea6de50c09
[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_11;
16
17 import java.io.*;
18 import java.util.*;
19
20 public class AltosEepromRecordSet implements AltosRecordSet {
21         AltosEepromNew                  eeprom;
22         TreeSet<AltosEepromRecord>      ordered;
23         AltosCalData                    cal_data;
24
25         public AltosConfigData config_data() {
26                 return eeprom.config_data();
27         }
28
29         public AltosCalData cal_data() {
30                 if (cal_data == null) {
31                         cal_data = new AltosCalData(config_data());
32                         for (AltosEepromRecord record : ordered) {
33                                 if (record.cmd() == AltosLib.AO_LOG_FLIGHT) {
34                                         cal_data.set_tick(record.tick());
35                                         cal_data.set_boost_tick();
36                                         break;
37                                 }
38                         }
39                 }
40                 return cal_data;
41         }
42
43         public void capture_series(AltosDataListener listener) {
44                 AltosCalData    cal_data = cal_data();
45                 for (AltosEepromRecord record : ordered) {
46                         record.provide_data(listener, cal_data);
47                 }
48                 listener.finish();
49         }
50
51         public AltosEepromRecordSet(AltosEepromNew eeprom) {
52                 this.eeprom = eeprom;
53
54                 AltosConfigData         config_data = eeprom.config_data();
55
56                 AltosEepromRecord       record = null;
57
58                 switch (config_data.log_format) {
59                 case AltosLib.AO_LOG_FORMAT_FULL:
60                         record = new AltosEepromRecordFull(eeprom);
61                         break;
62                 case AltosLib.AO_LOG_FORMAT_TINY:
63                         record = new AltosEepromRecordTiny(eeprom);
64                         break;
65                 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
66                 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
67                 case AltosLib.AO_LOG_FORMAT_TELEMEGA:
68                 case AltosLib.AO_LOG_FORMAT_TELEMEGA_OLD:
69                         record = new AltosEepromRecordMega(eeprom);
70                         break;
71                 case AltosLib.AO_LOG_FORMAT_TELEMETRUM:
72                         record = new AltosEepromRecordMetrum(eeprom);
73                         break;
74                 case AltosLib.AO_LOG_FORMAT_TELEMINI2:
75                 case AltosLib.AO_LOG_FORMAT_TELEMINI3:
76                 case AltosLib.AO_LOG_FORMAT_EASYMINI:
77                         record = new AltosEepromRecordMini(eeprom);
78                         break;
79                 case AltosLib.AO_LOG_FORMAT_TELEGPS:
80                         record = new AltosEepromRecordGps(eeprom);
81                         break;
82                 case AltosLib.AO_LOG_FORMAT_TELEFIRETWO:
83                         record = new AltosEepromRecordFireTwo(eeprom);
84                         break;
85                 }
86
87                 if (record == null) {
88                         System.out.printf("failed to parse log format %d\n", config_data.log_format);
89                         return;
90                 }
91                 ordered = new TreeSet<AltosEepromRecord>();
92                 int     tick = 0;
93                 boolean first = true;
94
95                 for (;;) {
96                         int     t = record.tick();
97
98                         if (first) {
99                                 tick = t;
100                                 first = false;
101                         } else {
102                                 while (t < tick - 32767)
103                                         t += 65536;
104                                 tick = t;
105                         }
106                         record.wide_tick = tick;
107                         ordered.add(record);
108                         if (!record.hasNext())
109                                 break;
110                         record = record.next();
111                 }
112         }
113
114         public AltosEepromRecordSet(InputStream input) throws IOException {
115                 this(new AltosEepromNew(input));
116         }
117 }