Make voice and channel menus work.
authorKeith Packard <keithp@keithp.com>
Wed, 28 Jul 2010 20:01:52 +0000 (13:01 -0700)
committerKeith Packard <keithp@keithp.com>
Wed, 28 Jul 2010 20:01:52 +0000 (13:01 -0700)
Stores voice and channel data to preferences.

Signed-off-by: Keith Packard <keithp@keithp.com>
ao-tools/altosui/AltosPreferences.java
ao-tools/altosui/AltosSerial.java
ao-tools/altosui/AltosUI.java
ao-tools/altosui/AltosVoice.java

index 0296d9355db7a2086ade1aa97ce737c028ed58d2..297e1aae77ba0819a71af5ef53718a648cf2f727 100644 (file)
@@ -31,6 +31,12 @@ class AltosPreferences {
        /* logdir preference name */
        final static String logdirPreference = "LOGDIR";
 
+       /* channel preference name */
+       final static String channelPreference = "CHANNEL";
+
+       /* voice preference name */
+       final static String voicePreference = "VOICE";
+
        /* Default logdir is ~/TeleMetrum */
        final static String logdirName = "TeleMetrum";
 
@@ -40,6 +46,12 @@ class AltosPreferences {
        /* Log directory */
        static File logdir;
 
+       /* Telemetry channel */
+       static int channel;
+
+       /* Voice preference */
+       static boolean voice;
+
        public static void init(Component ui) {
                preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
 
@@ -55,6 +67,10 @@ class AltosPreferences {
                        if (!logdir.exists())
                                logdir.mkdirs();
                }
+
+               channel = preferences.getInt(channelPreference, 0);
+
+               voice = preferences.getBoolean(voicePreference, true);
        }
 
        static void flush_preferences() {
@@ -114,4 +130,28 @@ class AltosPreferences {
        public static File logdir() {
                return logdir;
        }
+
+       public static void set_channel(int new_channel) {
+               channel = new_channel;
+               synchronized (preferences) {
+                       preferences.putInt(channelPreference, channel);
+                       flush_preferences();
+               }
+       }
+
+       public static int channel() {
+               return channel;
+       }
+
+       public static void set_voice(boolean new_voice) {
+               voice = new_voice;
+               synchronized (preferences) {
+                       preferences.putBoolean(voicePreference, voice);
+                       flush_preferences();
+               }
+       }
+
+       public static boolean voice() {
+               return voice;
+       }
 }
index b016c1d65fbecfe0463c0e97933148cf10c2d55b..f12b31b3187e9b253b4a3560080db9d8e2966d7e 100644 (file)
@@ -119,7 +119,8 @@ public class AltosSerial implements Runnable {
        }
 
        public void putc(char c) {
-               libaltos.altos_putchar(altos, c);
+               if (altos != null)
+                       libaltos.altos_putchar(altos, c);
        }
 
        public void print(String data) {
@@ -138,6 +139,17 @@ public class AltosSerial implements Runnable {
                        throw new FileNotFoundException(device.getPath());
                input_thread = new Thread(this);
                input_thread.start();
+               print("\nE 0\nm 1\n");
+               try {
+                       Thread.sleep(200);
+               } catch (InterruptedException e) {
+               }
+               flush();
+       }
+
+       public void set_channel(int channel) {
+               if (altos != null)
+                       printf("m 0\nc r %d\nm 1\n", channel);
        }
 
        public AltosSerial() {
index 5c92b9b4d3c0d3ead2638158ab248c89e4de4b6b..511c37093539fee08e7840862bc15e03088a807d 100644 (file)
@@ -210,6 +210,7 @@ public class AltosUI extends JFrame {
                                System.exit(0);
                        }
                });
+               voice.speak("Rocket flight monitor ready.");
        }
 
        public void info_reset() {
@@ -490,6 +491,7 @@ public class AltosUI extends JFrame {
                        try {
                                serial_line.open(device);
                                DeviceThread thread = new DeviceThread(serial_line);
+                               serial_line.set_channel(AltosPreferences.channel());
                                run_display(thread);
                        } catch (FileNotFoundException ee) {
                                JOptionPane.showMessageDialog(AltosUI.this,
@@ -706,9 +708,16 @@ public class AltosUI extends JFrame {
                        menu.setMnemonic(KeyEvent.VK_V);
                        menubar.add(menu);
 
-                       radioitem = new JRadioButtonMenuItem("Enable Voice");
+                       radioitem = new JRadioButtonMenuItem("Enable Voice", AltosPreferences.voice());
                        radioitem.addActionListener(new ActionListener() {
                                        public void actionPerformed(ActionEvent e) {
+                                               JRadioButtonMenuItem item = (JRadioButtonMenuItem) e.getSource();
+                                               boolean enabled = item.isSelected();
+                                               AltosPreferences.set_voice(enabled);
+                                               if (enabled)
+                                                       voice.speak_always("Enable voice.");
+                                               else
+                                                       voice.speak_always("Disable voice.");
                                        }
                                });
                        menu.add(radioitem);
@@ -724,12 +733,13 @@ public class AltosUI extends JFrame {
                        for (int c = 0; c <= 9; c++) {
                                radioitem = new JRadioButtonMenuItem(String.format("Channel %1d (%7.3fMHz)", c,
                                                                                   434.550 + c * 0.1),
-                                                                    c == 0);
+                                                                    c == AltosPreferences.channel());
                                radioitem.setActionCommand(String.format("%d", c));
                                radioitem.addActionListener(new ActionListener() {
                                                public void actionPerformed(ActionEvent e) {
-                                                       System.out.println("Command: " + e.getActionCommand() + " param: " +
-                                                                          e.paramString());
+                                                       int new_channel = Integer.parseInt(e.getActionCommand());
+                                                       AltosPreferences.set_channel(new_channel);
+                                                       serial_line.set_channel(new_channel);
                                                }
                                        });
                                menu.add(radioitem);
index c39bfb9b4694fb086df9278faad2008393663b26..ebe9d5a81c0d5c8b1cab4084db70925dc59049b8 100644 (file)
@@ -39,7 +39,8 @@ public class AltosVoice implements Runnable {
                } catch (InterruptedException e) {
                }
        }
-       public void speak(String s) {
+
+       public void speak_always(String s) {
                try {
                        if (voice != null)
                                phrases.put(s);
@@ -47,6 +48,11 @@ public class AltosVoice implements Runnable {
                }
        }
 
+       public void speak(String s) {
+               if (AltosPreferences.voice())
+                       speak_always(s);
+       }
+
        public void speak(String format, Object... parameters) {
                speak(String.format(format, parameters));
        }
@@ -59,7 +65,6 @@ public class AltosVoice implements Runnable {
                        phrases = new LinkedBlockingQueue<String> ();
                        thread = new Thread(this);
                        thread.start();
-                       speak("Rocket Flight Monitor Ready");
                } else {
                        System.out.printf("Voice manager failed to open %s\n", voice_name);
                        Voice[] voices = voice_manager.getVoices();