2 * This file is part of the libsigrok project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2011 Olivier Fauchon <olivier@aixmarseille.com>
6 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
7 * Copyright (C) 2015 Bartosz Golaszewski <bgolaszewski@baylibre.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include <libsigrok/libsigrok.h>
29 #include "libsigrok-internal.h"
31 #define LOG_PREFIX "demo"
33 #define DEFAULT_NUM_LOGIC_CHANNELS 8
34 #define DEFAULT_NUM_ANALOG_CHANNELS 4
36 /* The size in bytes of chunks to send through the session bus. */
37 #define LOGIC_BUFSIZE 4096
38 /* Size of the analog pattern space per channel. */
39 #define ANALOG_BUFSIZE 4096
41 #define DEFAULT_ANALOG_AMPLITUDE 25
42 #define ANALOG_SAMPLES_PER_PERIOD 20
44 /* Logic patterns we can generate. */
47 * Spells "sigrok" across 8 channels using '0's (with '1's as
48 * "background") when displayed using the 'bits' output format.
49 * The pattern is repeated every 8 channels, shifted to the right
54 /** Pseudo-random values on all channels. */
58 * Incrementing number across 8 channels. The pattern is repeated
59 * every 8 channels, shifted to the right in time by one bit.
63 /** All channels have a low logic state. */
66 /** All channels have a high logic state. */
70 /* Analog patterns we can generate. */
81 static const char *logic_pattern_str[] = {
89 static const char *analog_pattern_str[] = {
99 float pattern_data[ANALOG_BUFSIZE];
100 unsigned int num_samples;
101 struct sr_datafeed_analog packet;
102 float avg_val; /* Average value */
103 unsigned num_avgs; /* Number of samples averaged */
106 /* Private, per-device-instance driver context. */
108 uint64_t cur_samplerate;
110 uint64_t limit_samples;
112 uint64_t logic_counter;
113 uint64_t analog_counter;
117 int32_t num_logic_channels;
118 unsigned int logic_unitsize;
119 /* There is only ever one logic channel group, so its pattern goes here. */
120 uint8_t logic_pattern;
121 unsigned char logic_data[LOGIC_BUFSIZE];
123 int32_t num_analog_channels;
125 gboolean avg; /* True if averaging is enabled */
126 uint64_t avg_samples;
129 static const uint32_t drvopts[] = {
131 SR_CONF_LOGIC_ANALYZER,
132 SR_CONF_OSCILLOSCOPE,
135 static const uint32_t scanopts[] = {
136 SR_CONF_NUM_LOGIC_CHANNELS,
137 SR_CONF_NUM_ANALOG_CHANNELS,
140 static const uint32_t devopts[] = {
141 SR_CONF_CONTINUOUS | SR_CONF_SET,
142 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
143 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
144 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
145 SR_CONF_AVERAGING | SR_CONF_GET | SR_CONF_SET,
146 SR_CONF_AVG_SAMPLES | SR_CONF_GET | SR_CONF_SET,
149 static const uint32_t devopts_cg_logic[] = {
150 SR_CONF_PATTERN_MODE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
153 static const uint32_t devopts_cg_analog_group[] = {
154 SR_CONF_AMPLITUDE | SR_CONF_GET | SR_CONF_SET,
157 static const uint32_t devopts_cg_analog_channel[] = {
158 SR_CONF_PATTERN_MODE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
159 SR_CONF_AMPLITUDE | SR_CONF_GET | SR_CONF_SET,
162 static const uint64_t samplerates[] = {
168 static const uint8_t pattern_sigrok[] = {
169 0x4c, 0x92, 0x92, 0x92, 0x64, 0x00, 0x00, 0x00,
170 0x82, 0xfe, 0xfe, 0x82, 0x00, 0x00, 0x00, 0x00,
171 0x7c, 0x82, 0x82, 0x92, 0x74, 0x00, 0x00, 0x00,
172 0xfe, 0x12, 0x12, 0x32, 0xcc, 0x00, 0x00, 0x00,
173 0x7c, 0x82, 0x82, 0x82, 0x7c, 0x00, 0x00, 0x00,
174 0xfe, 0x10, 0x28, 0x44, 0x82, 0x00, 0x00, 0x00,
175 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
176 0xbe, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
179 SR_PRIV struct sr_dev_driver demo_driver_info;
181 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
183 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
185 return std_init(sr_ctx, di, LOG_PREFIX);
188 static void generate_analog_pattern(struct analog_gen *ag, uint64_t sample_rate)
192 unsigned int num_samples, i;
195 sr_dbg("Generating %s pattern.", analog_pattern_str[ag->pattern]);
197 num_samples = ANALOG_BUFSIZE / sizeof(float);
199 switch (ag->pattern) {
201 value = ag->amplitude;
203 for (i = 0; i < num_samples; i++) {
208 ag->pattern_data[i] = value;
210 ag->num_samples = last_end;
213 frequency = (double) sample_rate / ANALOG_SAMPLES_PER_PERIOD;
215 /* Make sure the number of samples we put out is an integer
216 * multiple of our period size */
217 /* FIXME we actually need only one period. A ringbuffer would be
219 while (num_samples % ANALOG_SAMPLES_PER_PERIOD != 0)
222 for (i = 0; i < num_samples; i++) {
223 t = (double) i / (double) sample_rate;
224 ag->pattern_data[i] = ag->amplitude *
225 sin(2 * G_PI * frequency * t);
228 ag->num_samples = num_samples;
230 case PATTERN_TRIANGLE:
231 frequency = (double) sample_rate / ANALOG_SAMPLES_PER_PERIOD;
233 while (num_samples % ANALOG_SAMPLES_PER_PERIOD != 0)
236 for (i = 0; i < num_samples; i++) {
237 t = (double) i / (double) sample_rate;
238 ag->pattern_data[i] = (2 * ag->amplitude / G_PI) *
239 asin(sin(2 * G_PI * frequency * t));
242 ag->num_samples = num_samples;
244 case PATTERN_SAWTOOTH:
245 frequency = (double) sample_rate / ANALOG_SAMPLES_PER_PERIOD;
247 while (num_samples % ANALOG_SAMPLES_PER_PERIOD != 0)
250 for (i = 0; i < num_samples; i++) {
251 t = (double) i / (double) sample_rate;
252 ag->pattern_data[i] = 2 * ag->amplitude *
253 ((t * frequency) - floor(0.5f + t * frequency));
256 ag->num_samples = num_samples;
261 static GSList *scan(struct sr_dev_driver *di, GSList *options)
263 struct drv_context *drvc;
264 struct dev_context *devc;
265 struct sr_dev_inst *sdi;
266 struct sr_channel *ch;
267 struct sr_channel_group *cg, *acg;
268 struct sr_config *src;
269 struct analog_gen *ag;
271 int num_logic_channels, num_analog_channels, pattern, i;
272 char channel_name[16];
276 num_logic_channels = DEFAULT_NUM_LOGIC_CHANNELS;
277 num_analog_channels = DEFAULT_NUM_ANALOG_CHANNELS;
278 for (l = options; l; l = l->next) {
281 case SR_CONF_NUM_LOGIC_CHANNELS:
282 num_logic_channels = g_variant_get_int32(src->data);
284 case SR_CONF_NUM_ANALOG_CHANNELS:
285 num_analog_channels = g_variant_get_int32(src->data);
292 sdi = g_malloc0(sizeof(struct sr_dev_inst));
293 sdi->status = SR_ST_ACTIVE;
294 sdi->model = g_strdup("Demo device");
297 devc = g_malloc(sizeof(struct dev_context));
298 devc->cur_samplerate = SR_KHZ(200);
299 devc->limit_samples = 0;
300 devc->limit_msec = 0;
302 devc->continuous = FALSE;
303 devc->num_logic_channels = num_logic_channels;
304 devc->logic_unitsize = (devc->num_logic_channels + 7) / 8;
305 devc->logic_pattern = PATTERN_SIGROK;
306 devc->num_analog_channels = num_analog_channels;
308 devc->avg_samples = 0;
310 /* Logic channels, all in one channel group. */
311 cg = g_malloc0(sizeof(struct sr_channel_group));
312 cg->name = g_strdup("Logic");
313 for (i = 0; i < num_logic_channels; i++) {
314 sprintf(channel_name, "D%d", i);
315 ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name);
316 cg->channels = g_slist_append(cg->channels, ch);
318 sdi->channel_groups = g_slist_append(NULL, cg);
320 /* Analog channels, channel groups and pattern generators. */
322 /* An "Analog" channel group with all analog channels in it. */
323 acg = g_malloc0(sizeof(struct sr_channel_group));
324 acg->name = g_strdup("Analog");
325 sdi->channel_groups = g_slist_append(sdi->channel_groups, acg);
327 devc->ch_ag = g_hash_table_new(g_direct_hash, g_direct_equal);
328 for (i = 0; i < num_analog_channels; i++) {
329 snprintf(channel_name, 16, "A%d", i);
330 ch = sr_channel_new(sdi, i + num_logic_channels, SR_CHANNEL_ANALOG,
332 acg->channels = g_slist_append(acg->channels, ch);
334 /* Every analog channel gets its own channel group as well. */
335 cg = g_malloc0(sizeof(struct sr_channel_group));
336 cg->name = g_strdup(channel_name);
337 cg->channels = g_slist_append(NULL, ch);
338 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
340 /* Every channel gets a generator struct. */
341 ag = g_malloc(sizeof(struct analog_gen));
342 ag->amplitude = DEFAULT_ANALOG_AMPLITUDE;
343 ag->packet.channels = cg->channels;
345 ag->packet.mqflags = 0;
346 ag->packet.unit = SR_UNIT_VOLT;
347 ag->packet.data = ag->pattern_data;
348 ag->pattern = pattern;
351 g_hash_table_insert(devc->ch_ag, ch, ag);
353 if (++pattern == ARRAY_SIZE(analog_pattern_str))
358 devices = g_slist_append(devices, sdi);
359 drvc->instances = g_slist_append(drvc->instances, sdi);
364 static GSList *dev_list(const struct sr_dev_driver *di)
366 return ((struct drv_context *)(di->context))->instances;
369 static int dev_open(struct sr_dev_inst *sdi)
371 sdi->status = SR_ST_ACTIVE;
376 static int dev_close(struct sr_dev_inst *sdi)
378 sdi->status = SR_ST_INACTIVE;
383 static void clear_helper(void *priv)
385 struct dev_context *devc;
391 /* Analog generators. */
392 g_hash_table_iter_init(&iter, devc->ch_ag);
393 while (g_hash_table_iter_next(&iter, NULL, &value))
395 g_hash_table_unref(devc->ch_ag);
399 static int cleanup(const struct sr_dev_driver *di)
401 return std_dev_clear(di, clear_helper);
404 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
405 const struct sr_channel_group *cg)
407 struct dev_context *devc;
408 struct sr_channel *ch;
409 struct analog_gen *ag;
417 case SR_CONF_SAMPLERATE:
418 *data = g_variant_new_uint64(devc->cur_samplerate);
420 case SR_CONF_LIMIT_SAMPLES:
421 *data = g_variant_new_uint64(devc->limit_samples);
423 case SR_CONF_LIMIT_MSEC:
424 *data = g_variant_new_uint64(devc->limit_msec);
426 case SR_CONF_AVERAGING:
427 *data = g_variant_new_boolean(devc->avg);
429 case SR_CONF_AVG_SAMPLES:
430 *data = g_variant_new_uint64(devc->avg_samples);
432 case SR_CONF_PATTERN_MODE:
434 return SR_ERR_CHANNEL_GROUP;
435 /* Any channel in the group will do. */
436 ch = cg->channels->data;
437 if (ch->type == SR_CHANNEL_LOGIC) {
438 pattern = devc->logic_pattern;
439 *data = g_variant_new_string(logic_pattern_str[pattern]);
440 } else if (ch->type == SR_CHANNEL_ANALOG) {
441 ag = g_hash_table_lookup(devc->ch_ag, ch);
442 pattern = ag->pattern;
443 *data = g_variant_new_string(analog_pattern_str[pattern]);
447 case SR_CONF_AMPLITUDE:
449 return SR_ERR_CHANNEL_GROUP;
450 /* Any channel in the group will do. */
451 ch = cg->channels->data;
452 if (ch->type != SR_CHANNEL_ANALOG)
454 ag = g_hash_table_lookup(devc->ch_ag, ch);
455 *data = g_variant_new_double(ag->amplitude);
464 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
465 const struct sr_channel_group *cg)
467 struct dev_context *devc;
468 struct analog_gen *ag;
469 struct sr_channel *ch;
471 int logic_pattern, analog_pattern, ret;
477 if (sdi->status != SR_ST_ACTIVE)
478 return SR_ERR_DEV_CLOSED;
482 case SR_CONF_SAMPLERATE:
483 devc->cur_samplerate = g_variant_get_uint64(data);
485 case SR_CONF_LIMIT_SAMPLES:
486 devc->limit_msec = 0;
487 devc->limit_samples = g_variant_get_uint64(data);
489 case SR_CONF_LIMIT_MSEC:
490 devc->limit_msec = g_variant_get_uint64(data);
491 devc->limit_samples = 0;
493 case SR_CONF_AVERAGING:
494 devc->avg = g_variant_get_boolean(data);
495 sr_dbg("%s averaging", devc->avg ? "Enabling" : "Disabling");
497 case SR_CONF_AVG_SAMPLES:
498 devc->avg_samples = g_variant_get_uint64(data);
499 sr_dbg("Setting averaging rate to %" PRIu64, devc->avg_samples);
501 case SR_CONF_PATTERN_MODE:
503 return SR_ERR_CHANNEL_GROUP;
504 stropt = g_variant_get_string(data, NULL);
505 logic_pattern = analog_pattern = -1;
506 for (i = 0; i < ARRAY_SIZE(logic_pattern_str); i++) {
507 if (!strcmp(stropt, logic_pattern_str[i])) {
512 for (i = 0; i < ARRAY_SIZE(analog_pattern_str); i++) {
513 if (!strcmp(stropt, analog_pattern_str[i])) {
518 if (logic_pattern == -1 && analog_pattern == -1)
520 for (l = cg->channels; l; l = l->next) {
522 if (ch->type == SR_CHANNEL_LOGIC) {
523 if (logic_pattern == -1)
525 sr_dbg("Setting logic pattern to %s",
526 logic_pattern_str[logic_pattern]);
527 devc->logic_pattern = logic_pattern;
528 /* Might as well do this now, these are static. */
529 if (logic_pattern == PATTERN_ALL_LOW)
530 memset(devc->logic_data, 0x00, LOGIC_BUFSIZE);
531 else if (logic_pattern == PATTERN_ALL_HIGH)
532 memset(devc->logic_data, 0xff, LOGIC_BUFSIZE);
533 } else if (ch->type == SR_CHANNEL_ANALOG) {
534 if (analog_pattern == -1)
536 sr_dbg("Setting analog pattern for channel %s to %s",
537 ch->name, analog_pattern_str[analog_pattern]);
538 ag = g_hash_table_lookup(devc->ch_ag, ch);
539 ag->pattern = analog_pattern;
544 case SR_CONF_AMPLITUDE:
546 return SR_ERR_CHANNEL_GROUP;
547 for (l = cg->channels; l; l = l->next) {
549 if (ch->type != SR_CHANNEL_ANALOG)
551 ag = g_hash_table_lookup(devc->ch_ag, ch);
552 ag->amplitude = g_variant_get_double(data);
562 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
563 const struct sr_channel_group *cg)
565 struct sr_channel *ch;
569 if (key == SR_CONF_SCAN_OPTIONS) {
570 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
571 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
575 if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
576 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
577 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
586 case SR_CONF_DEVICE_OPTIONS:
587 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
588 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
590 case SR_CONF_SAMPLERATE:
591 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
592 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates,
593 ARRAY_SIZE(samplerates), sizeof(uint64_t));
594 g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar);
595 *data = g_variant_builder_end(&gvb);
601 ch = cg->channels->data;
603 case SR_CONF_DEVICE_OPTIONS:
604 if (ch->type == SR_CHANNEL_LOGIC)
605 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
606 devopts_cg_logic, ARRAY_SIZE(devopts_cg_logic),
608 else if (ch->type == SR_CHANNEL_ANALOG) {
609 if (strcmp(cg->name, "Analog") == 0)
610 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
611 devopts_cg_analog_group, ARRAY_SIZE(devopts_cg_analog_group),
614 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
615 devopts_cg_analog_channel, ARRAY_SIZE(devopts_cg_analog_channel),
621 case SR_CONF_PATTERN_MODE:
622 /* The analog group (with all 4 channels) shall not have a pattern property. */
623 if (strcmp(cg->name, "Analog") == 0)
626 if (ch->type == SR_CHANNEL_LOGIC)
627 *data = g_variant_new_strv(logic_pattern_str,
628 ARRAY_SIZE(logic_pattern_str));
629 else if (ch->type == SR_CHANNEL_ANALOG)
630 *data = g_variant_new_strv(analog_pattern_str,
631 ARRAY_SIZE(analog_pattern_str));
643 static void logic_generator(struct sr_dev_inst *sdi, uint64_t size)
645 struct dev_context *devc;
651 switch (devc->logic_pattern) {
653 memset(devc->logic_data, 0x00, size);
654 for (i = 0; i < size; i += devc->logic_unitsize) {
655 for (j = 0; j < devc->logic_unitsize; j++) {
656 pat = pattern_sigrok[(devc->step + j) % sizeof(pattern_sigrok)] >> 1;
657 devc->logic_data[i + j] = ~pat;
663 for (i = 0; i < size; i++)
664 devc->logic_data[i] = (uint8_t)(rand() & 0xff);
667 for (i = 0; i < size; i++) {
668 for (j = 0; j < devc->logic_unitsize; j++) {
669 devc->logic_data[i + j] = devc->step;
674 case PATTERN_ALL_LOW:
675 case PATTERN_ALL_HIGH:
676 /* These were set when the pattern mode was selected. */
679 sr_err("Unknown pattern: %d.", devc->logic_pattern);
684 static void send_analog_packet(struct analog_gen *ag,
685 struct sr_dev_inst *sdi,
686 uint64_t *analog_sent,
687 uint64_t analog_todo)
689 struct sr_datafeed_packet packet;
690 struct dev_context *devc;
691 uint64_t sending_now, to_avg;
696 packet.type = SR_DF_ANALOG;
697 packet.payload = &ag->packet;
700 ag_pattern_pos = devc->analog_counter % ag->num_samples;
701 sending_now = MIN(analog_todo, ag->num_samples-ag_pattern_pos);
702 ag->packet.data = ag->pattern_data + ag_pattern_pos;
703 ag->packet.num_samples = sending_now;
704 sr_session_send(sdi, &packet);
706 /* Whichever channel group gets there first. */
707 *analog_sent = MAX(*analog_sent, sending_now);
709 ag_pattern_pos = devc->analog_counter % ag->num_samples;
710 to_avg = MIN(analog_todo, ag->num_samples-ag_pattern_pos);
712 for (i = 0; i < to_avg; i++) {
713 ag->avg_val = (ag->avg_val +
715 ag_pattern_pos + i)) / 2;
717 /* Time to send averaged data? */
718 if (devc->avg_samples > 0 &&
719 ag->num_avgs >= devc->avg_samples)
723 if (devc->avg_samples == 0) {
724 /* We're averaging all the samples, so wait with
725 * sending until the very end.
727 *analog_sent = ag->num_avgs;
732 ag->packet.data = &ag->avg_val;
733 ag->packet.num_samples = 1;
735 sr_session_send(sdi, &packet);
736 *analog_sent = ag->num_avgs;
743 /* Callback handling data */
744 static int prepare_data(int fd, int revents, void *cb_data)
746 struct sr_dev_inst *sdi;
747 struct dev_context *devc;
748 struct sr_datafeed_packet packet;
749 struct sr_datafeed_logic logic;
750 struct analog_gen *ag;
753 uint64_t logic_todo, analog_todo, expected_samplenum, analog_sent, sending_now;
754 int64_t time, elapsed;
761 logic_todo = analog_todo = 0;
763 /* How many samples should we have sent by now? */
764 time = g_get_monotonic_time();
765 elapsed = time - devc->starttime;
766 expected_samplenum = elapsed * devc->cur_samplerate / (1000 * 1000);
768 /* But never more than the limit, if there is one. */
769 if (!devc->continuous)
770 expected_samplenum = MIN(expected_samplenum, devc->limit_samples);
772 /* Of those, how many do we still have to send? */
773 if (devc->num_logic_channels && (devc->logic_counter < devc->limit_samples))
774 logic_todo = expected_samplenum - devc->logic_counter;
775 if (devc->num_analog_channels && (devc->analog_counter < devc->limit_samples))
776 analog_todo = expected_samplenum - devc->analog_counter;
778 while (logic_todo || analog_todo) {
780 if (logic_todo > 0) {
781 sending_now = MIN(logic_todo, LOGIC_BUFSIZE / devc->logic_unitsize);
782 logic_generator(sdi, sending_now * devc->logic_unitsize);
783 packet.type = SR_DF_LOGIC;
784 packet.payload = &logic;
785 logic.length = sending_now * devc->logic_unitsize;
786 logic.unitsize = devc->logic_unitsize;
787 logic.data = devc->logic_data;
788 sr_session_send(sdi, &packet);
789 logic_todo -= sending_now;
790 devc->logic_counter += sending_now;
793 /* Analog, one channel at a time */
794 if (analog_todo > 0) {
797 g_hash_table_iter_init(&iter, devc->ch_ag);
798 while (g_hash_table_iter_next(&iter, NULL, &value)) {
799 send_analog_packet(value, sdi,
800 &analog_sent, analog_todo);
802 analog_todo -= analog_sent;
803 devc->analog_counter += analog_sent;
807 if (!devc->continuous
808 && (!devc->num_logic_channels || devc->logic_counter >= devc->limit_samples)
809 && (!devc->num_analog_channels || devc->analog_counter >= devc->limit_samples)) {
810 /* If we're averaging everything - now is the time to send data */
811 if (devc->avg_samples == 0) {
812 g_hash_table_iter_init(&iter, devc->ch_ag);
813 while (g_hash_table_iter_next(&iter, NULL, &value)) {
815 packet.type = SR_DF_ANALOG;
816 packet.payload = &ag->packet;
817 ag->packet.data = &ag->avg_val;
818 ag->packet.num_samples = 1;
819 sr_session_send(sdi, &packet);
823 sr_dbg("Requested number of samples reached.");
824 dev_acquisition_stop(sdi, cb_data);
831 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
833 struct dev_context *devc;
839 if (sdi->status != SR_ST_ACTIVE)
840 return SR_ERR_DEV_CLOSED;
843 devc->continuous = !devc->limit_samples;
844 devc->logic_counter = devc->analog_counter = 0;
846 g_hash_table_iter_init(&iter, devc->ch_ag);
847 while (g_hash_table_iter_next(&iter, NULL, &value))
848 generate_analog_pattern(value, devc->cur_samplerate);
850 sr_session_source_add(sdi->session, -1, 0, 40, prepare_data, (void *)sdi);
852 /* Send header packet to the session bus. */
853 std_session_send_df_header(sdi, LOG_PREFIX);
855 /* We use this timestamp to decide how many more samples to send. */
856 devc->starttime = g_get_monotonic_time();
861 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
863 struct sr_datafeed_packet packet;
867 sr_dbg("Stopping acquisition.");
869 sr_session_source_remove(sdi->session, -1);
871 /* Send last packet. */
872 packet.type = SR_DF_END;
873 sr_session_send(sdi, &packet);
878 SR_PRIV struct sr_dev_driver demo_driver_info = {
880 .longname = "Demo driver and pattern generator",
885 .dev_list = dev_list,
887 .config_get = config_get,
888 .config_set = config_set,
889 .config_list = config_list,
890 .dev_open = dev_open,
891 .dev_close = dev_close,
892 .dev_acquisition_start = dev_acquisition_start,
893 .dev_acquisition_stop = dev_acquisition_stop,