altosui: Show only supported telemetry version
[fw/altos] / 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.*;
30
31 class AltosFlightStatusUpdate implements ActionListener {
32
33         public AltosState       saved_state;
34         AltosFlightStatus       flightStatus;
35
36         public void actionPerformed (ActionEvent e) {
37                 if (saved_state != null)
38                         flightStatus.show(saved_state, 0);
39         }
40
41         public AltosFlightStatusUpdate (AltosFlightStatus in_flightStatus) {
42                 flightStatus = in_flightStatus;
43         }
44 }
45
46 public class AltosFlightUI extends AltosFrame implements AltosFlightDisplay, AltosFontListener {
47         AltosVoice              voice;
48         AltosFlightReader       reader;
49         AltosDisplayThread      thread;
50
51         JTabbedPane     pane;
52
53         AltosPad        pad;
54         AltosAscent     ascent;
55         AltosDescent    descent;
56         AltosLanded     landed;
57         AltosCompanionInfo      companion;
58         AltosSiteMap    sitemap;
59         boolean         has_map;
60         boolean         has_companion;
61
62         private AltosFlightStatus flightStatus;
63         private AltosInfoTable flightInfo;
64
65         boolean exit_on_close = false;
66
67         JComponent cur_tab = null;
68         JComponent which_tab(AltosState state) {
69                 if (state.state < Altos.ao_flight_boost)
70                         return pad;
71                 if (state.state <= Altos.ao_flight_coast)
72                         return ascent;
73                 if (state.state <= Altos.ao_flight_main)
74                         return descent;
75                 return landed;
76         }
77
78         void stop_display() {
79                 if (thread != null && thread.isAlive()) {
80                         thread.interrupt();
81                         try {
82                                 thread.join();
83                         } catch (InterruptedException ie) {}
84                 }
85                 thread = null;
86         }
87
88         void disconnect() {
89                 stop_display();
90         }
91
92         public void reset() {
93                 pad.reset();
94                 ascent.reset();
95                 descent.reset();
96                 landed.reset();
97                 flightInfo.clear();
98                 sitemap.reset();
99         }
100
101         public void set_font() {
102                 pad.set_font();
103                 ascent.set_font();
104                 descent.set_font();
105                 landed.set_font();
106                 flightStatus.set_font();
107                 flightInfo.set_font();
108                 sitemap.set_font();
109                 companion.set_font();
110         }
111
112         public void font_size_changed(int font_size) {
113                 set_font();
114         }
115
116
117         AltosFlightStatusUpdate status_update;
118
119         public void show(AltosState state, int crc_errors) {
120                 status_update.saved_state = state;
121                 JComponent tab = which_tab(state);
122                 try {
123                 pad.show(state, crc_errors);
124                 ascent.show(state, crc_errors);
125                 descent.show(state, crc_errors);
126                 landed.show(state, crc_errors);
127                 if (tab != cur_tab) {
128                         if (cur_tab == pane.getSelectedComponent()) {
129                                 pane.setSelectedComponent(tab);
130                         }
131                         cur_tab = tab;
132                 }
133                 flightStatus.show(state, crc_errors);
134                 flightInfo.show(state, crc_errors);
135
136                 if (state.data.companion != null) {
137                         if (!has_companion) {
138                                 pane.add("Companion", companion);
139                                 has_companion= true;
140                         }
141                         companion.show(state, crc_errors);
142                 } else {
143                         if (has_companion) {
144                                 pane.remove(companion);
145                                 has_companion = false;
146                         }
147                 }
148                 if (state.gps != null && state.gps.connected) {
149                         if (!has_map) {
150                                 pane.add("Site Map", sitemap);
151                                 has_map = true;
152                         }
153                         sitemap.show(state, crc_errors);
154                 } else {
155                         if (has_map) {
156                                 pane.remove(sitemap);
157                                 has_map = false;
158                         }
159                 }
160                 } catch (Exception e) {
161                         System.out.print("Show exception" + e);
162                 }
163         }
164
165         public void set_exit_on_close() {
166                 exit_on_close = true;
167         }
168
169         Container       bag;
170         AltosFreqList   frequencies;
171         JComboBox       telemetries;
172         JLabel          telemetry;
173
174         ActionListener  show_timer;
175
176         public AltosFlightUI(AltosVoice in_voice, AltosFlightReader in_reader, final int serial) {
177                 AltosPreferences.set_component(this);
178
179                 voice = in_voice;
180                 reader = in_reader;
181
182                 bag = getContentPane();
183                 bag.setLayout(new GridBagLayout());
184
185                 GridBagConstraints c = new GridBagConstraints();
186
187                 java.net.URL imgURL = AltosUI.class.getResource("/altus-metrum-16x16.jpg");
188                 if (imgURL != null)
189                         setIconImage(new ImageIcon(imgURL).getImage());
190
191                 setTitle(String.format("AltOS %s", reader.name));
192
193                 /* Stick channel selector at top of table for telemetry monitoring */
194                 if (serial >= 0) {
195                         // Channel menu
196                         frequencies = new AltosFreqList(AltosPreferences.frequency(serial));
197                         frequencies.set_product("Monitor");
198                         frequencies.set_serial(serial);
199                         frequencies.addActionListener(new ActionListener() {
200                                         public void actionPerformed(ActionEvent e) {
201                                                 double frequency = frequencies.frequency();
202                                                 try {
203                                                         reader.set_frequency(frequency);
204                                                 } catch (TimeoutException te) {
205                                                 } catch (InterruptedException ie) {
206                                                 }
207                                                 reader.save_frequency();
208                                         }
209                         });
210                         c.gridx = 0;
211                         c.gridy = 0;
212                         c.weightx = 0;
213                         c.weighty = 0;
214                         c.insets = new Insets(3, 3, 3, 3);
215                         c.fill = GridBagConstraints.NONE;
216                         c.anchor = GridBagConstraints.WEST;
217                         bag.add (frequencies, c);
218
219                         // Telemetry format menu
220                         if (reader.supports_telemetry(Altos.ao_telemetry_standard)) {
221                                 telemetries = new JComboBox();
222                                 for (int i = 1; i <= Altos.ao_telemetry_max; i++) 
223                                         telemetries.addItem(Altos.telemetry_name(i));
224                                 int telemetry = AltosPreferences.telemetry(serial);
225                                 if (telemetry <= Altos.ao_telemetry_off ||
226                                     telemetry > Altos.ao_telemetry_max)
227                                         telemetry = Altos.ao_telemetry_standard;
228                                 telemetries.setSelectedIndex(telemetry - 1);
229                                 telemetries.setMaximumRowCount(Altos.ao_telemetry_max);
230                                 telemetries.setPreferredSize(null);
231                                 telemetries.revalidate();
232                                 telemetries.addActionListener(new ActionListener() {
233                                                 public void actionPerformed(ActionEvent e) {
234                                                         int telemetry = telemetries.getSelectedIndex() + 1;
235                                                         reader.set_telemetry(telemetry);
236                                                         reader.save_telemetry();
237                                                 }
238                                         });
239                                 c.gridx = 1;
240                                 c.gridy = 0;
241                                 c.weightx = 0;
242                                 c.weighty = 0;
243                                 c.fill = GridBagConstraints.NONE;
244                                 c.anchor = GridBagConstraints.WEST;
245                                 bag.add (telemetries, c);
246                                 c.insets = new Insets(0, 0, 0, 0);
247                         } else {
248                                 String  version;
249
250                                 if (reader.supports_telemetry(Altos.ao_telemetry_0_9))
251                                         version = "Telemetry: 0.9";
252                                 else if (reader.supports_telemetry(Altos.ao_telemetry_0_8))
253                                         version = "Telemetry: 0.8";
254                                 else
255                                         version = "Telemetry: None";
256
257                                 telemetry = new JLabel(version);
258                                 c.gridx = 1;
259                                 c.gridy = 0;
260                                 c.weightx = 0;
261                                 c.weighty = 0;
262                                 c.fill = GridBagConstraints.NONE;
263                                 c.anchor = GridBagConstraints.WEST;
264                                 bag.add (telemetry, c);
265                                 c.insets = new Insets(0, 0, 0, 0);
266                         }
267                 }
268
269                 /* Flight status is always visible */
270                 flightStatus = new AltosFlightStatus();
271                 c.gridx = 0;
272                 c.gridy = 1;
273                 c.fill = GridBagConstraints.HORIZONTAL;
274                 c.weightx = 1;
275                 c.gridwidth = 2;
276                 bag.add(flightStatus, c);
277                 c.gridwidth = 1;
278
279                 /* The rest of the window uses a tabbed pane to
280                  * show one of the alternate data views
281                  */
282                 pane = new JTabbedPane();
283
284                 pad = new AltosPad();
285                 pane.add("Launch Pad", pad);
286
287                 ascent = new AltosAscent();
288                 pane.add("Ascent", ascent);
289
290                 descent = new AltosDescent();
291                 pane.add("Descent", descent);
292
293                 landed = new AltosLanded(reader);
294                 pane.add("Landed", landed);
295
296                 flightInfo = new AltosInfoTable();
297                 pane.add("Table", new JScrollPane(flightInfo));
298
299                 companion = new AltosCompanionInfo();
300                 has_companion = false;
301
302                 sitemap = new AltosSiteMap();
303                 has_map = false;
304
305                 /* Make the tabbed pane use the rest of the window space */
306                 c.gridx = 0;
307                 c.gridy = 2;
308                 c.fill = GridBagConstraints.BOTH;
309                 c.weightx = 1;
310                 c.weighty = 1;
311                 c.gridwidth = 2;
312                 bag.add(pane, c);
313
314                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
315
316                 AltosPreferences.register_font_listener(this);
317
318                 addWindowListener(new WindowAdapter() {
319                                 @Override
320                                 public void windowClosing(WindowEvent e) {
321                                         disconnect();
322                                         setVisible(false);
323                                         dispose();
324                                         AltosPreferences.unregister_font_listener(AltosFlightUI.this);
325                                         if (exit_on_close)
326                                                 System.exit(0);
327                                 }
328                         });
329
330                 pack();
331                 setVisible(true);
332
333                 thread = new AltosDisplayThread(this, voice, this, reader);
334
335                 status_update = new AltosFlightStatusUpdate(flightStatus);
336
337                 new javax.swing.Timer(100, status_update).start();
338
339                 thread.start();
340         }
341
342         public AltosFlightUI (AltosVoice in_voice, AltosFlightReader in_reader) {
343                 this(in_voice, in_reader, -1);
344         }
345 }