siglent-sds: Bring driver up-to-date with current code conventions.

Use ARRAY_AND_SIZE where possible, use std.c helpers where possible,
make scanopts/drvopts/devopts consistent with other drivers.
This commit is contained in:
Uwe Hermann 2018-02-17 19:08:07 +01:00
parent 8856f173df
commit e4fb1a821d
1 changed files with 104 additions and 188 deletions

View File

@ -35,9 +35,12 @@ static const uint32_t scanopts[] = {
SR_CONF_SERIALCOMM, SR_CONF_SERIALCOMM,
}; };
static const uint32_t devopts[] = { static const uint32_t drvopts[] = {
SR_CONF_OSCILLOSCOPE, SR_CONF_OSCILLOSCOPE,
SR_CONF_LOGIC_ANALYZER, SR_CONF_LOGIC_ANALYZER,
};
static const uint32_t devopts[] = {
SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
@ -51,7 +54,7 @@ static const uint32_t devopts[] = {
SR_CONF_AVG_SAMPLES | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, SR_CONF_AVG_SAMPLES | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
}; };
static const uint32_t analog_devopts[] = { static const uint32_t devopts_cg_analog[] = {
SR_CONF_NUM_VDIV | SR_CONF_GET, SR_CONF_NUM_VDIV | SR_CONF_GET,
SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
@ -484,10 +487,8 @@ static int config_get(uint32_t key, GVariant **data,
return SR_ERR_NA; return SR_ERR_NA;
} }
for (i = 0; i < ARRAY_SIZE(vdivs); i++) { for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
float vdiv, diff; float vdiv = (float)vdivs[i][0] / vdivs[i][1];
float diff = fabsf(devc->vdiv[analog_channel] - vdiv);
vdiv = (float)vdivs[i][0] / vdivs[i][1];
diff = fabsf(devc->vdiv[analog_channel] - vdiv);
if (diff < smallest_diff) { if (diff < smallest_diff) {
smallest_diff = diff; smallest_diff = diff;
idx = i; idx = i;
@ -526,10 +527,12 @@ static int config_set(uint32_t key, GVariant *data,
struct dev_context *devc; struct dev_context *devc;
uint64_t p, q; uint64_t p, q;
double t_dbl; double t_dbl;
unsigned int i, j; unsigned int i;
int ret, idx; int ret, idx;
const char *tmp_str; const char *tmp_str;
char buffer[16]; char buffer[16];
char *cmd;
char cmd4[4];
devc = sdi->priv; devc = sdi->priv;
@ -549,9 +552,8 @@ static int config_set(uint32_t key, GVariant *data,
return SR_ERR_ARG; return SR_ERR_ARG;
g_free(devc->trigger_slope); g_free(devc->trigger_slope);
devc->trigger_slope = g_strdup((trigger_slopes[idx][0] == 'r') ? "POS" : "NEG"); devc->trigger_slope = g_strdup((trigger_slopes[idx][0] == 'r') ? "POS" : "NEG");
ret = siglent_sds_config_set(sdi, "%s:TRSL %s", return siglent_sds_config_set(sdi, "%s:TRSL %s",
devc->trigger_source, devc->trigger_slope); devc->trigger_source, devc->trigger_slope);
break;
case SR_CONF_HORIZ_TRIGGERPOS: case SR_CONF_HORIZ_TRIGGERPOS:
t_dbl = g_variant_get_double(data); t_dbl = g_variant_get_double(data);
if (t_dbl < 0.0 || t_dbl > 1.0) { if (t_dbl < 0.0 || t_dbl > 1.0) {
@ -563,8 +565,7 @@ static int config_set(uint32_t key, GVariant *data,
* need to express this in seconds. */ * need to express this in seconds. */
t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * devc->num_timebases; t_dbl = -(devc->horiz_triggerpos - 0.5) * devc->timebase * devc->num_timebases;
g_ascii_formatd(buffer, sizeof(buffer), "%.6f", t_dbl); g_ascii_formatd(buffer, sizeof(buffer), "%.6f", t_dbl);
ret = siglent_sds_config_set(sdi, ":TIM:OFFS %s", buffer); return siglent_sds_config_set(sdi, ":TIM:OFFS %s", buffer);
break;
case SR_CONF_TRIGGER_LEVEL: case SR_CONF_TRIGGER_LEVEL:
t_dbl = g_variant_get_double(data); t_dbl = g_variant_get_double(data);
g_ascii_formatd(buffer, sizeof(buffer), "%.3f", t_dbl); g_ascii_formatd(buffer, sizeof(buffer), "%.3f", t_dbl);
@ -573,140 +574,94 @@ static int config_set(uint32_t key, GVariant *data,
devc->trigger_level = t_dbl; devc->trigger_level = t_dbl;
break; break;
case SR_CONF_TIMEBASE: case SR_CONF_TIMEBASE:
g_variant_get(data, "(tt)", &p, &q); if ((idx = std_u64_tuple_idx(data, devc->timebases, devc->num_timebases)) < 0)
for (i = 0; i < devc->num_timebases; i++) { return SR_ERR_ARG;
char *cmd; devc->timebase = (float)devc->timebases[idx][0] / devc->timebases[idx][1];
if (devc->timebases[i][0] == p && devc->timebases[i][1] == q) { p = devc->timebases[idx][0];
cmd = ""; switch (devc->timebases[idx][1]) {
devc->timebase = (float) p / q; case 1:
switch (q) { cmd = g_strdup_printf("%" PRIu64 "S", p);
case 1: break;
cmd = g_strdup_printf("%" PRIu64 "S", p); case 1000:
break; cmd = g_strdup_printf("%" PRIu64 "MS", p);
case 1000: break;
cmd = g_strdup_printf("%" PRIu64 "MS", p); case 1000000:
break; cmd = g_strdup_printf("%" PRIu64 "US", p);
case 1000000: break;
cmd = g_strdup_printf("%" PRIu64 "US", p); case 100000000:
break; cmd = g_strdup_printf("%" PRIu64 "NS", p);
case 100000000: break;
cmd = g_strdup_printf("%" PRIu64 "NS", p);
break;
}
sr_dbg("Setting device timebase: TDIV %s.", cmd);
ret = siglent_sds_config_set(sdi, "TDIV %s", cmd);
break;
}
} }
if (i == devc->num_timebases) { sr_dbg("Setting device timebase: TDIV %s.", cmd);
sr_err("Invalid timebase index: %d.", i); return siglent_sds_config_set(sdi, "TDIV %s", cmd);
ret = SR_ERR_ARG;
}
break;
case SR_CONF_TRIGGER_SOURCE: case SR_CONF_TRIGGER_SOURCE:
tmp_str = g_variant_get_string(data, NULL); if ((idx = std_str_idx(data, ARRAY_AND_SIZE(trigger_sources))) < 0)
for (i = 0; i < ARRAY_SIZE(trigger_sources); i++) { return SR_ERR_ARG;
if (!strcmp(trigger_sources[i], tmp_str)) { g_free(devc->trigger_source);
g_free(devc->trigger_source); devc->trigger_source = g_strdup(trigger_sources[idx]);
devc->trigger_source = g_strdup(trigger_sources[i]); if (!strcmp(devc->trigger_source, "AC Line"))
if (!strcmp(devc->trigger_source, "AC Line")) tmp_str = "LINE";
tmp_str = "LINE"; else if (!strcmp(devc->trigger_source, "CH1"))
else if (!strcmp(devc->trigger_source, "CH1")) tmp_str = "C1";
tmp_str = "C1"; else if (!strcmp(devc->trigger_source, "CH2"))
else if (!strcmp(devc->trigger_source, "CH2")) tmp_str = "C2";
tmp_str = "C2"; else if (!strcmp(devc->trigger_source, "CH3"))
else if (!strcmp(devc->trigger_source, "CH3")) tmp_str = "C3";
tmp_str = "C3"; else if (!strcmp(devc->trigger_source, "CH4"))
else if (!strcmp(devc->trigger_source, "CH4")) tmp_str = "C4";
tmp_str = "C4"; else if (!strcmp(devc->trigger_source, "Ext"))
else if (!strcmp(devc->trigger_source, "Ext")) tmp_str = "EX";
tmp_str = "EX"; else if (!strcmp(devc->trigger_source, "Ext /5"))
else if (!strcmp(devc->trigger_source, "Ext /5")) tmp_str = "EX5";
tmp_str = "EX5"; else
else tmp_str = (char *)devc->trigger_source;
tmp_str = (char *) devc->trigger_source; return siglent_sds_config_set(sdi, "TRSE EDGE,SR,%s,OFF", tmp_str);
ret = siglent_sds_config_set(sdi, "TRSE EDGE,SR,%s,OFF", tmp_str);
break;
}
}
if (i == ARRAY_SIZE(trigger_sources)) {
sr_err("Invalid trigger source index: %d.", i);
ret = SR_ERR_ARG;
}
break;
case SR_CONF_VDIV: case SR_CONF_VDIV:
if (!cg) if (!cg)
return SR_ERR_CHANNEL_GROUP; return SR_ERR_CHANNEL_GROUP;
g_variant_get(data, "(tt)", &p, &q); if ((i = std_cg_idx(cg, devc->analog_groups, devc->model->analog_channels)) < 0)
for (i = 0; i < devc->model->analog_channels; i++) { return SR_ERR_ARG;
char *cmd; if ((idx = std_u64_tuple_idx(data, ARRAY_AND_SIZE(vdivs))) < 0)
if (cg == devc->analog_groups[i]) { return SR_ERR_ARG;
for (j = 0; j < ARRAY_SIZE(vdivs); j++) { devc->vdiv[i] = (float)vdivs[idx][0] / vdivs[idx][1];
if (vdivs[j][0] != p || vdivs[j][1] != q) p = vdivs[idx][0];
continue; switch (vdivs[idx][1]) {
cmd = ""; case 1:
switch (q) { cmd = g_strdup_printf("%" PRIu64 "V", p);
case 1: break;
cmd = g_strdup_printf("%" PRIu64 "V", p); case 1000:
break; cmd = g_strdup_printf("%" PRIu64 "MV", p);
case 1000: break;
cmd = g_strdup_printf("%" PRIu64 "MV", p); case 100000:
break; cmd = g_strdup_printf("%" PRIu64 "UV", p);
case 100000: break;
cmd = g_strdup_printf("%" PRIu64 "UV", p);
break;
}
return siglent_sds_config_set(sdi, "C%d:VDIV %s", i + 1, cmd);
}
sr_err("Invalid vdiv index: %d.", j);
return SR_ERR_ARG;
}
} }
sr_dbg("Didn't set vdiv, unknown channel(group)."); return siglent_sds_config_set(sdi, "C%d:VDIV %s", i + 1, cmd);
return SR_ERR_NA;
case SR_CONF_COUPLING: case SR_CONF_COUPLING:
if (!cg) if (!cg)
return SR_ERR_CHANNEL_GROUP; return SR_ERR_CHANNEL_GROUP;
tmp_str = g_variant_get_string(data, NULL); if ((i = std_cg_idx(cg, devc->analog_groups, devc->model->analog_channels)) < 0)
for (i = 0; i < devc->model->analog_channels; i++) { return SR_ERR_ARG;
char cmd[4]; if ((idx = std_str_idx(data, ARRAY_AND_SIZE(coupling))) < 0)
if (cg == devc->analog_groups[i]) { return SR_ERR_ARG;
for (j = 0; j < ARRAY_SIZE(coupling); j++) { g_free(devc->coupling[i]);
if (!strcmp(tmp_str, coupling[j])) { devc->coupling[i] = g_strdup(coupling[idx]);
g_free(devc->coupling[i]); strncpy(cmd4, devc->coupling[i], 3);
devc->coupling[i] = g_strdup(coupling[j]); cmd4[3] = 0;
strncpy(cmd, devc->coupling[i], 3); return siglent_sds_config_set(sdi, "C%d:CPL %s", i + 1, cmd4);
cmd[3] = 0;
return siglent_sds_config_set(sdi, "C%d:CPL %s", i + 1, cmd);
}
}
sr_err("Invalid coupling index: %d.", j);
return SR_ERR_ARG;
}
}
sr_dbg("Didn't set coupling, unknown channel(group).");
return SR_ERR_NA;
case SR_CONF_PROBE_FACTOR: case SR_CONF_PROBE_FACTOR:
if (!cg) if (!cg)
return SR_ERR_CHANNEL_GROUP; 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); p = g_variant_get_uint64(data);
for (i = 0; i < devc->model->analog_channels; i++) { devc->attenuation[i] = probe_factor[idx];
if (cg == devc->analog_groups[i]) { ret = siglent_sds_config_set(sdi, "C%d:ATTN %" PRIu64, i + 1, p);
for (j = 0; j < ARRAY_SIZE(probe_factor); j++) { if (ret == SR_OK)
if (p == probe_factor[j]) { siglent_sds_get_dev_cfg_vertical(sdi);
devc->attenuation[i] = p; return ret;
ret = siglent_sds_config_set(sdi, "C%d:ATTN %" PRIu64, i + 1, p);
if (ret == SR_OK)
siglent_sds_get_dev_cfg_vertical(sdi);
return ret;
}
}
sr_err("Invalid probe factor: %" PRIu64 ".", p);
return SR_ERR_ARG;
}
}
sr_dbg("Didn't set probe factor, unknown channel(group).");
return SR_ERR_NA;
case SR_CONF_DATA_SOURCE: case SR_CONF_DATA_SOURCE:
tmp_str = g_variant_get_string(data, NULL); tmp_str = g_variant_get_string(data, NULL);
if (!strcmp(tmp_str, "Display")) if (!strcmp(tmp_str, "Display"))
@ -733,61 +688,36 @@ static int config_set(uint32_t key, GVariant *data,
static int config_list(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) const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
{ {
GVariant *tuple, *rational[2]; struct dev_context *devc;
GVariantBuilder gvb;
unsigned int i;
struct dev_context *devc = NULL;
if (key == SR_CONF_SCAN_OPTIONS) { devc = (sdi) ? sdi->priv : NULL;
*data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
return SR_OK;
} else if (key == SR_CONF_DEVICE_OPTIONS && !cg) {
*data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
return SR_OK;
}
/* Every other option requires a valid device instance. */
if (!sdi)
return SR_ERR_ARG;
devc = sdi->priv;
/* If a channel group is specified, it must be a valid one. */
if (cg && !g_slist_find(sdi->channel_groups, cg)) {
sr_err("Invalid channel group specified.");
return SR_ERR;
}
switch (key) { switch (key) {
case SR_CONF_SCAN_OPTIONS:
case SR_CONF_DEVICE_OPTIONS: case SR_CONF_DEVICE_OPTIONS:
if (!cg) if (!cg)
return SR_ERR_CHANNEL_GROUP; return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
if (!devc)
return SR_ERR_ARG;
if (cg == devc->digital_group) { if (cg == devc->digital_group) {
*data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, *data = std_gvar_array_u32(NULL, 0);
NULL, 0, sizeof(uint32_t));
return SR_OK; return SR_OK;
} else { } else {
for (i = 0; i < devc->model->analog_channels; i++) { if (std_cg_idx(cg, devc->analog_groups, devc->model->analog_channels) < 0)
if (cg == devc->analog_groups[i]) { return SR_ERR_ARG;
*data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog));
analog_devopts, ARRAY_SIZE(analog_devopts), sizeof(uint32_t)); return SR_OK;
return SR_OK;
}
}
return SR_ERR_NA;
} }
break; break;
case SR_CONF_COUPLING: case SR_CONF_COUPLING:
if (!cg) if (!cg)
return SR_ERR_CHANNEL_GROUP; return SR_ERR_CHANNEL_GROUP;
*data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling)); *data = g_variant_new_strv(ARRAY_AND_SIZE(coupling));
break; break;
case SR_CONF_PROBE_FACTOR: case SR_CONF_PROBE_FACTOR:
if (!cg) if (!cg)
return SR_ERR_CHANNEL_GROUP; return SR_ERR_CHANNEL_GROUP;
*data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64, *data = std_gvar_array_u64(ARRAY_AND_SIZE(probe_factor));
probe_factor, ARRAY_SIZE(probe_factor), sizeof(uint64_t));
break; break;
case SR_CONF_VDIV: case SR_CONF_VDIV:
if (!devc) if (!devc)
@ -795,14 +725,7 @@ static int config_list(uint32_t key, GVariant **data,
return SR_ERR_ARG; return SR_ERR_ARG;
if (!cg) if (!cg)
return SR_ERR_CHANNEL_GROUP; return SR_ERR_CHANNEL_GROUP;
g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); *data = std_gvar_tuple_array(devc->vdivs, devc->num_vdivs);
for (i = 0; i < devc->num_vdivs; i++) {
rational[0] = g_variant_new_uint64(devc->vdivs[i][0]);
rational[1] = g_variant_new_uint64(devc->vdivs[i][1]);
tuple = g_variant_new_tuple(rational, 2);
g_variant_builder_add_value(&gvb, tuple);
}
*data = g_variant_builder_end(&gvb);
break; break;
case SR_CONF_TIMEBASE: case SR_CONF_TIMEBASE:
if (!devc) if (!devc)
@ -810,14 +733,7 @@ static int config_list(uint32_t key, GVariant **data,
return SR_ERR_ARG; return SR_ERR_ARG;
if (devc->num_timebases <= 0) if (devc->num_timebases <= 0)
return SR_ERR_NA; return SR_ERR_NA;
g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); *data = std_gvar_tuple_array(devc->timebases, devc->num_timebases);
for (i = 0; i < devc->num_timebases; i++) {
rational[0] = g_variant_new_uint64(devc->timebases[i][0]);
rational[1] = g_variant_new_uint64(devc->timebases[i][1]);
tuple = g_variant_new_tuple(rational, 2);
g_variant_builder_add_value(&gvb, tuple);
}
*data = g_variant_builder_end(&gvb);
break; break;
case SR_CONF_TRIGGER_SOURCE: case SR_CONF_TRIGGER_SOURCE:
if (!devc) if (!devc)
@ -827,7 +743,7 @@ static int config_list(uint32_t key, GVariant **data,
devc->model->has_digital ? ARRAY_SIZE(trigger_sources) : 5); devc->model->has_digital ? ARRAY_SIZE(trigger_sources) : 5);
break; break;
case SR_CONF_TRIGGER_SLOPE: case SR_CONF_TRIGGER_SLOPE:
*data = g_variant_new_strv(trigger_slopes, ARRAY_SIZE(trigger_slopes)); *data = g_variant_new_strv(ARRAY_AND_SIZE(trigger_slopes));
break; break;
case SR_CONF_DATA_SOURCE: case SR_CONF_DATA_SOURCE:
if (!devc) if (!devc)
@ -839,7 +755,7 @@ static int config_list(uint32_t key, GVariant **data,
*data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 1); *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources) - 1);
break; break;
case SPO_MODEL: case SPO_MODEL:
*data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources)); *data = g_variant_new_strv(ARRAY_AND_SIZE(data_sources));
break; break;
} }
break; break;