altosui: Generalize and centralize telemetry constants, parse v0.8 telemetry
[fw/altos] / altosui / AltosPreferences.java
index e2a3df3b7717bb5b92f9cf150706bdc49a7ecbb3..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";
 
@@ -52,9 +58,15 @@ class AltosPreferences {
        /* Log directory */
        static File logdir;
 
+       /* Map directory -- hangs of logdir */
+       static File mapdir;
+
        /* 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;
 
@@ -64,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);
@@ -79,9 +92,14 @@ class AltosPreferences {
                        if (!logdir.exists())
                                logdir.mkdirs();
                }
+               mapdir = new File(logdir, "maps");
+               if (!mapdir.exists())
+                       mapdir.mkdirs();
 
                channels = new Hashtable<Integer,Integer>();
 
+               telemetries = new Hashtable<Integer,Integer>();
+
                voice = preferences.getBoolean(voicePreference, true);
 
                callsign = preferences.get(callsignPreference,"N0CALL");
@@ -91,21 +109,36 @@ 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");
                }
        }
 
        public static void set_logdir(File new_logdir) {
                logdir = new_logdir;
+               mapdir = new File(logdir, "maps");
+               if (!mapdir.exists())
+                       mapdir.mkdirs();
                synchronized (preferences) {
                        preferences.put(logdirPreference, logdir.getPath());
                        flush_preferences();
@@ -151,6 +184,10 @@ class AltosPreferences {
                return logdir;
        }
 
+       public static File mapdir() {
+               return mapdir;
+       }
+
        public static void set_channel(int serial, int new_channel) {
                channels.put(serial, new_channel);
                synchronized (preferences) {
@@ -167,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) {
@@ -202,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");
+       }
 }