drivers: Use std_*idx*() helpers in some more places.

This commit is contained in:
Uwe Hermann 2017-08-02 23:40:18 +02:00
parent 76f0fa5dfb
commit bd633efa32
4 changed files with 94 additions and 178 deletions

View 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;

View File

@ -329,26 +329,21 @@ 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 ((idx = std_str_idx_s(tmp, *array, n)) < 0) {
g_free(tmp);
return SR_ERR_ARG;
}
if (tmp) {
g_free(tmp);
return SR_ERR;
}
*result = idx;
g_free(tmp);
return SR_OK;
}

View 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;

View 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;