altoslib: Hack up AltosEepromFile to support new eeprom code
[fw/altos] / altoslib / AltosEepromFile.java
1 /*
2  * Copyright © 2013 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  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altoslib_11;
20
21 import java.io.*;
22 import java.util.*;
23 import java.text.*;
24
25 class AltosEepromIterator implements Iterator<AltosState> {
26         AltosState              state;
27         Iterator<AltosEeprom>   body;
28         AltosEeprom             next;
29         boolean                 seen;
30
31         public boolean hasNext() {
32                 return !seen || body.hasNext();
33         }
34
35         public AltosState next() {
36                 if (seen) {
37                         AltosState      n = state.clone();
38                         AltosEeprom     e = body.next();
39
40                         e.update_state(n);
41                         state = n;
42                 }
43                 seen = true;
44                 return state;
45         }
46
47         public void remove () {
48         }
49
50         public AltosEepromIterator(AltosState start, Iterator<AltosEeprom> body) {
51                 this.state = start;
52                 this.body = body;
53                 this.seen = false;
54         }
55 }
56
57 public class AltosEepromFile extends AltosStateIterable {
58
59         AltosEepromIterable     headers;
60         AltosEepromIterable     body;
61         AltosEepromRecordSet    set;
62         AltosState              start;
63
64         public void write_comments(PrintStream out) {
65                 headers.write(out);
66         }
67
68         public void write(PrintStream out) {
69                 headers.write(out);
70                 body.write(out);
71         }
72
73         public AltosEepromFile(Reader input) throws IOException {
74                 set = new AltosEepromRecordSet(input);
75
76         }
77
78         public AltosEepromFile(FileInputStream input) {
79                 headers = new AltosEepromIterable(AltosEepromHeader.read(input));
80
81                 start = headers.state();
82                 if (start.state() != AltosLib.ao_flight_stateless)
83                         start.set_state(AltosLib.ao_flight_pad);
84
85                 if (start.log_format == AltosLib.MISSING) {
86                         if (start.product != null) {
87                                 if (start.product.startsWith("TeleMetrum"))
88                                         start.log_format = AltosLib.AO_LOG_FORMAT_FULL;
89                                 else if (start.product.startsWith("TeleMini"))
90                                         start.log_format = AltosLib.AO_LOG_FORMAT_TINY;
91                         }
92                 }
93
94                 switch (start.log_format) {
95                 case AltosLib.AO_LOG_FORMAT_FULL:
96                         body = new AltosEepromIterable(AltosEepromTM.read(input));
97                         break;
98                 case AltosLib.AO_LOG_FORMAT_TINY:
99                         body = new AltosEepromIterable(AltosEepromTMini.read(input));
100                         break;
101                 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
102                 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
103                 case AltosLib.AO_LOG_FORMAT_TELEMEGA:
104                 case AltosLib.AO_LOG_FORMAT_TELEMEGA_OLD:
105                         body = new AltosEepromIterable(AltosEepromMega.read(input, start.log_format));
106                         break;
107                 case AltosLib.AO_LOG_FORMAT_TELEMETRUM:
108                         body = new AltosEepromIterable(AltosEepromMetrum2.read(input));
109                         break;
110                 case AltosLib.AO_LOG_FORMAT_TELEMINI2:
111                 case AltosLib.AO_LOG_FORMAT_TELEMINI3:
112                 case AltosLib.AO_LOG_FORMAT_EASYMINI:
113                         body = new AltosEepromIterable(AltosEepromMini.read(input));
114                         break;
115                 case AltosLib.AO_LOG_FORMAT_TELEGPS:
116                         body = new AltosEepromIterable(AltosEepromGPS.read(input));
117                         break;
118                 case AltosLib.AO_LOG_FORMAT_TELEFIRETWO:
119                         body = new AltosEepromIterable(AltosEepromFireTwo.read(input));
120                         break;
121                 default:
122                         body = new AltosEepromIterable(new LinkedList<AltosEeprom>());
123                         break;
124                 }
125
126                 /* Find boost tick */
127                 AltosState      state = start.clone();
128                 for (AltosEeprom eeprom : body) {
129                         eeprom.update_state(state);
130                         state.finish_update();
131                         if (state.state() >= AltosLib.ao_flight_boost) {
132                                 start.set_boost_tick(state.tick);
133                                 break;
134                         }
135                 }
136         }
137
138         public Iterator<AltosState> iterator() {
139                 if (set != null)
140                         return set.iterator();
141
142                 AltosState              state = start.clone();
143                 Iterator<AltosEeprom>   i = body.iterator();
144
145                 while (i.hasNext() && !state.valid()) {
146                         i.next().update_state(state);
147                         state.finish_update();
148                 }
149                 return new AltosEepromIterator(state, i);
150         }
151 }