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