From 6457dd2400d85b997aa392dcc2dafc4f51c97e7d Mon Sep 17 00:00:00 2001
From: c vw <dl1ycf@darc.de>
Date: Tue, 12 Nov 2019 19:28:51 +0100
Subject: [PATCH] added CW facility to MIDI

---
 midi.h  |  2 ++
 midi2.c | 49 +++++++++++++++++++++----------------------------
 midi3.c | 14 ++++++++++++++
 3 files changed, 37 insertions(+), 28 deletions(-)

diff --git a/midi.h b/midi.h
index ce88981..f84fde8 100644
--- a/midi.h
+++ b/midi.h
@@ -59,6 +59,8 @@ enum MIDIaction {
   COMPRESS,		// COMPRESS:		TX compressor value
   MIDI_CTUN,		// CTUN:		toggle CTUN on/off
   VFO,			// CURRVFO:		change VFO frequency
+  CWL,			// CWL:			Left paddle pressed (use with ONOFF)
+  CWR,			// CWR:			Right paddle pressed (use with ONOFF)
   MIDI_DUP,		// DUP:			toggle duplex on/off
   FILTER_DOWN,		// FILTERDOWN:		cycle through filters downwards
   FILTER_UP,		// FILTERUP:		cycle through filters upwards
diff --git a/midi2.c b/midi2.c
index 10a1ebc..c29024a 100644
--- a/midi2.c
+++ b/midi2.c
@@ -21,21 +21,21 @@ void NewMidiEvent(enum MIDIevent event, int channel, int note, int val) {
     static struct timespec tp, last_wheel_tp={0,0};
     long delta;
 
-fprintf(stderr,"MIDI:EVENT=%d CHAN=%d NOTE=%d VAL=%d\n",event,channel,note,val);
+//fprintf(stderr,"MIDI:EVENT=%d CHAN=%d NOTE=%d VAL=%d\n",event,channel,note,val);
     if (event == MIDI_PITCH) {
 	desc=MidiCommandsTable.pitch;
     } else {
 	desc=MidiCommandsTable.desc[note];
     }
-fprintf(stderr,"MIDI:init DESC=%p\n",desc);
+//fprintf(stderr,"MIDI:init DESC=%p\n",desc);
     while (desc) {
-fprintf(stderr,"DESC=%p next=%p CHAN=%d EVENT=%d\n", desc,desc->next,desc->channel,desc->event);
+//fprintf(stderr,"DESC=%p next=%p CHAN=%d EVENT=%d\n", desc,desc->next,desc->channel,desc->event);
 	if ((desc->channel == channel || desc->channel == -1) && (desc->event == event)) {
 	    // Found matching entry
 	    switch (desc->event) {
 		case MIDI_NOTE:
-		    if ((val == 1 || (val == 0 && desc->onoff == 1)) && desc->type == MIDI_KEY) {
-			DoTheMidi(desc->action, desc->type, 0);
+		    if ((val == 1 || (desc->onoff == 1)) && desc->type == MIDI_KEY) {
+			DoTheMidi(desc->action, desc->type, val);
 		    }
 		    break;
 		case MIDI_CTRL:
@@ -112,6 +112,8 @@ static struct {
         { COMPRESS,     	"COMPRESS"},
 	{ MIDI_CTUN,  		"CTUN"},
 	{ VFO,			"CURRVFO"},
+	{ CWL,			"CWL"},
+	{ CWR,			"CWR"},
 	{ MIDI_DUP,  		"DUP"},
         { FILTER_DOWN,  	"FILTERDOWN"},
         { FILTER_UP,    	"FILTERUP"},
@@ -214,7 +216,7 @@ void MIDIstartup() {
 	cp++;
       }
       
-fprintf(stderr,"\nMIDI:INP:%s\n",zeile);
+//fprintf(stderr,"\nMIDI:INP:%s\n",zeile);
 
       if ((cp = strstr(zeile, "DEVICE="))) {
         // Delete comments and trailing blanks
@@ -223,7 +225,7 @@ fprintf(stderr,"\nMIDI:INP:%s\n",zeile);
 	*cq--=0;
 	while (cq > cp+7 && (*cq == ' ' || *cq == '\t')) cq--;
 	*(cq+1)=0;
-fprintf(stderr,"MIDI:REG:>>>%s<<<\n",cp+7);
+//fprintf(stderr,"MIDI:REG:>>>%s<<<\n",cp+7);
 	register_midi_device(cp+7);
         continue; // nothing more in this line
       }
@@ -245,24 +247,24 @@ fprintf(stderr,"MIDI:REG:>>>%s<<<\n",cp+7);
         sscanf(cp+4, "%d", &key);
         event=MIDI_NOTE;
 	type=MIDI_KEY;
-fprintf(stderr,"MIDI:KEY:%d\n", key);
+//fprintf(stderr,"MIDI:KEY:%d\n", key);
       }
       if ((cp = strstr(zeile, "CTRL="))) {
         sscanf(cp+5, "%d", &key);
 	event=MIDI_CTRL;
 	type=MIDI_KNOB;
-fprintf(stderr,"MIDI:CTL:%d\n", key);
+//fprintf(stderr,"MIDI:CTL:%d\n", key);
       }
       if ((cp = strstr(zeile, "PITCH "))) {
         event=MIDI_PITCH;
 	type=MIDI_KNOB;
-fprintf(stderr,"MIDI:PITCH\n");
+//fprintf(stderr,"MIDI:PITCH\n");
       }
       //
       // If event is still undefined, skip line
       //
       if (event == EVENT_NONE) {
-fprintf(stderr,"MIDI:ERR:NO_EVENT\n");
+//fprintf(stderr,"MIDI:ERR:NO_EVENT\n");
 	continue;
       }
 
@@ -276,25 +278,25 @@ fprintf(stderr,"MIDI:ERR:NO_EVENT\n");
         sscanf(cp+5, "%d", &chan);
 	chan--;
         if (chan<0 || chan>15) chan=-1;
-fprintf(stderr,"MIDI:CHA:%d\n",chan);
+//fprintf(stderr,"MIDI:CHA:%d\n",chan);
       }
       if ((cp = strstr(zeile, "WHEEL")) && (type == MIDI_KNOB)) {
 	// change type from MIDI_KNOB to MIDI_WHEEL
         type=MIDI_WHEEL;
-fprintf(stderr,"MIDI:WHEEL\n");
+//fprintf(stderr,"MIDI:WHEEL\n");
       }
       if ((cp = strstr(zeile, "ONOFF"))) {
         onoff=1;
-fprintf(stderr,"MIDI:ONOFF\n");
+//fprintf(stderr,"MIDI:ONOFF\n");
       }
       if ((cp = strstr(zeile, "DELAY="))) {
         sscanf(cp+6, "%d", &delay);
-fprintf(stderr,"MIDI:DELAY:%d\n",delay);
+//fprintf(stderr,"MIDI:DELAY:%d\n",delay);
       }
       if ((cp = strstr(zeile, "THR="))) {
         sscanf(cp+4, "%d %d %d %d %d %d %d %d %d %d %d %d",
                &t1,&t2,&t3,&t4,&t5,&t6,&t7,&t8,&t9,&t10,&t11,&t12);
-fprintf(stderr,"MIDI:THR:%d/%d, %d/%d, %d/%d, %d/%d, %d/%d, %d/%d\n",t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12);
+//fprintf(stderr,"MIDI:THR:%d/%d, %d/%d, %d/%d, %d/%d, %d/%d, %d/%d\n",t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12);
       }
       if ((cp = strstr(zeile, "ACTION="))) {
         // cut zeile at the first blank character following
@@ -302,17 +304,8 @@ fprintf(stderr,"MIDI:THR:%d/%d, %d/%d, %d/%d, %d/%d, %d/%d, %d/%d\n",t1,t2,t3,t4
         while (*cq != 0 && *cq != '\n' && *cq != ' ' && *cq != '\t') cq++;
 	*cq=0;
         action=keyword2action(cp+7);
-fprintf(stderr,"MIDI:ACTION:%s (%d)\n",cp+7, action);
+//fprintf(stderr,"MIDI:ACTION:%s (%d)\n",cp+7, action);
       }
-#if 0
-  fprintf(stderr,"K=%d C=%d T=%d E=%d A=%d OnOff=%d\n",key,chan,type, event, action, onoff);
-  if (t1 <= t2 ) fprintf(stderr,"Range for very fast  left: %d -- %d\n",t1,t2);
-  if (t3 <= t4 ) fprintf(stderr,"Range for      fast  left: %d -- %d\n",t3,t4);
-  if (t5 <= t6 ) fprintf(stderr,"Range for    normal  left: %d -- %d\n",t5,t6);
-  if (t7 <= t8 ) fprintf(stderr,"Range for    normal right: %d -- %d\n",t7,t8);
-  if (t9 <= t10) fprintf(stderr,"Range for      fast right: %d -- %d\n",t9,t10);
-  if (t11<= t12) fprintf(stderr,"Range for very fast right: %d -- %d\n",t11,t12);
-#endif
       //
       // All data for a descriptor has been read. Construct it!
       //
@@ -341,7 +334,7 @@ fprintf(stderr,"MIDI:ACTION:%s (%d)\n",cp+7, action);
       // We have a linked list for each key value to speed up searches
       //
       if (event == MIDI_PITCH) {
-fprintf(stderr,"MIDI:TAB:Insert desc=%p in PITCH table\n",desc);
+//fprintf(stderr,"MIDI:TAB:Insert desc=%p in PITCH table\n",desc);
 	dp = MidiCommandsTable.pitch;
 	if (dp == NULL) {
 	  MidiCommandsTable.pitch = desc;
@@ -351,7 +344,7 @@ fprintf(stderr,"MIDI:TAB:Insert desc=%p in PITCH table\n",desc);
 	}
       }
       if (event == MIDI_KEY || event == MIDI_CTRL) {
-fprintf(stderr,"MIDI:TAB:Insert desc=%p in CMDS[%d] table\n",desc,key);
+//fprintf(stderr,"MIDI:TAB:Insert desc=%p in CMDS[%d] table\n",desc,key);
 	dp = MidiCommandsTable.desc[key];
 	if (dp == NULL) {
 	  MidiCommandsTable.desc[key]=desc;
diff --git a/midi3.c b/midi3.c
index bdf5748..f2c9409 100644
--- a/midi3.c
+++ b/midi3.c
@@ -22,6 +22,9 @@
 #include "ext.h"
 #include "agc.h"
 #include "midi.h"
+#ifdef LOCALCW
+#include "iambic.h"
+#endif
 
 void DoTheMidi(enum MIDIaction action, enum MIDItype type, int val) {
 
@@ -193,6 +196,17 @@ void DoTheMidi(enum MIDIaction action, enum MIDItype type, int val) {
 		g_idle_add(ext_vfo_step, GINT_TO_POINTER(val));
 	    }
 	    break;
+	/////////////////////////////////////////////////////////// "CWL"
+	/////////////////////////////////////////////////////////// "CWR"
+	case CWL: // only key
+	case CWR: // only key
+#ifdef LOCALCW
+	    if (type == MIDI_KEY) {
+		new=(action == CWL);
+		keyer_event(new,val);
+	    }
+#endif
+	    break;
 	/////////////////////////////////////////////////////////// "DUP"
         case MIDI_DUP:
             if(duplex) {
-- 
2.45.2