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