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