drivers: Factor out std_cg_idx().

This commit is contained in:
Uwe Hermann 2017-08-03 23:18:01 +02:00
parent d1ac53ccd5
commit fcd6a8bdf1
6 changed files with 138 additions and 197 deletions

View File

@ -130,7 +130,6 @@ static int dev_close(struct sr_dev_inst *sdi)
static int check_channel_group(struct dev_context *devc,
const struct sr_channel_group *cg)
{
unsigned int i;
const struct scope_config *model;
model = devc->model_config;
@ -138,13 +137,11 @@ static int check_channel_group(struct dev_context *devc,
if (!cg)
return CG_NONE;
for (i = 0; i < model->analog_channels; i++)
if (cg == devc->analog_groups[i])
return CG_ANALOG;
if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) >= 0)
return CG_ANALOG;
for (i = 0; i < model->digital_pods; i++)
if (cg == devc->digital_groups[i])
return CG_DIGITAL;
if (std_cg_idx(cg, devc->digital_groups, model->digital_pods) >= 0)
return CG_DIGITAL;
sr_err("Invalid channel group specified.");
@ -154,8 +151,7 @@ static int check_channel_group(struct dev_context *devc,
static int config_get(uint32_t key, GVariant **data,
const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
{
int cg_type;
unsigned int i;
int cg_type, idx;
struct dev_context *devc;
const struct scope_config *model;
struct scope_state *state;
@ -183,12 +179,9 @@ static int config_get(uint32_t key, GVariant **data,
if (cg_type == CG_NONE) {
return SR_ERR_CHANNEL_GROUP;
} else if (cg_type == CG_ANALOG) {
for (i = 0; i < model->analog_channels; i++) {
if (cg != devc->analog_groups[i])
continue;
*data = g_variant_new_int32(model->num_ydivs);
break;
}
if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) < 0)
return SR_ERR_ARG;
*data = g_variant_new_int32(model->num_ydivs);
} else {
return SR_ERR_NA;
}
@ -197,14 +190,11 @@ static int config_get(uint32_t key, GVariant **data,
if (cg_type == CG_NONE) {
return SR_ERR_CHANNEL_GROUP;
} else if (cg_type == CG_ANALOG) {
for (i = 0; i < model->analog_channels; i++) {
if (cg != devc->analog_groups[i])
continue;
*data = g_variant_new("(tt)",
(*model->vdivs)[state->analog_channels[i].vdiv][0],
(*model->vdivs)[state->analog_channels[i].vdiv][1]);
break;
}
if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
return SR_ERR_ARG;
*data = g_variant_new("(tt)",
(*model->vdivs)[state->analog_channels[idx].vdiv][0],
(*model->vdivs)[state->analog_channels[idx].vdiv][1]);
} else {
return SR_ERR_NA;
}
@ -222,12 +212,9 @@ static int config_get(uint32_t key, GVariant **data,
if (cg_type == CG_NONE) {
return SR_ERR_CHANNEL_GROUP;
} else if (cg_type == CG_ANALOG) {
for (i = 0; i < model->analog_channels; i++) {
if (cg != devc->analog_groups[i])
continue;
*data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
break;
}
if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
return SR_ERR_ARG;
*data = g_variant_new_string((*model->coupling_options)[state->analog_channels[idx].coupling]);
} else {
return SR_ERR_NA;
}
@ -245,8 +232,7 @@ 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, idx;
unsigned int j;
int ret, cg_type, idx, j;
char command[MAX_COMMAND_SIZE], float_str[30];
struct dev_context *devc;
const struct scope_config *model;
@ -287,20 +273,17 @@ static int config_set(uint32_t key, GVariant *data,
return SR_ERR_CHANNEL_GROUP;
if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_vdivs)) < 0)
return SR_ERR_ARG;
for (j = 0; j < model->analog_channels; j++) {
if (cg != devc->analog_groups[j])
continue;
state->analog_channels[j].vdiv = idx;
g_ascii_formatd(float_str, sizeof(float_str), "%E",
(float) (*model->vdivs)[idx][0] / (*model->vdivs)[idx][1]);
g_snprintf(command, sizeof(command),
(*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
j + 1, float_str);
if (sr_scpi_send(sdi->conn, command) != SR_OK ||
sr_scpi_get_opc(sdi->conn) != SR_OK)
return SR_ERR;
break;
}
if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
return SR_ERR_ARG;
state->analog_channels[j].vdiv = idx;
g_ascii_formatd(float_str, sizeof(float_str), "%E",
(float) (*model->vdivs)[idx][0] / (*model->vdivs)[idx][1]);
g_snprintf(command, sizeof(command),
(*model->scpi_dialect)[SCPI_CMD_SET_VERTICAL_DIV],
j + 1, float_str);
if (sr_scpi_send(sdi->conn, command) != SR_OK ||
sr_scpi_get_opc(sdi->conn) != SR_OK)
return SR_ERR;
ret = SR_OK;
break;
case SR_CONF_TIMEBASE:
@ -344,18 +327,15 @@ static int config_set(uint32_t key, GVariant *data,
return SR_ERR_CHANNEL_GROUP;
if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0)
return SR_ERR_ARG;
for (j = 0; j <= model->analog_channels; j++) {
if (cg != devc->analog_groups[j])
continue;
state->analog_channels[j].coupling = idx;
g_snprintf(command, sizeof(command),
(*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
j + 1, (*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;
}
if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
return SR_ERR_ARG;
state->analog_channels[j].coupling = idx;
g_snprintf(command, sizeof(command),
(*model->scpi_dialect)[SCPI_CMD_SET_COUPLING],
j + 1, (*model->coupling_options)[idx]);
if (sr_scpi_send(sdi->conn, command) != SR_OK ||
sr_scpi_get_opc(sdi->conn) != SR_OK)
return SR_ERR;
ret = SR_OK;
break;
default:

View File

@ -134,7 +134,7 @@ static int dev_close(struct sr_dev_inst *sdi)
static int config_get(uint32_t key, GVariant **data,
const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
{
unsigned int i;
int idx;
struct dev_context *devc;
const struct scope_config *model;
struct scope_state *state;
@ -158,20 +158,16 @@ static int config_get(uint32_t key, GVariant **data,
(*model->timebases)[state->timebase][1]);
break;
case SR_CONF_NUM_VDIV:
for (i = 0; i < model->analog_channels; i++) {
if (cg != devc->analog_groups[i])
continue;
*data = g_variant_new_int32(model->num_ydivs);
}
if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) < 0)
return SR_ERR_ARG;
*data = g_variant_new_int32(model->num_ydivs);
break;
case SR_CONF_VDIV:
for (i = 0; i < model->analog_channels; i++) {
if (cg != devc->analog_groups[i])
continue;
*data = g_variant_new("(tt)",
(*model->vdivs)[state->analog_channels[i].vdiv][0],
(*model->vdivs)[state->analog_channels[i].vdiv][1]);
}
if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
return SR_ERR_ARG;
*data = g_variant_new("(tt)",
(*model->vdivs)[state->analog_channels[idx].vdiv][0],
(*model->vdivs)[state->analog_channels[idx].vdiv][1]);
break;
case SR_CONF_TRIGGER_SOURCE:
*data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
@ -183,11 +179,9 @@ static int config_get(uint32_t key, GVariant **data,
*data = g_variant_new_double(state->horiz_triggerpos);
break;
case SR_CONF_COUPLING:
for (i = 0; i < model->analog_channels; i++) {
if (cg != devc->analog_groups[i])
continue;
*data = g_variant_new_string((*model->coupling_options)[state->analog_channels[i].coupling]);
}
if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
return SR_ERR_ARG;
*data = g_variant_new_string((*model->coupling_options)[state->analog_channels[idx].coupling]);
break;
case SR_CONF_SAMPLERATE:
*data = g_variant_new_uint64(state->sample_rate);
@ -205,8 +199,7 @@ 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, idx;
unsigned int j;
int ret, idx, j;
char command[MAX_COMMAND_SIZE];
struct dev_context *devc;
const struct scope_config *model;
@ -241,17 +234,14 @@ static int config_set(uint32_t key, GVariant *data,
case SR_CONF_VDIV:
if ((idx = std_u64_tuple_idx(data, *model->vdivs, model->num_vdivs)) < 0)
return SR_ERR_ARG;
for (j = 0; j < model->analog_channels; j++) {
if (cg != devc->analog_groups[j])
continue;
state->analog_channels[j].vdiv = idx;
g_snprintf(command, sizeof(command),
"C%d:VDIV %E", j + 1, (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;
}
if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
return SR_ERR_ARG;
state->analog_channels[j].vdiv = idx;
g_snprintf(command, sizeof(command),
"C%d:VDIV %E", j + 1, (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;
ret = SR_OK;
break;
case SR_CONF_TIMEBASE:
@ -290,17 +280,14 @@ static int config_set(uint32_t key, GVariant *data,
case SR_CONF_COUPLING:
if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0)
return SR_ERR_ARG;
for (j = 0; j < model->analog_channels; j++) {
if (cg != devc->analog_groups[j])
continue;
state->analog_channels[j].coupling = idx;
g_snprintf(command, sizeof(command), "C%d:COUPLING %s",
j + 1, (*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;
}
if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
return SR_ERR_ARG;
state->analog_channels[j].coupling = idx;
g_snprintf(command, sizeof(command), "C%d:COUPLING %s",
j + 1, (*model->coupling_options)[idx]);
if (sr_scpi_send(sdi->conn, command) != SR_OK ||
sr_scpi_get_opc(sdi->conn) != SR_OK)
return SR_ERR;
ret = SR_OK;
break;
default:

View File

@ -619,8 +619,7 @@ static int config_set(uint32_t key, GVariant *data,
struct dev_context *devc;
uint64_t p;
double t_dbl;
unsigned int i;
int ret, idx;
int ret, idx, i;
const char *tmp_str;
char buffer[16];
@ -689,52 +688,36 @@ static int config_set(uint32_t key, GVariant *data,
case SR_CONF_VDIV:
if (!cg)
return SR_ERR_CHANNEL_GROUP;
for (i = 0; i < devc->model->analog_channels; i++) {
if (cg != devc->analog_groups[i])
continue;
if ((idx = std_u64_tuple_idx(data, ARRAY_AND_SIZE(vdivs))) < 0)
return SR_ERR_ARG;
devc->vdiv[i] = (float)vdivs[idx][0] / vdivs[idx][1];
g_ascii_formatd(buffer, sizeof(buffer), "%.3f",
devc->vdiv[i]);
return rigol_ds_config_set(sdi, ":CHAN%d:SCAL %s", i + 1,
buffer);
}
sr_dbg("Didn't set vdiv, unknown channel(group).");
return SR_ERR_NA;
if ((i = std_cg_idx(cg, devc->analog_groups, devc->model->analog_channels)) < 0)
return SR_ERR_ARG;
if ((idx = std_u64_tuple_idx(data, ARRAY_AND_SIZE(vdivs))) < 0)
return SR_ERR_ARG;
devc->vdiv[i] = (float)vdivs[idx][0] / vdivs[idx][1];
g_ascii_formatd(buffer, sizeof(buffer), "%.3f", devc->vdiv[i]);
return rigol_ds_config_set(sdi, ":CHAN%d:SCAL %s", i + 1, buffer);
case SR_CONF_COUPLING:
if (!cg)
return SR_ERR_CHANNEL_GROUP;
for (i = 0; i < devc->model->analog_channels; i++) {
if (cg != devc->analog_groups[i])
continue;
if ((idx = std_str_idx(data, ARRAY_AND_SIZE(coupling))) < 0)
return SR_ERR_ARG;
g_free(devc->coupling[i]);
devc->coupling[i] = g_strdup(coupling[idx]);
return rigol_ds_config_set(sdi, ":CHAN%d:COUP %s", i + 1,
devc->coupling[i]);
}
sr_dbg("Didn't set coupling, unknown channel(group).");
return SR_ERR_NA;
if ((i = std_cg_idx(cg, devc->analog_groups, devc->model->analog_channels)) < 0)
return SR_ERR_ARG;
if ((idx = std_str_idx(data, ARRAY_AND_SIZE(coupling))) < 0)
return SR_ERR_ARG;
g_free(devc->coupling[i]);
devc->coupling[i] = g_strdup(coupling[idx]);
return rigol_ds_config_set(sdi, ":CHAN%d:COUP %s", i + 1, devc->coupling[i]);
case SR_CONF_PROBE_FACTOR:
if (!cg)
return SR_ERR_CHANNEL_GROUP;
if ((i = std_cg_idx(cg, devc->analog_groups, devc->model->analog_channels)) < 0)
return SR_ERR_ARG;
if ((idx = std_u64_idx(data, ARRAY_AND_SIZE(probe_factor))) < 0)
return SR_ERR_ARG;
p = g_variant_get_uint64(data);
for (i = 0; i < devc->model->analog_channels; i++) {
if (cg != devc->analog_groups[i])
continue;
if ((idx = std_u64_idx(data, ARRAY_AND_SIZE(probe_factor))) < 0)
return SR_ERR_ARG;
devc->attenuation[i] = probe_factor[idx];
ret = rigol_ds_config_set(sdi, ":CHAN%d:PROB %"PRIu64,
i + 1, p);
if (ret == SR_OK)
rigol_ds_get_dev_cfg_vertical(sdi);
return ret;
}
sr_dbg("Didn't set probe factor, unknown channel(group).");
return SR_ERR_NA;
devc->attenuation[i] = probe_factor[idx];
ret = rigol_ds_config_set(sdi, ":CHAN%d:PROB %"PRIu64, i + 1, p);
if (ret == SR_OK)
rigol_ds_get_dev_cfg_vertical(sdi);
return ret;
case SR_CONF_DATA_SOURCE:
tmp_str = g_variant_get_string(data, NULL);
if (!strcmp(tmp_str, "Live"))
@ -760,7 +743,6 @@ static int config_set(uint32_t key, GVariant *data,
static int config_list(uint32_t key, GVariant **data,
const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
{
unsigned int i;
struct dev_context *devc;
devc = (sdi) ? sdi->priv : NULL;
@ -774,13 +756,10 @@ static int config_list(uint32_t key, GVariant **data,
*data = std_gvar_array_u32(NULL, 0);
return SR_OK;
} else {
for (i = 0; i < devc->model->analog_channels; i++) {
if (cg == devc->analog_groups[i]) {
*data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog));
return SR_OK;
}
}
return SR_ERR_NA;
if (std_cg_idx(cg, devc->analog_groups, devc->model->analog_channels) < 0)
return SR_ERR_ARG;
*data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog));
return SR_OK;
}
break;
case SR_CONF_COUPLING:

View File

@ -162,7 +162,6 @@ static int dev_close(struct sr_dev_inst *sdi)
static int check_channel_group(struct dev_context *devc,
const struct sr_channel_group *cg)
{
unsigned int i;
const struct scope_config *model;
model = devc->model_config;
@ -170,23 +169,21 @@ static int check_channel_group(struct dev_context *devc,
if (!cg)
return CG_NONE;
for (i = 0; i < model->analog_channels; i++)
if (cg == devc->analog_groups[i])
return CG_ANALOG;
if (std_cg_idx(cg, devc->analog_groups, model->analog_channels) >= 0)
return CG_ANALOG;
for (i = 0; i < model->pods; i++)
if (cg == devc->digital_groups[i])
return CG_DIGITAL;
if (std_cg_idx(cg, devc->digital_groups, model->pods) >= 0)
return CG_DIGITAL;
sr_err("Invalid channel group specified.");
return CG_INVALID;
}
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;
int ret, cg_type, idx;
struct dev_context *devc;
const struct scope_config *model;
struct scope_state *state;
@ -230,16 +227,12 @@ static int config_get(uint32_t key, GVariant **data,
return SR_ERR_CHANNEL_GROUP;
} else if (cg_type != CG_ANALOG)
break;
for (i = 0; i < model->analog_channels; i++) {
if (cg != devc->analog_groups[i])
continue;
*data = g_variant_new("(tt)",
dlm_vdivs[state->analog_states[i].vdiv][0],
dlm_vdivs[state->analog_states[i].vdiv][1]);
ret = SR_OK;
break;
}
if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
return SR_ERR_ARG;
*data = g_variant_new("(tt)",
dlm_vdivs[state->analog_states[idx].vdiv][0],
dlm_vdivs[state->analog_states[idx].vdiv][1]);
ret = SR_OK;
break;
case SR_CONF_TRIGGER_SOURCE:
*data = g_variant_new_string((*model->trigger_sources)[state->trigger_source]);
@ -259,14 +252,10 @@ static int config_get(uint32_t key, GVariant **data,
return SR_ERR_CHANNEL_GROUP;
} else if (cg_type != CG_ANALOG)
break;
for (i = 0; i < model->analog_channels; i++) {
if (cg != devc->analog_groups[i])
continue;
*data = g_variant_new_string((*model->coupling_options)[state->analog_states[i].coupling]);
ret = SR_OK;
break;
}
if ((idx = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
return SR_ERR_ARG;
*data = g_variant_new_string((*model->coupling_options)[state->analog_states[idx].coupling]);
ret = SR_OK;
break;
case SR_CONF_SAMPLERATE:
*data = g_variant_new_uint64(state->sample_rate);
@ -282,8 +271,7 @@ 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, idx;
unsigned int j;
int ret, cg_type, idx, j;
char float_str[30];
struct dev_context *devc;
const struct scope_config *model;
@ -320,17 +308,14 @@ static int config_set(uint32_t key, GVariant *data,
return SR_ERR_CHANNEL_GROUP;
if ((idx = std_u64_tuple_idx(data, ARRAY_AND_SIZE(dlm_vdivs))) < 0)
return SR_ERR_ARG;
for (j = 0; j < model->analog_channels; j++) {
if (cg != devc->analog_groups[j])
continue;
state->analog_states[j].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 + 1, float_str) != SR_OK ||
sr_scpi_get_opc(sdi->conn) != SR_OK)
return SR_ERR;
break;
}
if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
return SR_ERR_ARG;
state->analog_states[j].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 + 1, float_str) != SR_OK ||
sr_scpi_get_opc(sdi->conn) != SR_OK)
return SR_ERR;
ret = SR_OK;
break;
case SR_CONF_TIMEBASE:
@ -370,15 +355,12 @@ static int config_set(uint32_t key, GVariant *data,
return SR_ERR_CHANNEL_GROUP;
if ((idx = std_str_idx(data, *model->coupling_options, model->num_coupling_options)) < 0)
return SR_ERR_ARG;
for (j = 0; j <= model->analog_channels; j++) {
if (cg != devc->analog_groups[j])
continue;
state->analog_states[j].coupling = idx;
if (dlm_analog_chan_coupl_set(sdi->conn, j + 1, (*model->coupling_options)[idx]) != SR_OK ||
sr_scpi_get_opc(sdi->conn) != SR_OK)
return SR_ERR;
break;
}
if ((j = std_cg_idx(cg, devc->analog_groups, model->analog_channels)) < 0)
return SR_ERR_ARG;
state->analog_states[j].coupling = idx;
if (dlm_analog_chan_coupl_set(sdi->conn, j + 1, (*model->coupling_options)[idx]) != SR_OK ||
sr_scpi_get_opc(sdi->conn) != SR_OK)
return SR_ERR;
ret = SR_OK;
break;
default:

View File

@ -984,6 +984,8 @@ SR_PRIV int std_u64_tuple_idx(GVariant *data, const uint64_t a[][2], unsigned in
SR_PRIV int std_double_tuple_idx(GVariant *data, const double a[][2], unsigned int n);
SR_PRIV int std_double_tuple_idx_d0(const double d, const double a[][2], unsigned int n);
SR_PRIV int std_cg_idx(const struct sr_channel_group *cg, struct sr_channel_group *a[], unsigned int n);
/*--- resource.c ------------------------------------------------------------*/
SR_PRIV int64_t sr_file_get_size(FILE *file);

View File

@ -828,3 +828,14 @@ SR_PRIV int std_double_tuple_idx_d0(const double d, const double a[][2], unsigne
return -1;
}
SR_PRIV int std_cg_idx(const struct sr_channel_group *cg, struct sr_channel_group *a[], unsigned int n)
{
unsigned int i;
for (i = 0; i < n; i++)
if (cg == a[i])
return i;
return -1;
}