From 9b506e889783eeef855e2177ebc1b05c80c24fdf Mon Sep 17 00:00:00 2001
From: c vw <dl1ycf@darc.de>
Date: Wed, 23 Oct 2019 17:07:37 +0200
Subject: [PATCH] Added VFOA/VFOB support to MIDI

---
 ext.c   | 10 ++++++++++
 ext.h   |  1 +
 midi.h  |  2 ++
 midi2.c |  2 ++
 midi3.c | 10 ++++++++++
 vfo.c   | 20 ++++++++++++++++++++
 vfo.h   |  1 +
 7 files changed, 46 insertions(+)

diff --git a/ext.c b/ext.c
index 0d68f25..1cb1802 100644
--- a/ext.c
+++ b/ext.c
@@ -200,6 +200,15 @@ int ext_vfo_step(void *data) {
   return 0;
 }
 
+int ext_vfo_id_step(void *data) {
+  int *ip=(int *) data;
+  int id=ip[0];
+  int step=ip[1];
+  vfo_id_step(id,step);
+  free(data);
+  return 0;
+}
+
 int ext_set_mic_gain(void * data) {
   double d=*(double *)data;
   set_mic_gain(d);
@@ -500,5 +509,6 @@ int ext_function_update(void *data) {
   }
   update_toolbar_labels();
   vfo_update();
+  return 0;
 }
 
diff --git a/ext.h b/ext.h
index ac99555..bc93cf2 100644
--- a/ext.h
+++ b/ext.h
@@ -86,6 +86,7 @@ extern int ext_tx_set_ps(void *data);
 #endif
 
 int ext_vfo_step(void *data);
+int ext_vfo_id_step(void *data);
 int ext_vfo_mode_changed(void *data);
 int ext_set_af_gain(void *data);
 int ext_set_mic_gain(void *data);
diff --git a/midi.h b/midi.h
index e21cab2..3c9e8b0 100644
--- a/midi.h
+++ b/midi.h
@@ -76,6 +76,8 @@ enum MIDIaction {
   MIDI_TUNE,			// toggle "tune" state
   TX_DRIVE,		// RF output power
   VFO,			// change VFO frequency
+  VFOA,			// change VFO-A frequency
+  VFOB,			// change VFO-B frequency
   VFO_A2B,		// VFO A -> B
   VFO_B2A,		// VFO B -> A
   VOX 			// VOX on/off
diff --git a/midi2.c b/midi2.c
index c8f81f9..3ac2ece 100644
--- a/midi2.c
+++ b/midi2.c
@@ -119,6 +119,8 @@ static struct {
         { MIDI_TUNE,    "TUNE"},
         { TX_DRIVE,     "RFPOWER"},
         { VFO,          "VFO"},
+        { VFOA,         "VFOA"},
+        { VFOB,         "VFOB"},
 	{ VFO_A2B,	"VFOA2B"},
 	{ VFO_B2A,	"VFOB2A"},
 	{ VOX,   	"VOX"},
diff --git a/midi3.c b/midi3.c
index 7bb8f0c..87f4213 100644
--- a/midi3.c
+++ b/midi3.c
@@ -28,6 +28,7 @@ void DoTheMidi(enum MIDIaction action, enum MIDItype type, int val) {
     int new;
     double dnew;
     double *dp;
+    int    *ip;
 
     switch (action) {
 	case SWAP_VFO:	// only key supported
@@ -40,6 +41,15 @@ void DoTheMidi(enum MIDIaction action, enum MIDItype type, int val) {
 		g_idle_add(ext_vfo_step, GINT_TO_POINTER(val));
 	    }
 	    break;
+	case VFOA: // only wheel supported
+	case VFOB: // only wheel supported
+	    if (type == MIDI_WHEEL) {
+	        ip=malloc(2*sizeof(int));
+		*ip = (action == VFOA) ? 0 : 1;   // could use (action - VFOA) to support even more VFOs
+		*(ip+1)=val;
+		g_idle_add(ext_vfo_id_step, ip);
+	    }
+	    break;
 	case MIDI_TUNE: // only key supported
 	    if (type == MIDI_KEY) {
 	        new = !tune;
diff --git a/vfo.c b/vfo.c
index 006c4e4..3693531 100644
--- a/vfo.c
+++ b/vfo.c
@@ -558,6 +558,26 @@ void vfo_step(int steps) {
     g_idle_add(ext_vfo_update,NULL);
   }
 }
+//
+// DL1YCF: essentially a duplicate of vfo_step but
+//         changing a specific VFO freq instead of
+//         changing the VFO of the active receiver
+//
+void vfo_id_step(int id, int steps) {
+  if(!locked) {
+    if(vfo[id].ctun) {
+      vfo[id].ctun_frequency=vfo[id].ctun_frequency+(steps*step);
+    } else {
+      vfo[id].frequency=vfo[id].frequency+(steps*step);
+    }
+    receiver_frequency_changed(active_receiver);
+#ifdef INCLUDED
+    BANDSTACK_ENTRY* entry=bandstack_entry_get_current();
+    setFrequency(active_receiver->frequency+(steps*step));
+#endif
+    g_idle_add(ext_vfo_update,NULL);
+  }
+}
 
 void vfo_move(long long hz) {
   int id=active_receiver->id;
diff --git a/vfo.h b/vfo.h
index c221885..dc9a5cd 100644
--- a/vfo.h
+++ b/vfo.h
@@ -65,6 +65,7 @@ extern char *step_labels[];
 
 extern GtkWidget* vfo_init(int width,int height,GtkWidget *parent);
 extern void vfo_step(int steps);
+extern void vfo_id_step(int id, int steps);
 extern void vfo_move(long long hz);
 extern void vfo_move_to(long long hz);
 extern void vfo_update();
-- 
2.45.2