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