]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/yokogawa-dlm/api.c
drivers: Use std_*idx*() helpers in some more places.
[libsigrok.git] / src / hardware / yokogawa-dlm / api.c
index f6f0ce8a6d7c756e993470fadda51c40c974208e..c3574a906ff9d2c5762d1da47ec582be722c4265 100644 (file)
@@ -62,7 +62,7 @@ enum {
        CG_DIGITAL,
 };
 
-static struct sr_dev_inst *probe_usbtmc_device(struct sr_scpi_dev_inst *scpi)
+static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
 {
        struct sr_dev_inst *sdi;
        struct dev_context *devc;
@@ -117,7 +117,7 @@ fail:
 
 static GSList *scan(struct sr_dev_driver *di, GSList *options)
 {
-       return sr_scpi_scan(di->context, options, probe_usbtmc_device);
+       return sr_scpi_scan(di->context, options, probe_device);
 }
 
 static void clear_helper(struct dev_context *devc)
@@ -215,7 +215,6 @@ static int config_get(uint32_t key, GVariant **data,
                break;
        case SR_CONF_NUM_VDIV:
                if (cg_type == CG_NONE) {
-                       sr_err("No channel group specified.");
                        return SR_ERR_CHANNEL_GROUP;
                } else if (cg_type == CG_ANALOG) {
                                *data = g_variant_new_int32(model->num_ydivs);
@@ -228,7 +227,6 @@ static int config_get(uint32_t key, GVariant **data,
        case SR_CONF_VDIV:
                ret = SR_ERR_NA;
                if (cg_type == CG_NONE) {
-                       sr_err("No channel group specified.");
                        return SR_ERR_CHANNEL_GROUP;
                } else if (cg_type != CG_ANALOG)
                        break;
@@ -258,7 +256,6 @@ static int config_get(uint32_t key, GVariant **data,
        case SR_CONF_COUPLING:
                ret = SR_ERR_NA;
                if (cg_type == CG_NONE) {
-                       sr_err("No channel group specified.");
                        return SR_ERR_CHANNEL_GROUP;
                } else if (cg_type != CG_ANALOG)
                        break;
@@ -285,14 +282,12 @@ static int config_get(uint32_t key, GVariant **data,
 static int config_set(uint32_t key, GVariant *data,
        const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
-       int ret, cg_type;
-       unsigned int i, j;
+       int ret, cg_type, idx;
+       unsigned int j;
        char float_str[30];
        struct dev_context *devc;
        const struct scope_config *model;
        struct scope_state *state;
-       const char *tmp;
-       uint64_t p, q;
        double tmp_d;
        gboolean update_sample_rate;
 
@@ -314,59 +309,38 @@ static int config_set(uint32_t key, GVariant *data,
                ret = SR_OK;
                break;
        case SR_CONF_TRIGGER_SOURCE:
-               tmp = g_variant_get_string(data, NULL);
-               for (i = 0; (*model->trigger_sources)[i]; i++) {
-                       if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
-                               continue;
-                       state->trigger_source = i;
-                       /* TODO: A and B trigger support possible? */
-                       ret = dlm_trigger_source_set(sdi->conn, (*model->trigger_sources)[i]);
-                       break;
-               }
+               if ((idx = std_str_idx(data, *model->trigger_sources, model->num_trigger_sources)) < 0)
+                       return SR_ERR_ARG;
+               state->trigger_source = idx;
+               /* TODO: A and B trigger support possible? */
+               ret = dlm_trigger_source_set(sdi->conn, (*model->trigger_sources)[idx]);
                break;
        case SR_CONF_VDIV:
-               if (cg_type == CG_NONE) {
-                       sr_err("No channel group specified.");
+               if (cg_type == CG_NONE)
                        return SR_ERR_CHANNEL_GROUP;
-               }
-
-               g_variant_get(data, "(tt)", &p, &q);
-
-               for (i = 0; i < ARRAY_SIZE(dlm_vdivs); i++) {
-                       if (p != dlm_vdivs[i][0] ||
-                                       q != dlm_vdivs[i][1])
+               if ((idx = std_u64_tuple_idx(data, ARRAY_AND_SIZE(dlm_vdivs))) < 0)
+                       return SR_ERR_ARG;
+               for (j = 1; j <= model->analog_channels; j++) {
+                       if (cg != devc->analog_groups[j - 1])
                                continue;
-                       for (j = 1; j <= model->analog_channels; j++) {
-                               if (cg != devc->analog_groups[j - 1])
-                                       continue;
-                               state->analog_states[j - 1].vdiv = i;
-                               g_ascii_formatd(float_str, sizeof(float_str),
-                                               "%E", (float) p / q);
-                               if (dlm_analog_chan_vdiv_set(sdi->conn, j, float_str) != SR_OK ||
-                                               sr_scpi_get_opc(sdi->conn) != SR_OK)
-                                       return SR_ERR;
-
-                               break;
-                       }
-
-                       ret = SR_OK;
+                       state->analog_states[j - 1].vdiv = idx;
+                       g_ascii_formatd(float_str, sizeof(float_str),
+                                       "%E", (float) dlm_vdivs[idx][0] / dlm_vdivs[idx][1]);
+                       if (dlm_analog_chan_vdiv_set(sdi->conn, j, float_str) != SR_OK ||
+                                       sr_scpi_get_opc(sdi->conn) != SR_OK)
+                               return SR_ERR;
                        break;
                }
+               ret = SR_OK;
                break;
        case SR_CONF_TIMEBASE:
-               g_variant_get(data, "(tt)", &p, &q);
-
-               for (i = 0; i < ARRAY_SIZE(dlm_timebases); i++) {
-                       if (p != dlm_timebases[i][0] ||
-                                       q != dlm_timebases[i][1])
-                               continue;
-                       state->timebase = i;
-                       g_ascii_formatd(float_str, sizeof(float_str),
-                                       "%E", (float) p / q);
-                       ret = dlm_timebase_set(sdi->conn, float_str);
-                       update_sample_rate = TRUE;
-                       break;
-               }
+               if ((idx = std_u64_tuple_idx(data, ARRAY_AND_SIZE(dlm_timebases))) < 0)
+                       return SR_ERR_ARG;
+               state->timebase = idx;
+               g_ascii_formatd(float_str, sizeof(float_str),
+                               "%E", (float) dlm_timebases[idx][0] / dlm_timebases[idx][1]);
+               ret = dlm_timebase_set(sdi->conn, float_str);
+               update_sample_rate = TRUE;
                break;
        case SR_CONF_HORIZ_TRIGGERPOS:
                tmp_d = g_variant_get_double(data);
@@ -385,42 +359,27 @@ static int config_set(uint32_t key, GVariant *data,
                ret = dlm_horiz_trigger_pos_set(sdi->conn, float_str);
                break;
        case SR_CONF_TRIGGER_SLOPE:
-               tmp = g_variant_get_string(data, NULL);
-
-               if (!tmp || !(tmp[0] == 'f' || tmp[0] == 'r'))
+               if ((idx = std_str_idx(data, ARRAY_AND_SIZE(dlm_trigger_slopes))) < 0)
                        return SR_ERR_ARG;
-
                /* Note: See dlm_trigger_slopes[] in protocol.c. */
-               state->trigger_slope = (tmp[0] == 'r') ?
-                               SLOPE_POSITIVE : SLOPE_NEGATIVE;
-
+               state->trigger_slope = idx;
                ret = dlm_trigger_slope_set(sdi->conn, state->trigger_slope);
                break;
        case SR_CONF_COUPLING:
-               if (cg_type == CG_NONE) {
-                       sr_err("No channel group specified.");
+               if (cg_type == CG_NONE)
                        return SR_ERR_CHANNEL_GROUP;
-               }
-
-               tmp = g_variant_get_string(data, NULL);
-
-               for (i = 0; (*model->coupling_options)[i]; i++) {
-                       if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
+               if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0)
+                       return SR_ERR_ARG;
+               for (j = 1; j <= model->analog_channels; j++) {
+                       if (cg != devc->analog_groups[j - 1])
                                continue;
-                       for (j = 1; j <= model->analog_channels; j++) {
-                               if (cg != devc->analog_groups[j - 1])
-                                       continue;
-                               state->analog_states[j-1].coupling = i;
-
-                               if (dlm_analog_chan_coupl_set(sdi->conn, j, tmp) != SR_OK ||
-                                               sr_scpi_get_opc(sdi->conn) != SR_OK)
-                                       return SR_ERR;
-                               break;
-                       }
-
-                       ret = SR_OK;
+                       state->analog_states[j - 1].coupling = idx;
+                       if (dlm_analog_chan_coupl_set(sdi->conn, j, (*model->coupling_options)[idx]) != SR_OK ||
+                                       sr_scpi_get_opc(sdi->conn) != SR_OK)
+                               return SR_ERR;
                        break;
                }
+               ret = SR_OK;
                break;
        default:
                ret = SR_ERR_NA;
@@ -467,12 +426,10 @@ static int config_list(uint32_t key, GVariant **data,
                case SR_CONF_TRIGGER_SOURCE:
                        if (!model)
                                return SR_ERR_ARG;
-                       *data = g_variant_new_strv(*model->trigger_sources,
-                                       g_strv_length((char **)*model->trigger_sources));
+                       *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources);
                        return SR_OK;
                case SR_CONF_TRIGGER_SLOPE:
-                       *data = g_variant_new_strv(dlm_trigger_slopes,
-                                       g_strv_length((char **)dlm_trigger_slopes));
+                       *data = g_variant_new_strv(ARRAY_AND_SIZE(dlm_trigger_slopes));
                        return SR_OK;
                case SR_CONF_NUM_HDIV:
                        *data = g_variant_new_uint32(model->num_xdivs);
@@ -497,8 +454,7 @@ static int config_list(uint32_t key, GVariant **data,
        case SR_CONF_COUPLING:
                if (cg_type == CG_NONE)
                        return SR_ERR_CHANNEL_GROUP;
-               *data = g_variant_new_strv(*model->coupling_options,
-                               g_strv_length((char **)*model->coupling_options));
+               *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options);
                break;
        case SR_CONF_VDIV:
                if (cg_type == CG_NONE)