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