44e5ad8f8d9c2fe9977cd0550abf8ab67c8d9dc0
[fw/altos] / altosui / 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 altosui;
19
20 import java.io.*;
21 import java.util.*;
22 import java.text.*;
23
24 public class AltosTelemetryIterable extends AltosRecordIterable {
25         LinkedList<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                 records = new LinkedList<AltosRecord> ();
44
45                 try {
46                         for (;;) {
47                                 String line = AltosRecord.gets(input);
48                                 if (line == null) {
49                                         break;
50                                 }
51                                 try {
52                                         AltosTelemetry record = new AltosTelemetry(line);
53                                         if (record == null)
54                                                 break;
55                                         if (records.isEmpty()) {
56                                                 current_tick = record.tick;
57                                         } else {
58                                                 int tick = record.tick | (current_tick & ~ 0xffff);
59                                                 if (tick < current_tick - 0x1000)
60                                                         tick += 0x10000;
61                                                 current_tick = tick;
62                                                 record.tick = current_tick;
63                                         }
64                                         if (!saw_boost && record.state >= Altos.ao_flight_boost)
65                                         {
66                                                 saw_boost = true;
67                                                 boost_tick = record.tick;
68                                         }
69                                         if (record.accel != AltosRecord.MISSING)
70                                                 has_accel = true;
71                                         if (record.gps != null)
72                                                 has_gps = true;
73                                         if (record.main != AltosRecord.MISSING)
74                                                 has_ignite = true;
75                                         records.add(record);
76                                 } catch (ParseException pe) {
77                                         System.out.printf("parse exception %s\n", pe.getMessage());
78                                 } catch (AltosCRCException ce) {
79                                 }
80                         }
81                 } catch (IOException io) {
82                         System.out.printf("io exception\n");
83                 }
84
85                 /* adjust all tick counts to be relative to boost time */
86                 for (AltosRecord r : this)
87                         r.time = (r.tick - boost_tick) / 100.0;
88
89                 try {
90                         input.close();
91                 } catch (IOException ie) {
92                 }
93         }
94 }