]> sigrok.org Git - libsigrok.git/commitdiff
drivers: Factor out std_gvar_thresholds().
authorUwe Hermann <redacted>
Tue, 25 Jul 2017 18:09:42 +0000 (20:09 +0200)
committerUwe Hermann <redacted>
Mon, 31 Jul 2017 14:23:31 +0000 (16:23 +0200)
src/hardware/dreamsourcelab-dslogic/api.c
src/hardware/saleae-logic16/api.c
src/libsigrok-internal.h
src/std.c

index e57477f3f6da074a4c268f0f5187a3b88d0a1f21..d7e1b15f1eb5366ae8b2fe4f49663302b66ecc15 100644 (file)
@@ -72,10 +72,7 @@ static const char *const signal_edge_names[] = {
        [DS_EDGE_FALLING] = "falling",
 };
 
-static const struct {
-       gdouble low;
-       gdouble high;
-} voltage_thresholds[] = {
+static const struct voltage_threshold voltage_thresholds[] = {
        { 0.7, 1.4 },
        { 1.4, 3.6 },
 };
@@ -545,9 +542,6 @@ static int config_list(uint32_t key, GVariant **data,
        const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
-       GVariant *gvar, *range[2];
-       GVariantBuilder gvb;
-       unsigned int i;
 
        devc = (sdi) ? sdi->priv : NULL;
 
@@ -556,18 +550,10 @@ static int config_list(uint32_t key, GVariant **data,
        case SR_CONF_DEVICE_OPTIONS:
                return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
        case SR_CONF_VOLTAGE_THRESHOLD:
-               if (!strcmp(devc->profile->model, "DSLogic")) {
-                       g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
-                       for (i = 0; i < ARRAY_SIZE(voltage_thresholds); i++) {
-                               range[0] = g_variant_new_double(voltage_thresholds[i].low);
-                               range[1] = g_variant_new_double(voltage_thresholds[i].high);
-                               gvar = g_variant_new_tuple(range, 2);
-                               g_variant_builder_add_value(&gvb, gvar);
-                       }
-                       *data = g_variant_builder_end(&gvb);
-               } else {
+               if (!strcmp(devc->profile->model, "DSLogic"))
+                       *data = std_gvar_thresholds(ARRAY_AND_SIZE(voltage_thresholds));
+               else
                        *data = std_gvar_min_max_step_thresholds(0.0, 5.0, 0.1);
-               }
                break;
        case SR_CONF_SAMPLERATE:
                *data = std_gvar_samplerates(devc->samplerates, devc->num_samplerates);
index 6a1161c9bf8715d14ac73888c6042ce06ac64f94..ecb2953b1b56616105564c881378e5076da9293d 100644 (file)
@@ -72,11 +72,14 @@ static const char *channel_names[] = {
 
 static const struct {
        enum voltage_range range;
-       gdouble low;
-       gdouble high;
-} volt_thresholds[] = {
-       { VOLTAGE_RANGE_18_33_V, 0.7, 1.4 },
-       { VOLTAGE_RANGE_5_V,     1.4, 3.6 },
+} volt_thresholds_ranges[] = {
+       { VOLTAGE_RANGE_18_33_V, },
+       { VOLTAGE_RANGE_5_V, },
+};
+
+static const struct voltage_threshold volt_thresholds[] = {
+       { 0.7, 1.4 },
+       { 1.4, 3.6 },
 };
 
 static const uint64_t samplerates[] = {
@@ -438,7 +441,7 @@ static int config_get(uint32_t key, GVariant **data,
                ret = SR_ERR;
                for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
                        if (devc->selected_voltage_range !=
-                           volt_thresholds[i].range)
+                           volt_thresholds_ranges[i].range)
                                continue;
                        *data = std_gvar_tuple_double(volt_thresholds[i].low, volt_thresholds[i].high);
                        ret = SR_OK;
@@ -483,7 +486,7 @@ static int config_set(uint32_t key, GVariant *data,
                        if (fabs(volt_thresholds[i].low - low) < 0.1 &&
                            fabs(volt_thresholds[i].high - high) < 0.1) {
                                devc->selected_voltage_range =
-                                       volt_thresholds[i].range;
+                                       volt_thresholds_ranges[i].range;
                                ret = SR_OK;
                                break;
                        }
@@ -499,10 +502,6 @@ static int config_set(uint32_t key, GVariant *data,
 static int config_list(uint32_t key, GVariant **data,
        const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
-       GVariant *gvar, *range[2];
-       GVariantBuilder gvb;
-       unsigned int i;
-
        switch (key) {
        case SR_CONF_SCAN_OPTIONS:
        case SR_CONF_DEVICE_OPTIONS:
@@ -511,14 +510,7 @@ static int config_list(uint32_t key, GVariant **data,
                *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates));
                break;
        case SR_CONF_VOLTAGE_THRESHOLD:
-               g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
-               for (i = 0; i < ARRAY_SIZE(volt_thresholds); i++) {
-                       range[0] = g_variant_new_double(volt_thresholds[i].low);
-                       range[1] = g_variant_new_double(volt_thresholds[i].high);
-                       gvar = g_variant_new_tuple(range, 2);
-                       g_variant_builder_add_value(&gvb, gvar);
-               }
-               *data = g_variant_builder_end(&gvb);
+               *data = std_gvar_thresholds(ARRAY_AND_SIZE(volt_thresholds));
                break;
        case SR_CONF_TRIGGER_MATCH:
                *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
index 5566b6288b4d7cc770effd71d7e7b4fa88141315..97422c8ac2708a96d2648878e0a00f1d5608fbc7 100644 (file)
@@ -971,6 +971,13 @@ SR_PRIV GVariant *std_gvar_array_i32(const int32_t *a, unsigned int n);
 SR_PRIV GVariant *std_gvar_array_u32(const uint32_t *a, unsigned int n);
 SR_PRIV GVariant *std_gvar_array_u64(const uint64_t *a, unsigned int n);
 
+struct voltage_threshold {
+        double low;
+        double high;
+};
+
+SR_PRIV GVariant *std_gvar_thresholds(const struct voltage_threshold a[], unsigned int n);
+
 /*--- resource.c ------------------------------------------------------------*/
 
 SR_PRIV int64_t sr_file_get_size(FILE *file);
index 3561c590bbe60d0d74f58de55ab1ca53b583e809..33e926563b2df1f6c0b82a181d017207b7f8953f 100644 (file)
--- a/src/std.c
+++ b/src/std.c
@@ -684,3 +684,21 @@ SR_PRIV GVariant *std_gvar_array_u64(const uint64_t *a, unsigned int n)
        return g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
                                a, n, sizeof(uint64_t));
 }
+
+SR_PRIV GVariant *std_gvar_thresholds(const struct voltage_threshold a[], unsigned int n)
+{
+       unsigned int i;
+       GVariant *gvar, *range[2];
+       GVariantBuilder gvb;
+
+       g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
+
+       for (i = 0; i < n; i++) {
+               range[0] = g_variant_new_double(a[i].low);
+               range[1] = g_variant_new_double(a[i].high);
+               gvar = g_variant_new_tuple(range, 2);
+               g_variant_builder_add_value(&gvb, gvar);
+       }
+
+       return g_variant_builder_end(&gvb);
+}