altoslib: Save separate config for local and remote. Use in idle
authorKeith Packard <keithp@keithp.com>
Sat, 14 Oct 2017 19:18:26 +0000 (12:18 -0700)
committerKeith Packard <keithp@keithp.com>
Sat, 14 Oct 2017 19:18:26 +0000 (12:18 -0700)
When using the remote link, there are two separate configuration data
blocks, that for the local device and for remote. Make the link report
both versions, depending on whether it is in remote mode or not.

Request config data in remote mode when running idle monitoring so
that the presented data is for the remote device, not the local one.

Signed-off-by: Keith Packard <keithp@keithp.com>
altoslib/AltosIdleMonitor.java
altoslib/AltosLink.java
altosui/AltosIdleMonitorUI.java

index fc5d4cc..834d9aa 100644 (file)
@@ -33,6 +33,7 @@ public class AltosIdleMonitor extends Thread {
        double                  frequency;
        String                  callsign;
 
+       AltosState              state;
        AltosListenerState      listener_state;
        AltosConfigData         config_data;
        AltosGPS                gps;
@@ -52,20 +53,23 @@ public class AltosIdleMonitor extends Thread {
                return link.reply_abort;
        }
 
-       boolean provide_data(AltosDataListener listener) throws InterruptedException, TimeoutException, AltosUnknownProduct {
+       boolean provide_data() throws InterruptedException, TimeoutException, AltosUnknownProduct {
                boolean         worked = false;
                boolean         aborted = false;
 
                try {
                        start_link();
-                       fetch.provide_data(listener);
+                       link.config_data();
+                       if (state == null)
+                               state = new AltosState(new AltosCalData(link.config_data()));
+                       fetch.provide_data(state);
                        if (!link.has_error && !link.reply_abort)
                                worked = true;
                } finally {
                        aborted = stop_link();
                        if (worked) {
                                if (remote)
-                                       listener.set_rssi(link.rssi(), 0);
+                                       state.set_rssi(link.rssi(), 0);
                                listener_state.battery = link.monitor_battery();
                        }
                }
@@ -92,14 +96,11 @@ public class AltosIdleMonitor extends Thread {
        }
 
        public void run() {
-               AltosState state = null;
+               state = null;
                try {
                        for (;;) {
                                try {
-                                       link.config_data();
-                                       if (state == null)
-                                               state = new AltosState(new AltosCalData(link.config_data()));
-                                       provide_data(state);
+                                       provide_data();
                                        listener.update(state, listener_state);
                                } catch (TimeoutException te) {
                                } catch (AltosUnknownProduct ae) {
index 5413de9..829a1a6 100644 (file)
@@ -355,7 +355,8 @@ public abstract class AltosLink implements Runnable {
        public int telemetry_rate = -1;
        public double frequency;
        public String callsign;
-       AltosConfigData config_data;
+       private AltosConfigData config_data_local;
+       private AltosConfigData config_data_remote;
 
        private Object config_data_lock = new Object();
 
@@ -390,7 +391,7 @@ public abstract class AltosLink implements Runnable {
 
        public void set_radio_frequency(double in_frequency) throws InterruptedException, TimeoutException {
                frequency = in_frequency;
-               config_data();
+               AltosConfigData config_data = config_data();
                set_radio_frequency(frequency,
                                    config_data.radio_frequency > 0,
                                    config_data.radio_setting > 0,
@@ -446,11 +447,24 @@ public abstract class AltosLink implements Runnable {
 
        public AltosConfigData config_data() throws InterruptedException, TimeoutException {
                synchronized(config_data_lock) {
-                       if (config_data == null) {
-                               printf("m 0\n");
-                               config_data = new AltosConfigData(this);
-                               if (monitor_mode)
-                                       set_monitor(true);
+                       AltosConfigData config_data;
+
+                       if (remote) {
+                               if (config_data_remote == null) {
+                                       printf("m 0\n");
+                                       config_data_remote = new AltosConfigData(this);
+                                       if (monitor_mode)
+                                               set_monitor(true);
+                               }
+                               config_data = config_data_remote;
+                       } else {
+                               if (config_data_local == null) {
+                                       printf("m 0\n");
+                                       config_data_local = new AltosConfigData(this);
+                                       if (monitor_mode)
+                                               set_monitor(true);
+                               }
+                               config_data = config_data_local;
                        }
                        return config_data;
                }
@@ -551,14 +565,23 @@ public abstract class AltosLink implements Runnable {
        }
 
        public boolean has_monitor_battery() {
-               return config_data.has_monitor_battery();
+               try {
+                       return config_data().has_monitor_battery();
+               } catch (InterruptedException ie) {
+                       return false;
+               } catch (TimeoutException te) {
+                       return false;
+               }
        }
 
        public double monitor_battery() throws InterruptedException {
-               int monitor_batt = AltosLib.MISSING;
+               double  volts = AltosLib.MISSING;
 
-               if (config_data.has_monitor_battery()) {
-                       try {
+               try {
+                       AltosConfigData config_data = config_data();
+                       int monitor_batt = AltosLib.MISSING;
+
+                       if (config_data.has_monitor_battery()) {
                                String[] items = adc();
                                for (int i = 0; i < items.length;) {
                                        if (items[i].equals("batt")) {
@@ -568,19 +591,17 @@ public abstract class AltosLink implements Runnable {
                                        }
                                        i++;
                                }
-                       } catch (TimeoutException te) {
                        }
-               }
-               if (monitor_batt == AltosLib.MISSING)
-                       return AltosLib.MISSING;
+                       if (monitor_batt != AltosLib.MISSING) {
+                               if (config_data.product.startsWith("TeleBT-v3") || config_data.product.startsWith("TeleBT-v4")) {
+                                       volts = AltosConvert.tele_bt_3_battery(monitor_batt);
+                               } else {
+                                       volts = AltosConvert.cc_battery_to_voltage(monitor_batt);
+                               }
+                       }
 
-               double  volts = AltosLib.MISSING;
-               if (config_data.product.startsWith("TeleBT-v3") || config_data.product.startsWith("TeleBT-v4")) {
-                       volts = AltosConvert.tele_bt_3_battery(monitor_batt);
-               } else {
-                       volts = AltosConvert.cc_battery_to_voltage(monitor_batt);
+               } catch (TimeoutException te) {
                }
-
                return volts;
        }
 
index a2696f1..584f143 100644 (file)
@@ -295,7 +295,7 @@ public class AltosIdleMonitorUI extends AltosUIFrame implements AltosFlightDispl
                pack();
                setVisible(true);
 
-               thread = new AltosIdleMonitor((AltosIdleMonitorListener) this, link, (boolean) remote);
+               thread = new AltosIdleMonitor(this, link, (boolean) remote);
 
                status_update = new AltosFlightStatusUpdate(flightStatus);