]> sigrok.org Git - libsigrok.git/commitdiff
drivers: Use std_*idx*() helpers in some more places.
authorUwe Hermann <redacted>
Wed, 2 Aug 2017 21:40:18 +0000 (23:40 +0200)
committerUwe Hermann <redacted>
Thu, 3 Aug 2017 14:35:19 +0000 (16:35 +0200)
src/hardware/hameg-hmo/api.c
src/hardware/hameg-hmo/protocol.c
src/hardware/lecroy-xstream/api.c
src/hardware/yokogawa-dlm/api.c

index db6c67df4dfd7755bd3bb40d22c99e7edac68924..7619f18485e1efc9f8a82e43dbd22f1db3cd7c75 100644 (file)
@@ -246,12 +246,11 @@ 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, idx;
-       unsigned int i, j;
+       unsigned int j;
        char command[MAX_COMMAND_SIZE], float_str[30];
        struct dev_context *devc;
        const struct scope_config *model;
        struct scope_state *state;
-       const char *tmp;
        double tmp_d;
        gboolean update_sample_rate;
 
@@ -275,18 +274,13 @@ 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; i < model->num_trigger_sources; i++) {
-                       if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
-                               continue;
-                       state->trigger_source = i;
-                       g_snprintf(command, sizeof(command),
-                                  (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
-                                  (*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),
+                          (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SOURCE],
+                          (*model->trigger_sources)[idx]);
+               ret = sr_scpi_send(sdi->conn, command);
                break;
        case SR_CONF_VDIV:
                if (cg_type == CG_NONE)
@@ -323,64 +317,46 @@ static int config_set(uint32_t key, GVariant *data,
                break;
        case SR_CONF_HORIZ_TRIGGERPOS:
                tmp_d = g_variant_get_double(data);
-
                if (tmp_d < 0.0 || tmp_d > 1.0)
                        return SR_ERR;
-
                state->horiz_triggerpos = tmp_d;
                tmp_d = -(tmp_d - 0.5) *
                        ((double) (*model->timebases)[state->timebase][0] /
                        (*model->timebases)[state->timebase][1])
                         * model->num_xdivs;
-
                g_ascii_formatd(float_str, sizeof(float_str), "%E", tmp_d);
                g_snprintf(command, sizeof(command),
                           (*model->scpi_dialect)[SCPI_CMD_SET_HORIZ_TRIGGERPOS],
                           float_str);
-
                ret = sr_scpi_send(sdi->conn, command);
                break;
        case SR_CONF_TRIGGER_SLOPE:
-               tmp = g_variant_get_string(data, NULL);
-               for (i = 0; i < model->num_trigger_slopes; i++) {
-                       if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0)
-                               continue;
-                       state->trigger_slope = i;
-                       g_snprintf(command, sizeof(command),
-                                  (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
-                                  (*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),
+                          (*model->scpi_dialect)[SCPI_CMD_SET_TRIGGER_SLOPE],
+                          (*model->trigger_slopes)[idx]);
+               ret = sr_scpi_send(sdi->conn, command);
                break;
        case SR_CONF_COUPLING:
                if (cg_type == CG_NONE)
                        return SR_ERR_CHANNEL_GROUP;
-
-               tmp = g_variant_get_string(data, NULL);
-
-               for (i = 0; i < model->num_coupling_options; 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),
-                                          (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
-                                          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),
+                                  (*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
+                                  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;
index 6d2ad3c413a92466e36ba88297dfbfa5725bf386..0c4d57f3f6cf465ba34ff17b590a2924192e944c 100644 (file)
@@ -329,27 +329,22 @@ static int scope_state_get_array_option(struct sr_scpi_dev_inst *scpi,
                const char *command, const char *(*array)[], unsigned int n, int *result)
 {
        char *tmp;
-       unsigned int i;
+       int idx;
 
        if (sr_scpi_get_string(scpi, command, &tmp) != SR_OK) {
                g_free(tmp);
                return SR_ERR;
        }
 
-       for (i = 0; i < n; i++) {
-               if (!g_strcmp0(tmp, (*array)[i])) {
-                       *result = i;
-                       g_free(tmp);
-                       tmp = NULL;
-                       break;
-               }
-       }
-
-       if (tmp) {
+       if ((idx = std_str_idx_s(tmp, *array, n)) < 0) {
                g_free(tmp);
-               return SR_ERR;
+               return SR_ERR_ARG;
        }
 
+       *result = idx;
+
+       g_free(tmp);
+
        return SR_OK;
 }
 
index 582474c9913a278429edbf18443bee277aa87e75..62cb967b560ad8530ad96bf034df43c387d68018 100644 (file)
@@ -205,14 +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;
-       uint64_t p, q;
        double tmp_d;
        gboolean update_sample_rate;
 
@@ -233,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; i < model->num_trigger_sources; 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][0] || q != (*model->vdivs)[i][1])
+               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][0] ||
-                           q != (*model->timebases)[i][1])
-                               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);
@@ -303,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; i < model->num_trigger_slopes; 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; i < model->num_coupling_options; 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;
index 7545d00b3766c07e2cb8fcc09c5b7f4a395cacb2..c3574a906ff9d2c5762d1da47ec582be722c4265 100644 (file)
@@ -283,12 +283,11 @@ 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, idx;
-       unsigned int i, j;
+       unsigned int j;
        char float_str[30];
        struct dev_context *devc;
        const struct scope_config *model;
        struct scope_state *state;
-       const char *tmp;
        double tmp_d;
        gboolean update_sample_rate;
 
@@ -310,15 +309,11 @@ 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; i < model->num_trigger_sources; 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)
@@ -364,40 +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)
                        return SR_ERR_CHANNEL_GROUP;
-
-               tmp = g_variant_get_string(data, NULL);
-
-               for (i = 0; i < model->num_coupling_options; 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;