altosui: Fix telemetry file reader to handle tick count wrapping
[fw/altos] / ao-tools / altosui / AltosTelemetryReader.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 import altosui.AltosTelemetry;
24
25 public class AltosTelemetryReader extends AltosReader {
26         LinkedList<AltosRecord> records;
27
28         Iterator<AltosRecord> record_iterator;
29
30         int     boost_tick;
31
32         public AltosRecord read() throws IOException, ParseException {
33                 AltosRecord     r;
34                 if (!record_iterator.hasNext())
35                         return null;
36
37                 r = record_iterator.next();
38                 r.time = (r.tick - boost_tick) / 100.0;
39                 return r;
40         }
41
42         public AltosTelemetryReader (FileInputStream input) {
43                 boolean saw_boost = false;
44                 int     current_tick = 0;
45
46                 records = new LinkedList<AltosRecord> ();
47
48                 try {
49                         for (;;) {
50                                 String line = AltosRecord.gets(input);
51                                 if (line == null) {
52                                         break;
53                                 }
54                                 try {
55                                         System.out.printf("%s\n", line);
56                                         AltosTelemetry record = new AltosTelemetry(line);
57                                         if (record == null)
58                                                 break;
59                                         if (records.isEmpty()) {
60                                                 current_tick = record.tick;
61                                         } else {
62                                                 int tick = record.tick | (current_tick & ~ 0xffff);
63                                                 if (tick < current_tick - 0x1000)
64                                                         tick += 0x10000;
65                                                 current_tick = tick;
66                                                 record.tick = current_tick;
67                                         }
68                                         System.out.printf("\tRSSI %d tick %d\n", record.rssi, record.tick);
69                                         if (!saw_boost && record.state >= Altos.ao_flight_boost)
70                                         {
71                                                 saw_boost = true;
72                                                 boost_tick = record.tick;
73                                         }
74                                         records.add(record);
75                                 } catch (ParseException pe) {
76                                         System.out.printf("parse exception %s\n", pe.getMessage());
77                                 } catch (AltosCRCException ce) {
78                                         System.out.printf("crc error\n");
79                                 }
80                         }
81                 } catch (IOException io) {
82                         System.out.printf("io exception\n");
83                 }
84                 record_iterator = records.iterator();
85                 try {
86                         input.close();
87                 } catch (IOException ie) {
88                 }
89         }
90 }