19fd369454ff9b4403486c5976485966b7ddc393
[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                         if (state.cal_data.firmware_version == null)
128                                 show("Missing");
129                         else
130                                 show(state.cal_data.firmware_version);
131                 }
132
133                 public FirmwareVersion(Container container, int y) {
134                         super(container, y, "Firmware Version", 1, false, 2);
135                 }
136         }
137
138         class FlightLogMax extends AltosUIIndicator {
139                 public void show(AltosState state, AltosListenerState listener_state) {
140                         int storage = state.cal_data.flight_log_max;
141                         if (storage == AltosLib.MISSING)
142                                 show("Missing");
143                         else
144                                 show(String.format("%dkB", storage));
145                 }
146
147                 public FlightLogMax(Container container, int y) {
148                         super(container, y, "Flight Log Storage", 1, false, 2);
149                 }
150         }
151
152         class BatteryVoltage extends AltosUIVoltageIndicator {
153                 public double voltage(AltosState state) {
154                         return state.battery_voltage;
155                 }
156
157                 public double good() {
158                         return AltosLib.ao_battery_good;
159                 }
160
161                 public BatteryVoltage(Container container, int y) {
162                         super(container, y, "Battery Voltage", 2);
163                 }
164         }
165
166         class ReceiverBattery extends AltosUIVoltageIndicator {
167
168                 public double voltage(AltosState state) { return AltosLib.MISSING; }
169
170                 public double good() { return AltosLib.ao_battery_good; }
171
172                 public boolean hide(AltosState state, AltosListenerState listener_state, int i) {
173                         return value(state, listener_state, i) == AltosLib.MISSING;
174                 }
175
176                 public double value(AltosState state, AltosListenerState listener_state, int i) {
177                         if (listener_state == null)
178                                 return AltosLib.MISSING;
179                         return listener_state.battery;
180                 }
181
182                 public ReceiverBattery (AltosUIFlightTab container, int y) {
183                         super(container, y, "Receiver Battery", 2);
184                 }
185         }
186
187         public void labels(Container container, int y) {
188                 GridBagLayout           layout = (GridBagLayout)(container.getLayout());
189                 GridBagConstraints      c;
190
191                 cur = new JLabel("Current");
192                 cur.setFont(AltosUILib.label_font);
193                 c = new GridBagConstraints();
194                 c.gridx = 2; c.gridy = y;
195                 c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
196                 layout.setConstraints(cur, c);
197                 add(cur);
198
199                 max = new JLabel("Maximum");
200                 max.setFont(AltosUILib.label_font);
201                 c.gridx = 3; c.gridy = y;
202                 layout.setConstraints(max, c);
203                 add(max);
204         }
205
206         public void font_size_changed(int font_size) {
207                 cur.setFont(AltosUILib.label_font);
208                 max.setFont(AltosUILib.label_font);
209                 super.font_size_changed(font_size);
210         }
211
212         public String getName() {
213                 return "Status";
214         }
215
216         public TeleGPSState() {
217                 int y = 0;
218                 labels(this, y++);
219                 add(new Height(this, y++));
220                 add(new Speed(this, y++));
221                 add(new Distance(this, y++));
222                 add(new Range(this, y++));
223                 add(new Bearing(this, y++));
224                 add(new Elevation(this, y++));
225                 add(new FirmwareVersion(this, y++));
226                 add(new FlightLogMax(this, y++));
227                 add(new BatteryVoltage(this, y++));
228                 add(new ReceiverBattery(this, y++));
229         }
230 }