altosui: Generalize and centralize telemetry constants, parse v0.8 telemetry
[fw/altos] / altosui / AltosPreferences.java
index c6ae6cbd176d39cdae30b075f77fc2812f065df4..c8dee743f2318950efc834da3a917d92aa5d0ddd 100644 (file)
@@ -34,6 +34,9 @@ class AltosPreferences {
        /* channel preference name */
        final static String channelPreferenceFormat = "CHANNEL-%d";
 
+       /* telemetry format preference name */
+       final static String telemetryPreferenceFormat = "TELEMETRY-%d";
+
        /* voice preference name */
        final static String voicePreference = "VOICE";
 
@@ -43,6 +46,9 @@ class AltosPreferences {
        /* firmware directory preference name */
        final static String firmwaredirPreference = "FIRMWARE";
 
+       /* serial debug preference name */
+       final static String serialDebugPreference = "SERIAL-DEBUG";
+
        /* Default logdir is ~/TeleMetrum */
        final static String logdirName = "TeleMetrum";
 
@@ -58,6 +64,9 @@ class AltosPreferences {
        /* Channel (map serial to channel) */
        static Hashtable<Integer, Integer> channels;
 
+       /* Telemetry (map serial to telemetry format) */
+       static Hashtable<Integer, Integer> telemetries;
+
        /* Voice preference */
        static boolean voice;
 
@@ -67,10 +76,11 @@ class AltosPreferences {
        /* Firmware directory */
        static File firmwaredir;
 
-       public static void init(Component ui) {
-               preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
+       /* Serial debug */
+       static boolean serial_debug;
 
-               component = ui;
+       public static void init() {
+               preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
 
                /* Initialize logdir from preferences */
                String logdir_string = preferences.get(logdirPreference, null);
@@ -88,6 +98,8 @@ class AltosPreferences {
 
                channels = new Hashtable<Integer,Integer>();
 
+               telemetries = new Hashtable<Integer,Integer>();
+
                voice = preferences.getBoolean(voicePreference, true);
 
                callsign = preferences.get(callsignPreference,"N0CALL");
@@ -97,16 +109,28 @@ class AltosPreferences {
                        firmwaredir = new File(firmwaredir_string);
                else
                        firmwaredir = null;
+
+               serial_debug = preferences.getBoolean(serialDebugPreference, false);
+               AltosSerial.set_debug(serial_debug);
+       }
+
+       static { init(); }
+
+       static void set_component(Component in_component) {
+               component = in_component;
        }
 
        static void flush_preferences() {
                try {
                        preferences.flush();
                } catch (BackingStoreException ee) {
-                       JOptionPane.showMessageDialog(component,
-                                                     preferences.absolutePath(),
-                                                     "Cannot save prefernces",
-                                                     JOptionPane.ERROR_MESSAGE);
+                       if (component != null)
+                               JOptionPane.showMessageDialog(component,
+                                                             preferences.absolutePath(),
+                                                             "Cannot save prefernces",
+                                                             JOptionPane.ERROR_MESSAGE);
+                       else
+                               System.err.printf("Cannot save preferences\n");
                }
        }
 
@@ -180,6 +204,23 @@ class AltosPreferences {
                return channel;
        }
 
+       public static void set_telemetry(int serial, int new_telemetry) {
+               telemetries.put(serial, new_telemetry);
+               synchronized (preferences) {
+                       preferences.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
+                       flush_preferences();
+               }
+       }
+
+       public static int telemetry(int serial) {
+               if (telemetries.containsKey(serial))
+                       return telemetries.get(serial);
+               int telemetry = preferences.getInt(String.format(telemetryPreferenceFormat, serial),
+                                                  Altos.ao_telemetry_standard);
+               telemetries.put(serial, telemetry);
+               return telemetry;
+       }
+
        public static void set_voice(boolean new_voice) {
                voice = new_voice;
                synchronized (preferences) {
@@ -215,4 +256,21 @@ class AltosPreferences {
        public static File firmwaredir() {
                return firmwaredir;
        }
+
+       public static void set_serial_debug(boolean new_serial_debug) {
+               serial_debug = new_serial_debug;
+               AltosSerial.set_debug(serial_debug);
+               synchronized (preferences) {
+                       preferences.putBoolean(serialDebugPreference, serial_debug);
+                       flush_preferences();
+               }
+       }
+
+       public static boolean serial_debug() {
+               return serial_debug;
+       }
+
+       public static Preferences bt_devices() {
+               return preferences.node("bt_devices");
+       }
 }