altos: Build pdclib locally if necessary
[fw/altos] / altosui / AltosGraph.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 java.io.*;
21 import java.util.ArrayList;
22
23 import java.awt.*;
24 import javax.swing.*;
25 import org.altusmetrum.altoslib_2.*;
26 import org.altusmetrum.altosuilib_1.*;
27
28 import org.jfree.ui.*;
29 import org.jfree.chart.*;
30 import org.jfree.chart.plot.*;
31 import org.jfree.chart.axis.*;
32 import org.jfree.chart.renderer.*;
33 import org.jfree.chart.renderer.xy.*;
34 import org.jfree.chart.labels.*;
35 import org.jfree.data.xy.*;
36 import org.jfree.data.*;
37
38 class AltosVoltage extends AltosUnits {
39
40         public double value(double v, boolean imperial_units) {
41                 return v;
42         }
43
44         public double inverse(double v, boolean imperial_units) {
45                 return v;
46         }
47
48         public String show_units(boolean imperial_units) {
49                 return "V";
50         }
51
52         public String say_units(boolean imperial_units) {
53                 return "volts";
54         }
55
56         public int show_fraction(int width, boolean imperial_units) {
57                 return width / 2;
58         }
59 }
60
61 class AltosNsat extends AltosUnits {
62
63         public double value(double v, boolean imperial_units) {
64                 return v;
65         }
66
67         public double inverse(double v, boolean imperial_units) {
68                 return v;
69         }
70
71         public String show_units(boolean imperial_units) {
72                 return "Sats";
73         }
74
75         public String say_units(boolean imperial_units) {
76                 return "Satellites";
77         }
78
79         public int show_fraction(int width, boolean imperial_units) {
80                 return 0;
81         }
82 }
83
84 class AltosPressure extends AltosUnits {
85
86         public double value(double p, boolean imperial_units) {
87                 return p;
88         }
89
90         public double inverse(double p, boolean imperial_units) {
91                 return p;
92         }
93
94         public String show_units(boolean imperial_units) {
95                 return "Pa";
96         }
97
98         public String say_units(boolean imperial_units) {
99                 return "pascals";
100         }
101
102         public int show_fraction(int width, boolean imperial_units) {
103                 return 0;
104         }
105 }
106
107 class AltosDbm extends AltosUnits {
108
109         public double value(double d, boolean imperial_units) {
110                 return d;
111         }
112
113         public double inverse(double d, boolean imperial_units) {
114                 return d;
115         }
116
117         public String show_units(boolean imperial_units) {
118                 return "dBm";
119         }
120
121         public String say_units(boolean imperial_units) {
122                 return "D B M";
123         }
124
125         public int show_fraction(int width, boolean imperial_units) {
126                 return 0;
127         }
128 }
129
130 public class AltosGraph extends AltosUIGraph {
131
132         static final private Color height_color = new Color(194,31,31);
133         static final private Color gps_height_color = new Color(150,31,31);
134         static final private Color pressure_color = new Color (225,31,31);
135         static final private Color range_color = new Color(100, 31, 31);
136         static final private Color distance_color = new Color(100, 31, 194);
137         static final private Color speed_color = new Color(31,194,31);
138         static final private Color accel_color = new Color(31,31,194);
139         static final private Color voltage_color = new Color(194, 194, 31);
140         static final private Color battery_voltage_color = new Color(194, 194, 31);
141         static final private Color drogue_voltage_color = new Color(150, 150, 31);
142         static final private Color main_voltage_color = new Color(100, 100, 31);
143         static final private Color gps_nsat_color = new Color (194, 31, 194);
144         static final private Color gps_nsat_solution_color = new Color (194, 31, 194);
145         static final private Color gps_nsat_view_color = new Color (150, 31, 150);
146         static final private Color temperature_color = new Color (31, 194, 194);
147         static final private Color dbm_color = new Color(31, 100, 100);
148         static final private Color state_color = new Color(0,0,0);
149
150         static AltosVoltage voltage_units = new AltosVoltage();
151         static AltosPressure pressure_units = new AltosPressure();
152         static AltosNsat nsat_units = new AltosNsat();
153         static AltosDbm dbm_units = new AltosDbm();
154
155         AltosUIAxis     height_axis, speed_axis, accel_axis, voltage_axis, temperature_axis, nsat_axis, dbm_axis;
156         AltosUIAxis     distance_axis, pressure_axis;
157
158         public AltosGraph(AltosUIEnable enable, AltosFlightStats stats, AltosGraphDataSet dataSet) {
159                 super(enable);
160
161                 height_axis = newAxis("Height", AltosConvert.height, height_color);
162                 pressure_axis = newAxis("Pressure", pressure_units, pressure_color, 0);
163                 speed_axis = newAxis("Speed", AltosConvert.speed, speed_color);
164                 accel_axis = newAxis("Acceleration", AltosConvert.accel, accel_color);
165                 voltage_axis = newAxis("Voltage", voltage_units, voltage_color);
166                 temperature_axis = newAxis("Temperature", AltosConvert.temperature, temperature_color, 0);
167                 nsat_axis = newAxis("Satellites", nsat_units, gps_nsat_color,
168                                     AltosUIAxis.axis_include_zero | AltosUIAxis.axis_integer);
169                 dbm_axis = newAxis("Signal Strength", dbm_units, dbm_color, 0);
170                 distance_axis = newAxis("Distance", AltosConvert.distance, range_color);
171
172                 addMarker("State", AltosGraphDataPoint.data_state, state_color);
173                 addSeries("Height",
174                           AltosGraphDataPoint.data_height,
175                           AltosConvert.height,
176                           height_color,
177                           true,
178                           height_axis);
179                 addSeries("Pressure",
180                           AltosGraphDataPoint.data_pressure,
181                           pressure_units,
182                           pressure_color,
183                           false,
184                           pressure_axis);
185                 addSeries("Speed",
186                           AltosGraphDataPoint.data_speed,
187                           AltosConvert.speed,
188                           speed_color,
189                           true,
190                           speed_axis);
191                 addSeries("Acceleration",
192                           AltosGraphDataPoint.data_accel,
193                           AltosConvert.accel,
194                           accel_color,
195                           true,
196                           accel_axis);
197                 if (stats.has_gps) {
198                         addSeries("Range",
199                                   AltosGraphDataPoint.data_range,
200                                   AltosConvert.distance,
201                                   range_color,
202                                   false,
203                                   distance_axis);
204                         addSeries("Distance",
205                                   AltosGraphDataPoint.data_distance,
206                                   AltosConvert.distance,
207                                   distance_color,
208                                   false,
209                                   distance_axis);
210                         addSeries("GPS Height",
211                                   AltosGraphDataPoint.data_gps_height,
212                                   AltosConvert.height,
213                                   gps_height_color,
214                                   false,
215                                   height_axis);
216                         addSeries("GPS Satellites in Solution",
217                                   AltosGraphDataPoint.data_gps_nsat_solution,
218                                   nsat_units,
219                                   gps_nsat_solution_color,
220                                   false,
221                                   nsat_axis);
222                         addSeries("GPS Satellites in View",
223                                   AltosGraphDataPoint.data_gps_nsat_view,
224                                   nsat_units,
225                                   gps_nsat_view_color,
226                                   false,
227                           nsat_axis);
228                 }
229                 if (stats.has_rssi)
230                         addSeries("Received Signal Strength",
231                                   AltosGraphDataPoint.data_rssi,
232                                   dbm_units,
233                                   dbm_color,
234                                   false,
235                                   dbm_axis);
236                 if (stats.has_other_adc) {
237                         addSeries("Temperature",
238                                   AltosGraphDataPoint.data_temperature,
239                                   AltosConvert.temperature,
240                                   temperature_color,
241                                   false,
242                                   temperature_axis);
243                         addSeries("Battery Voltage",
244                                   AltosGraphDataPoint.data_battery_voltage,
245                                   voltage_units,
246                                   battery_voltage_color,
247                                   false,
248                                   voltage_axis);
249                         addSeries("Drogue Voltage",
250                                   AltosGraphDataPoint.data_drogue_voltage,
251                                   voltage_units,
252                                   drogue_voltage_color,
253                                   false,
254                                   voltage_axis);
255                         addSeries("Main Voltage",
256                                   AltosGraphDataPoint.data_main_voltage,
257                                   voltage_units,
258                                   main_voltage_color,
259                                   false,
260                                   voltage_axis);
261                 }
262
263                 setDataSet(dataSet);
264         }
265 }