From: Gerhard Sittig Date: Thu, 16 Mar 2023 04:18:10 +0000 (+0100) Subject: rdtech-tc: migrate to feed queue helper X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=b3df76683179cbb9559e1cad8efe0d10c02dacce;p=libsigrok.git rdtech-tc: migrate to feed queue helper Implement the submission of analog data to the sigrok session by means of the common feed queue logic. Use the feed queue's scaling support. Implement a .dev_clear() routine which releases resources. This exlusively leaves the extraction of numbers from binary data as the only remaining use case for the binary helpers logic. --- diff --git a/src/hardware/rdtech-tc/api.c b/src/hardware/rdtech-tc/api.c index 5c6bb70e..5dd277ae 100644 --- a/src/hardware/rdtech-tc/api.c +++ b/src/hardware/rdtech-tc/api.c @@ -54,6 +54,9 @@ static GSList *rdtech_tc_scan(struct sr_dev_driver *di, struct dev_context *devc; struct sr_dev_inst *sdi; size_t i; + const struct rdtech_tc_channel_desc *pch; + struct sr_channel *ch; + struct feed_queue_analog *feed; serial = sr_serial_dev_inst_new(conn, serialcomm); if (serial_open(serial, SERIAL_RDWR) != SR_OK) @@ -77,9 +80,14 @@ static GSList *rdtech_tc_scan(struct sr_dev_driver *di, sdi->inst_type = SR_INST_SERIAL; sdi->conn = serial; + devc->feeds = g_malloc0(devc->channel_count * sizeof(devc->feeds[0])); for (i = 0; devc->channels[i].name; i++) { - sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, - devc->channels[i].name); + pch = &devc->channels[i]; + ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, pch->name); + feed = feed_queue_analog_alloc(sdi, 1, pch->digits, ch); + feed_queue_analog_mq_unit(feed, pch->mq, 0, pch->unit); + feed_queue_analog_scale_offset(feed, &pch->scale, NULL); + devc->feeds[i] = feed; } devices = g_slist_append(NULL, sdi); @@ -98,6 +106,25 @@ err_out: return NULL; } +static void clear_helper(struct dev_context *devc) +{ + size_t idx; + + if (!devc) + return; + + if (devc->feeds) { + for (idx = 0; idx < devc->channel_count; idx++) + feed_queue_analog_free(devc->feeds[idx]); + g_free(devc->feeds); + } +} + +static int dev_clear(const struct sr_dev_driver *driver) +{ + return std_dev_clear_with_callback(driver, (std_dev_clear_callback)clear_helper); +} + static GSList *scan(struct sr_dev_driver *di, GSList *options) { const char *conn; @@ -154,7 +181,7 @@ static struct sr_dev_driver rdtech_tc_driver_info = { .cleanup = std_cleanup, .scan = scan, .dev_list = std_dev_list, - .dev_clear = std_dev_clear, + .dev_clear = dev_clear, .config_get = NULL, .config_set = config_set, .config_list = config_list, diff --git a/src/hardware/rdtech-tc/protocol.c b/src/hardware/rdtech-tc/protocol.c index f2a439e4..32ada461 100644 --- a/src/hardware/rdtech-tc/protocol.c +++ b/src/hardware/rdtech-tc/protocol.c @@ -71,14 +71,13 @@ static const uint8_t AES_KEY[] = { 0xa7, 0xf1, 0x06, 0x61, 0x9a, 0xb8, 0x72, 0x88, }; -static const struct binary_analog_channel rdtech_tc_channels[] = { - { "V", { 0 + 48, BVT_LE_UINT32, 1e-4, }, 4, SR_MQ_VOLTAGE, SR_UNIT_VOLT }, - { "I", { 0 + 52, BVT_LE_UINT32, 1e-5, }, 5, SR_MQ_CURRENT, SR_UNIT_AMPERE }, - { "D+", { 64 + 32, BVT_LE_UINT32, 1e-2, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT }, - { "D-", { 64 + 36, BVT_LE_UINT32, 1e-2, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT }, - { "E0", { 64 + 12, BVT_LE_UINT32, 1e-3, }, 3, SR_MQ_ENERGY, SR_UNIT_WATT_HOUR }, - { "E1", { 64 + 20, BVT_LE_UINT32, 1e-3, }, 3, SR_MQ_ENERGY, SR_UNIT_WATT_HOUR }, - ALL_ZERO, +static const struct rdtech_tc_channel_desc rdtech_tc_channels[] = { + { "V", { 0 + 48, BVT_LE_UINT32, 1, }, { 100, 1e6, }, 4, SR_MQ_VOLTAGE, SR_UNIT_VOLT }, + { "I", { 0 + 52, BVT_LE_UINT32, 1, }, { 10, 1e6, }, 5, SR_MQ_CURRENT, SR_UNIT_AMPERE }, + { "D+", { 64 + 32, BVT_LE_UINT32, 1, }, { 10, 1e3, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT }, + { "D-", { 64 + 36, BVT_LE_UINT32, 1, }, { 10, 1e3, }, 2, SR_MQ_VOLTAGE, SR_UNIT_VOLT }, + { "E0", { 64 + 12, BVT_LE_UINT32, 1, }, { 1, 1e3, }, 3, SR_MQ_ENERGY, SR_UNIT_WATT_HOUR }, + { "E1", { 64 + 20, BVT_LE_UINT32, 1, }, { 1, 1e3, }, 3, SR_MQ_ENERGY, SR_UNIT_WATT_HOUR }, }; static gboolean check_pac_crc(uint8_t *data) @@ -149,6 +148,7 @@ SR_PRIV int rdtech_tc_probe(struct sr_serial_dev_inst *serial, struct dev_contex } devc->channels = rdtech_tc_channels; + devc->channel_count = ARRAY_SIZE(rdtech_tc_channels); devc->dev_info.model_name = g_strndup((const char *)&poll_pkt[OFF_MODEL], LEN_MODEL); devc->dev_info.fw_ver = g_strndup((const char *)&poll_pkt[OFF_FW_VER], LEN_FW_VER); devc->dev_info.serial_num = read_u32le(&poll_pkt[OFF_SERIAL]); @@ -199,9 +199,10 @@ static int handle_poll_data(struct sr_dev_inst *sdi) { struct dev_context *devc; uint8_t poll_pkt[TC_POLL_LEN]; - size_t i; - GSList *ch; + size_t ch_idx; + const struct rdtech_tc_channel_desc *pch; int ret; + float v; devc = sdi->priv; sr_spew("Received poll packet (len: %zu).", devc->buflen); @@ -215,11 +216,12 @@ static int handle_poll_data(struct sr_dev_inst *sdi) return SR_ERR_DATA; } - i = 0; - for (ch = sdi->channels; ch; ch = g_slist_next(ch)) { - ret = bv_send_analog_channel(sdi, ch->data, - &devc->channels[i], poll_pkt, TC_POLL_LEN); - i++; + for (ch_idx = 0; ch_idx < devc->channel_count; ch_idx++) { + pch = &devc->channels[ch_idx]; + ret = bv_get_value(&v, &pch->spec, poll_pkt, TC_POLL_LEN); + if (ret != SR_OK) + return ret; + ret = feed_queue_analog_submit(devc->feeds[ch_idx], v, 1); if (ret != SR_OK) return ret; } diff --git a/src/hardware/rdtech-tc/protocol.h b/src/hardware/rdtech-tc/protocol.h index d13527df..6458460a 100644 --- a/src/hardware/rdtech-tc/protocol.h +++ b/src/hardware/rdtech-tc/protocol.h @@ -33,9 +33,20 @@ struct rdtech_dev_info { uint32_t serial_num; }; +struct rdtech_tc_channel_desc { + const char *name; + struct binary_value_spec spec; + struct sr_rational scale; + int digits; + enum sr_mq mq; + enum sr_unit unit; +}; + struct dev_context { struct rdtech_dev_info dev_info; - const struct binary_analog_channel *channels; + const struct rdtech_tc_channel_desc *channels; + size_t channel_count; + struct feed_queue_analog **feeds; struct sr_sw_limits limits; uint8_t buf[RDTECH_TC_BUFSIZE]; size_t buflen;