altos: Clean up serial initialization
[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 java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.LinkedBlockingQueue;
30
31 public class AltosFlightStatus extends JComponent implements AltosFlightDisplay {
32         GridBagLayout   layout;
33
34         public class FlightValue {
35                 JLabel          label;
36                 JTextField      value;
37
38                 void show(AltosState state, int crc_errors) {}
39
40                 void reset() {
41                         value.setText("");
42                 }
43                 public FlightValue (GridBagLayout layout, int x, String text) {
44                         GridBagConstraints      c = new GridBagConstraints();
45                         c.insets = new Insets(5, 5, 5, 5);
46                         c.anchor = GridBagConstraints.CENTER;
47                         c.fill = GridBagConstraints.BOTH;
48                         c.weightx = 1;
49                         c.weighty = 1;
50
51                         label = new JLabel(text);
52                         label.setFont(Altos.status_font);
53                         label.setHorizontalAlignment(SwingConstants.CENTER);
54                         c.gridx = x; c.gridy = 0;
55                         layout.setConstraints(label, c);
56                         add(label);
57
58                         value = new JTextField("");
59                         value.setFont(Altos.status_font);
60                         value.setHorizontalAlignment(SwingConstants.CENTER);
61                         c.gridx = x; c.gridy = 1;
62                         layout.setConstraints(value, c);
63                         add(value);
64                 }
65         }
66
67         class Call extends FlightValue {
68                 void show(AltosState state, int crc_errors) {
69                         value.setText(state.data.callsign);
70                 }
71                 public Call (GridBagLayout layout, int x) {
72                         super (layout, x, "Callsign");
73                 }
74         }
75
76         Call call;
77
78         class Serial extends FlightValue {
79                 void show(AltosState state, int crc_errors) {
80                         value.setText(String.format("%d", state.data.serial));
81                 }
82                 public Serial (GridBagLayout layout, int x) {
83                         super (layout, x, "Serial");
84                 }
85         }
86
87         Serial serial;
88
89         class Flight extends FlightValue {
90                 void show(AltosState state, int crc_errors) {
91                         value.setText(String.format("%d", state.data.flight));
92                 }
93                 public Flight (GridBagLayout layout, int x) {
94                         super (layout, x, "Flight");
95                 }
96         }
97
98         Flight flight;
99
100         class FlightState extends FlightValue {
101                 void show(AltosState state, int crc_errors) {
102                         value.setText(state.data.state());
103                 }
104                 public FlightState (GridBagLayout layout, int x) {
105                         super (layout, x, "State");
106                 }
107         }
108
109         FlightState flight_state;
110
111         class RSSI extends FlightValue {
112                 void show(AltosState state, int crc_errors) {
113                         value.setText(String.format("%d", state.data.rssi));
114                 }
115                 public RSSI (GridBagLayout layout, int x) {
116                         super (layout, x, "RSSI (dBm)");
117                 }
118         }
119
120         RSSI rssi;
121
122         public void reset () {
123                 call.reset();
124                 serial.reset();
125                 flight.reset();
126                 flight_state.reset();
127                 rssi.reset();
128         }
129
130         public void show (AltosState state, int crc_errors) {
131                 call.show(state, crc_errors);
132                 serial.show(state, crc_errors);
133                 flight.show(state, crc_errors);
134                 flight_state.show(state, crc_errors);
135                 rssi.show(state, crc_errors);
136         }
137
138         public int height() {
139                 Dimension d = layout.preferredLayoutSize(this);
140                 return d.height;
141         }
142
143         public AltosFlightStatus() {
144                 layout = new GridBagLayout();
145
146                 setLayout(layout);
147
148                 call = new Call(layout, 0);
149                 serial = new Serial(layout, 1);
150                 flight = new Flight(layout, 2);
151                 flight_state = new FlightState(layout, 3);
152                 rssi = new RSSI(layout, 4);
153         }
154 }