From 5d7db6b516affc9a4e1c6e6f6cba393bbe49a077 Mon Sep 17 00:00:00 2001 From: Mike Black W9MDB Date: Wed, 8 Nov 2023 17:14:10 -0600 Subject: [PATCH] Fix FLRig SWR value Change float level print to %g format for better appearance https://github.com/Hamlib/Hamlib/issues/1417 --- rigs/dummy/flrig.c | 41 ++++++++++++++++++++++++++++++++++++++++- tests/rigctl_parse.c | 14 +++++++------- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/rigs/dummy/flrig.c b/rigs/dummy/flrig.c index 5c0b6fcd8..60dd8b5cf 100644 --- a/rigs/dummy/flrig.c +++ b/rigs/dummy/flrig.c @@ -23,6 +23,7 @@ #include #include #include /* String function definitions */ +#include #include #include @@ -138,7 +139,7 @@ const struct rig_caps flrig_caps = RIG_MODEL(RIG_MODEL_FLRIG), .model_name = "FLRig", .mfg_name = "FLRig", - .version = "20231107.0", + .version = "20231108.0", .copyright = "LGPL", .status = RIG_STATUS_STABLE, .rig_type = RIG_TYPE_TRANSCEIVER, @@ -2111,6 +2112,39 @@ static int flrig_set_level(RIG *rig, vfo_t vfo, setting_t level, value_t val) RETURNFUNC(RIG_OK); } +typedef struct { + float mtr; + float swr; +} swrpair; + +static swrpair swrtbl[] = { + {0.0, 1.0}, + {10.5, 1.5}, + {23.0, 2.0}, + {35.0, 2.5}, + {48.0, 3.0}, + {100.0, 10.0 } // assuming 10.0 is infinity for FLRig +}; + +// Function to interpolate SWR from MTR +float interpolateSWR(float mtr) { + int i; + for (i = 0; i < sizeof(swrtbl)/sizeof(swrpair) - 1; i++) { + if (mtr == swrtbl[i].mtr) { + // Exact match + return swrtbl[i].swr; + } + if (mtr < swrtbl[i + 1].mtr) { + // Perform linear interpolation + float slope = (swrtbl[i + 1].swr - swrtbl[i].swr) / (swrtbl[i + 1].mtr - swrtbl[i].mtr); + float swr = round((swrtbl[i].swr + slope * (mtr - swrtbl[i].mtr))*10)/10.0; + rig_debug(RIG_DEBUG_VERBOSE,"%s: swr=%f\n", __func__, swr); + return swr; + } + } + // If mtr is not within the range of values in the table, you could choose to return an error or extrapolate + return 10; // Example er +} /* * flrig_get_level * Assumes rig!=NULL, rig->state.priv!=NULL, val!=NULL @@ -2161,6 +2195,11 @@ static int flrig_get_level(RIG *rig, vfo_t vfo, setting_t level, value_t *val) // most levels are 0-100 -- may have to allow for different ranges switch (level) { + case RIG_LEVEL_SWR: + { + val->f = interpolateSWR(atoi(value)); + break; + } case RIG_LEVEL_STRENGTH: val->i = atoi(value) - 54; //if (val->i > 0) val->i /= 10; diff --git a/tests/rigctl_parse.c b/tests/rigctl_parse.c index 668034b8b..c47a87fe6 100644 --- a/tests/rigctl_parse.c +++ b/tests/rigctl_parse.c @@ -1952,7 +1952,7 @@ int print_conf_list(const struct confparams *cfp, rig_ptr_t data) cfp->u.n.step); break; case RIG_CONF_NUMERIC: - printf("\tRange: %.1f..%.1f, step %.1f\n", + printf("\tRange: %g..%g, step %.1f\n", cfp->u.n.min, cfp->u.n.max, cfp->u.n.step); @@ -3287,7 +3287,7 @@ declare_proto_rig(set_level) break; case RIG_CONF_NUMERIC: - CHKSCN1ARG(sscanf(arg2, "%f", &val.f)); + CHKSCN1ARG(sscanf(arg2, "%g", &val.f)); break; case RIG_CONF_STRING: @@ -3393,7 +3393,7 @@ declare_proto_rig(get_level) break; case RIG_CONF_NUMERIC: - fprintf(fout, "%f%c", val.f, resp_sep); + fprintf(fout, "%g%c", val.f, resp_sep); break; case RIG_CONF_INT: @@ -3445,7 +3445,7 @@ declare_proto_rig(get_level) if (RIG_LEVEL_IS_FLOAT(level)) { - fprintf(fout, "%f%c", val.f, resp_sep); + fprintf(fout, "%g%c", val.f, resp_sep); } else { @@ -3629,7 +3629,7 @@ declare_proto_rig(set_parm) break; case RIG_CONF_NUMERIC: - CHKSCN1ARG(sscanf(arg2, "%f", &val.f)); + CHKSCN1ARG(sscanf(arg2, "%g", &val.f)); break; case RIG_CONF_STRING: @@ -3747,7 +3747,7 @@ declare_proto_rig(get_parm) break; case RIG_CONF_NUMERIC: - fprintf(fout, "%f%c", val.f, resp_sep); + fprintf(fout, "%g%c", val.f, resp_sep); break; case RIG_CONF_STRING: @@ -4442,7 +4442,7 @@ int dump_chan(FILE *fout, RIG *rig, channel_t *chan) break; case RIG_CONF_NUMERIC: - SNPRINTF(lstr, sizeof(lstr), "%f", chan->ext_levels[idx].val.f); + SNPRINTF(lstr, sizeof(lstr), "%g", chan->ext_levels[idx].val.f); break; case RIG_CONF_CHECKBUTTON: