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