telegps: Add status tab
[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_4.*;
25 import org.altusmetrum.altosuilib_2.*;
26
27 public class TeleGPSState extends JComponent implements AltosFlightDisplay, HierarchyListener {
28         GridBagLayout                   layout;
29         JLabel                          cur, max;
30
31         private AltosState              last_state;
32         private AltosListenerState      last_listener_state;
33
34         abstract class Value extends AltosUIUnitsIndicator {
35                 public Value (Container container, int y, AltosUnits units, String text) {
36                         super(container, y, units, text, 1, false, 2);
37                 }
38         }
39
40         abstract class DualValue extends AltosUIUnitsIndicator {
41                 public DualValue (Container container, int y, AltosUnits units, String text) {
42                         super(container, y, units, text, 2, false, 1);
43                 }
44         }
45
46         abstract class ValueHold extends DualValue {
47                 public void reset() {
48                         super.reset();
49                         last_values[1] = AltosLib.MISSING;
50                 }
51                 public ValueHold (Container container, int y, AltosUnits units, String text) {
52                         super(container, y, units, text);
53                 }
54         }
55
56         class Height extends ValueHold {
57                 public double value(AltosState state, int i) {
58                         if (i == 0)
59                                 return state.height();
60                         else
61                                 return state.max_height();
62                 }
63
64                 public Height(Container container, int y) {
65                         super(container, y, AltosConvert.height, "Height");
66                 }
67         }
68
69         class Speed extends ValueHold {
70                 public double value(AltosState state, int i) {
71                         if (i == 0)
72                                 return state.gps_speed();
73                         else
74                                 return state.max_gps_speed();
75                 }
76
77                 public Speed(Container container, int y) {
78                         super(container, y, AltosConvert.speed, "Speed");
79                 }
80         }
81
82         class Distance extends Value {
83                 public double value(AltosState state, int i) {
84                         if (state.from_pad != null)
85                                 return state.from_pad.distance;
86                         else
87                                 return AltosLib.MISSING;
88                 }
89
90                 public Distance(Container container, int y) {
91                         super(container, y, AltosConvert.distance, "Distance");
92                 }
93         }
94
95         class Range extends Value {
96                 public double value(AltosState state, int i) {
97                         return state.range;
98                 }
99                 public Range (Container container, int y) {
100                         super (container, y, AltosConvert.distance, "Range");
101                 }
102         }
103
104         class Bearing extends AltosUIIndicator {
105                 public void show (AltosState state, AltosListenerState listener_state) {
106                         if (state.from_pad != null) {
107                                 show( String.format("%3.0f°", state.from_pad.bearing),
108                                       state.from_pad.bearing_words(
109                                               AltosGreatCircle.BEARING_LONG));
110                         } else {
111                                 show("???", "???");
112                         }
113                 }
114                 public Bearing (Container container, int y) {
115                         super (container, y, "Bearing", 2, false, 1);
116                 }
117         }
118
119         class Elevation extends AltosUIIndicator {
120                 public void show (AltosState state, AltosListenerState listener_state) {
121                         show("%3.0f°", state.elevation);
122                 }
123                 public Elevation (Container container, int y) {
124                         super (container, y, "Elevation", 1, false, 2);
125                 }
126         }
127
128         class FirmwareVersion extends AltosUIIndicator {
129                 public void show(AltosState state, AltosListenerState listener_state) {
130                         if (state.firmware_version == null)
131                                 show("Missing");
132                         else
133                                 show(state.firmware_version);
134                 }
135
136                 public FirmwareVersion(Container container, int y) {
137                         super(container, y, "Firmware Version", 1, false, 2);
138                 }
139         }
140
141         class FlightLogMax extends AltosUIIndicator {
142                 public void show(AltosState state, AltosListenerState listener_state) {
143                         if (state.flight_log_max == AltosLib.MISSING)
144                                 show("Missing");
145                         else
146                                 show(String.format("%dkB", state.flight_log_max));
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         LinkedList<AltosUIIndicator> indicators = new LinkedList<AltosUIIndicator>();
169
170         public void labels(Container container, int y) {
171                 GridBagLayout           layout = (GridBagLayout)(container.getLayout());
172                 GridBagConstraints      c;
173
174                 cur = new JLabel("Current");
175                 cur.setFont(AltosUILib.label_font);
176                 c = new GridBagConstraints();
177                 c.gridx = 2; c.gridy = y;
178                 c.insets = new Insets(AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad, AltosUILib.tab_elt_pad);
179                 layout.setConstraints(cur, c);
180                 add(cur);
181
182                 max = new JLabel("Maximum");
183                 max.setFont(AltosUILib.label_font);
184                 c.gridx = 3; c.gridy = y;
185                 layout.setConstraints(max, c);
186                 add(max);
187         }
188
189         public void reset() {
190                 for (AltosUIIndicator i : indicators)
191                         i.reset();
192         }
193
194         public void font_size_changed(int font_size) {
195                 for (AltosUIIndicator i : indicators)
196                         i.font_size_changed(font_size);
197         }
198
199         public void units_changed(boolean imperial_units) {
200                 for (AltosUIIndicator i : indicators)
201                         i.units_changed(imperial_units);
202         }
203
204         public void show(AltosState state, AltosListenerState listener_state) {
205                 if (!isShowing()) {
206                         last_state = state;
207                         last_listener_state = listener_state;
208                         return;
209                 }
210
211                 for (AltosUIIndicator i : indicators)
212                         i.show(state, listener_state);
213         }
214
215         public String getName() {
216                 return "Status";
217         }
218
219         public void hierarchyChanged(HierarchyEvent e) {
220                 if (last_state != null && isShowing()) {
221                         AltosState              state = last_state;
222                         AltosListenerState      listener_state = last_listener_state;
223
224                         last_state = null;
225                         last_listener_state = null;
226                         show(state, listener_state);
227                 }
228         }
229
230         public TeleGPSState() {
231                 layout = new GridBagLayout();
232
233                 setLayout(layout);
234
235                 /* Elements in state display:
236                  *
237                  * config_version;
238                  * lon
239                  * height
240                  */
241                 int y = 0;
242                 labels(this, y++);
243                 indicators.add(new Height(this, y++));
244                 indicators.add(new Speed(this, y++));
245                 indicators.add(new Distance(this, y++));
246                 indicators.add(new Range(this, y++));
247                 indicators.add(new Bearing(this, y++));
248                 indicators.add(new Elevation(this, y++));
249                 indicators.add(new FirmwareVersion(this, y++));
250                 indicators.add(new FlightLogMax(this, y++));
251                 indicators.add(new BatteryVoltage(this, y++));
252                 addHierarchyListener(this);
253         }
254 }