911b90b9f9f471f5c26167eeb8ad93a7cc347629
[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 Iterable<AltosState> {
21         AltosEepromNew                  eeprom;
22         TreeSet<AltosEepromRecord>      ordered;
23         AltosState                      start_state;
24
25         class RecordIterator implements Iterator<AltosState> {
26                 Iterator<AltosEepromRecord> riterator;
27                 AltosState state;
28                 boolean started;
29
30                 public boolean hasNext() {
31                         return state == null || riterator.hasNext();
32                 }
33
34                 public AltosState next() {
35                         if (state == null)
36                                 state = start_state.clone();
37                         else {
38                                 state = state.clone();
39                                 AltosEepromRecord       r = riterator.next();
40                                 r.update_state(state);
41                         }
42                         return state;
43                 }
44
45                 public RecordIterator() {
46                         riterator = ordered.iterator();
47                         state = null;
48                 }
49         }
50
51         public Iterator<AltosState> iterator() {
52                 return new RecordIterator();
53         }
54
55         public AltosEepromRecordSet(AltosEepromNew eeprom) {
56                 this.eeprom = eeprom;
57
58                 AltosConfigData         config_data = eeprom.config_data();
59
60                 AltosEepromRecord       record = null;
61
62                 switch (config_data.log_format) {
63                 case AltosLib.AO_LOG_FORMAT_FULL:
64                         record = new AltosEepromRecordFull(eeprom);
65                         break;
66                 case AltosLib.AO_LOG_FORMAT_TINY:
67                         record = new AltosEepromRecordTiny(eeprom);
68                         break;
69                 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
70                 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
71                 case AltosLib.AO_LOG_FORMAT_TELEMEGA:
72                 case AltosLib.AO_LOG_FORMAT_TELEMEGA_OLD:
73                         record = new AltosEepromRecordMega(eeprom);
74                         break;
75                 case AltosLib.AO_LOG_FORMAT_TELEMETRUM:
76                         record = new AltosEepromRecordMetrum(eeprom);
77                         break;
78                 case AltosLib.AO_LOG_FORMAT_TELEMINI2:
79                 case AltosLib.AO_LOG_FORMAT_TELEMINI3:
80                 case AltosLib.AO_LOG_FORMAT_EASYMINI:
81                         record = new AltosEepromRecordMini(eeprom);
82                         break;
83                 case AltosLib.AO_LOG_FORMAT_TELEGPS:
84                         record = new AltosEepromRecordGps(eeprom);
85                         break;
86                 case AltosLib.AO_LOG_FORMAT_TELEFIRETWO:
87                         record = new AltosEepromRecordFireTwo(eeprom);
88                         break;
89                 }
90
91                 if (record == null) {
92                         System.out.printf("failed to parse log format %d\n", config_data.log_format);
93                         return;
94                 }
95                 ordered = new TreeSet<AltosEepromRecord>();
96                 int     tick = 0;
97                 boolean first = true;
98
99                 start_state = new AltosState();
100                 start_state.set_config_data(record.eeprom.config_data());
101
102                 for (;;) {
103                         int     t = record.tick();
104
105                         if (first) {
106                                 tick = t;
107                                 first = false;
108                         } else {
109                                 while (t < tick - 32767)
110                                         t += 65536;
111                                 tick = t;
112                         }
113                         record.wide_tick = tick;
114                         ordered.add(record);
115                         if (!record.hasNext())
116                                 break;
117                         record = record.next();
118                 }
119         }
120
121         public AltosEepromRecordSet(Reader input) throws IOException {
122                 this(new AltosEepromNew(input));
123         }
124 }