From f81bbcbbbce5004d80c9cd8084d727b7aaf5d3f8 Mon Sep 17 00:00:00 2001
From: John Melton G0ORX <john.d.melton@googlemail.com>
Date: Thu, 26 Mar 2020 12:48:22 +0000
Subject: [PATCH] Fix setting rx frequency for SoapySDR devices on band change

---
 soapy_protocol.c |  4 ----
 vfo.c            | 19 ++++++++++++++++---
 2 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/soapy_protocol.c b/soapy_protocol.c
index 8ae87c0..23ce181 100644
--- a/soapy_protocol.c
+++ b/soapy_protocol.c
@@ -85,12 +85,10 @@ SoapySDRDevice *get_soapy_device() {
 
 void soapy_protocol_set_mic_sample_rate(int rate) {
   mic_sample_divisor=rate/48000;
-g_print("soapy_protocol_set_mic_sample_rate: rate=%d mic_sample_divisor=%d\n",rate,mic_sample_divisor);
 }
 
 void soapy_protocol_change_sample_rate(RECEIVER *rx) {
 // rx->mutex already locked
-g_print("soapy_protocol_change_sample_rate: %d\n",rx->sample_rate);
   if(rx->sample_rate==radio_sample_rate) {
     if(rx->resample_buffer!=NULL) {
       g_free(rx->resample_buffer);
@@ -114,7 +112,6 @@ g_print("soapy_protocol_change_sample_rate: %d\n",rx->sample_rate);
     rx->resample_buffer=g_new(double,rx->resample_buffer_size);
     rx->resampler=create_resample (1,max_samples,rx->buffer,rx->resample_buffer,radio_sample_rate,rx->sample_rate,0.0,0,1.0);
 
-g_print("soapy_protocol_change_sample_rate: buffer=%p buffer_size=%d resampler=%p\n",rx->resample_buffer,rx->resample_buffer_size,rx->resampler);
   }
 
 }
@@ -413,7 +410,6 @@ void soapy_protocol_set_rx_frequency(RECEIVER *rx,int v) {
 
   if(soapy_device!=NULL) {
     double f=(double)(vfo[v].frequency-vfo[v].lo);
-//g_print("soapy_protocol_set_rx_frequency: %f (%f %f)\n",f,(double)vfo[v].frequency,(double)vfo[v].lo);
     rc=SoapySDRDevice_setFrequency(soapy_device,SOAPY_SDR_RX,rx->adc,f,NULL);
     if(rc!=0) {
       fprintf(stderr,"soapy_protocol: SoapySDRDevice_setFrequency(RX) failed: %s\n",SoapySDR_errToStr(rc));
diff --git a/vfo.c b/vfo.c
index 88ddf3b..16a00f7 100644
--- a/vfo.c
+++ b/vfo.c
@@ -38,10 +38,14 @@
 #include "filter.h"
 #include "bandstack.h"
 #include "band.h"
-#include "new_protocol.h"
 #include "property.h"
 #include "radio.h"
 #include "receiver.h"
+#include "transmitter.h"
+#include "new_protocol.h"
+#ifdef SOAPYSDR
+#include "soapy_protocol.h"
+#endif
 #include "vfo.h"
 #include "channel.h"
 #include "toolbar.h"
@@ -290,6 +294,7 @@ void vfo_band_changed(int b) {
 
   // turn off ctun
   vfo[id].ctun=0;
+  vfo[id].ctun_frequency=0LL;
   vfo[id].offset=0;
   // tell WDSP about the offset
   set_offset(active_receiver, vfo[id].offset);
@@ -322,8 +327,16 @@ void vfo_band_changed(int b) {
     //
     calcDriveLevel();  // sends HighPrio packet if in new protocol
   }
-  if (protocol == NEW_PROTOCOL) {
-    schedule_general();
+
+  switch(protocol) {
+    case NEW_PROTOCOL:
+      schedule_general();
+      break;
+#ifdef SOAPYSDR
+    case SOAPYSDR_PROTOCOL:
+      soapy_protocol_set_rx_frequency(active_receiver,id);
+      break;
+#endif
   }
   g_idle_add(ext_vfo_update,NULL);
 }
-- 
2.45.2