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