altoslib: Clean up random rebase failures
[fw/altos] / altoslib / AltosTelemetryIterable.java
1 /*
2  * Copyright © 2010 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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.AltosLib;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23
24 public class AltosTelemetryIterable extends AltosRecordIterable {
25         TreeSet<AltosRecord>    records;
26
27         public Iterator<AltosRecord> iterator () {
28                 return records.iterator();
29         }
30
31         boolean has_gps = false;
32         boolean has_accel = false;
33         boolean has_ignite = false;
34         public boolean has_gps() { return has_gps; }
35         public boolean has_accel() { return has_accel; }
36         public boolean has_ignite() { return has_ignite; };
37
38         public AltosTelemetryIterable (FileInputStream input) {
39                 boolean saw_boost = false;
40                 int     current_tick = 0;
41                 int     boost_tick = 0;
42
43                 AltosRecord     previous = null;
44                 records = new TreeSet<AltosRecord> ();
45
46                 try {
47                         for (;;) {
48                                 String line = AltosRecord.gets(input);
49                                 if (line == null) {
50                                         break;
51                                 }
52                                 try {
53                                         AltosRecord record = AltosTelemetry.parse(line, previous);
54                                         if (record == null)
55                                                 break;
56                                         if (records.isEmpty()) {
57                                                 current_tick = record.tick;
58                                         } else {
59                                                 int tick = record.tick;
60                                                 while (tick < current_tick - 0x1000)
61                                                         tick += 0x10000;
62                                                 current_tick = tick;
63                                                 record.tick = current_tick;
64                                         }
65                                         if (!saw_boost && record.state >= AltosLib.ao_flight_boost)
66                                         {
67                                                 saw_boost = true;
68                                                 boost_tick = record.tick;
69                                         }
70                                         if (record.accel != AltosRecord.MISSING)
71                                                 has_accel = true;
72                                         if (record.gps != null)
73                                                 has_gps = true;
74                                         if (record.main != AltosRecord.MISSING)
75                                                 has_ignite = true;
76                                         if (previous != null && previous.tick != record.tick)
77                                                 records.add(previous);
78                                         previous = record;
79                                 } catch (ParseException pe) {
80                                         System.out.printf("parse exception %s\n", pe.getMessage());
81                                 } catch (AltosCRCException ce) {
82                                 }
83                         }
84                 } catch (IOException io) {
85                         System.out.printf("io exception\n");
86                 }
87
88                 if (previous != null)
89                         records.add(previous);
90
91                 /* Adjust all tick counts to match expected eeprom values,
92                  * which starts with a 16-bit tick count 16 samples before boost
93                  */
94
95                 int tick_adjust = (boost_tick - 16) & 0xffff0000;
96                 for (AltosRecord r : this)
97                         r.tick -= tick_adjust;
98                 boost_tick -= tick_adjust;
99
100                 /* adjust all tick counts to be relative to boost time */
101                 for (AltosRecord r : this)
102                         r.time = (r.tick - boost_tick) / 100.0;
103
104                 try {
105                         input.close();
106                 } catch (IOException ie) {
107                 }
108         }
109 }