doc: Document flight computer wiring connections
[fw/altos] / altosui / AltosFlightStatus.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 altosui;
19
20 import java.awt.*;
21 import javax.swing.*;
22 import org.altusmetrum.altoslib_3.*;
23
24 public class AltosFlightStatus extends JComponent implements AltosFlightDisplay {
25         GridBagLayout   layout;
26
27         public class FlightValue {
28                 JLabel          label;
29                 JTextField      value;
30
31                 void show(AltosState state, AltosListenerState listener_state) {}
32
33                 void reset() {
34                         value.setText("");
35                 }
36
37                 void set_font() {
38                         label.setFont(Altos.status_font);
39                         value.setFont(Altos.status_font);
40                 }
41
42                 public FlightValue (GridBagLayout layout, int x, String text) {
43                         GridBagConstraints      c = new GridBagConstraints();
44                         c.insets = new Insets(5, 5, 5, 5);
45                         c.anchor = GridBagConstraints.CENTER;
46                         c.fill = GridBagConstraints.BOTH;
47                         c.weightx = 1;
48                         c.weighty = 1;
49
50                         label = new JLabel(text);
51                         label.setFont(Altos.status_font);
52                         label.setHorizontalAlignment(SwingConstants.CENTER);
53                         c.gridx = x; c.gridy = 0;
54                         layout.setConstraints(label, c);
55                         add(label);
56
57                         value = new JTextField("");
58                         value.setFont(Altos.status_font);
59                         value.setHorizontalAlignment(SwingConstants.CENTER);
60                         c.gridx = x; c.gridy = 1;
61                         layout.setConstraints(value, c);
62                         add(value);
63                 }
64         }
65
66         class Call extends FlightValue {
67                 void show(AltosState state, AltosListenerState listener_state) {
68                         value.setText(state.callsign);
69                 }
70                 public Call (GridBagLayout layout, int x) {
71                         super (layout, x, "Callsign");
72                 }
73         }
74
75         Call call;
76
77         class Serial extends FlightValue {
78                 void show(AltosState state, AltosListenerState listener_state) {
79                         if (state.serial == AltosLib.MISSING)
80                                 value.setText("none");
81                         else
82                                 value.setText(String.format("%d", state.serial));
83                 }
84                 public Serial (GridBagLayout layout, int x) {
85                         super (layout, x, "Serial");
86                 }
87         }
88
89         Serial serial;
90
91         class Flight extends FlightValue {
92                 void show(AltosState state, AltosListenerState listener_state) {
93                         if (state.flight == AltosLib.MISSING)
94                                 value.setText("none");
95                         else
96                                 value.setText(String.format("%d", state.flight));
97                 }
98                 public Flight (GridBagLayout layout, int x) {
99                         super (layout, x, "Flight");
100                 }
101         }
102
103         Flight flight;
104
105         class FlightState extends FlightValue {
106                 void show(AltosState state, AltosListenerState listener_state) {
107                         value.setText(state.state_name());
108                 }
109                 public FlightState (GridBagLayout layout, int x) {
110                         super (layout, x, "State");
111                 }
112         }
113
114         FlightState flight_state;
115
116         class RSSI extends FlightValue {
117                 void show(AltosState state, AltosListenerState listener_state) {
118                         value.setText(String.format("%d", state.rssi()));
119                 }
120                 public RSSI (GridBagLayout layout, int x) {
121                         super (layout, x, "RSSI");
122                 }
123         }
124
125         RSSI rssi;
126
127         class LastPacket extends FlightValue {
128                 void show(AltosState state, AltosListenerState listener_state) {
129                         long secs = (System.currentTimeMillis() - state.received_time + 500) / 1000;
130                         value.setText(String.format("%d", secs));
131                 }
132                 public LastPacket(GridBagLayout layout, int x) {
133                         super (layout, x, "Age");
134                 }
135         }
136
137         LastPacket last_packet;
138
139         public void reset () {
140                 call.reset();
141                 serial.reset();
142                 flight.reset();
143                 flight_state.reset();
144                 rssi.reset();
145                 last_packet.reset();
146         }
147
148         public void set_font () {
149                 call.set_font();
150                 serial.set_font();
151                 flight.set_font();
152                 flight_state.set_font();
153                 rssi.set_font();
154                 last_packet.set_font();
155         }
156
157         public void show (AltosState state, AltosListenerState listener_state) {
158                 call.show(state, listener_state);
159                 serial.show(state, listener_state);
160                 flight.show(state, listener_state);
161                 flight_state.show(state, listener_state);
162                 rssi.show(state, listener_state);
163                 last_packet.show(state, listener_state);
164         }
165
166         public int height() {
167                 Dimension d = layout.preferredLayoutSize(this);
168                 return d.height;
169         }
170
171         public AltosFlightStatus() {
172                 layout = new GridBagLayout();
173
174                 setLayout(layout);
175
176                 call = new Call(layout, 0);
177                 serial = new Serial(layout, 1);
178                 flight = new Flight(layout, 2);
179                 flight_state = new FlightState(layout, 3);
180                 rssi = new RSSI(layout, 4);
181                 last_packet = new LastPacket(layout, 5);
182         }
183 }