X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fyokogawa-dlm%2Fapi.c;h=c3574a906ff9d2c5762d1da47ec582be722c4265;hb=bd633efa3215c1bb8600a5728e5b3a692940c679;hp=4cdec52aa0c4e95b0534fd274a81e4acf2e708d1;hpb=58ffcf97121453fb53e885338e23e54c7ed78659;p=libsigrok.git diff --git a/src/hardware/yokogawa-dlm/api.c b/src/hardware/yokogawa-dlm/api.c index 4cdec52a..c3574a90 100644 --- a/src/hardware/yokogawa-dlm/api.c +++ b/src/hardware/yokogawa-dlm/api.c @@ -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) @@ -182,8 +182,8 @@ static int check_channel_group(struct dev_context *devc, return CG_INVALID; } -static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, - const struct sr_channel_group *cg) +static int config_get(uint32_t key, GVariant **data, + const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { int ret, cg_type; unsigned int i; @@ -215,7 +215,6 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s 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, const struct sr_dev_inst *s 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, const struct sr_dev_inst *s 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; @@ -282,17 +279,15 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s return ret; } -static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, - const struct sr_channel_group *cg) +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, const struct sr_dev_inst *sd 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, const struct sr_dev_inst *sd 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; @@ -437,7 +396,7 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd } static int config_channel_set(const struct sr_dev_inst *sdi, - struct sr_channel *ch, unsigned int changes) + struct sr_channel *ch, unsigned int changes) { /* Currently we only handle SR_CHANNEL_SET_ENABLED. */ if (changes != SR_CHANNEL_SET_ENABLED) @@ -446,8 +405,8 @@ static int config_channel_set(const struct sr_dev_inst *sdi, return dlm_channel_state_set(sdi, ch->index, ch->enabled); } -static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, - const struct sr_channel_group *cg) +static int config_list(uint32_t key, GVariant **data, + const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { int cg_type = CG_NONE; struct dev_context *devc; @@ -467,12 +426,10 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * 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, const struct sr_dev_inst * 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)