telegps: Use log_space when flight_log_max is missing
[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; 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 org.altusmetrum.telegps;
19
20 import java.util.*;
21 import java.awt.*;
22 import java.awt.event.*;
23 import javax.swing.*;
24 import org.altusmetrum.altoslib_11.*;
25 import org.altusmetrum.altosuilib_11.*;
26
27 public class TeleGPSState extends AltosUIFlightTab {
28
29         JLabel  cur, max;
30
31         abstract class Value extends AltosUIUnitsIndicator {
32                 public Value (Container container, int y, AltosUnits units, String text) {
33                         super(container, y, units, text, 1, false, 2);
34                 }
35         }
36
37         abstract class DualValue extends AltosUIUnitsIndicator {
38                 public DualValue (Container container, int y, AltosUnits units, String text) {
39                         super(container, y, units, text, 2, false, 1);
40                 }
41         }
42
43         abstract class ValueHold extends DualValue {
44                 public ValueHold (Container container, int y, AltosUnits units, String text) {
45                         super(container, y, units, text);
46                 }
47         }
48
49         class Height extends ValueHold {
50                 public double value(AltosState state, int i) {
51                         if (i == 0)
52                                 return state.height();
53                         else
54                                 return state.max_height();
55                 }
56
57                 public Height(Container container, int y) {
58                         super(container, y, AltosConvert.height, "Height");
59                 }
60         }
61
62         class Speed extends ValueHold {
63                 public double value(AltosState state, int i) {
64                         if (i == 0)
65                                 return state.gps_speed();
66                         else
67                                 return state.max_gps_speed();
68                 }
69
70                 public Speed(Container container, int y) {
71                         super(container, y, AltosConvert.speed, "Speed");
72                 }
73         }
74
75         class Distance extends Value {
76                 public double value(AltosState state, int i) {
77                         if (state.from_pad != null)
78                                 return state.from_pad.distance;
79                         else
80                                 return AltosLib.MISSING;
81                 }
82
83                 public Distance(Container container, int y) {
84                         super(container, y, AltosConvert.distance, "Distance");
85                 }
86         }
87
88         class Range extends Value {
89                 public double value(AltosState state, int i) {
90                         return state.range;
91                 }
92                 public Range (Container container, int y) {
93                         super (container, y, AltosConvert.distance, "Range");
94                 }
95         }
96
97         class Bearing extends AltosUIIndicator {
98                 public void show (AltosState state, AltosListenerState listener_state) {
99                         if (state.from_pad != null && state.from_pad.bearing != AltosLib.MISSING) {
100                                 show( String.format("%3.0f°", state.from_pad.bearing),
101                                       state.from_pad.bearing_words(
102                                               AltosGreatCircle.BEARING_LONG));
103                         } else {
104                                 show("Missing", "Missing");
105                         }
106                 }
107                 public Bearing (Container container, int y) {
108                         super (container, y, "Bearing", 2, false, 1);
109                 }
110         }
111
112         class Elevation extends AltosUIIndicator {
113                 public void show (AltosState state, AltosListenerState listener_state) {
114                         if (state.elevation == AltosLib.MISSING)
115                                 show("Missing");
116                         else
117                                 show("%3.0f°", state.elevation);
118                 }
119                 public Elevation (Container container, int y) {
120                         super (container, y, "Elevation", 1, false, 2);
121                 }
122         }
123
124         class FirmwareVersion extends AltosUIIndicator {
125                 public void show(AltosState state, AltosListenerState listener_state) {
126                         if (state.firmware_version == null)
127                                 show("Missing");
128                         else
129                                 show(state.firmware_version);
130                 }
131
132                 public FirmwareVersion(Container container, int y) {
133                         super(container, y, "Firmware Version", 1, false, 2);
134                 }
135         }
136
137         class FlightLogMax extends AltosUIIndicator {
138                 public void show(AltosState state, AltosListenerState listener_state) {
139                         int storage = state.flight_log_max;
140                         if (storage == AltosLib.MISSING)
141                                 storage = state.log_space >> 10;
142                         if (storage == AltosLib.MISSING)
143                                 show("Missing");
144                         else
145                                 show(String.format("%dkB", storage));
146                 }
147
148                 public FlightLogMax(Container container, int y) {
149                         super(container, y, "Flight Log Storage", 1, false, 2);
150                 }
151         }
152
153         class BatteryVoltage extends AltosUIVoltageIndicator {
154                 public double voltage(AltosState state) {
155                         return state.battery_voltage;
156                 }
157
158                 public double good() {
159                         return AltosLib.ao_battery_good;
160                 }
161
162                 public BatteryVoltage(Container container, int y) {
163                         super(container, y, "Battery Voltage", 2);
164                 }
165         }
166
167         class ReceiverBattery extends AltosUIVoltageIndicator {
168
169                 public double voltage(AltosState state) { return AltosLib.MISSING; }
170
171                 public double good() { return AltosLib.ao_battery_good; }
172
173                 public boolean hide(AltosState state, AltosListenerState listener_state, int i) {
174                         return value(state, listener_state, i) == AltosLib.MISSING;
175                 }
176
177                 public double value(AltosState state, AltosListenerState listener_state, int i) {
178                         if (listener_state == null)
179                                 return AltosLib.MISSING;
180                         return listener_state.battery;
181                 }
182
183                 public ReceiverBattery (AltosUIFlightTab container, int y) {
184                         super(container, y, "Receiver Battery", 2);
185                 }
186         }
187
188         public void labels(Container container, int y) {
189                 GridBagLayout           layout = (GridBagLayout)(container.getLayout());
190                 GridBagConstraints      c;
191
192                 cur = new JLabel("Current");
193                 cur.setFont(AltosUILib.label_font);
194                 c = new GridBagConstraints();
195                 c.gridx = 2; c.gridy = y;
196                 c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
197                 layout.setConstraints(cur, c);
198                 add(cur);
199
200                 max = new JLabel("Maximum");
201                 max.setFont(AltosUILib.label_font);
202                 c.gridx = 3; c.gridy = y;
203                 layout.setConstraints(max, c);
204                 add(max);
205         }
206
207         public void font_size_changed(int font_size) {
208                 cur.setFont(AltosUILib.label_font);
209                 max.setFont(AltosUILib.label_font);
210                 super.font_size_changed(font_size);
211         }
212
213         public String getName() {
214                 return "Status";
215         }
216
217         public TeleGPSState() {
218                 int y = 0;
219                 labels(this, y++);
220                 add(new Height(this, y++));
221                 add(new Speed(this, y++));
222                 add(new Distance(this, y++));
223                 add(new Range(this, y++));
224                 add(new Bearing(this, y++));
225                 add(new Elevation(this, y++));
226                 add(new FirmwareVersion(this, y++));
227                 add(new FlightLogMax(this, y++));
228                 add(new BatteryVoltage(this, y++));
229                 add(new ReceiverBattery(this, y++));
230         }
231 }