]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/saleae-logic16/api.c
drivers: Factor out std_*_idx*().
[libsigrok.git] / src / hardware / saleae-logic16 / api.c
index 00a4c1bc9b87d0d045ef887a0a5bf35e301ea985..e66d845b9f6d41189fa4dddbaed8bcd517cba7b9 100644 (file)
@@ -77,7 +77,7 @@ static const struct {
        { VOLTAGE_RANGE_5_V, },
 };
 
-static const struct voltage_threshold volt_thresholds[] = {
+static const double volt_thresholds[][2] = {
        { 0.7, 1.4 },
        { 1.4, 3.6 },
 };
@@ -441,7 +441,7 @@ static int config_get(uint32_t key, GVariant **data,
                        if (devc->selected_voltage_range !=
                            volt_thresholds_ranges[i].range)
                                continue;
-                       *data = std_gvar_tuple_double(volt_thresholds[i].low, volt_thresholds[i].high);
+                       *data = std_gvar_tuple_double(volt_thresholds[i][0], volt_thresholds[i][1]);
                        ret = SR_OK;
                        break;
                }
@@ -457,9 +457,7 @@ static int config_set(uint32_t key, GVariant *data,
        const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
-       gdouble low, high;
-       int ret;
-       unsigned int i;
+       int ret, idx;
 
        (void)cg;
 
@@ -478,17 +476,9 @@ static int config_set(uint32_t key, GVariant *data,
                ret = (devc->capture_ratio > 100) ? SR_ERR : SR_OK;
                break;
        case SR_CONF_VOLTAGE_THRESHOLD:
-               g_variant_get(data, "(dd)", &low, &high);
-               ret = SR_ERR_ARG;
-               for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
-                       if (fabs(volt_thresholds[i].low - low) < 0.1 &&
-                           fabs(volt_thresholds[i].high - high) < 0.1) {
-                               devc->selected_voltage_range =
-                                       volt_thresholds_ranges[i].range;
-                               ret = SR_OK;
-                               break;
-                       }
-               }
+               if ((idx = std_double_tuple_idx(data, ARRAY_AND_SIZE(volt_thresholds))) < 0)
+                       return SR_ERR_ARG;
+               devc->selected_voltage_range = volt_thresholds_ranges[idx].range;
                break;
        default:
                ret = SR_ERR_NA;