X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fdreamsourcelab-dslogic%2Fapi.c;h=83090f2d147a0823ac4520ad5d016a00a2fa66ca;hb=697fb6ddfc2522b2e6d35511c7837e2c61d8ae73;hp=177932245aa673ece4f7d590c3a71b500aef2976;hpb=6e43c3d5318a8ff320bf6539a9efe7f0b8497e2f;p=libsigrok.git diff --git a/src/hardware/dreamsourcelab-dslogic/api.c b/src/hardware/dreamsourcelab-dslogic/api.c index 17793224..83090f2d 100644 --- a/src/hardware/dreamsourcelab-dslogic/api.c +++ b/src/hardware/dreamsourcelab-dslogic/api.c @@ -47,14 +47,14 @@ static const struct dslogic_profile supported_device[] = { ALL_ZERO }; -static const uint32_t drvopts[] = { - SR_CONF_LOGIC_ANALYZER, -}; - static const uint32_t scanopts[] = { SR_CONF_CONN, }; +static const uint32_t drvopts[] = { + SR_CONF_LOGIC_ANALYZER, +}; + static const uint32_t devopts[] = { SR_CONF_CONTINUOUS | SR_CONF_SET | SR_CONF_GET, SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, @@ -67,16 +67,12 @@ static const uint32_t devopts[] = { SR_CONF_CLOCK_EDGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, }; -/* Names assigned to available edge slope choices. */ -static const char *const signal_edge_names[] = { +static const char *signal_edge_names[] = { [DS_EDGE_RISING] = "rising", [DS_EDGE_FALLING] = "falling", }; -static const struct { - gdouble low; - gdouble high; -} voltage_thresholds[] = { +static const double voltage_thresholds[][2] = { { 0.7, 1.4 }, { 1.4, 3.6 }, }; @@ -223,7 +219,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) } } - /* Skip if the device was not found. */ if (!prof) continue; @@ -283,19 +278,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) return std_scan_complete(di, devices); } -static void clear_dev_context(void *priv) -{ - struct dev_context *devc; - - devc = priv; - g_free(devc); -} - -static int dev_clear(const struct sr_dev_driver *di) -{ - return std_dev_clear_with_callback(di, clear_dev_context); -} - static int dev_open(struct sr_dev_inst *sdi) { struct sr_dev_driver *di = sdi->driver; @@ -398,9 +380,7 @@ static int config_get(uint32_t key, GVariant **data, { struct dev_context *devc; struct sr_usb_dev_inst *usb; - GVariant *range[2]; - unsigned int i, voltage_range; - char str[128]; + int idx; (void)cg; @@ -418,28 +398,18 @@ static int config_get(uint32_t key, GVariant **data, /* Device still needs to re-enumerate after firmware * upload, so we don't know its (future) address. */ return SR_ERR; - snprintf(str, 128, "%d.%d", usb->bus, usb->address); - *data = g_variant_new_string(str); + *data = g_variant_new_printf("%d.%d", usb->bus, usb->address); break; case SR_CONF_VOLTAGE_THRESHOLD: if (!strcmp(devc->profile->model, "DSLogic")) { - voltage_range = 0; - - for (i = 0; i < ARRAY_SIZE(voltage_thresholds); i++) - if (voltage_thresholds[i].low == devc->cur_threshold) { - voltage_range = i; - break; - } - - range[0] = g_variant_new_double( - voltage_thresholds[voltage_range].low); - range[1] = g_variant_new_double( - voltage_thresholds[voltage_range].high); + if ((idx = std_double_tuple_idx_d0(devc->cur_threshold, + ARRAY_AND_SIZE(voltage_thresholds))) < 0) + return SR_ERR_BUG; + *data = std_gvar_tuple_double(voltage_thresholds[idx][0], + voltage_thresholds[idx][1]); } else { - range[0] = g_variant_new_double(devc->cur_threshold); - range[1] = g_variant_new_double(devc->cur_threshold); + *data = std_gvar_tuple_double(devc->cur_threshold, devc->cur_threshold); } - *data = g_variant_new_tuple(range, 2); break; case SR_CONF_LIMIT_SAMPLES: *data = g_variant_new_uint64(devc->limit_samples); @@ -457,8 +427,8 @@ static int config_get(uint32_t key, GVariant **data, *data = g_variant_new_boolean(devc->continuous_mode); break; case SR_CONF_CLOCK_EDGE: - i = devc->clock_edge; - if (i >= ARRAY_SIZE(signal_edge_names)) + idx = devc->clock_edge; + if (idx >= (int)ARRAY_SIZE(signal_edge_names)) return SR_ERR_BUG; *data = g_variant_new_string(signal_edge_names[0]); break; @@ -469,34 +439,11 @@ static int config_get(uint32_t key, GVariant **data, return SR_OK; } -/* - * Helper for mapping a string-typed configuration value to an index - * within a table of possible values. - */ -static int lookup_index(GVariant *value, const char *const *table, int len) -{ - const char *entry; - int i; - - entry = g_variant_get_string(value, NULL); - if (!entry) - return -1; - - /* Linear search is fine for very small tables. */ - for (i = 0; i < len; i++) { - if (strcmp(entry, table[i]) == 0) - return i; - } - - return -1; -} - static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { struct dev_context *devc; - uint64_t arg; - int i, ret; + int idx, ret; gdouble low, high; (void)cg; @@ -510,15 +457,9 @@ static int config_set(uint32_t key, GVariant *data, switch (key) { case SR_CONF_SAMPLERATE: - arg = g_variant_get_uint64(data); - for (i = 0; i < devc->num_samplerates; i++) { - if (devc->samplerates[i] == arg) { - devc->cur_samplerate = arg; - break; - } - } - if (i == devc->num_samplerates) - ret = SR_ERR_ARG; + if ((idx = std_u64_idx(data, devc->samplerates, devc->num_samplerates)) < 0) + return SR_ERR_ARG; + devc->cur_samplerate = devc->samplerates[idx]; break; case SR_CONF_LIMIT_SAMPLES: devc->limit_samples = g_variant_get_uint64(data); @@ -528,18 +469,13 @@ static int config_set(uint32_t key, GVariant *data, ret = (devc->capture_ratio > 100) ? SR_ERR : SR_OK; break; case SR_CONF_VOLTAGE_THRESHOLD: - g_variant_get(data, "(dd)", &low, &high); if (!strcmp(devc->profile->model, "DSLogic")) { - for (i = 0; (unsigned int)i < ARRAY_SIZE(voltage_thresholds); i++) { - if (fabs(voltage_thresholds[i].low - low) < 0.1 && - fabs(voltage_thresholds[i].high - high) < 0.1) { - devc->cur_threshold = - voltage_thresholds[i].low; - break; - } - } + if ((idx = std_double_tuple_idx(data, ARRAY_AND_SIZE(voltage_thresholds))) < 0) + return SR_ERR_ARG; + devc->cur_threshold = voltage_thresholds[idx][0]; ret = dslogic_fpga_firmware_upload(sdi); } else { + g_variant_get(data, "(dd)", &low, &high); ret = dslogic_set_voltage_threshold(sdi, (low + high) / 2.0); } break; @@ -550,11 +486,9 @@ static int config_set(uint32_t key, GVariant *data, devc->continuous_mode = g_variant_get_boolean(data); break; case SR_CONF_CLOCK_EDGE: - i = lookup_index(data, signal_edge_names, - ARRAY_SIZE(signal_edge_names)); - if (i < 0) + if ((idx = std_str_idx(data, ARRAY_AND_SIZE(signal_edge_names))) < 0) return SR_ERR_ARG; - devc->clock_edge = i; + devc->clock_edge = idx; break; default: ret = SR_ERR_NA; @@ -566,61 +500,25 @@ 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) { - struct dev_context *devc = NULL; - GVariant *gvar, *range[2]; - GVariantBuilder gvb; - unsigned int i; - double v; + struct dev_context *devc; - (void)cg; + devc = (sdi) ? sdi->priv : NULL; switch (key) { case SR_CONF_SCAN_OPTIONS: - *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, - scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); - break; case SR_CONF_DEVICE_OPTIONS: - if (!sdi) { - *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, - drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); - } else { - devc = sdi->priv; - *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, - devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); - } - break; + return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); case SR_CONF_VOLTAGE_THRESHOLD: - if (sdi->priv) - devc = sdi->priv; - g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); - if (devc && !strcmp(devc->profile->model, "DSLogic")) { - for (i = 0; i < ARRAY_SIZE(voltage_thresholds); i++) { - range[0] = g_variant_new_double(voltage_thresholds[i].low); - range[1] = g_variant_new_double(voltage_thresholds[i].high); - gvar = g_variant_new_tuple(range, 2); - g_variant_builder_add_value(&gvb, gvar); - } - } else { - for (v = 0.0; v <= 5.0; v += 0.1) { - range[0] = g_variant_new_double(v); - range[1] = g_variant_new_double(v); - gvar = g_variant_new_tuple(range, 2); - g_variant_builder_add_value(&gvb, gvar); - } - } - *data = g_variant_builder_end(&gvb); + if (!strcmp(devc->profile->model, "DSLogic")) + *data = std_gvar_thresholds(ARRAY_AND_SIZE(voltage_thresholds)); + else + *data = std_gvar_min_max_step_thresholds(0.0, 5.0, 0.1); break; case SR_CONF_SAMPLERATE: - devc = sdi->priv; - g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); - gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), devc->samplerates, - devc->num_samplerates, sizeof(uint64_t)); - g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); - *data = g_variant_builder_end(&gvb); + *data = std_gvar_samplerates(devc->samplerates, devc->num_samplerates); break; case SR_CONF_CLOCK_EDGE: - *data = g_variant_new_strv(signal_edge_names, - ARRAY_SIZE(signal_edge_names)); + *data = g_variant_new_strv(ARRAY_AND_SIZE(signal_edge_names)); break; default: return SR_ERR_NA; @@ -637,7 +535,7 @@ static struct sr_dev_driver dreamsourcelab_dslogic_driver_info = { .cleanup = std_cleanup, .scan = scan, .dev_list = std_dev_list, - .dev_clear = dev_clear, + .dev_clear = std_dev_clear, .config_get = config_get, .config_set = config_set, .config_list = config_list,