cc9966b859f7f2efac601eea1817543f267c204d
[fw/altos] / telegps / TeleGPSStatus.java
1 /*
2  * Copyright © 2010 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.awt.*;
21 import javax.swing.*;
22 import org.altusmetrum.altoslib_7.*;
23 import org.altusmetrum.altosuilib_7.*;
24
25 public class TeleGPSStatus extends JComponent implements AltosFlightDisplay {
26         GridBagLayout   layout;
27
28         public class Value {
29                 JLabel          label;
30                 JTextField      value;
31
32                 void show(AltosState state, AltosListenerState listener_state) {}
33
34                 void reset() {
35                         value.setText("");
36                 }
37
38                 void set_font() {
39                         label.setFont(AltosUILib.status_font);
40                         value.setFont(AltosUILib.status_font);
41                 }
42
43                 void setVisible(boolean visible) {
44                         label.setVisible(visible);
45                         value.setVisible(visible);
46                 }
47
48                 public Value (GridBagLayout layout, int x, String text) {
49                         GridBagConstraints      c = new GridBagConstraints();
50                         c.insets = new Insets(5, 5, 5, 5);
51                         c.anchor = GridBagConstraints.CENTER;
52                         c.fill = GridBagConstraints.BOTH;
53                         c.weightx = 1;
54                         c.weighty = 1;
55
56                         label = new JLabel(text);
57                         label.setFont(AltosUILib.status_font);
58                         label.setHorizontalAlignment(SwingConstants.CENTER);
59                         c.gridx = x; c.gridy = 0;
60                         layout.setConstraints(label, c);
61                         add(label);
62
63                         value = new JTextField("");
64                         value.setEditable(false);
65                         value.setFont(AltosUILib.status_font);
66                         value.setHorizontalAlignment(SwingConstants.CENTER);
67                         c.gridx = x; c.gridy = 1;
68                         layout.setConstraints(value, c);
69                         add(value);
70                 }
71         }
72
73         class Call extends Value {
74                 String  call;
75
76                 void show(AltosState state, AltosListenerState listener_state) {
77                         if (state.callsign != call) {
78                                 value.setText(state.callsign);
79                                 call = state.callsign;
80                         }
81                         if (state.callsign == null)
82                                 setVisible(false);
83                         else
84                                 setVisible(true);
85                 }
86
87                 public void reset() {
88                         super.reset();
89                         call = "";
90                 }
91
92                 public Call (GridBagLayout layout, int x) {
93                         super (layout, x, "Callsign");
94                 }
95         }
96
97         Call call;
98
99         class Serial extends Value {
100                 int     serial = -1;
101                 void show(AltosState state, AltosListenerState listener_state) {
102                         if (state.serial != serial) {
103                                 if (state.serial == AltosLib.MISSING)
104                                         value.setText("none");
105                                 else
106                                         value.setText(String.format("%d", state.serial));
107                                 serial = state.serial;
108                         }
109                 }
110
111                 public void reset() {
112                         super.reset();
113                         serial = -1;
114                 }
115
116                 public Serial (GridBagLayout layout, int x) {
117                         super (layout, x, "Serial");
118                 }
119         }
120
121         Serial serial;
122
123         class Flight extends Value {
124
125                 int     last_flight = -1;
126
127                 void show(AltosState state, AltosListenerState listener_state) {
128                         if (state.flight != last_flight) {
129                                 if (state.flight == AltosLib.MISSING)
130                                         value.setText("none");
131                                 else
132                                         value.setText(String.format("%d", state.flight));
133                                 last_flight = state.flight;
134                         }
135                 }
136
137                 public void reset() {
138                         super.reset();
139                         last_flight = -1;
140                 }
141
142                 public Flight (GridBagLayout layout, int x) {
143                         super (layout, x, "Flight");
144                 }
145         }
146
147         Flight flight;
148
149         class RSSI extends Value {
150                 int     rssi = 10000;
151
152                 void show(AltosState state, AltosListenerState listener_state) {
153                         int     new_rssi = state.rssi();
154
155                         if (new_rssi != rssi) {
156                                 value.setText(String.format("%d", new_rssi));
157                                 if (state.rssi == AltosLib.MISSING)
158                                         setVisible(false);
159                                 else
160                                         setVisible(true);
161                                 rssi = new_rssi;
162                         }
163                 }
164
165                 public void reset() {
166                         super.reset();
167                         rssi = 10000;
168                 }
169
170                 public RSSI (GridBagLayout layout, int x) {
171                         super (layout, x, "RSSI");
172                 }
173         }
174
175         RSSI rssi;
176
177         class LastPacket extends Value {
178
179                 long    last_secs = -1;
180
181                 void show(AltosState state, AltosListenerState listener_state) {
182                         if (listener_state.running) {
183                                 long secs = (System.currentTimeMillis() - state.received_time + 500) / 1000;
184
185                                 if (secs != last_secs) {
186                                         value.setText(String.format("%d", secs));
187                                         last_secs = secs;
188                                 }
189                         } else {
190                                 value.setText("done");
191                         }
192                 }
193
194                 void reset() {
195                         super.reset();
196                         last_secs = -1;
197                 }
198
199                 void disable() {
200                         value.setText("");
201                 }
202
203                 public LastPacket(GridBagLayout layout, int x) {
204                         super (layout, x, "Age");
205                 }
206         }
207
208         LastPacket last_packet;
209
210         public void disable_receive() {
211                 last_packet.disable();
212         }
213
214         public void reset () {
215                 call.reset();
216                 serial.reset();
217                 flight.reset();
218                 rssi.reset();
219                 last_packet.reset();
220         }
221
222         public void font_size_changed(int font_size) {
223                 call.set_font();
224                 serial.set_font();
225                 flight.set_font();
226                 rssi.set_font();
227                 last_packet.set_font();
228         }
229
230         public void units_changed(boolean imperial_units) {
231         }
232
233         public void show (AltosState state, AltosListenerState listener_state) {
234                 call.show(state, listener_state);
235                 serial.show(state, listener_state);
236                 flight.show(state, listener_state);
237                 rssi.show(state, listener_state);
238                 last_packet.show(state, listener_state);
239                 if (!listener_state.running)
240                         stop();
241         }
242
243         public int height() {
244                 Dimension d = layout.preferredLayoutSize(this);
245                 return d.height;
246         }
247
248         TeleGPSStatusUpdate     status_update;
249         javax.swing.Timer       timer;
250
251         public void start(TeleGPSStatusUpdate status_update) {
252                 this.status_update = status_update;
253                 timer = new javax.swing.Timer(100, status_update);
254                 timer.start();
255         }
256
257         public void stop() {
258                 if (timer != null) {
259                         timer.stop();
260                         timer = null;
261                 }
262         }
263
264         public TeleGPSStatus() {
265                 layout = new GridBagLayout();
266
267                 setLayout(layout);
268
269                 call = new Call(layout, 0);
270                 serial = new Serial(layout, 1);
271                 flight = new Flight(layout, 2);
272                 rssi = new RSSI(layout, 4);
273                 last_packet = new LastPacket(layout, 5);
274         }
275 }