]> sigrok.org Git - libsigrok.git/commitdiff
drivers: Factor out std_gvar_min_max_step{,_array}().
authorUwe Hermann <redacted>
Thu, 20 Jul 2017 20:03:55 +0000 (22:03 +0200)
committerUwe Hermann <redacted>
Fri, 21 Jul 2017 14:49:01 +0000 (16:49 +0200)
src/hardware/arachnid-labs-re-load-pro/api.c
src/hardware/atten-pps3xxx/api.c
src/hardware/korad-kaxxxxp/api.c
src/hardware/manson-hcs-3xxx/api.c
src/hardware/maynuo-m97/api.c
src/hardware/motech-lps-30x/api.c
src/hardware/scpi-pps/api.c
src/libsigrok-internal.h
src/std.c

index 11a6e3059119ad51d466061985d10624bf58a9ef..48d3cb8fd5b0151d7d6a1414419e45a7f7786435 100644 (file)
@@ -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;
index 0a16ca5f2c5b245d7c51455666abb1ddd9e6654f..0f27b4f87fc6385b5b37efade302bd04cf481aa1 100644 (file)
@@ -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;
index acd50961f25a1487f32ace54a56cb98e169b6164..c5c75da1e24f6ccfcec671cf71bfd5b6d2e4d298 100644 (file)
@@ -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;
index ab1e3b9bb954602e23d604d86fc6c4f47ce9e356..6eaef2d025a0806df1c72a70dcbe65b3d45e6fbb 100644 (file)
@@ -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;
index 970e843733d19299daba96fad30078db0512ea12..9d0ddbe4cd7142f28f92bc501c35265fdc2c2185 100644 (file)
@@ -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;
index fb1c177dc5c41e2c0146d9a863e975cf745e6b46..464ce7db8ae923ac5a94537bde4d941d6ed347fb 100644 (file)
@@ -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;
index cad2a8747fd83cdbacbc3708928132e7e720ad3c..70b5435106846fffa65813b887efa34442c7484a 100644 (file)
@@ -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;
index 8c7e35ddbf2ec3d0339354bd48afd62244b73fe6..61858ebb95107b76b9f00c15d282ff6497d83c24 100644 (file)
@@ -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 ------------------------------------------------------------*/
index e36b7ccc77910637dd0e0bbc484d3150e40b66a2..5748ba02eb9c62b869c693f60eac2d5942aa6ed2 100644 (file)
--- 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);
+}