565866ea1f61a582065d2326cefa20e4d577580f
[fw/altos] / ao-tools / altosui / AltosUI.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 import altosui.Altos;
32 import altosui.AltosSerial;
33 import altosui.AltosSerialMonitor;
34 import altosui.AltosRecord;
35 import altosui.AltosTelemetry;
36 import altosui.AltosState;
37 import altosui.AltosDeviceDialog;
38 import altosui.AltosPreferences;
39 import altosui.AltosLog;
40 import altosui.AltosVoice;
41 import altosui.AltosFlightStatusTableModel;
42 import altosui.AltosFlightInfoTableModel;
43 import altosui.AltosChannelMenu;
44 import altosui.AltosFlash;
45
46 import libaltosJNI.*;
47
48 public class AltosUI extends JFrame {
49         private int channel = -1;
50
51         private AltosFlightStatusTableModel flightStatusModel;
52         private JTable flightStatus;
53
54         static final int info_columns = 3;
55
56         private AltosFlightInfoTableModel[] flightInfoModel;
57         private JTable[] flightInfo;
58         private AltosSerial serial_line;
59         private AltosLog altos_log;
60         private Box[] ibox;
61         private Box vbox;
62         private Box hbox;
63
64         private Font statusFont = new Font("SansSerif", Font.BOLD, 24);
65         private Font infoLabelFont = new Font("SansSerif", Font.PLAIN, 14);
66         private Font infoValueFont = new Font("Monospaced", Font.PLAIN, 14);
67
68         public AltosVoice voice = new AltosVoice();
69
70         public AltosUI() {
71
72                 String[] statusNames = { "Height (m)", "State", "RSSI (dBm)", "Speed (m/s)" };
73                 Object[][] statusData = { { "0", "pad", "-50", "0" } };
74
75                 AltosPreferences.init(this);
76
77                 vbox = Box.createVerticalBox();
78                 this.add(vbox);
79
80                 flightStatusModel = new AltosFlightStatusTableModel();
81                 flightStatus = new JTable(flightStatusModel);
82                 flightStatus.setFont(statusFont);
83                 TableColumnModel tcm = flightStatus.getColumnModel();
84                 for (int i = 0; i < flightStatusModel.getColumnCount(); i++) {
85                         DefaultTableCellRenderer       r = new DefaultTableCellRenderer();
86                         r.setFont(statusFont);
87                         r.setHorizontalAlignment(SwingConstants.CENTER);
88                         tcm.getColumn(i).setCellRenderer(r);
89                 }
90
91                 FontMetrics     statusMetrics = flightStatus.getFontMetrics(statusFont);
92                 int statusHeight = (statusMetrics.getHeight() + statusMetrics.getLeading()) * 15 / 10;
93                 flightStatus.setRowHeight(statusHeight);
94                 flightStatus.setShowGrid(false);
95
96                 vbox.add(flightStatus);
97
98                 hbox = Box.createHorizontalBox();
99                 vbox.add(hbox);
100
101                 flightInfo = new JTable[3];
102                 flightInfoModel = new AltosFlightInfoTableModel[3];
103                 ibox = new Box[3];
104                 FontMetrics     infoValueMetrics = flightStatus.getFontMetrics(infoValueFont);
105                 int infoHeight = (infoValueMetrics.getHeight() + infoValueMetrics.getLeading()) * 20 / 10;
106
107                 for (int i = 0; i < info_columns; i++) {
108                         ibox[i] = Box.createVerticalBox();
109                         flightInfoModel[i] = new AltosFlightInfoTableModel();
110                         flightInfo[i] = new JTable(flightInfoModel[i]);
111                         flightInfo[i].setFont(infoValueFont);
112                         flightInfo[i].setRowHeight(infoHeight);
113                         flightInfo[i].setShowGrid(true);
114                         ibox[i].add(flightInfo[i].getTableHeader());
115                         ibox[i].add(flightInfo[i]);
116                         hbox.add(ibox[i]);
117                 }
118
119                 setTitle("AltOS");
120
121                 createMenu();
122
123                 serial_line = new AltosSerial();
124                 altos_log = new AltosLog(serial_line);
125                 int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
126                 this.setSize(new Dimension (infoValueMetrics.charWidth('0') * 6 * 20,
127                                             statusHeight * 4 + infoHeight * 17));
128                 this.validate();
129                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
130                 addWindowListener(new WindowAdapter() {
131                         @Override
132                         public void windowClosing(WindowEvent e) {
133                                 System.exit(0);
134                         }
135                 });
136                 voice.speak("Rocket flight monitor ready.");
137         }
138
139         public void info_reset() {
140                 for (int i = 0; i < info_columns; i++)
141                         flightInfoModel[i].resetRow();
142         }
143
144         public void info_add_row(int col, String name, String value) {
145                 flightInfoModel[col].addRow(name, value);
146         }
147
148         public void info_add_row(int col, String name, String format, Object... parameters) {
149                 flightInfoModel[col].addRow(name, String.format(format, parameters));
150         }
151
152         public void info_add_deg(int col, String name, double v, int pos, int neg) {
153                 int     c = pos;
154                 if (v < 0) {
155                         c = neg;
156                         v = -v;
157                 }
158                 double  deg = Math.floor(v);
159                 double  min = (v - deg) * 60;
160
161                 flightInfoModel[col].addRow(name, String.format("%3.0f°%08.5f'", deg, min));
162         }
163
164         public void info_finish() {
165                 for (int i = 0; i < info_columns; i++)
166                         flightInfoModel[i].finish();
167         }
168
169         public void show(AltosState state) {
170                 flightStatusModel.set(state);
171
172                 info_reset();
173                 if (state.gps_ready)
174                         info_add_row(0, "Ground state", "%s", "ready");
175                 else
176                         info_add_row(0, "Ground state", "wait (%d)",
177                                      state.gps_waiting);
178                 info_add_row(0, "Rocket state", "%s", state.data.state());
179                 info_add_row(0, "Callsign", "%s", state.data.callsign);
180                 info_add_row(0, "Rocket serial", "%6d", state.data.serial);
181                 info_add_row(0, "Rocket flight", "%6d", state.data.flight);
182
183                 info_add_row(0, "RSSI", "%6d    dBm", state.data.rssi);
184                 info_add_row(0, "Height", "%6.0f    m", state.height);
185                 info_add_row(0, "Max height", "%6.0f    m", state.max_height);
186                 info_add_row(0, "Acceleration", "%8.1f  m/s²", state.acceleration);
187                 info_add_row(0, "Max acceleration", "%8.1f  m/s²", state.max_acceleration);
188                 info_add_row(0, "Speed", "%8.1f  m/s", state.ascent ? state.speed : state.baro_speed);
189                 info_add_row(0, "Max Speed", "%8.1f  m/s", state.max_speed);
190                 info_add_row(0, "Temperature", "%9.2f °C", state.temperature);
191                 info_add_row(0, "Battery", "%9.2f V", state.battery);
192                 info_add_row(0, "Drogue", "%9.2f V", state.drogue_sense);
193                 info_add_row(0, "Main", "%9.2f V", state.main_sense);
194                 info_add_row(0, "Pad altitude", "%6.0f    m", state.ground_altitude);
195                 if (state.gps == null) {
196                         info_add_row(1, "GPS", "not available");
197                 } else {
198                         if (state.data.gps.locked)
199                                 info_add_row(1, "GPS", "   locked");
200                         else if (state.data.gps.connected)
201                                 info_add_row(1, "GPS", " unlocked");
202                         else
203                                 info_add_row(1, "GPS", "  missing");
204                         info_add_row(1, "Satellites", "%6d", state.data.gps.nsat);
205                         info_add_deg(1, "Latitude", state.gps.lat, 'N', 'S');
206                         info_add_deg(1, "Longitude", state.gps.lon, 'E', 'W');
207                         info_add_row(1, "GPS altitude", "%6d", state.gps.alt);
208                         info_add_row(1, "GPS height", "%6.0f", state.gps_height);
209
210                         /* The SkyTraq GPS doesn't report these values */
211                         if (false) {
212                                 info_add_row(1, "GPS ground speed", "%8.1f m/s %3d°",
213                                              state.gps.ground_speed,
214                                              state.gps.course);
215                                 info_add_row(1, "GPS climb rate", "%8.1f m/s",
216                                              state.gps.climb_rate);
217                                 info_add_row(1, "GPS error", "%6d m(h)%3d m(v)",
218                                              state.gps.h_error, state.gps.v_error);
219                         }
220                         info_add_row(1, "GPS hdop", "%8.1f", state.gps.hdop);
221
222                         if (state.npad > 0) {
223                                 if (state.from_pad != null) {
224                                         info_add_row(1, "Distance from pad", "%6.0f m", state.from_pad.distance);
225                                         info_add_row(1, "Direction from pad", "%6.0f°", state.from_pad.bearing);
226                                 } else {
227                                         info_add_row(1, "Distance from pad", "unknown");
228                                         info_add_row(1, "Direction from pad", "unknown");
229                                 }
230                                 info_add_deg(1, "Pad latitude", state.pad_lat, 'N', 'S');
231                                 info_add_deg(1, "Pad longitude", state.pad_lon, 'E', 'W');
232                                 info_add_row(1, "Pad GPS alt", "%6.0f m", state.pad_alt);
233                         }
234                         info_add_row(1, "GPS date", "%04d-%02d-%02d",
235                                        state.gps.year,
236                                        state.gps.month,
237                                        state.gps.day);
238                         info_add_row(1, "GPS time", "  %02d:%02d:%02d",
239                                        state.gps.hour,
240                                        state.gps.minute,
241                                        state.gps.second);
242                         int     nsat_vis = 0;
243                         int     c;
244
245                         if (state.gps.cc_gps_sat == null)
246                                 info_add_row(2, "Satellites Visible", "%4d", 0);
247                         else {
248                                 info_add_row(2, "Satellites Visible", "%4d", state.gps.cc_gps_sat.length);
249                                 for (c = 0; c < state.gps.cc_gps_sat.length; c++) {
250                                         info_add_row(2, "Satellite id,C/N0",
251                                                      "%4d, %4d",
252                                                      state.gps.cc_gps_sat[c].svid,
253                                                      state.gps.cc_gps_sat[c].c_n0);
254                                 }
255                         }
256                 }
257                 info_finish();
258         }
259
260         class IdleThread extends Thread {
261
262                 private AltosState state;
263                 int     reported_landing;
264
265                 public void report(boolean last) {
266                         if (state == null)
267                                 return;
268
269                         /* reset the landing count once we hear about a new flight */
270                         if (state.state < Altos.ao_flight_drogue)
271                                 reported_landing = 0;
272
273                         /* Shut up once the rocket is on the ground */
274                         if (reported_landing > 2) {
275                                 return;
276                         }
277
278                         /* If the rocket isn't on the pad, then report height */
279                         if (state.state > Altos.ao_flight_pad) {
280                                 voice.speak("%d meters", (int) (state.height + 0.5));
281                         } else {
282                                 reported_landing = 0;
283                         }
284
285                         /* If the rocket is coming down, check to see if it has landed;
286                          * either we've got a landed report or we haven't heard from it in
287                          * a long time
288                          */
289                         if (!state.ascent &&
290                             (last ||
291                              System.currentTimeMillis() - state.report_time >= 15000 ||
292                              state.state == Altos.ao_flight_landed))
293                         {
294                                 if (Math.abs(state.baro_speed) < 20 && state.height < 100)
295                                         voice.speak("rocket landed safely");
296                                 else
297                                         voice.speak("rocket may have crashed");
298                                 if (state.from_pad != null)
299                                         voice.speak("bearing %d degrees, range %d meters",
300                                                     (int) (state.from_pad.bearing + 0.5),
301                                                     (int) (state.from_pad.distance + 0.5));
302                                 ++reported_landing;
303                         }
304                 }
305
306                 public void run () {
307
308                         reported_landing = 0;
309                         state = null;
310                         try {
311                                 for (;;) {
312                                         Thread.sleep(10000);
313                                         report(false);
314                                 }
315                         } catch (InterruptedException ie) {
316                         }
317                 }
318
319                 public void notice(AltosState new_state) {
320                         state = new_state;
321                 }
322         }
323
324         private void tell(AltosState state, AltosState old_state) {
325                 if (old_state == null || old_state.state != state.state) {
326                         voice.speak(state.data.state());
327                         if ((old_state == null || old_state.state <= Altos.ao_flight_boost) &&
328                             state.state > Altos.ao_flight_boost) {
329                                 voice.speak("max speed: %d meters per second.",
330                                             (int) (state.max_speed + 0.5));
331                         } else if ((old_state == null || old_state.state < Altos.ao_flight_drogue) &&
332                                    state.state >= Altos.ao_flight_drogue) {
333                                 voice.speak("max height: %d meters.",
334                                             (int) (state.max_height + 0.5));
335                         }
336                 }
337                 if (old_state == null || old_state.gps_ready != state.gps_ready) {
338                         if (state.gps_ready)
339                                 voice.speak("GPS ready");
340                         else if (old_state != null)
341                                 voice.speak("GPS lost");
342                 }
343                 old_state = state;
344         }
345
346         class DisplayThread extends Thread {
347                 IdleThread      idle_thread;
348
349                 String          name;
350
351                 AltosRecord read() throws InterruptedException, ParseException { return null; }
352
353                 void close() { }
354
355                 void update(AltosState state) throws InterruptedException { }
356
357                 public void run() {
358                         String          line;
359                         AltosState      state = null;
360                         AltosState      old_state = null;
361
362                         idle_thread = new IdleThread();
363
364                         info_reset();
365                         info_finish();
366                         idle_thread.start();
367                         try {
368                                 for (;;) {
369                                         try {
370                                                 AltosRecord record = read();
371                                                 if (record == null)
372                                                         break;
373                                                 old_state = state;
374                                                 state = new AltosState(record, state);
375                                                 update(state);
376                                                 show(state);
377                                                 tell(state, old_state);
378                                                 idle_thread.notice(state);
379                                         } catch (ParseException pp) {
380                                                 System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
381                                         }
382                                 }
383                         } catch (InterruptedException ee) {
384                         } finally {
385                                 close();
386                                 idle_thread.interrupt();
387                         }
388                 }
389
390                 public void report() {
391                         if (idle_thread != null)
392                                 idle_thread.report(true);
393                 }
394         }
395
396         class DeviceThread extends DisplayThread {
397                 AltosSerial     serial;
398                 LinkedBlockingQueue<String> telem;
399
400                 AltosRecord read() throws InterruptedException, ParseException {
401                         return new AltosTelemetry(telem.take());
402                 }
403
404                 void close() {
405                         serial.close();
406                         serial.remove_monitor(telem);
407                 }
408
409                 public DeviceThread(AltosSerial s) {
410                         serial = s;
411                         telem = new LinkedBlockingQueue<String>();
412                         serial.add_monitor(telem);
413                         name = "telemetry";
414                 }
415         }
416
417         private void ConnectToDevice() {
418                 AltosDevice     device = AltosDeviceDialog.show(AltosUI.this, AltosDevice.BaseStation);
419
420                 if (device != null) {
421                         try {
422                                 serial_line.open(device);
423                                 DeviceThread thread = new DeviceThread(serial_line);
424                                 serial_line.set_channel(AltosPreferences.channel());
425                                 serial_line.set_callsign(AltosPreferences.callsign());
426                                 run_display(thread);
427                         } catch (FileNotFoundException ee) {
428                                 JOptionPane.showMessageDialog(AltosUI.this,
429                                                               String.format("Cannot open device \"%s\"",
430                                                                             device.getPath()),
431                                                               "Cannot open target device",
432                                                               JOptionPane.ERROR_MESSAGE);
433                         } catch (IOException ee) {
434                                 JOptionPane.showMessageDialog(AltosUI.this,
435                                                               device.getPath(),
436                                                               "Unkonwn I/O error",
437                                                               JOptionPane.ERROR_MESSAGE);
438                         }
439                 }
440         }
441
442         void DisconnectFromDevice () {
443                 stop_display();
444         }
445
446         void ConfigureCallsign() {
447                 String  result;
448                 result = JOptionPane.showInputDialog(AltosUI.this,
449                                                      "Configure Callsign",
450                                                      AltosPreferences.callsign());
451                 if (result != null) {
452                         AltosPreferences.set_callsign(result);
453                         if (serial_line != null)
454                                 serial_line.set_callsign(result);
455                 }
456         }
457
458         void ConfigureTeleMetrum() {
459                 new AltosConfig(AltosUI.this);
460         }
461
462         void FlashImage() {
463                 new AltosFlash(AltosUI.this);
464         }
465
466         /*
467          * Open an existing telemetry file and replay it in realtime
468          */
469
470         class ReplayThread extends DisplayThread {
471                 AltosReader     reader;
472                 String          name;
473
474                 public AltosRecord read() {
475                         try {
476                                 return reader.read();
477                         } catch (IOException ie) {
478                                 JOptionPane.showMessageDialog(AltosUI.this,
479                                                               name,
480                                                               "error reading",
481                                                               JOptionPane.ERROR_MESSAGE);
482                         } catch (ParseException pe) {
483                         }
484                         return null;
485                 }
486
487                 public void close () {
488                         report();
489                 }
490
491                 public ReplayThread(AltosReader in_reader, String in_name) {
492                         reader = in_reader;
493                 }
494                 void update(AltosState state) throws InterruptedException {
495                         /* Make it run in realtime after the rocket leaves the pad */
496                         if (state.state > Altos.ao_flight_pad)
497                                 Thread.sleep((int) (Math.min(state.time_change,10) * 1000));
498                 }
499         }
500
501         class ReplayTelemetryThread extends ReplayThread {
502                 ReplayTelemetryThread(FileInputStream in, String in_name) {
503                         super(new AltosTelemetryReader(in), in_name);
504                 }
505
506         }
507
508         class ReplayEepromThread extends ReplayThread {
509                 ReplayEepromThread(FileInputStream in, String in_name) {
510                         super(new AltosEepromReader(in), in_name);
511                 }
512         }
513
514         Thread          display_thread;
515
516         private void stop_display() {
517                 if (display_thread != null && display_thread.isAlive())
518                         display_thread.interrupt();
519                 display_thread = null;
520         }
521
522         private void run_display(Thread thread) {
523                 stop_display();
524                 display_thread = thread;
525                 display_thread.start();
526         }
527
528         /*
529          * Replay a flight from telemetry data
530          */
531         private void Replay() {
532                 JFileChooser    logfile_chooser = new JFileChooser();
533
534                 logfile_chooser.setDialogTitle("Select Flight Record File");
535                 logfile_chooser.setFileFilter(new FileNameExtensionFilter("Flight data file", "eeprom", "telem"));
536                 logfile_chooser.setCurrentDirectory(AltosPreferences.logdir());
537                 int returnVal = logfile_chooser.showOpenDialog(AltosUI.this);
538
539                 if (returnVal == JFileChooser.APPROVE_OPTION) {
540                         File file = logfile_chooser.getSelectedFile();
541                         if (file == null)
542                                 System.out.println("No file selected?");
543                         String  filename = file.getName();
544                         try {
545                                 FileInputStream replay = new FileInputStream(file);
546                                 DisplayThread   thread;
547                                 if (filename.endsWith("eeprom"))
548                                     thread = new ReplayEepromThread(replay, filename);
549                                 else
550                                     thread = new ReplayTelemetryThread(replay, filename);
551                                 run_display(thread);
552                         } catch (FileNotFoundException ee) {
553                                 JOptionPane.showMessageDialog(AltosUI.this,
554                                                               filename,
555                                                               "Cannot open telemetry file",
556                                                               JOptionPane.ERROR_MESSAGE);
557                         }
558                 }
559         }
560
561         /* Connect to TeleMetrum, either directly or through
562          * a TeleDongle over the packet link
563          */
564         private void SaveFlightData() {
565                 new AltosEepromDownload(AltosUI.this);
566         }
567
568         /* Create the AltosUI menus
569          */
570         private void createMenu() {
571                 JMenuBar menubar = new JMenuBar();
572                 JMenu menu;
573                 JMenuItem item;
574                 JRadioButtonMenuItem radioitem;
575
576                 // File menu
577                 {
578                         menu = new JMenu("File");
579                         menu.setMnemonic(KeyEvent.VK_F);
580                         menubar.add(menu);
581
582                         item = new JMenuItem("Replay File",KeyEvent.VK_R);
583                         item.addActionListener(new ActionListener() {
584                                         public void actionPerformed(ActionEvent e) {
585                                                 Replay();
586                                         }
587                                 });
588                         menu.add(item);
589
590                         item = new JMenuItem("Save Flight Data",KeyEvent.VK_S);
591                         item.addActionListener(new ActionListener() {
592                                         public void actionPerformed(ActionEvent e) {
593                                                 SaveFlightData();
594                                         }
595                                 });
596                         menu.add(item);
597
598                         item = new JMenuItem("Flash Image",KeyEvent.VK_F);
599                         item.addActionListener(new ActionListener() {
600                                         public void actionPerformed(ActionEvent e) {
601                                                 FlashImage();
602                                         }
603                                 });
604                         menu.add(item);
605
606                         item = new JMenuItem("Quit",KeyEvent.VK_Q);
607                         item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
608                                                                    ActionEvent.CTRL_MASK));
609                         item.addActionListener(new ActionListener() {
610                                         public void actionPerformed(ActionEvent e) {
611                                                 System.exit(0);
612                                         }
613                                 });
614                         menu.add(item);
615                 }
616
617                 // Device menu
618                 {
619                         menu = new JMenu("Device");
620                         menu.setMnemonic(KeyEvent.VK_D);
621                         menubar.add(menu);
622
623                         item = new JMenuItem("Connect to Device",KeyEvent.VK_C);
624                         item.addActionListener(new ActionListener() {
625                                         public void actionPerformed(ActionEvent e) {
626                                                 ConnectToDevice();
627                                         }
628                                 });
629                         menu.add(item);
630
631                         item = new JMenuItem("Disconnect from Device",KeyEvent.VK_D);
632                         item.addActionListener(new ActionListener() {
633                                         public void actionPerformed(ActionEvent e) {
634                                                 DisconnectFromDevice();
635                                         }
636                                 });
637                         menu.add(item);
638
639                         menu.addSeparator();
640
641                         item = new JMenuItem("Set Callsign",KeyEvent.VK_S);
642                         item.addActionListener(new ActionListener() {
643                                         public void actionPerformed(ActionEvent e) {
644                                                 ConfigureCallsign();
645                                         }
646                                 });
647
648                         menu.add(item);
649
650                         item = new JMenuItem("Configure TeleMetrum device",KeyEvent.VK_T);
651                         item.addActionListener(new ActionListener() {
652                                         public void actionPerformed(ActionEvent e) {
653                                                 ConfigureTeleMetrum();
654                                         }
655                                 });
656
657                         menu.add(item);
658                 }
659                 // Log menu
660                 {
661                         menu = new JMenu("Log");
662                         menu.setMnemonic(KeyEvent.VK_L);
663                         menubar.add(menu);
664
665                         item = new JMenuItem("New Log",KeyEvent.VK_N);
666                         item.addActionListener(new ActionListener() {
667                                         public void actionPerformed(ActionEvent e) {
668                                         }
669                                 });
670                         menu.add(item);
671
672                         item = new JMenuItem("Configure Log",KeyEvent.VK_C);
673                         item.addActionListener(new ActionListener() {
674                                         public void actionPerformed(ActionEvent e) {
675                                                 AltosPreferences.ConfigureLog();
676                                         }
677                                 });
678                         menu.add(item);
679                 }
680                 // Voice menu
681                 {
682                         menu = new JMenu("Voice", true);
683                         menu.setMnemonic(KeyEvent.VK_V);
684                         menubar.add(menu);
685
686                         radioitem = new JRadioButtonMenuItem("Enable Voice", AltosPreferences.voice());
687                         radioitem.addActionListener(new ActionListener() {
688                                         public void actionPerformed(ActionEvent e) {
689                                                 JRadioButtonMenuItem item = (JRadioButtonMenuItem) e.getSource();
690                                                 boolean enabled = item.isSelected();
691                                                 AltosPreferences.set_voice(enabled);
692                                                 if (enabled)
693                                                         voice.speak_always("Enable voice.");
694                                                 else
695                                                         voice.speak_always("Disable voice.");
696                                         }
697                                 });
698                         menu.add(radioitem);
699                         item = new JMenuItem("Test Voice",KeyEvent.VK_T);
700                         item.addActionListener(new ActionListener() {
701                                         public void actionPerformed(ActionEvent e) {
702                                                 voice.speak("That's one small step for man; one giant leap for mankind.");
703                                         }
704                                 });
705                         menu.add(item);
706                 }
707
708                 // Channel menu
709                 {
710                         menu = new AltosChannelMenu(AltosPreferences.channel());
711                         menu.addActionListener(new ActionListener() {
712                                                 public void actionPerformed(ActionEvent e) {
713                                                         int new_channel = Integer.parseInt(e.getActionCommand());
714                                                         AltosPreferences.set_channel(new_channel);
715                                                         serial_line.set_channel(new_channel);
716                                                 }
717                                 });
718                         menu.setMnemonic(KeyEvent.VK_C);
719                         menubar.add(menu);
720                 }
721
722                 this.setJMenuBar(menubar);
723
724         }
725         public static void main(final String[] args) {
726                 AltosUI altosui = new AltosUI();
727                 altosui.setVisible(true);
728         }
729 }