From ee13edb4e8dc3ae3c343f475858e2684617ae1ee Mon Sep 17 00:00:00 2001
From: Michael Black W9MDB <mdblack98@yahoo.com>
Date: Fri, 18 May 2018 07:29:55 -0500
Subject: [PATCH] Improve emulation performance and promote TS2000 to stable

---
 kenwood/kenwood.c | 19 +++++++++++++++++++
 kenwood/kenwood.h |  3 ++-
 kenwood/ts2000.c  |  2 +-
 3 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/kenwood/kenwood.c b/kenwood/kenwood.c
index 960fe1298..31078096e 100644
--- a/kenwood/kenwood.c
+++ b/kenwood/kenwood.c
@@ -218,6 +218,9 @@ int kenwood_transaction(RIG *rig, const char *cmdstr, char *data, size_t datasiz
   rs = &rig->state;
   rs->hold_decode = 1;
 
+  /* Emulators don't need any post_write_delay */
+  if (priv->is_emulation) rs->rigport.post_write_delay = 0;
+
   cmdtrm[0] = caps->cmdtrm;
   cmdtrm[1] = '\0';
 
@@ -496,6 +499,7 @@ int kenwood_init(RIG *rig)
   strcpy (priv->verify_cmd, RIG_MODEL_XG3 == rig->caps->rig_model ? ";" : "ID;");
   priv->split = RIG_SPLIT_OFF;
   priv->trn_state = -1;
+  priv->curr_mode = 0;
   rig->state.priv = priv;
 
   /* default mode_table */
@@ -713,6 +717,14 @@ int kenwood_set_vfo(RIG *rig, vfo_t vfo)
     return -RIG_EINVAL;
 
   struct kenwood_priv_data *priv = rig->state.priv;
+
+  /* Emulations do not need to set VFO since VFOB is a copy of VFOA
+   * except for frequency.  And we can change freq without changing VFOS
+   * This prevents a 1.8 second delay in PowerSDR when switching VFOs
+   * We'll do this once if curr_mode has not been set yet
+   */
+  if (priv->is_emulation && priv->curr_mode > 0) return RIG_OK;
+
   char cmdbuf[6];
   int retval;
   char vfo_function;
@@ -1564,6 +1576,12 @@ int kenwood_get_mode(RIG *rig, vfo_t vfo, rmode_t *mode, pbwidth_t *width)
   int offs;
   int retval;
 
+  /* for emulation do not read mode from VFOB as it is copy of VFOA */
+  /* we avoid the VFO swapping most of the time this way */
+  /* only need to get it if it has to be initialized */
+  if (priv->curr_mode > 0 && priv->is_emulation && vfo == RIG_VFO_B) {
+	  return priv->curr_mode;
+  }
   if (RIG_MODEL_TS990S == rig->caps->rig_model)
     {
       char c;
@@ -3345,6 +3363,7 @@ DECLARE_PROBERIG_BACKEND(kenwood)
         "with ID %03d, please report to Hamlib "
         "developers.\n", k_id);
 
+  rig_debug(RIG_DEBUG_TRACE, "%s: post_write_delay=%d\n", __func__, port->post_write_delay);
   return RIG_MODEL_NONE;
 }
 
diff --git a/kenwood/kenwood.h b/kenwood/kenwood.h
index cb6e4a84a..e93e06881 100644
--- a/kenwood/kenwood.h
+++ b/kenwood/kenwood.h
@@ -27,7 +27,7 @@
 #include <string.h>
 #include "token.h"
 
-#define BACKEND_VER "0.9"
+#define BACKEND_VER "1.0"
 
 #define EOM_KEN ';'
 #define EOM_TH '\r'
@@ -80,6 +80,7 @@ struct kenwood_priv_data {
     char verify_cmd[4];   /* command used to verify set commands */
     int is_emulation;     /* flag for TS-2000 emulations */
     void * data;          /* model specific data */
+    rmode_t curr_mode;     /* used for is_emulation to avoid get_mode on VFOB */
 };
 
 
diff --git a/kenwood/ts2000.c b/kenwood/ts2000.c
index 4f21e360e..04c71f7d1 100644
--- a/kenwood/ts2000.c
+++ b/kenwood/ts2000.c
@@ -126,7 +126,7 @@ const struct rig_caps ts2000_caps = {
 .mfg_name =  "Kenwood",
 .version =  BACKEND_VER ".4",
 .copyright =  "LGPL",
-.status =  RIG_STATUS_BETA,
+.status =  RIG_STATUS_STABLE,
 .rig_type =  RIG_TYPE_TRANSCEIVER,
 .ptt_type =  RIG_PTT_RIG,
 .dcd_type =  RIG_DCD_RIG,