X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fdemo%2Fdemo.c;h=9fa744b1f4e965b448b5509ba277d068adedbc75;hb=dd5c48a6d567a3cac62c4b0058588273bbeea171;hp=1587d5e7b8c5c64eaf45c324508a276fd058985b;hpb=1f8f5bc08e0f684156baac513431b40d79ab4ea1;p=libsigrok.git diff --git a/src/hardware/demo/demo.c b/src/hardware/demo/demo.c index 1587d5e7..9fa744b1 100644 --- a/src/hardware/demo/demo.c +++ b/src/hardware/demo/demo.c @@ -175,15 +175,8 @@ static const uint8_t pattern_sigrok[] = { 0xbe, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; -SR_PRIV struct sr_dev_driver demo_driver_info; - static int dev_acquisition_stop(struct sr_dev_inst *sdi); -static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) -{ - return std_init(di, sr_ctx, LOG_PREFIX); -} - static void generate_analog_pattern(struct analog_gen *ag, uint64_t sample_rate) { double t, frequency; @@ -289,7 +282,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) devices = NULL; sdi = g_malloc0(sizeof(struct sr_dev_inst)); - sdi->status = SR_ST_ACTIVE; + sdi->status = SR_ST_INACTIVE; sdi->model = g_strdup("Demo device"); sdi->driver = di; @@ -300,51 +293,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; @@ -749,7 +746,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); @@ -879,11 +876,11 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi) return SR_OK; } -SR_PRIV struct sr_dev_driver demo_driver_info = { +static struct sr_dev_driver demo_driver_info = { .name = "demo", .longname = "Demo driver and pattern generator", .api_version = 1, - .init = init, + .init = std_init, .cleanup = std_cleanup, .scan = scan, .dev_list = std_dev_list, @@ -897,3 +894,4 @@ SR_PRIV struct sr_dev_driver demo_driver_info = { .dev_acquisition_stop = dev_acquisition_stop, .context = NULL, }; +SR_REGISTER_DEV_DRIVER(demo_driver_info);