altosui: New AltosSerial.set_radio function sets channel/call
[fw/altos] / ao-tools / altosui / AltosFlightUI.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 AltosFlightUI extends JFrame implements AltosFlightDisplay {
32         String[] statusNames = { "Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
33         Object[][] statusData = { { "0", "pad", "-50", "0" } };
34
35         AltosVoice              voice;
36         AltosFlightReader       reader;
37         AltosDisplayThread      thread;
38
39         JTabbedPane     pane;
40
41         AltosPad        pad;
42         AltosAscent     ascent;
43         AltosDescent    descent;
44         AltosLanded     landed;
45         AltosSiteMap    sitemap;
46
47         private AltosFlightStatus flightStatus;
48         private AltosInfoTable flightInfo;
49
50         static final int tab_pad = 1;
51         static final int tab_ascent = 2;
52         static final int tab_descent = 3;
53         static final int tab_landed = 4;
54
55         int cur_tab = 0;
56
57         boolean exit_on_close = false;
58
59         int which_tab(AltosState state) {
60                 if (state.state < Altos.ao_flight_boost)
61                         return tab_pad;
62                 if (state.state <= Altos.ao_flight_coast)
63                         return tab_ascent;
64                 if (state.state <= Altos.ao_flight_main)
65                         return tab_descent;
66                 return tab_landed;
67         }
68
69         void stop_display() {
70                 if (thread != null && thread.isAlive()) {
71                         thread.interrupt();
72                         try {
73                                 thread.join();
74                         } catch (InterruptedException ie) {}
75                 }
76                 thread = null;
77         }
78
79         void disconnect() {
80                 stop_display();
81         }
82
83         public void reset() {
84                 pad.reset();
85                 ascent.reset();
86                 descent.reset();
87                 landed.reset();
88                 flightInfo.clear();
89                 sitemap.reset();
90         }
91
92         public void show(AltosState state, int crc_errors) {
93                 int     tab = which_tab(state);
94                 pad.show(state, crc_errors);
95                 ascent.show(state, crc_errors);
96                 descent.show(state, crc_errors);
97                 landed.show(state, crc_errors);
98                 if (tab != cur_tab) {
99                         switch (tab) {
100                         case tab_pad:
101                                 pane.setSelectedComponent(pad);
102                                 break;
103                         case tab_ascent:
104                                 pane.setSelectedComponent(ascent);
105                                 break;
106                         case tab_descent:
107                                 pane.setSelectedComponent(descent);
108                                 break;
109                         case tab_landed:
110                                 pane.setSelectedComponent(landed);
111                         }
112                         cur_tab = tab;
113                 }
114                 flightStatus.show(state, crc_errors);
115                 flightInfo.show(state, crc_errors);
116                 sitemap.show(state, crc_errors);
117         }
118
119         public void set_exit_on_close() {
120                 exit_on_close = true;
121         }
122
123         Container       bag;
124         JComboBox       channels;
125
126         public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
127                 AltosPreferences.init(this);
128
129                 voice = in_voice;
130                 reader = in_reader;
131
132                 bag = getContentPane();
133                 bag.setLayout(new GridBagLayout());
134
135                 GridBagConstraints c = new GridBagConstraints();
136
137                 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
138                 if (imgURL != null)
139                         setIconImage(new ImageIcon(imgURL).getImage());
140
141                 setTitle(String.format("AltOS %s", reader.name));
142
143                 /* Stick channel selector at top of table for telemetry monitoring */
144                 if (serial >= 0) {
145                         // Channel menu
146                         channels = new AltosChannelMenu(AltosPreferences.channel(serial));
147                         channels.addActionListener(new ActionListener() {
148                                 public void actionPerformed(ActionEvent e) {
149                                         int channel = channels.getSelectedIndex();
150                                         reader.set_channel(channel);
151                                 }
152                         });
153                         c.gridx = 0;
154                         c.gridy = 0;
155                         c.anchor = GridBagConstraints.WEST;
156                         bag.add (channels, c);
157                 }
158
159                 /* Flight status is always visible */
160                 flightStatus = new AltosFlightStatus();
161                 c.gridx = 0;
162                 c.gridy = 1;
163                 c.fill = GridBagConstraints.HORIZONTAL;
164                 c.weightx = 1;
165                 bag.add(flightStatus, c);
166
167                 /* The rest of the window uses a tabbed pane to
168                  * show one of the alternate data views
169                  */
170                 pane = new JTabbedPane();
171
172                 pad = new AltosPad();
173                 pane.add("Launch Pad", pad);
174
175                 ascent = new AltosAscent();
176                 pane.add("Ascent", ascent);
177
178                 descent = new AltosDescent();
179                 pane.add("Descent", descent);
180
181                 landed = new AltosLanded();
182                 pane.add("Landed", landed);
183
184                 flightInfo = new AltosInfoTable();
185                 pane.add("Table", new JScrollPane(flightInfo));
186
187                 sitemap = new AltosSiteMap();
188                 pane.add("Site Map", sitemap);
189
190                 /* Make the tabbed pane use the rest of the window space */
191                 c.gridx = 0;
192                 c.gridy = 2;
193                 c.fill = GridBagConstraints.BOTH;
194                 c.weightx = 1;
195                 c.weighty = 1;
196                 bag.add(pane, c);
197
198                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
199                 addWindowListener(new WindowAdapter() {
200                                 @Override
201                                 public void windowClosing(WindowEvent e) {
202                                         disconnect();
203                                         setVisible(false);
204                                         dispose();
205                                         if (exit_on_close)
206                                                 System.exit(0);
207                                 }
208                         });
209
210                 pack();
211                 setVisible(true);
212
213                 thread = new AltosDisplayThread(this, voice, this, reader);
214
215                 thread.start();
216         }
217
218         public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
219                 this(in_voice, in_reader, -1);
220         }
221 }