]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/lecroy-xstream/api.c
drivers: Use std_*idx*() helpers in some more places.
[libsigrok.git] / src / hardware / lecroy-xstream / api.c
index 033299f5cad1ba2aad0210f19af651e9c7a2be5b..62cb967b560ad8530ad96bf034df43c387d68018 100644 (file)
@@ -52,18 +52,7 @@ static const uint32_t devopts_cg_analog[] = {
        SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
 };
 
-static int check_manufacturer(const char *manufacturer)
-{
-       unsigned int i;
-
-       for (i = 0; i < ARRAY_SIZE(manufacturers); i++)
-               if (!strcmp(manufacturer, manufacturers[i]))
-                       return SR_OK;
-
-       return SR_ERR;
-}
-
-static struct sr_dev_inst *probe_serial_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;
@@ -78,7 +67,7 @@ static struct sr_dev_inst *probe_serial_device(struct sr_scpi_dev_inst *scpi)
                goto fail;
        }
 
-       if (check_manufacturer(hw_info->manufacturer) != SR_OK)
+       if (std_str_idx_s(hw_info->manufacturer, ARRAY_AND_SIZE(manufacturers)) < 0)
                goto fail;
 
        sdi = g_malloc0(sizeof(struct sr_dev_inst));
@@ -112,7 +101,7 @@ fail:
 
 static GSList *scan(struct sr_dev_driver *di, GSList *options)
 {
-       return sr_scpi_scan(di->context, options, probe_serial_device);
+       return sr_scpi_scan(di->context, options, probe_device);
 }
 
 static void clear_helper(struct dev_context *devc)
@@ -165,8 +154,8 @@ static int config_get(uint32_t key, GVariant **data,
                break;
        case SR_CONF_TIMEBASE:
                *data = g_variant_new("(tt)",
-                               model->timebases[state->timebase].p,
-                               model->timebases[state->timebase].q);
+                               (*model->timebases)[state->timebase][0],
+                               (*model->timebases)[state->timebase][1]);
                break;
        case SR_CONF_NUM_VDIV:
                for (i = 0; i < model->analog_channels; i++) {
@@ -180,8 +169,8 @@ static int config_get(uint32_t key, GVariant **data,
                        if (cg != devc->analog_groups[i])
                                continue;
                        *data = g_variant_new("(tt)",
-                               model->vdivs[state->analog_channels[i].vdiv].p,
-                               model->vdivs[state->analog_channels[i].vdiv].q);
+                               (*model->vdivs)[state->analog_channels[i].vdiv][0],
+                               (*model->vdivs)[state->analog_channels[i].vdiv][1]);
                }
                break;
        case SR_CONF_TRIGGER_SOURCE:
@@ -216,15 +205,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;
-       unsigned int i, j;
+       int ret, idx;
+       unsigned int j;
        char command[MAX_COMMAND_SIZE];
        struct dev_context *devc;
        const struct scope_config *model;
        struct scope_state *state;
-       const char *tmp;
-       int64_t p;
-       uint64_t q;
        double tmp_d;
        gboolean update_sample_rate;
 
@@ -245,58 +231,37 @@ 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;
-                       g_snprintf(command, sizeof(command),
-                                       "SET TRIGGER SOURCE %s",
-                                       (*model->trigger_sources)[i]);
-
-                       ret = sr_scpi_send(sdi->conn, command);
-                       break;
-               }
+               if ((idx = std_str_idx(data, *model->trigger_sources, model->num_trigger_sources)) < 0)
+                       return SR_ERR_ARG;
+               state->trigger_source = idx;
+               g_snprintf(command, sizeof(command),
+                       "SET TRIGGER SOURCE %s", (*model->trigger_sources)[idx]);
+               ret = sr_scpi_send(sdi->conn, command);
                break;
        case SR_CONF_VDIV:
-               g_variant_get(data, "(tt)", &p, &q);
-
-               for (i = 0; i < model->num_vdivs; i++) {
-                       if (p != model->vdivs[i].p || q != model->vdivs[i].q)
+               if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_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_channels[j - 1].vdiv = i;
-                               g_snprintf(command, sizeof(command),
-                                               "C%d:VDIV %E", j, (float)p/q);
-
-                               if (sr_scpi_send(sdi->conn, command) != SR_OK ||
-                                   sr_scpi_get_opc(sdi->conn) != SR_OK)
-                                       return SR_ERR;
-
-                               break;
-                       }
-
-                       ret = SR_OK;
+                       state->analog_channels[j - 1].vdiv = idx;
+                       g_snprintf(command, sizeof(command),
+                               "C%d:VDIV %E", j, (float) (*model->vdivs)[idx][0] / (*model->vdivs)[idx][1]);
+                       if (sr_scpi_send(sdi->conn, command) != 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 < model->num_timebases; i++) {
-                       if (p != model->timebases[i].p ||
-                           q != model->timebases[i].q)
-                               continue;
-                       state->timebase = i;
-                       g_snprintf(command, sizeof(command),
-                                       "TIME_DIV %E", (float)p/q);
-
-                       ret = sr_scpi_send(sdi->conn, command);
-                       update_sample_rate = TRUE;
-                       break;
-               }
+               if ((idx = std_u64_tuple_idx(data, *model->timebases, model->num_timebases)) < 0)
+                       return SR_ERR_ARG;
+               state->timebase = idx;
+               g_snprintf(command, sizeof(command),
+                       "TIME_DIV %E", (float) (*model->timebases)[idx][0] / (*model->timebases)[idx][1]);
+               ret = sr_scpi_send(sdi->conn, command);
+               update_sample_rate = TRUE;
                break;
        case SR_CONF_HORIZ_TRIGGERPOS:
                tmp_d = g_variant_get_double(data);
@@ -306,8 +271,8 @@ static int config_set(uint32_t key, GVariant *data,
 
                state->horiz_triggerpos = tmp_d;
                tmp_d = -(tmp_d - 0.5) *
-                       ((double)model->timebases[state->timebase].p /
-                        model->timebases[state->timebase].q)
+                       ((double)(*model->timebases)[state->timebase][0] /
+                        (*model->timebases)[state->timebase][1])
                         * model->num_xdivs;
 
                g_snprintf(command, sizeof(command), "TRIG POS %e S", tmp_d);
@@ -315,42 +280,28 @@ static int config_set(uint32_t key, GVariant *data,
                ret = sr_scpi_send(sdi->conn, command);
                break;
        case SR_CONF_TRIGGER_SLOPE:
-               tmp = g_variant_get_string(data, NULL);
-               for (i = 0; (*model->trigger_slopes)[i]; i++) {
-                       if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0)
-                               continue;
-                       state->trigger_slope = i;
-                       g_snprintf(command, sizeof(command),
-                                       "SET TRIGGER SLOPE %s",
-                                       (*model->trigger_slopes)[i]);
-
-                       ret = sr_scpi_send(sdi->conn, command);
-                       break;
-               }
+               if ((idx = std_str_idx(data, *model->trigger_slopes, model->num_trigger_slopes)) < 0)
+                       return SR_ERR_ARG;
+               state->trigger_slope = idx;
+               g_snprintf(command, sizeof(command),
+                       "SET TRIGGER SLOPE %s", (*model->trigger_slopes)[idx]);
+               ret = sr_scpi_send(sdi->conn, command);
                break;
        case SR_CONF_COUPLING:
-               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_channels[j - 1].coupling = i;
-
-                               g_snprintf(command, sizeof(command),
-                                               "C%d:COUPLING %s", j, tmp);
-
-                               if (sr_scpi_send(sdi->conn, command) != SR_OK ||
-                                   sr_scpi_get_opc(sdi->conn) != SR_OK)
-                                       return SR_ERR;
-                               break;
-                       }
-
-                       ret = SR_OK;
+                       state->analog_channels[j - 1].coupling = idx;
+                       g_snprintf(command, sizeof(command), "C%d:COUPLING %s",
+                                       j, (*model->coupling_options)[idx]);
+                       if (sr_scpi_send(sdi->conn, command) != SR_OK ||
+                           sr_scpi_get_opc(sdi->conn) != SR_OK)
+                               return SR_ERR;
                        break;
                }
+               ret = SR_OK;
                break;
        default:
                ret = SR_ERR_NA;
@@ -381,39 +332,35 @@ static int config_list(uint32_t key, GVariant **data,
        case SR_CONF_DEVICE_OPTIONS:
                if (!cg)
                        return STD_CONFIG_LIST(key, data, sdi, cg, NULL, drvopts, devopts);
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                       devopts_cg_analog, ARRAY_SIZE(devopts_cg_analog),
-                       sizeof(uint32_t));
+               *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog));
                break;
        case SR_CONF_COUPLING:
-               *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_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);
                break;
        case SR_CONF_TRIGGER_SLOPE:
                if (!model)
                        return SR_ERR_ARG;
-               *data = g_variant_new_strv(*model->trigger_slopes,
-                          g_strv_length((char **)*model->trigger_slopes));
+               *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes);
                break;
        case SR_CONF_TIMEBASE:
                if (!model)
                        return SR_ERR_ARG;
-               *data = std_gvar_tuple_rational(model->timebases, model->num_timebases);
+               *data = std_gvar_tuple_array(*model->timebases, model->num_timebases);
                break;
        case SR_CONF_VDIV:
                if (!model)
                        return SR_ERR_ARG;
-               *data = std_gvar_tuple_rational(model->vdivs, model->num_vdivs);
+               *data = std_gvar_tuple_array(*model->vdivs, model->num_vdivs);
                break;
        default:
                return SR_ERR_NA;
        }
+
        return SR_OK;
 }