Add GPS height to the usual plot
[fw/altos] / altosui / AltosDataPointReader.java
1
2 // Copyright (c) 2010 Anthony Towns
3 // GPL v2 or later
4
5 package altosui;
6
7 import java.lang.UnsupportedOperationException;
8 import java.util.NoSuchElementException;
9 import java.util.Iterator;
10 import org.altusmetrum.AltosLib.*;
11
12 class AltosDataPointReader implements Iterable<AltosDataPoint> {
13     Iterator<AltosRecord> iter;
14     AltosState state;
15     boolean has_gps;
16     boolean has_accel;
17     boolean has_ignite;
18
19     final static int MISSING = AltosRecord.MISSING;
20
21     public AltosDataPointReader(AltosRecordIterable reader) {
22         this.iter = reader.iterator();
23         this.state = null;
24         has_accel = true;
25         has_gps = reader.has_gps();
26         has_ignite = reader.has_ignite();
27     }
28
29     private void read_next_record() 
30         throws NoSuchElementException
31     {
32         state = new AltosState(iter.next(), state);
33     }
34
35     private AltosDataPoint current_dp() {
36         assert this.state != null;
37         
38         return new AltosDataPoint() {
39             public int version() { return state.data.version; }
40             public int serial() { return state.data.serial; }
41             public int flight() { return state.data.flight; }
42             public String callsign() { return state.data.callsign; }
43             public double time() { return state.data.time; }
44             public double rssi() { return state.data.rssi; }
45
46             public int state() { return state.state; }
47             public String state_name() { return state.data.state(); }
48
49             public double acceleration() { return state.acceleration; }
50             public double height() { return state.height; }
51             public double gps_height() { return state.gps_height; }
52             public double speed() { return state.speed(); }
53             public double temperature() { return state.temperature; }
54             public double battery_voltage() { return state.battery; }
55             public double drogue_voltage() { return state.drogue_sense; }
56             public double main_voltage() { return state.main_sense; }
57             public boolean has_accel() { return true; } // return state.acceleration != AltosRecord.MISSING; }
58         };
59     }
60
61     public Iterator<AltosDataPoint> iterator() {
62         return new Iterator<AltosDataPoint>() {
63             public void remove() { 
64                 throw new UnsupportedOperationException(); 
65             }
66             public boolean hasNext() {
67                 if (state != null && state.state == Altos.ao_flight_landed)
68                     return false;
69                 return iter.hasNext();
70             }
71             public AltosDataPoint next() {
72                 do {
73                     read_next_record();
74                 } while (state.data.time < -1.0 && hasNext());
75                 return current_dp();
76             }
77         };
78     }
79 }
80