From bf4c37f845b1264cea8cd7675a4d110922698d39 Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Sat, 6 Jan 2024 21:23:37 +0530
Subject: [PATCH] ctun: while turning ctun from on to off, copy last ctun freq

---
 ext.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/ext.c b/ext.c
index 06f52c0..58fc2a9 100644
--- a/ext.c
+++ b/ext.c
@@ -604,12 +604,18 @@ int ext_mode_minus(void *data) {
 }
 
 void ctun_update(int id,int state) {
-  vfo[id].ctun=state;
-  if(!vfo[id].ctun) {
-    vfo[id].offset=0;
-  }
-  vfo[id].ctun_frequency=vfo[id].frequency;
-  set_offset(receiver[id],vfo[id].offset);
+    if (vfo[id].ctun == 1 && state == 0) {
+	// ctune was on and now we are switching off
+	vfo[id].frequency = vfo[id].ctun_frequency;
+    } else if (vfo[id].ctun == 0 && state == 1) {
+	// ctun was off, now we turned on
+	vfo[id].ctun_frequency=vfo[id].frequency;
+    }
+    vfo[id].ctun=state;
+    if(!vfo[id].ctun) {
+	vfo[id].offset=0;
+    }
+    set_offset(receiver[id],vfo[id].offset);
 }
 
 int ext_ctun_update(void *data) {
-- 
2.45.2