]> sigrok.org Git - libsigrok.git/blobdiff - hardware/demo/demo.c
demo: Require sample limit.
[libsigrok.git] / hardware / demo / demo.c
index cf82630adfca221cd73be987e85a611f3c2da439..3d59ae5f7d71357401539a23c5d74ec283d4e8f0 100644 (file)
@@ -111,7 +111,8 @@ struct dev_context {
        uint64_t cur_samplerate;
        uint64_t limit_samples;
        uint64_t limit_msec;
-       uint64_t samples_counter;
+       uint64_t logic_counter;
+       uint64_t analog_counter;
        int64_t starttime;
        uint64_t step;
        /* Logic */
@@ -192,7 +193,7 @@ static void generate_analog_pattern(const struct sr_probe_group *probe_group, ui
 
        switch (ag->pattern) {
        case PATTERN_SQUARE:
-               value = 5.0;
+               value = ANALOG_AMPLITUDE;
                last_end = 0;
                for (i = 0; i < num_samples; i++) {
                        if (i % 5 == 0)
@@ -265,7 +266,7 @@ static GSList *scan(GSList *options)
        struct sr_config *src;
        struct analog_gen *ag;
        GSList *devices, *l;
-       int num_logic_probes, num_analog_probes, i;
+       int num_logic_probes, num_analog_probes, pattern, i;
        char probe_name[16];
 
        drvc = di->priv;
@@ -322,9 +323,12 @@ static GSList *scan(GSList *options)
        sdi->probe_groups = g_slist_append(NULL, pg);
 
        /* Analog probes, probe groups and pattern generators. */
+
+       pattern = 0;
        for (i = 0; i < num_analog_probes; i++) {
                sprintf(probe_name, "A%d", i);
-               if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, probe_name)))
+               if (!(probe = sr_probe_new(i + num_logic_probes,
+                               SR_PROBE_ANALOG, TRUE, probe_name)))
                        return NULL;
                sdi->probes = g_slist_append(sdi->probes, probe);
 
@@ -342,11 +346,14 @@ static GSList *scan(GSList *options)
                ag->packet.mqflags = 0;
                ag->packet.unit = SR_UNIT_VOLT;
                ag->packet.data = ag->pattern_data;
-               ag->pattern = PATTERN_SINE;
+               ag->pattern = pattern;
                pg->priv = ag;
 
                sdi->probe_groups = g_slist_append(sdi->probe_groups, pg);
                devc->analog_probe_groups = g_slist_append(devc->analog_probe_groups, pg);
+
+               if (++pattern == ARRAY_SIZE(analog_pattern_str))
+                       pattern = 0;
        }
 
        sdi->priv = devc;
@@ -618,7 +625,7 @@ static int prepare_data(int fd, int revents, void *cb_data)
        struct sr_probe_group *pg;
        struct analog_gen *ag;
        GSList *l;
-       uint64_t samples_to_send, expected_samplenum, analog_samples, sending_now;
+       uint64_t logic_todo, analog_todo, expected_samplenum, analog_samples, sending_now;
        int64_t time, elapsed;
 
        (void)fd;
@@ -631,20 +638,15 @@ static int prepare_data(int fd, int revents, void *cb_data)
        time = g_get_monotonic_time();
        elapsed = time - devc->starttime;
        expected_samplenum = elapsed * devc->cur_samplerate / 1000000;
-       /* Of those, how many do we still have to send? */
-       samples_to_send = expected_samplenum - devc->samples_counter;
-
-       if (devc->limit_samples) {
-               samples_to_send = MIN(samples_to_send,
-                               devc->limit_samples - devc->samples_counter);
-       }
 
-       while (samples_to_send > 0) {
-               sending_now = 0;
+       /* Of those, how many do we still have to send? */
+       logic_todo = MIN(expected_samplenum, devc->limit_samples) - devc->logic_counter;
+       analog_todo = MIN(expected_samplenum, devc->limit_samples) - devc->analog_counter;
 
+       while (logic_todo || analog_todo) {
                /* Logic */
-               if (devc->num_logic_probes > 0) {
-                       sending_now = MIN(samples_to_send,
+               if (devc->num_logic_probes > 0 && logic_todo > 0) {
+                       sending_now = MIN(logic_todo,
                                        LOGIC_BUFSIZE / devc->logic_unitsize);
                        logic_generator(sdi, sending_now * devc->logic_unitsize);
                        packet.type = SR_DF_LOGIC;
@@ -653,10 +655,12 @@ static int prepare_data(int fd, int revents, void *cb_data)
                        logic.unitsize = devc->logic_unitsize;
                        logic.data = devc->logic_data;
                        sr_session_send(sdi, &packet);
+                       logic_todo -= sending_now;
+                       devc->logic_counter += sending_now;
                }
 
                /* Analog, one probe at a time */
-               if (devc->num_analog_probes > 0) {
+               if (devc->num_analog_probes > 0 && analog_todo > 0) {
                        sending_now = 0;
                        for (l = devc->analog_probe_groups; l; l = l->next) {
                                pg = l->data;
@@ -669,21 +673,20 @@ static int prepare_data(int fd, int revents, void *cb_data)
                                 * beginning of our buffer. A ring buffer would
                                 * help here as well */
 
-                               analog_samples = MIN(samples_to_send, ag->num_samples);
+                               analog_samples = MIN(analog_todo, ag->num_samples);
                                /* Whichever probe group gets there first. */
                                sending_now = MAX(sending_now, analog_samples);
                                ag->packet.num_samples = analog_samples;
                                sr_session_send(sdi, &packet);
                        }
+                       analog_todo -= sending_now;
+                       devc->analog_counter += sending_now;
                }
-
-               samples_to_send -= sending_now;
-               devc->samples_counter += sending_now;
        }
 
-       if (devc->limit_samples &&
-               devc->samples_counter >= devc->limit_samples) {
-               sr_info("Requested number of samples reached.");
+       if (devc->logic_counter >= devc->limit_samples &&
+                       devc->analog_counter >= devc->limit_samples) {
+               sr_dbg("Requested number of samples reached.");
                dev_acquisition_stop(sdi, cb_data);
                return TRUE;
        }
@@ -699,9 +702,10 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
-       /* TODO: don't start without a sample limit set */
        devc = sdi->priv;
-       devc->samples_counter = 0;
+       if (devc->limit_samples == 0)
+               return SR_ERR;
+       devc->logic_counter = devc->analog_counter = 0;
 
        /*
         * Setting two channels connected by a pipe is a remnant from when the