java: Refactor AltosFlightDisplay units and font update handling
[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_2.*;
21 import org.altusmetrum.altoslib_4.*;
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_gps_course = 26;
54         public static final int data_gps_ground_speed = 27;
55         public static final int data_gps_climb_rate = 28;
56         public static final int data_ignitor_0 = 29;
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 && state.gps.cc_gps_sat != null)
105                                 y = state.gps.cc_gps_sat.length;
106                         break;
107                 case data_temperature:
108                         y = state.temperature;
109                         break;
110                 case data_range:
111                         y = state.range;
112                         break;
113                 case data_distance:
114                         if (state.from_pad != null)
115                                 y = state.from_pad.distance;
116                         break;
117                 case data_pressure:
118                         y = state.pressure();
119                         break;
120
121                 case data_accel_x:
122                 case data_accel_y:
123                 case data_accel_z:
124                 case data_gyro_x:
125                 case data_gyro_y:
126                 case data_gyro_z:
127                         AltosIMU        imu = state.imu;
128                         if (imu == null)
129                                 break;
130                         switch (index) {
131                         case data_accel_x:
132                                 y = imu.accel_x;
133                                 break;
134                         case data_accel_y:
135                                 y = imu.accel_y;
136                                 break;
137                         case data_accel_z:
138                                 y = imu.accel_z;
139                                 break;
140                         case data_gyro_x:
141                                 y = imu.gyro_x;
142                                 break;
143                         case data_gyro_y:
144                                 y = imu.gyro_y;
145                                 break;
146                         case data_gyro_z:
147                                 y = imu.gyro_z;
148                                 break;
149                         }
150                         break;
151                 case data_mag_x:
152                 case data_mag_y:
153                 case data_mag_z:
154                         AltosMag        mag = state.mag;
155                         if (mag == null)
156                                 break;
157                         switch (index) {
158                         case data_mag_x:
159                                 y = mag.x;
160                                 break;
161                         case data_mag_y:
162                                 y = mag.y;
163                                 break;
164                         case data_mag_z:
165                                 y = mag.z;
166                                 break;
167                         }
168                         break;
169                 case data_orient:
170                         y = state.orient();
171                         break;
172                 case data_gps_course:
173                         if (state.gps != null)
174                                 y = state.gps.course;
175                         else
176                                 y = AltosLib.MISSING;
177                         break;
178                 case data_gps_ground_speed:
179                         if (state.gps != null)
180                                 y = state.gps.ground_speed;
181                         else
182                                 y = AltosLib.MISSING;
183                         break;
184                 case data_gps_climb_rate:
185                         if (state.gps != null)
186                                 y = state.gps.climb_rate;
187                         else
188                                 y = AltosLib.MISSING;
189                         break;
190                 default:
191                         if (data_ignitor_0 <= index && index <= data_ignitor_max) {
192                                 int ignitor = index - data_ignitor_0;
193                                 if (state.ignitor_voltage != null && ignitor < state.ignitor_voltage.length)
194                                         y = state.ignitor_voltage[ignitor];
195                         } else if (data_ignitor_fired_0 <= index && index <= data_ignitor_fired_max) {
196                                 int ignitor = index - data_ignitor_fired_0;
197                                 if (state.ignitor_voltage != null && ignitor < state.ignitor_voltage.length) {
198                                         if ((state.pyro_fired & (1 << ignitor)) != 0)
199                                                 y = 1;
200                                         else
201                                                 y = 0;
202                                 }
203                         }
204                         break;
205                 }
206                 if (y == AltosLib.MISSING)
207                         throw new AltosUIDataMissing(index);
208                 return y;
209         }
210
211         public int id(int index) {
212                 if (index == data_state) {
213                         int s = state.state;
214                         if (Altos.ao_flight_boost <= s && s <= Altos.ao_flight_landed)
215                                 return s;
216                 } else if (data_ignitor_fired_0 <= index && index <= data_ignitor_fired_max) {
217                         int ignitor = index - data_ignitor_fired_0;
218                         if (state.ignitor_voltage != null && ignitor < state.ignitor_voltage.length) {
219                                 if (state.ignitor_voltage != null && ignitor < state.ignitor_voltage.length) {
220                                         if ((state.pyro_fired & (1 << ignitor)) != 0)
221                                                 return 1;
222                                 }
223                         }
224                 }
225                 return -1;
226         }
227
228         public String id_name(int index) {
229                 if (index == data_state) {
230                         return state.state_name();
231                 } else if (data_ignitor_fired_0 <= index && index <= data_ignitor_fired_max) {
232                         int ignitor = index - data_ignitor_fired_0;
233                         if (state.ignitor_voltage != null && ignitor < state.ignitor_voltage.length)
234                                 return AltosIgnitor.ignitor_name(ignitor);
235                 }
236                 return "";
237         }
238
239         public AltosGraphDataPoint (AltosState state) {
240                 this.state = state;
241         }
242 }