altoslib: Add TeleFireTwo eeprom support
[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         AltosState              start;
62
63         public void write_comments(PrintStream out) {
64                 headers.write(out);
65         }
66
67         public void write(PrintStream out) {
68                 headers.write(out);
69                 body.write(out);
70         }
71
72         public AltosEepromFile(FileInputStream input) {
73                 headers = new AltosEepromIterable(AltosEepromHeader.read(input));
74
75                 start = headers.state();
76                 if (start.state() != AltosLib.ao_flight_stateless)
77                         start.set_state(AltosLib.ao_flight_pad);
78
79                 if (start.log_format == AltosLib.MISSING) {
80                         if (start.product != null) {
81                                 if (start.product.startsWith("TeleMetrum"))
82                                         start.log_format = AltosLib.AO_LOG_FORMAT_FULL;
83                                 else if (start.product.startsWith("TeleMini"))
84                                         start.log_format = AltosLib.AO_LOG_FORMAT_TINY;
85                         }
86                 }
87
88                 switch (start.log_format) {
89                 case AltosLib.AO_LOG_FORMAT_FULL:
90                         body = new AltosEepromIterable(AltosEepromTM.read(input));
91                         break;
92                 case AltosLib.AO_LOG_FORMAT_TINY:
93                         body = new AltosEepromIterable(AltosEepromTMini.read(input));
94                         break;
95                 case AltosLib.AO_LOG_FORMAT_TELEMETRY:
96                 case AltosLib.AO_LOG_FORMAT_TELESCIENCE:
97                 case AltosLib.AO_LOG_FORMAT_TELEMEGA:
98                 case AltosLib.AO_LOG_FORMAT_TELEMEGA_OLD:
99                         body = new AltosEepromIterable(AltosEepromMega.read(input, start.log_format));
100                         break;
101                 case AltosLib.AO_LOG_FORMAT_TELEMETRUM:
102                         body = new AltosEepromIterable(AltosEepromMetrum2.read(input));
103                         break;
104                 case AltosLib.AO_LOG_FORMAT_TELEMINI2:
105                 case AltosLib.AO_LOG_FORMAT_TELEMINI3:
106                 case AltosLib.AO_LOG_FORMAT_EASYMINI:
107                         body = new AltosEepromIterable(AltosEepromMini.read(input));
108                         break;
109                 case AltosLib.AO_LOG_FORMAT_TELEGPS:
110                         body = new AltosEepromIterable(AltosEepromGPS.read(input));
111                         break;
112                 case AltosLib.AO_LOG_FORMAT_TELEFIRETWO:
113                         body = new AltosEepromIterable(AltosEepromFireTwo.read(input));
114                         break;
115                 default:
116                         body = new AltosEepromIterable(new LinkedList<AltosEeprom>());
117                         break;
118                 }
119
120                 /* Find boost tick */
121                 AltosState      state = start.clone();
122                 for (AltosEeprom eeprom : body) {
123                         eeprom.update_state(state);
124                         state.finish_update();
125                         if (state.state() >= AltosLib.ao_flight_boost) {
126                                 start.set_boost_tick(state.tick);
127                                 break;
128                         }
129                 }
130         }
131
132         public Iterator<AltosState> iterator() {
133                 AltosState              state = start.clone();
134                 Iterator<AltosEeprom>   i = body.iterator();
135
136                 while (i.hasNext() && !state.valid()) {
137                         i.next().update_state(state);
138                         state.finish_update();
139                 }
140                 return new AltosEepromIterator(state, i);
141         }
142 }