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;
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.");
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;
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;
}
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;
}
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;
}
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;
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:
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:
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;
(*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]);
*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);
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;
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:
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:
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];
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"))
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;
*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:
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;
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;
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]);
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);
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;
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:
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:
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);
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;
+}