altosuilib/micropeak: Add state markers to micropeak graph
[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_1.*;
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 speed() { return state.speed(); }
52             public double temperature() { return state.temperature; }
53             public double battery_voltage() { return state.battery; }
54             public double drogue_voltage() { return state.drogue_sense; }
55             public double main_voltage() { return state.main_sense; }
56             public boolean has_accel() { return true; } // return state.acceleration != AltosRecord.MISSING; }
57         };
58     }
59
60     public Iterator<AltosDataPoint> iterator() {
61         return new Iterator<AltosDataPoint>() {
62             public void remove() { 
63                 throw new UnsupportedOperationException(); 
64             }
65             public boolean hasNext() {
66                 if (state != null && state.state == Altos.ao_flight_landed)
67                     return false;
68                 return iter.hasNext();
69             }
70             public AltosDataPoint next() {
71                 do {
72                     read_next_record();
73                 } while (state.data.time < -1.0 && hasNext());
74                 return current_dp();
75             }
76         };
77     }
78 }
79