]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/saleae-logic16/api.c
drivers: Use g_variant_new_printf() where possible.
[libsigrok.git] / src / hardware / saleae-logic16 / api.c
index 2aca1fac54038c60c3a21dc0900e4a337d793e12..00a4c1bc9b87d0d045ef887a0a5bf35e301ea985 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[] = {
@@ -395,13 +398,11 @@ static int dev_close(struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
-               const struct sr_channel_group *cg)
+static int config_get(uint32_t key, GVariant **data,
+       const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
        struct sr_usb_dev_inst *usb;
-       GVariant *range[2];
-       char str[128];
        int ret;
        unsigned int i;
 
@@ -417,8 +418,7 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
                        /* Device still needs to re-enumerate after firmware
                         * upload, so we don't know its (future) address. */
                        return SR_ERR;
-               snprintf(str, 128, "%d.%d", usb->bus, usb->address);
-               *data = g_variant_new_string(str);
+               *data = g_variant_new_printf("%d.%d", usb->bus, usb->address);
                break;
        case SR_CONF_SAMPLERATE:
                if (!sdi)
@@ -439,11 +439,9 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
                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;
-                       range[0] = g_variant_new_double(volt_thresholds[i].low);
-                       range[1] = g_variant_new_double(volt_thresholds[i].high);
-                       *data = g_variant_new_tuple(range, 2);
+                       *data = std_gvar_tuple_double(volt_thresholds[i].low, volt_thresholds[i].high);
                        ret = SR_OK;
                        break;
                }
@@ -455,8 +453,8 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
        return ret;
 }
 
-static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
-               const struct sr_channel_group *cg)
+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;
@@ -486,7 +484,7 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
                        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,13 +497,9 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
        return ret;
 }
 
-static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
-               const struct sr_channel_group *cg)
+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:
@@ -514,14 +508,7 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *
                *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));