altoslib: Make cal_data private in AltosDataListener
[fw/altos] / telegps / TeleGPSState.java
1 /*
2  * Copyright © 2014 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.telegps;
20
21 import java.util.*;
22 import java.awt.*;
23 import java.awt.event.*;
24 import javax.swing.*;
25 import org.altusmetrum.altoslib_12.*;
26 import org.altusmetrum.altosuilib_12.*;
27
28 public class TeleGPSState extends AltosUIFlightTab {
29
30         JLabel  cur, max;
31
32         abstract class Value extends AltosUIUnitsIndicator {
33                 public Value (Container container, int y, AltosUnits units, String text) {
34                         super(container, y, units, text, 1, false, 2);
35                 }
36         }
37
38         abstract class DualValue extends AltosUIUnitsIndicator {
39                 public DualValue (Container container, int y, AltosUnits units, String text) {
40                         super(container, y, units, text, 2, false, 1);
41                 }
42         }
43
44         abstract class ValueHold extends DualValue {
45                 public ValueHold (Container container, int y, AltosUnits units, String text) {
46                         super(container, y, units, text);
47                 }
48         }
49
50         class Height extends ValueHold {
51                 public double value(AltosState state, int i) {
52                         if (i == 0)
53                                 return state.height();
54                         else
55                                 return state.max_height();
56                 }
57
58                 public Height(Container container, int y) {
59                         super(container, y, AltosConvert.height, "Height");
60                 }
61         }
62
63         class Speed extends ValueHold {
64                 public double value(AltosState state, int i) {
65                         if (i == 0)
66                                 return state.gps_speed();
67                         else
68                                 return state.max_gps_speed();
69                 }
70
71                 public Speed(Container container, int y) {
72                         super(container, y, AltosConvert.speed, "Speed");
73                 }
74         }
75
76         class Distance extends Value {
77                 public double value(AltosState state, int i) {
78                         if (state.from_pad != null)
79                                 return state.from_pad.distance;
80                         else
81                                 return AltosLib.MISSING;
82                 }
83
84                 public Distance(Container container, int y) {
85                         super(container, y, AltosConvert.distance, "Distance");
86                 }
87         }
88
89         class Range extends Value {
90                 public double value(AltosState state, int i) {
91                         return state.range;
92                 }
93                 public Range (Container container, int y) {
94                         super (container, y, AltosConvert.distance, "Range");
95                 }
96         }
97
98         class Bearing extends AltosUIIndicator {
99                 public void show (AltosState state, AltosListenerState listener_state) {
100                         if (state.from_pad != null && state.from_pad.bearing != AltosLib.MISSING) {
101                                 show( String.format("%3.0f°", state.from_pad.bearing),
102                                       state.from_pad.bearing_words(
103                                               AltosGreatCircle.BEARING_LONG));
104                         } else {
105                                 show("Missing", "Missing");
106                         }
107                 }
108                 public Bearing (Container container, int y) {
109                         super (container, y, "Bearing", 2, false, 1);
110                 }
111         }
112
113         class Elevation extends AltosUIIndicator {
114                 public void show (AltosState state, AltosListenerState listener_state) {
115                         if (state.elevation == AltosLib.MISSING)
116                                 show("Missing");
117                         else
118                                 show("%3.0f°", state.elevation);
119                 }
120                 public Elevation (Container container, int y) {
121                         super (container, y, "Elevation", 1, false, 2);
122                 }
123         }
124
125         class FirmwareVersion extends AltosUIIndicator {
126                 public void show(AltosState state, AltosListenerState listener_state) {
127                         AltosCalData cal_data = state.cal_data();
128                         if (cal_data.firmware_version == null)
129                                 show("Missing");
130                         else
131                                 show(cal_data.firmware_version);
132                 }
133
134                 public FirmwareVersion(Container container, int y) {
135                         super(container, y, "Firmware Version", 1, false, 2);
136                 }
137         }
138
139         class FlightLogMax extends AltosUIIndicator {
140                 public void show(AltosState state, AltosListenerState listener_state) {
141                         AltosCalData cal_data = state.cal_data();
142                         int storage = cal_data.flight_log_max;
143                         if (storage == AltosLib.MISSING)
144                                 show("Missing");
145                         else
146                                 show(String.format("%dkB", storage));
147                 }
148
149                 public FlightLogMax(Container container, int y) {
150                         super(container, y, "Flight Log Storage", 1, false, 2);
151                 }
152         }
153
154         class BatteryVoltage extends AltosUIVoltageIndicator {
155                 public double voltage(AltosState state) {
156                         return state.battery_voltage;
157                 }
158
159                 public double good() {
160                         return AltosLib.ao_battery_good;
161                 }
162
163                 public BatteryVoltage(Container container, int y) {
164                         super(container, y, "Battery Voltage", 2);
165                 }
166         }
167
168         class ReceiverBattery extends AltosUIVoltageIndicator {
169
170                 public double voltage(AltosState state) { return AltosLib.MISSING; }
171
172                 public double good() { return AltosLib.ao_battery_good; }
173
174                 public boolean hide(AltosState state, AltosListenerState listener_state, int i) {
175                         return value(state, listener_state, i) == AltosLib.MISSING;
176                 }
177
178                 public double value(AltosState state, AltosListenerState listener_state, int i) {
179                         if (listener_state == null)
180                                 return AltosLib.MISSING;
181                         return listener_state.battery;
182                 }
183
184                 public ReceiverBattery (AltosUIFlightTab container, int y) {
185                         super(container, y, "Receiver Battery", 2);
186                 }
187         }
188
189         public void labels(Container container, int y) {
190                 GridBagLayout           layout = (GridBagLayout)(container.getLayout());
191                 GridBagConstraints      c;
192
193                 cur = new JLabel("Current");
194                 cur.setFont(AltosUILib.label_font);
195                 c = new GridBagConstraints();
196                 c.gridx = 2; c.gridy = y;
197                 c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
198                 layout.setConstraints(cur, c);
199                 add(cur);
200
201                 max = new JLabel("Maximum");
202                 max.setFont(AltosUILib.label_font);
203                 c.gridx = 3; c.gridy = y;
204                 layout.setConstraints(max, c);
205                 add(max);
206         }
207
208         public void font_size_changed(int font_size) {
209                 cur.setFont(AltosUILib.label_font);
210                 max.setFont(AltosUILib.label_font);
211                 super.font_size_changed(font_size);
212         }
213
214         public String getName() {
215                 return "Status";
216         }
217
218         public TeleGPSState() {
219                 int y = 0;
220                 labels(this, y++);
221                 add(new Height(this, y++));
222                 add(new Speed(this, y++));
223                 add(new Distance(this, y++));
224                 add(new Range(this, y++));
225                 add(new Bearing(this, y++));
226                 add(new Elevation(this, y++));
227                 add(new FirmwareVersion(this, y++));
228                 add(new FlightLogMax(this, y++));
229                 add(new BatteryVoltage(this, y++));
230                 add(new ReceiverBattery(this, y++));
231         }
232 }