]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/demo/protocol.c
Various log message cleanups.
[libsigrok.git] / src / hardware / demo / protocol.c
index 7f5950b6b87188af6fa495eba402f69e8f37d8b7..8b89d3548f8ce0fd3fb88ccb25c38733fc6deb1d 100644 (file)
@@ -333,6 +333,34 @@ static void logic_generator(struct sr_dev_inst *sdi, uint64_t size)
        }
 }
 
+/*
+ * Fixup a memory image of generated logic data before it gets sent to
+ * the session's datafeed. Mask out content from disabled channels.
+ *
+ * TODO: Need we apply a channel map, and enforce a dense representation
+ * of the enabled channels' data?
+ */
+static void logic_fixup_feed(struct dev_context *devc,
+               struct sr_datafeed_logic *logic)
+{
+       size_t fp_off;
+       uint8_t fp_mask;
+       size_t off, idx;
+       uint8_t *sample;
+
+       fp_off = devc->first_partial_logic_index;
+       fp_mask = devc->first_partial_logic_mask;
+       if (fp_off == logic->unitsize)
+               return;
+
+       for (off = 0; off < logic->length; off += logic->unitsize) {
+               sample = logic->data + off;
+               sample[fp_off] &= fp_mask;
+               for (idx = fp_off + 1; idx < logic->unitsize; idx++)
+                       sample[idx] = 0x00;
+       }
+}
+
 static void send_analog_packet(struct analog_gen *ag,
                struct sr_dev_inst *sdi, uint64_t *analog_sent,
                uint64_t analog_pos, uint64_t analog_todo)
@@ -343,13 +371,16 @@ static void send_analog_packet(struct analog_gen *ag,
        int ag_pattern_pos;
        unsigned int i;
 
+       if (!ag->ch || !ag->ch->enabled)
+               return;
+
        devc = sdi->priv;
        packet.type = SR_DF_ANALOG;
        packet.payload = &ag->packet;
 
        if (!devc->avg) {
                ag_pattern_pos = analog_pos % ag->num_samples;
-               sending_now = MIN(analog_todo, ag->num_samples-ag_pattern_pos);
+               sending_now = MIN(analog_todo, ag->num_samples - ag_pattern_pos);
                ag->packet.data = ag->pattern_data + ag_pattern_pos;
                ag->packet.num_samples = sending_now;
                sr_session_send(sdi, &packet);
@@ -358,7 +389,7 @@ static void send_analog_packet(struct analog_gen *ag,
                *analog_sent = MAX(*analog_sent, sending_now);
        } else {
                ag_pattern_pos = analog_pos % ag->num_samples;
-               to_avg = MIN(analog_todo, ag->num_samples-ag_pattern_pos);
+               to_avg = MIN(analog_todo, ag->num_samples - ag_pattern_pos);
 
                for (i = 0; i < to_avg; i++) {
                        ag->avg_val = (ag->avg_val +
@@ -414,7 +445,7 @@ SR_PRIV int demo_prepare_data(int fd, int revents, void *cb_data)
        if (devc->cur_samplerate <= 0
                        || (devc->num_logic_channels <= 0
                        && devc->num_analog_channels <= 0)) {
-               sdi->driver->dev_acquisition_stop(sdi);
+               sr_dev_acquisition_stop(sdi);
                return G_SOURCE_CONTINUE;
        }
 
@@ -442,7 +473,11 @@ SR_PRIV int demo_prepare_data(int fd, int revents, void *cb_data)
        todo_us = samples_todo * G_USEC_PER_SEC / devc->cur_samplerate;
 
        logic_done  = devc->num_logic_channels  > 0 ? 0 : samples_todo;
+       if (!devc->enabled_logic_channels)
+               logic_done = samples_todo;
        analog_done = devc->num_analog_channels > 0 ? 0 : samples_todo;
+       if (!devc->enabled_analog_channels)
+               analog_done = samples_todo;
 
        while (logic_done < samples_todo || analog_done < samples_todo) {
                /* Logic */
@@ -455,6 +490,7 @@ SR_PRIV int demo_prepare_data(int fd, int revents, void *cb_data)
                        logic.length = sending_now * devc->logic_unitsize;
                        logic.unitsize = devc->logic_unitsize;
                        logic.data = devc->logic_data;
+                       logic_fixup_feed(devc, &logic);
                        sr_session_send(sdi, &packet);
                        logic_done += sending_now;
                }
@@ -486,7 +522,7 @@ SR_PRIV int demo_prepare_data(int fd, int revents, void *cb_data)
                        || (limit_us > 0 && devc->spent_us >= limit_us)) {
 
                /* If we're averaging everything - now is the time to send data */
-               if (devc->avg_samples == 0) {
+               if (devc->avg && devc->avg_samples == 0) {
                        g_hash_table_iter_init(&iter, devc->ch_ag);
                        while (g_hash_table_iter_next(&iter, NULL, &value)) {
                                ag = value;
@@ -498,7 +534,7 @@ SR_PRIV int demo_prepare_data(int fd, int revents, void *cb_data)
                        }
                }
                sr_dbg("Requested number of samples reached.");
-               sdi->driver->dev_acquisition_stop(sdi);
+               sr_dev_acquisition_stop(sdi);
        }
 
        return G_SOURCE_CONTINUE;