From 4f2f5bff73b55f3858094055d1fd8a3da1e5159e Mon Sep 17 00:00:00 2001
From: c vw <dl1ycf@darc.de>
Date: Fri, 15 Jan 2021 09:42:47 +0100
Subject: [PATCH] polished MIDI CWKEY message handling

---
 iambic.c | 23 ++++++++++++++++++-----
 iambic.h |  1 +
 midi3.c  | 28 ++++++++++++++--------------
 3 files changed, 33 insertions(+), 19 deletions(-)

diff --git a/iambic.c b/iambic.c
index f9e27ed..6ce933f 100644
--- a/iambic.c
+++ b/iambic.c
@@ -248,6 +248,22 @@ int gpio_cw_sidetone_enabled() { return 0; }
 void gpio_cw_sidetone_set(int level) {}
 #endif
 
+void keyer_straight_key(state) {
+  //
+  // Interface for simple key-down action e.g. from a MIDI message
+  //
+  if (state != 0) {
+    cw_key_down=960000;  // max. 20 sec to protect hardware
+    cw_key_up=0;
+    cw_key_active=1;
+    gpio_cw_sidetone_set(1);
+  } else {
+    cw_key_down=0;
+    cw_key_up=0;
+    gpio_cw_sidetone_set(0);
+  }
+}
+
 void keyer_update() {
     //
     // This function will take notice of changes in the following variables
@@ -441,9 +457,7 @@ static void* keyer_thread(void *arg) {
                     // If both paddles are pressed (should not happen), then
                     // the dash paddle wins.
                     if (*kdash) {                  // send manual dashes
-                      cw_key_down=960000;            // 20 sec maximum
-                      cw_key_up=0;
-                      gpio_cw_sidetone_set(1);
+                      keyer_straight_key(1);       // do key down
                       key_state=STRAIGHT;
                     }
                 } else {
@@ -461,8 +475,7 @@ static void* keyer_thread(void *arg) {
 		// Wait for dash paddle being released in "straight key" mode.
                 //
                 if (! *kdash) {
-                  cw_key_down=0;
-                  gpio_cw_sidetone_set(0);
+                  keyer_straight_key(0);   // key-up
                   key_state=CHECK;
                 }
                 break;
diff --git a/iambic.h b/iambic.h
index 91fcd43..f1b381a 100644
--- a/iambic.h
+++ b/iambic.h
@@ -14,6 +14,7 @@ enum {
     EXITLOOP
 };
 
+void keyer_straight_key(state);
 void keyer_event(int left, int state);
 void keyer_update();
 void keyer_close();
diff --git a/midi3.c b/midi3.c
index 0e53ed6..751bd20 100644
--- a/midi3.c
+++ b/midi3.c
@@ -220,23 +220,23 @@ void DoTheMidi(enum MIDIaction action, enum MIDItype type, int val) {
 	/////////////////////////////////////////////////////////// "CW"
 	case MIDI_ACTION_CWKEY: // only key
 	  //
-	  // This is a CW key-up/down which works outside the keyer.
-	  // It is intended for the use of external keyers
-	  // and works if not compiled with LOCALCW
-	  // and/or using "CW handled in radio". 
-	  // NO BREAK-IN! The keyer has to send a MIDI "PTT on" command and
-	  // wait a decent amount of time before doing key-down.
+	  // This is a CW key-up/down which uses functions from the keyer
+	  // that by-pass the interrupt-driven standard action.
+	  // It is intended to
+	  // be used with external keyers that send key-up/down messages via
+	  // MIDI using this command.
+	  //
+	  // NO BREAK-IN! The keyer has to take care of sending MIDI PTT
+	  // on/off messages at appropriate times.
           //
+#ifdef LOCALCW
 	    if (type == MIDI_TYPE_KEY) {
-              if (val != 0) {
-		cw_key_down=960000;  // max. 20 sec to protect hardware
-		cw_key_up=0;
-		cw_key_hit=1;        // abort any pending CAT CW messages
-	      } else {
-		cw_key_down=0;
-		cw_key_up=0;
-	      }
+              keyer_straight_key(val);
             }
+#else
+	    g_print("%s: CWKEY:%d\n",__FUNCTION__,val);
+
+#endif
 	    break;
 	/////////////////////////////////////////////////////////// "CWL"
 	/////////////////////////////////////////////////////////// "CWR"
-- 
2.45.2