From bc8b5073a3e6ba1cbb6bff6d88cdf40ab964691c Mon Sep 17 00:00:00 2001
From: c vw <dl1ycf@darc.de>
Date: Thu, 8 Dec 2022 19:21:52 +0100
Subject: [PATCH] Some more corrections involved in CTUN toggling

---
 actions.c |  9 +++++++--
 radio.c   | 38 +++++++++++++++++++-------------------
 store.c   |  6 +++++-
 3 files changed, 31 insertions(+), 22 deletions(-)

diff --git a/actions.c b/actions.c
index 8dfe013..322a494 100644
--- a/actions.c
+++ b/actions.c
@@ -566,9 +566,14 @@ int process_action(void *data) {
       if(a->mode==PRESSED) {
         vfo[active_receiver->id].ctun=!vfo[active_receiver->id].ctun;
         if(!vfo[active_receiver->id].ctun) {
-          vfo[active_receiver->id].offset=0;
+          // when deactivating CTUN,  keep frequency
+          setFrequency(vfo[active_receiver->id].ctun_frequency);
+        } else {
+          // when activating CTUN, continue with current frequency
+          vfo[active_receiver->id].ctun_frequency=vfo[active_receiver->id].frequency;
         }
-        vfo[active_receiver->id].ctun_frequency=vfo[active_receiver->id].frequency;
+        // in either case, start with zero offset after toggling CTUN
+        vfo[active_receiver->id].offset=0;
         set_offset(receiver[active_receiver->id],vfo[active_receiver->id].offset);
         g_idle_add(ext_vfo_update, NULL);
       }
diff --git a/radio.c b/radio.c
index 3708262..6c9193a 100644
--- a/radio.c
+++ b/radio.c
@@ -1849,26 +1849,26 @@ void setFrequency(long long f) {
   int v=active_receiver->id;
   vfo[v].band=get_band_from_frequency(f);
 
-  switch(protocol) {
-    case NEW_PROTOCOL:
-    case ORIGINAL_PROTOCOL:
-#ifdef SOAPYSDR
-    case SOAPYSDR_PROTOCOL:
-#endif
-      if(vfo[v].ctun) {
-        long long minf=vfo[v].frequency-(long long)(active_receiver->sample_rate/2);
-        long long maxf=vfo[v].frequency+(long long)(active_receiver->sample_rate/2);
-        if(f<minf) f=minf;
-        if(f>maxf) f=maxf;
-        vfo[v].ctun_frequency=f;
-        vfo[v].offset=f-vfo[v].frequency;
-        set_offset(active_receiver,vfo[v].offset);
-        return;
-      } else {
-        vfo[v].frequency=f;
-      }
-      break;
+  if(vfo[v].ctun) {
+    //
+    // If new frequency is within "window", change the CTUN frequency and
+    // update the offset. If it is outside, deactivate CTUN and fall
+    // through
+    //
+    long long minf=vfo[v].frequency-(long long)(active_receiver->sample_rate/2);
+    long long maxf=vfo[v].frequency+(long long)(active_receiver->sample_rate/2);
+    if(f > minf && f < maxf) {
+      vfo[v].ctun_frequency=f;
+      vfo[v].offset=f-vfo[v].frequency;
+      set_offset(active_receiver,vfo[v].offset);
+      return;
+    }
+    vfo[v].ctun=0;
+    vfo[v].ctun_frequency=0;
+    vfo[v].offset=0;
+    set_offset(active_receiver,vfo[v].offset);
   }
+  vfo[v].frequency=f;
 
   switch(protocol) {
     case NEW_PROTOCOL:
diff --git a/store.c b/store.c
index 0c7be6f..2aaae2c 100644
--- a/store.c
+++ b/store.c
@@ -129,7 +129,11 @@ void store_memory_slot(int index) {
    //
    // Store current frequency, mode, and filter in slot #index
    //
-   mem[index].frequency = vfo[id].frequency;
+   if (vfo[id].ctun) {
+     mem[index].frequency = vfo[id].ctun_frequency;
+   } else {
+     mem[index].frequency = vfo[id].frequency;
+   }
    mem[index].mode = vfo[id].mode;
    mem[index].filter=vfo[id].filter;
 
-- 
2.45.2