14486abfa7b1d76f5027bd61a50db61dc59df650
[fw/altos] / altosuilib / AltosGraphDataPoint.java
1 /*
2  * Copyright © 2013 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.altosuilib_3;
19
20 import org.altusmetrum.altoslib_5.*;
21
22 public class AltosGraphDataPoint implements AltosUIDataPoint {
23
24         AltosState      state;
25
26         public static final int data_height = 0;
27         public static final int data_speed = 1;
28         public static final int data_accel = 2;
29         public static final int data_temp = 3;
30         public static final int data_battery_voltage = 4;
31         public static final int data_drogue_voltage = 5;
32         public static final int data_main_voltage = 6;
33         public static final int data_rssi = 7;
34         public static final int data_state = 8;
35         public static final int data_gps_height = 9;
36         public static final int data_gps_nsat_solution = 10;
37         public static final int data_gps_nsat_view = 11;
38         public static final int data_gps_altitude = 12;
39         public static final int data_temperature = 13;
40         public static final int data_range = 14;
41         public static final int data_distance = 15;
42         public static final int data_pressure = 16;
43         public static final int data_accel_x = 17;
44         public static final int data_accel_y = 18;
45         public static final int data_accel_z = 19;
46         public static final int data_gyro_x = 20;
47         public static final int data_gyro_y = 21;
48         public static final int data_gyro_z = 22;
49         public static final int data_mag_x = 23;
50         public static final int data_mag_y = 24;
51         public static final int data_mag_z = 25;
52         public static final int data_orient = 26;
53         public static final int data_gps_course = 27;
54         public static final int data_gps_ground_speed = 28;
55         public static final int data_gps_climb_rate = 29;
56         public static final int data_ignitor_0 = 30;
57         public static final int data_ignitor_num = 32;
58         public static final int data_ignitor_max = data_ignitor_0 + data_ignitor_num - 1;
59         public static final int data_ignitor_fired_0 = data_ignitor_0 + data_ignitor_num;
60         public static final int data_ignitor_fired_max = data_ignitor_fired_0 + data_ignitor_num - 1;
61
62         public double x() throws AltosUIDataMissing {
63                 double  time = state.time_since_boost();
64                 if (time < -2)
65                         throw new AltosUIDataMissing(-1);
66                 return time;
67         }
68
69         public double y(int index) throws AltosUIDataMissing {
70                 double y = AltosLib.MISSING;
71                 switch (index) {
72                 case data_height:
73                         y = state.height();
74                         break;
75                 case data_speed:
76                         y = state.speed();
77                         break;
78                 case data_accel:
79                         y = state.acceleration();
80                         break;
81                 case data_temp:
82                         y = state.temperature;
83                         break;
84                 case data_battery_voltage:
85                         y = state.battery_voltage;
86                         break;
87                 case data_drogue_voltage:
88                         y = state.apogee_voltage;
89                         break;
90                 case data_main_voltage:
91                         y = state.main_voltage;
92                         break;
93                 case data_rssi:
94                         y = state.rssi;
95                         break;
96                 case data_gps_height:
97                         y = state.gps_height;
98                         break;
99                 case data_gps_nsat_solution:
100                         if (state.gps != null)
101                                 y = state.gps.nsat;
102                         break;
103                 case data_gps_nsat_view:
104                         if (state.gps != null) {
105                                 if (state.gps.cc_gps_sat != null)
106                                         y = state.gps.cc_gps_sat.length;
107                                 else
108                                         y = 0;
109                         }
110                         break;
111                 case data_gps_altitude:
112                         y = state.gps_altitude();
113                         break;
114                 case data_temperature:
115                         y = state.temperature;
116                         break;
117                 case data_range:
118                         y = state.range;
119                         break;
120                 case data_distance:
121                         if (state.from_pad != null)
122                                 y = state.from_pad.distance;
123                         break;
124                 case data_pressure:
125                         y = state.pressure();
126                         break;
127
128                 case data_accel_x:
129                 case data_accel_y:
130                 case data_accel_z:
131                 case data_gyro_x:
132                 case data_gyro_y:
133                 case data_gyro_z:
134                         AltosIMU        imu = state.imu;
135                         if (imu == null)
136                                 break;
137                         switch (index) {
138                         case data_accel_x:
139                                 y = imu.accel_x;
140                                 break;
141                         case data_accel_y:
142                                 y = imu.accel_y;
143                                 break;
144                         case data_accel_z:
145                                 y = imu.accel_z;
146                                 break;
147                         case data_gyro_x:
148                                 y = imu.gyro_x;
149                                 break;
150                         case data_gyro_y:
151                                 y = imu.gyro_y;
152                                 break;
153                         case data_gyro_z:
154                                 y = imu.gyro_z;
155                                 break;
156                         }
157                         break;
158                 case data_mag_x:
159                 case data_mag_y:
160                 case data_mag_z:
161                         AltosMag        mag = state.mag;
162                         if (mag == null)
163                                 break;
164                         switch (index) {
165                         case data_mag_x:
166                                 y = mag.x;
167                                 break;
168                         case data_mag_y:
169                                 y = mag.y;
170                                 break;
171                         case data_mag_z:
172                                 y = mag.z;
173                                 break;
174                         }
175                         break;
176                 case data_orient:
177                         y = state.orient();
178                         break;
179                 case data_gps_course:
180                         if (state.gps != null)
181                                 y = state.gps.course;
182                         else
183                                 y = AltosLib.MISSING;
184                         break;
185                 case data_gps_ground_speed:
186                         if (state.gps != null)
187                                 y = state.gps.ground_speed;
188                         else
189                                 y = AltosLib.MISSING;
190                         break;
191                 case data_gps_climb_rate:
192                         if (state.gps != null)
193                                 y = state.gps.climb_rate;
194                         else
195                                 y = AltosLib.MISSING;
196                         break;
197                 default:
198                         if (data_ignitor_0 <= index && index <= data_ignitor_max) {
199                                 int ignitor = index - data_ignitor_0;
200                                 if (state.ignitor_voltage != null && ignitor < state.ignitor_voltage.length)
201                                         y = state.ignitor_voltage[ignitor];
202                         } else if (data_ignitor_fired_0 <= index && index <= data_ignitor_fired_max) {
203                                 int ignitor = index - data_ignitor_fired_0;
204                                 if (state.ignitor_voltage != null && ignitor < state.ignitor_voltage.length) {
205                                         if ((state.pyro_fired & (1 << ignitor)) != 0)
206                                                 y = 1;
207                                         else
208                                                 y = 0;
209                                 }
210                         }
211                         break;
212                 }
213                 if (y == AltosLib.MISSING)
214                         throw new AltosUIDataMissing(index);
215                 return y;
216         }
217
218         public int id(int index) {
219                 if (index == data_state) {
220                         int s = state.state;
221                         if (AltosLib.ao_flight_boost <= s && s <= AltosLib.ao_flight_landed)
222                                 return s;
223                 } else if (data_ignitor_fired_0 <= index && index <= data_ignitor_fired_max) {
224                         int ignitor = index - data_ignitor_fired_0;
225                         if (state.ignitor_voltage != null && ignitor < state.ignitor_voltage.length) {
226                                 if (state.ignitor_voltage != null && ignitor < state.ignitor_voltage.length) {
227                                         if ((state.pyro_fired & (1 << ignitor)) != 0)
228                                                 return 1;
229                                 }
230                         }
231                 }
232                 return -1;
233         }
234
235         public String id_name(int index) {
236                 if (index == data_state) {
237                         return state.state_name();
238                 } else if (data_ignitor_fired_0 <= index && index <= data_ignitor_fired_max) {
239                         int ignitor = index - data_ignitor_fired_0;
240                         if (state.ignitor_voltage != null && ignitor < state.ignitor_voltage.length)
241                                 return AltosLib.ignitor_name(ignitor);
242                 }
243                 return "";
244         }
245
246         public AltosGraphDataPoint (AltosState state) {
247                 this.state = state;
248         }
249 }