From: Gerhard Sittig Date: Sat, 22 Jan 2022 10:13:18 +0000 (+0100) Subject: kingst-la2016: unobfuscate "user specified threshold" reference X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=da2cb50d7311fe66a50169e33ee1be9f7b901709;p=libsigrok.git kingst-la2016: unobfuscate "user specified threshold" reference Use a symbolic name for the "user specified input threshold" item's position in the set of choices. Stick with array size for the complete list of items instead of hiding the size behind an unrelated name. This improves readability of the config get/set/list routines. Trim a text line length while we are here. The single variable for several purposes (user specified value, and fixed value derived from currently selected choice out of a premade set) still looks suspicious, and is not addressed in this commit. --- diff --git a/src/hardware/kingst-la2016/api.c b/src/hardware/kingst-la2016/api.c index 53a6bb49..4337bd0a 100644 --- a/src/hardware/kingst-la2016/api.c +++ b/src/hardware/kingst-la2016/api.c @@ -125,7 +125,7 @@ static const char *logic_threshold[] = { "USER", }; -#define MAX_NUM_LOGIC_THRESHOLD_ENTRIES ARRAY_SIZE(logic_threshold) +#define LOGIC_THRESHOLD_IDX_USER (ARRAY_SIZE(logic_threshold) - 1) static GSList *scan(struct sr_dev_driver *di, GSList *options) { @@ -412,6 +412,7 @@ static int config_get(uint32_t key, GVariant **data, struct dev_context *devc; struct sr_usb_dev_inst *usb; double rounded; + const char *label; (void)cg; @@ -447,7 +448,8 @@ static int config_get(uint32_t key, GVariant **data, *data = std_gvar_tuple_double(rounded, rounded + 0.1); return SR_OK; case SR_CONF_LOGIC_THRESHOLD: - *data = g_variant_new_string(logic_threshold[devc->threshold_voltage_idx]); + label = logic_threshold[devc->threshold_voltage_idx]; + *data = g_variant_new_string(label); break; case SR_CONF_LOGIC_THRESHOLD_CUSTOM: *data = g_variant_new_double(devc->threshold_voltage); @@ -484,14 +486,12 @@ static int config_set(uint32_t key, GVariant *data, case SR_CONF_VOLTAGE_THRESHOLD: g_variant_get(data, "(dd)", &low, &high); devc->threshold_voltage = (low + high) / 2.0; - devc->threshold_voltage_idx = MAX_NUM_LOGIC_THRESHOLD_ENTRIES - 1; /* USER */ + devc->threshold_voltage_idx = LOGIC_THRESHOLD_IDX_USER; break; case SR_CONF_LOGIC_THRESHOLD: { - if ((idx = std_str_idx(data, logic_threshold, MAX_NUM_LOGIC_THRESHOLD_ENTRIES)) < 0) + if ((idx = std_str_idx(data, ARRAY_AND_SIZE(logic_threshold))) < 0) return SR_ERR_ARG; - if (idx == MAX_NUM_LOGIC_THRESHOLD_ENTRIES - 1) { - /* user threshold */ - } else { + if (idx != LOGIC_THRESHOLD_IDX_USER) { devc->threshold_voltage = logic_threshold_value[idx]; } devc->threshold_voltage_idx = idx; @@ -538,7 +538,7 @@ static int config_list(uint32_t key, GVariant **data, *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches)); break; case SR_CONF_LOGIC_THRESHOLD: - *data = g_variant_new_strv(logic_threshold, MAX_NUM_LOGIC_THRESHOLD_ENTRIES); + *data = g_variant_new_strv(ARRAY_AND_SIZE(logic_threshold)); break; default: return SR_ERR_NA;