From: Uwe Hermann Date: Thu, 20 Jul 2017 20:03:55 +0000 (+0200) Subject: drivers: Factor out std_gvar_min_max_step{,_array}(). X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=54d471f4980f1b975188b5e8bdf5aa90acd3cece;p=libsigrok.git drivers: Factor out std_gvar_min_max_step{,_array}(). --- diff --git a/src/hardware/arachnid-labs-re-load-pro/api.c b/src/hardware/arachnid-labs-re-load-pro/api.c index 11a6e305..48d3cb8f 100644 --- a/src/hardware/arachnid-labs-re-load-pro/api.c +++ b/src/hardware/arachnid-labs-re-load-pro/api.c @@ -150,8 +150,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { - GVariantBuilder gvb; - if (!cg) { return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); } else { @@ -161,12 +159,7 @@ static int config_list(uint32_t key, GVariant **data, devopts_cg, ARRAY_SIZE(devopts_cg), sizeof(uint32_t)); break; case SR_CONF_CURRENT_LIMIT: - g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); - /* Min, max, step. */ - g_variant_builder_add_value(&gvb, g_variant_new_double(0.0)); - g_variant_builder_add_value(&gvb, g_variant_new_double(6.0)); - g_variant_builder_add_value(&gvb, g_variant_new_double(0.001)); /* 1mA steps */ - *data = g_variant_builder_end(&gvb); + *data = std_gvar_min_max_step(0.0, 6.0, 0.001); break; default: return SR_ERR_NA; diff --git a/src/hardware/atten-pps3xxx/api.c b/src/hardware/atten-pps3xxx/api.c index 0a16ca5f..0f27b4f8 100644 --- a/src/hardware/atten-pps3xxx/api.c +++ b/src/hardware/atten-pps3xxx/api.c @@ -332,9 +332,7 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * { struct dev_context *devc; struct sr_channel *ch; - GVariant *gvar; - GVariantBuilder gvb; - int channel, i; + int channel; devc = (sdi) ? sdi->priv : NULL; @@ -366,22 +364,10 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * devopts_cg, ARRAY_SIZE(devopts_cg), sizeof(uint32_t)); break; case SR_CONF_VOLTAGE_TARGET: - g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); - /* Min, max, step. */ - for (i = 0; i < 3; i++) { - gvar = g_variant_new_double(devc->model->channels[channel].voltage[i]); - g_variant_builder_add_value(&gvb, gvar); - } - *data = g_variant_builder_end(&gvb); + *data = std_gvar_min_max_step_array(devc->model->channels[channel].voltage); break; case SR_CONF_CURRENT_LIMIT: - g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); - /* Min, max, step. */ - for (i = 0; i < 3; i++) { - gvar = g_variant_new_double(devc->model->channels[channel].current[i]); - g_variant_builder_add_value(&gvb, gvar); - } - *data = g_variant_builder_end(&gvb); + *data = std_gvar_min_max_step_array(devc->model->channels[channel].current); break; default: return SR_ERR_NA; diff --git a/src/hardware/korad-kaxxxxp/api.c b/src/hardware/korad-kaxxxxp/api.c index acd50961..c5c75da1 100644 --- a/src/hardware/korad-kaxxxxp/api.c +++ b/src/hardware/korad-kaxxxxp/api.c @@ -271,10 +271,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; - GVariantBuilder gvb; - double dval; - int idx; devc = (sdi) ? sdi->priv : NULL; @@ -283,24 +279,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_TARGET: - g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); - /* Min, max, step. */ - for (idx = 0; idx < 3; idx++) { - dval = devc->model->voltage[idx]; - gvar = g_variant_new_double(dval); - g_variant_builder_add_value(&gvb, gvar); - } - *data = g_variant_builder_end(&gvb); + *data = std_gvar_min_max_step_array(devc->model->voltage); break; case SR_CONF_CURRENT_LIMIT: - g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); - /* Min, max, step. */ - for (idx = 0; idx < 3; idx++) { - dval = devc->model->current[idx]; - gvar = g_variant_new_double(dval); - g_variant_builder_add_value(&gvb, gvar); - } - *data = g_variant_builder_end(&gvb); + *data = std_gvar_min_max_step_array(devc->model->current); break; default: return SR_ERR_NA; diff --git a/src/hardware/manson-hcs-3xxx/api.c b/src/hardware/manson-hcs-3xxx/api.c index ab1e3b9b..6eaef2d0 100644 --- a/src/hardware/manson-hcs-3xxx/api.c +++ b/src/hardware/manson-hcs-3xxx/api.c @@ -266,11 +266,8 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { + const double *a; struct dev_context *devc; - GVariant *gvar; - GVariantBuilder gvb; - double dval; - int idx; devc = (sdi) ? sdi->priv : NULL; @@ -279,30 +276,12 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * case SR_CONF_DEVICE_OPTIONS: return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); case SR_CONF_VOLTAGE_TARGET: - g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); - /* Min, max, step. */ - for (idx = 0; idx < 3; idx++) { - if (idx == 1) - dval = devc->voltage_max_device; - else - dval = devc->model->voltage[idx]; - gvar = g_variant_new_double(dval); - g_variant_builder_add_value(&gvb, gvar); - } - *data = g_variant_builder_end(&gvb); + a = devc->model->voltage; + *data = std_gvar_min_max_step(a[0], devc->voltage_max_device, a[2]); break; case SR_CONF_CURRENT_LIMIT: - g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); - /* Min, max, step. */ - for (idx = 0; idx < 3; idx++) { - if (idx == 1) - dval = devc->current_max_device; - else - dval = devc->model->current[idx]; - gvar = g_variant_new_double(dval); - g_variant_builder_add_value(&gvb, gvar); - } - *data = g_variant_builder_end(&gvb); + a = devc->model->current; + *data = std_gvar_min_max_step(a[0], devc->current_max_device, a[2]); break; default: return SR_ERR_NA; diff --git a/src/hardware/maynuo-m97/api.c b/src/hardware/maynuo-m97/api.c index 970e8437..9d0ddbe4 100644 --- a/src/hardware/maynuo-m97/api.c +++ b/src/hardware/maynuo-m97/api.c @@ -360,7 +360,6 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * const struct sr_channel_group *cg) { struct dev_context *devc; - GVariantBuilder gvb; devc = (sdi) ? sdi->priv : NULL; @@ -373,20 +372,10 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * devopts_cg, ARRAY_SIZE(devopts_cg), sizeof(uint32_t)); break; case SR_CONF_VOLTAGE_TARGET: - g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); - /* Min, max, write resolution. */ - g_variant_builder_add_value(&gvb, g_variant_new_double(0.0)); - g_variant_builder_add_value(&gvb, g_variant_new_double(devc->model->max_voltage)); - g_variant_builder_add_value(&gvb, g_variant_new_double(0.001)); - *data = g_variant_builder_end(&gvb); + *data = std_gvar_min_max_step(0.0, devc->model->max_voltage, 0.001); break; case SR_CONF_CURRENT_LIMIT: - g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); - /* Min, max, step. */ - g_variant_builder_add_value(&gvb, g_variant_new_double(0.0)); - g_variant_builder_add_value(&gvb, g_variant_new_double(devc->model->max_current)); - g_variant_builder_add_value(&gvb, g_variant_new_double(0.0001)); - *data = g_variant_builder_end(&gvb); + *data = std_gvar_min_max_step(0.0, devc->model->max_current, 0.0001); break; default: return SR_ERR_NA; diff --git a/src/hardware/motech-lps-30x/api.c b/src/hardware/motech-lps-30x/api.c index fb1c177d..464ce7db 100644 --- a/src/hardware/motech-lps-30x/api.c +++ b/src/hardware/motech-lps-30x/api.c @@ -652,9 +652,7 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * { struct dev_context *devc; struct sr_channel *ch; - int ch_idx, i; - GVariant *gvar; - GVariantBuilder gvb; + int ch_idx; devc = (sdi) ? sdi->priv : NULL; @@ -691,22 +689,10 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * devopts_cg_ch3, ARRAY_SIZE(devopts_cg_ch3), sizeof(uint32_t)); break; case SR_CONF_VOLTAGE_TARGET: - g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); - /* Min, max, step. */ - for (i = 0; i < 3; i++) { - gvar = g_variant_new_double(devc->model->channels[ch_idx].voltage[i]); - g_variant_builder_add_value(&gvb, gvar); - } - *data = g_variant_builder_end(&gvb); + *data = std_gvar_min_max_step_array(devc->model->channels[ch_idx].voltage); break; case SR_CONF_CURRENT_LIMIT: - g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); - /* Min, max, step. */ - for (i = 0; i < 3; i++) { - gvar = g_variant_new_double(devc->model->channels[ch_idx].current[i]); - g_variant_builder_add_value(&gvb, gvar); - } - *data = g_variant_builder_end(&gvb); + *data = std_gvar_min_max_step_array(devc->model->channels[ch_idx].current); break; default: return SR_ERR_NA; diff --git a/src/hardware/scpi-pps/api.c b/src/hardware/scpi-pps/api.c index cad2a874..70b54351 100644 --- a/src/hardware/scpi-pps/api.c +++ b/src/hardware/scpi-pps/api.c @@ -514,8 +514,6 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * struct dev_context *devc; struct sr_channel *ch; const struct channel_spec *ch_spec; - GVariant *gvar; - GVariantBuilder gvb; int i; const char *s[16]; @@ -560,6 +558,7 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * * specification for use in series or parallel mode. */ ch = cg->channels->data; + ch_spec = &(devc->device->channels[ch->index]); switch (key) { case SR_CONF_DEVICE_OPTIONS: @@ -568,34 +567,13 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * sizeof(uint32_t)); break; case SR_CONF_VOLTAGE_TARGET: - ch_spec = &(devc->device->channels[ch->index]); - g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); - /* Min, max, write resolution. */ - for (i = 0; i < 3; i++) { - gvar = g_variant_new_double(ch_spec->voltage[i]); - g_variant_builder_add_value(&gvb, gvar); - } - *data = g_variant_builder_end(&gvb); + *data = std_gvar_min_max_step_array(ch_spec->voltage); break; case SR_CONF_OUTPUT_FREQUENCY_TARGET: - ch_spec = &(devc->device->channels[ch->index]); - g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); - /* Min, max, write resolution. */ - for (i = 0; i < 3; i++) { - gvar = g_variant_new_double(ch_spec->frequency[i]); - g_variant_builder_add_value(&gvb, gvar); - } - *data = g_variant_builder_end(&gvb); + *data = std_gvar_min_max_step_array(ch_spec->frequency); break; case SR_CONF_CURRENT_LIMIT: - g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); - /* Min, max, step. */ - for (i = 0; i < 3; i++) { - ch_spec = &(devc->device->channels[ch->index]); - gvar = g_variant_new_double(ch_spec->current[i]); - g_variant_builder_add_value(&gvb, gvar); - } - *data = g_variant_builder_end(&gvb); + *data = std_gvar_min_max_step_array(ch_spec->current); break; default: return SR_ERR_NA; diff --git a/src/libsigrok-internal.h b/src/libsigrok-internal.h index 8c7e35dd..61858ebb 100644 --- a/src/libsigrok-internal.h +++ b/src/libsigrok-internal.h @@ -960,6 +960,8 @@ SR_PRIV GVariant *std_gvar_tuple_array(const uint64_t (*a)[][2], unsigned int n) SR_PRIV GVariant *std_gvar_tuple_rational(const struct sr_rational *r, unsigned int n); SR_PRIV GVariant *std_gvar_samplerates(const uint64_t samplerates[], unsigned int n); SR_PRIV GVariant *std_gvar_samplerates_steps(const uint64_t samplerates[], unsigned int n); +SR_PRIV GVariant *std_gvar_min_max_step(double min, double max, double step); +SR_PRIV GVariant *std_gvar_min_max_step_array(const double a[3]); /*--- resource.c ------------------------------------------------------------*/ diff --git a/src/std.c b/src/std.c index e36b7ccc..5748ba02 100644 --- a/src/std.c +++ b/src/std.c @@ -601,3 +601,29 @@ SR_PRIV GVariant *std_gvar_samplerates_steps(const uint64_t samplerates[], unsig { return samplerate_helper(samplerates, n, "samplerate-steps"); } + +SR_PRIV GVariant *std_gvar_min_max_step(double min, double max, double step) +{ + GVariantBuilder gvb; + + g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); + + g_variant_builder_add_value(&gvb, g_variant_new_double(min)); + g_variant_builder_add_value(&gvb, g_variant_new_double(max)); + g_variant_builder_add_value(&gvb, g_variant_new_double(step)); + + return g_variant_builder_end(&gvb); +} + +SR_PRIV GVariant *std_gvar_min_max_step_array(const double a[3]) +{ + unsigned int i; + GVariantBuilder gvb; + + g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); + + for (i = 0; i < 3; i++) + g_variant_builder_add_value(&gvb, g_variant_new_double(a[i])); + + return g_variant_builder_end(&gvb); +}