From: Lars-Peter Clausen Date: Fri, 13 May 2016 15:36:06 +0000 (+0200) Subject: demo: Handle the case when zero analog or logic channels were requested X-Git-Tag: libsigrok-0.5.0~413 X-Git-Url: https://sigrok.org/gitweb/?a=commitdiff_plain;ds=inline;h=f18e0db3b8e8c24703e74fe36f9353381221b3c7;p=libsigrok.git demo: Handle the case when zero analog or logic channels were requested The demo device has support for specifying the number of analog and logic channels it should have. Currently this does not work correctly if one of them is set to zero. Being able to set the number of channels to zero for one of the channel types is quite useful for corner case testing though. Make the following modifications to handle it correctly: 1) If the channel count is zero no channel group for that channel type should be created since a channel group needs at least one channel. 2) Drop the check if logic_unitsize is less or equal to zero in prepare_data() since this condition will always be true if the number of logic channels is zero and it is not possible to create a demo device with only analog channels. Signed-off-by: Lars-Peter Clausen --- diff --git a/src/hardware/demo/demo.c b/src/hardware/demo/demo.c index 0a220fe7..e70cf966 100644 --- a/src/hardware/demo/demo.c +++ b/src/hardware/demo/demo.c @@ -295,51 +295,55 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) devc->logic_pattern = PATTERN_SIGROK; devc->num_analog_channels = num_analog_channels; - /* Logic channels, all in one channel group. */ - cg = g_malloc0(sizeof(struct sr_channel_group)); - cg->name = g_strdup("Logic"); - for (i = 0; i < num_logic_channels; i++) { - sprintf(channel_name, "D%d", i); - ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name); - cg->channels = g_slist_append(cg->channels, ch); + if (num_logic_channels > 0) { + /* Logic channels, all in one channel group. */ + cg = g_malloc0(sizeof(struct sr_channel_group)); + cg->name = g_strdup("Logic"); + for (i = 0; i < num_logic_channels; i++) { + sprintf(channel_name, "D%d", i); + ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name); + cg->channels = g_slist_append(cg->channels, ch); + } + sdi->channel_groups = g_slist_append(NULL, cg); } - sdi->channel_groups = g_slist_append(NULL, cg); /* Analog channels, channel groups and pattern generators. */ - pattern = 0; - /* An "Analog" channel group with all analog channels in it. */ - acg = g_malloc0(sizeof(struct sr_channel_group)); - acg->name = g_strdup("Analog"); - sdi->channel_groups = g_slist_append(sdi->channel_groups, acg); - - devc->ch_ag = g_hash_table_new(g_direct_hash, g_direct_equal); - for (i = 0; i < num_analog_channels; i++) { - snprintf(channel_name, 16, "A%d", i); - ch = sr_channel_new(sdi, i + num_logic_channels, SR_CHANNEL_ANALOG, - TRUE, channel_name); - acg->channels = g_slist_append(acg->channels, ch); - - /* Every analog channel gets its own channel group as well. */ - cg = g_malloc0(sizeof(struct sr_channel_group)); - cg->name = g_strdup(channel_name); - cg->channels = g_slist_append(NULL, ch); - sdi->channel_groups = g_slist_append(sdi->channel_groups, cg); - - /* Every channel gets a generator struct. */ - ag = g_malloc(sizeof(struct analog_gen)); - ag->amplitude = DEFAULT_ANALOG_AMPLITUDE; - ag->packet.channels = cg->channels; - ag->packet.mq = 0; - ag->packet.mqflags = 0; - ag->packet.unit = SR_UNIT_VOLT; - ag->packet.data = ag->pattern_data; - ag->pattern = pattern; - ag->avg_val = 0.0f; - ag->num_avgs = 0; - g_hash_table_insert(devc->ch_ag, ch, ag); - - if (++pattern == ARRAY_SIZE(analog_pattern_str)) - pattern = 0; + if (num_analog_channels > 0) { + pattern = 0; + /* An "Analog" channel group with all analog channels in it. */ + acg = g_malloc0(sizeof(struct sr_channel_group)); + acg->name = g_strdup("Analog"); + sdi->channel_groups = g_slist_append(sdi->channel_groups, acg); + + devc->ch_ag = g_hash_table_new(g_direct_hash, g_direct_equal); + for (i = 0; i < num_analog_channels; i++) { + snprintf(channel_name, 16, "A%d", i); + ch = sr_channel_new(sdi, i + num_logic_channels, SR_CHANNEL_ANALOG, + TRUE, channel_name); + acg->channels = g_slist_append(acg->channels, ch); + + /* Every analog channel gets its own channel group as well. */ + cg = g_malloc0(sizeof(struct sr_channel_group)); + cg->name = g_strdup(channel_name); + cg->channels = g_slist_append(NULL, ch); + sdi->channel_groups = g_slist_append(sdi->channel_groups, cg); + + /* Every channel gets a generator struct. */ + ag = g_malloc(sizeof(struct analog_gen)); + ag->amplitude = DEFAULT_ANALOG_AMPLITUDE; + ag->packet.channels = cg->channels; + ag->packet.mq = 0; + ag->packet.mqflags = 0; + ag->packet.unit = SR_UNIT_VOLT; + ag->packet.data = ag->pattern_data; + ag->pattern = pattern; + ag->avg_val = 0.0f; + ag->num_avgs = 0; + g_hash_table_insert(devc->ch_ag, ch, ag); + + if (++pattern == ARRAY_SIZE(analog_pattern_str)) + pattern = 0; + } } sdi->priv = devc; @@ -744,7 +748,7 @@ static int prepare_data(int fd, int revents, void *cb_data) devc = sdi->priv; /* Just in case. */ - if (devc->cur_samplerate <= 0 || devc->logic_unitsize <= 0 + if (devc->cur_samplerate <= 0 || (devc->num_logic_channels <= 0 && devc->num_analog_channels <= 0)) { dev_acquisition_stop(sdi);