]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/demo/protocol.c
demo: Minor cosmetics.
[libsigrok.git] / src / hardware / demo / protocol.c
index 4358d290ce1250a4d3c80b5385f7fa41515f54c9..05be1d31682a309b1514957bacfee4941877e522 100644 (file)
@@ -5,6 +5,7 @@
  * Copyright (C) 2011 Olivier Fauchon <olivier@aixmarseille.com>
  * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
  * Copyright (C) 2015 Bartosz Golaszewski <bgolaszewski@baylibre.com>
+ * Copyright (C) 2019 Frank Stettner <frank-stettner@gmx.net>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -172,77 +173,77 @@ static const uint8_t pattern_squid[128][128 / 8] = {
        { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
 };
 
-SR_PRIV void demo_generate_analog_pattern(struct analog_gen *ag, uint64_t sample_rate)
+SR_PRIV void demo_generate_analog_pattern(struct dev_context *devc)
 {
        double t, frequency;
-       float value;
+       float amplitude, offset;
+       struct analog_pattern *pattern;
        unsigned int num_samples, i;
+       float value;
        int last_end;
 
-       sr_dbg("Generating %s pattern.", analog_pattern_str[ag->pattern]);
-
        num_samples = ANALOG_BUFSIZE / sizeof(float);
+       frequency = (double) devc->cur_samplerate / ANALOG_SAMPLES_PER_PERIOD;
+       amplitude = DEFAULT_ANALOG_AMPLITUDE;
+       offset = DEFAULT_ANALOG_OFFSET;
+
+       /*
+        * FIXME: We actually need only one period. A ringbuffer would be
+        * useful here.
+        * Make sure the number of samples we put out is an integer
+        * multiple of our period size.
+        */
 
-       switch (ag->pattern) {
-       case PATTERN_SQUARE:
-               value = ag->amplitude;
-               last_end = 0;
-               for (i = 0; i < num_samples; i++) {
-                       if (i % 5 == 0)
-                               value = -value;
-                       if (i % 10 == 0)
-                               last_end = i;
-                       ag->pattern_data[i] = value;
-               }
-               ag->num_samples = last_end;
-               break;
-       case PATTERN_SINE:
-               frequency = (double) sample_rate / ANALOG_SAMPLES_PER_PERIOD;
-
-               /* Make sure the number of samples we put out is an integer
-                * multiple of our period size */
-               /* FIXME we actually need only one period. A ringbuffer would be
-                * useful here. */
-               while (num_samples % ANALOG_SAMPLES_PER_PERIOD != 0)
-                       num_samples--;
-
-               for (i = 0; i < num_samples; i++) {
-                       t = (double) i / (double) sample_rate;
-                       ag->pattern_data[i] = ag->amplitude *
-                                               sin(2 * G_PI * frequency * t);
-               }
-
-               ag->num_samples = num_samples;
-               break;
-       case PATTERN_TRIANGLE:
-               frequency = (double) sample_rate / ANALOG_SAMPLES_PER_PERIOD;
-
-               while (num_samples % ANALOG_SAMPLES_PER_PERIOD != 0)
-                       num_samples--;
-
-               for (i = 0; i < num_samples; i++) {
-                       t = (double) i / (double) sample_rate;
-                       ag->pattern_data[i] = (2 * ag->amplitude / G_PI) *
-                                               asin(sin(2 * G_PI * frequency * t));
-               }
-
-               ag->num_samples = num_samples;
-               break;
-       case PATTERN_SAWTOOTH:
-               frequency = (double) sample_rate / ANALOG_SAMPLES_PER_PERIOD;
-
-               while (num_samples % ANALOG_SAMPLES_PER_PERIOD != 0)
-                       num_samples--;
-
-               for (i = 0; i < num_samples; i++) {
-                       t = (double) i / (double) sample_rate;
-                       ag->pattern_data[i] = 2 * ag->amplitude *
-                                               ((t * frequency) - floor(0.5f + t * frequency));
-               }
-
-               ag->num_samples = num_samples;
-               break;
+       /* PATTERN_SQUARE: */
+       sr_dbg("Generating %s pattern.", analog_pattern_str[PATTERN_SQUARE]);
+       pattern = g_malloc(sizeof(struct analog_pattern));
+       value = amplitude;
+       last_end = 0;
+       for (i = 0; i < num_samples; i++) {
+               if (i % 5 == 0)
+                       value = -value;
+               if (i % 10 == 0)
+                       last_end = i;
+               pattern->data[i] = value + offset;
+       }
+       pattern->num_samples = last_end;
+       devc->analog_patterns[PATTERN_SQUARE] = pattern;
+
+       /* Readjusting num_samples for all other patterns. */
+       while (num_samples % ANALOG_SAMPLES_PER_PERIOD != 0)
+               num_samples--;
+
+       /* PATTERN_SINE: */
+       sr_dbg("Generating %s pattern.", analog_pattern_str[PATTERN_SINE]);
+       pattern = g_malloc(sizeof(struct analog_pattern));
+       for (i = 0; i < num_samples; i++) {
+               t = (double) i / (double) devc->cur_samplerate;
+               pattern->data[i] = sin(2 * G_PI * frequency * t) * amplitude + offset;
        }
+       pattern->num_samples = last_end;
+       devc->analog_patterns[PATTERN_SINE] = pattern;
+
+       /* PATTERN_TRIANGLE: */
+       sr_dbg("Generating %s pattern.", analog_pattern_str[PATTERN_TRIANGLE]);
+       pattern = g_malloc(sizeof(struct analog_pattern));
+       for (i = 0; i < num_samples; i++) {
+               t = (double) i / (double) devc->cur_samplerate;
+               pattern->data[i] = (2 / G_PI) * asin(sin(2 * G_PI * frequency * t)) *
+                       amplitude + offset;
+       }
+       pattern->num_samples = last_end;
+       devc->analog_patterns[PATTERN_TRIANGLE] = pattern;
+
+       /* PATTERN_SAWTOOTH: */
+       sr_dbg("Generating %s pattern.", analog_pattern_str[PATTERN_SAWTOOTH]);
+       pattern = g_malloc(sizeof(struct analog_pattern));
+       for (i = 0; i < num_samples; i++) {
+               t = (double) i / (double) devc->cur_samplerate;
+               pattern->data[i] = 2 * ((t * frequency) - floor(0.5f + t * frequency)) *
+                       amplitude + offset;
+       }
+       pattern->num_samples = last_end;
+       devc->analog_patterns[PATTERN_SAWTOOTH] = pattern;
 }
 
 static uint64_t encode_number_to_gray(uint64_t nr)
@@ -294,7 +295,7 @@ static void logic_generator(struct sr_dev_inst *sdi, uint64_t size)
                break;
        case PATTERN_WALKING_ONE:
                /* j contains the value of the highest bit */
-               j = 1 << (devc->num_logic_channels - 1);
+               j = 1 << (devc->num_logic_channels - 1);
                for (i = 0; i < size; i++) {
                        devc->logic_data[i] = devc->step;
                        if (devc->step == 0)
@@ -309,7 +310,7 @@ static void logic_generator(struct sr_dev_inst *sdi, uint64_t size)
        case PATTERN_WALKING_ZERO:
                /* Same as walking one, only with inverted output */
                /* j contains the value of the highest bit */
-               j = 1 << (devc->num_logic_channels - 1);
+               j = 1 << (devc->num_logic_channels - 1);
                for (i = 0; i < size; i++) {
                        devc->logic_data[i] = ~devc->step;
                        if (devc->step == 0)
@@ -389,9 +390,12 @@ static void send_analog_packet(struct analog_gen *ag,
 {
        struct sr_datafeed_packet packet;
        struct dev_context *devc;
+       struct analog_pattern *pattern;
        uint64_t sending_now, to_avg;
        int ag_pattern_pos;
        unsigned int i;
+       float amplitude, offset, value;
+       float *data;
 
        if (!ag->ch || !ag->ch->enabled)
                return;
@@ -400,32 +404,121 @@ static void send_analog_packet(struct analog_gen *ag,
        packet.type = SR_DF_ANALOG;
        packet.payload = &ag->packet;
 
+       pattern = devc->analog_patterns[ag->pattern];
+
+       ag->packet.meaning->channels = g_slist_append(NULL, ag->ch);
+       ag->packet.meaning->mq = ag->mq;
+       ag->packet.meaning->mqflags = ag->mq_flags;
+
+       /* Set a unit for the given quantity. */
+       if (ag->mq == SR_MQ_VOLTAGE)
+               ag->packet.meaning->unit = SR_UNIT_VOLT;
+       else if (ag->mq == SR_MQ_CURRENT)
+               ag->packet.meaning->unit = SR_UNIT_AMPERE;
+       else if (ag->mq == SR_MQ_RESISTANCE)
+               ag->packet.meaning->unit = SR_UNIT_OHM;
+       else if (ag->mq == SR_MQ_CAPACITANCE)
+               ag->packet.meaning->unit = SR_UNIT_FARAD;
+       else if (ag->mq == SR_MQ_TEMPERATURE)
+               ag->packet.meaning->unit = SR_UNIT_CELSIUS;
+       else if (ag->mq == SR_MQ_FREQUENCY)
+               ag->packet.meaning->unit = SR_UNIT_HERTZ;
+       else if (ag->mq == SR_MQ_DUTY_CYCLE)
+               ag->packet.meaning->unit = SR_UNIT_PERCENTAGE;
+       else if (ag->mq == SR_MQ_CONTINUITY)
+               ag->packet.meaning->unit = SR_UNIT_OHM;
+       else if (ag->mq == SR_MQ_PULSE_WIDTH)
+               ag->packet.meaning->unit = SR_UNIT_PERCENTAGE;
+       else if (ag->mq == SR_MQ_CONDUCTANCE)
+               ag->packet.meaning->unit = SR_UNIT_SIEMENS;
+       else if (ag->mq == SR_MQ_POWER)
+               ag->packet.meaning->unit = SR_UNIT_WATT;
+       else if (ag->mq == SR_MQ_GAIN)
+               ag->packet.meaning->unit = SR_UNIT_UNITLESS;
+       else if (ag->mq == SR_MQ_SOUND_PRESSURE_LEVEL)
+               ag->packet.meaning->unit = SR_UNIT_DECIBEL_SPL;
+       else if (ag->mq == SR_MQ_CARBON_MONOXIDE)
+               ag->packet.meaning->unit = SR_UNIT_CONCENTRATION;
+       else if (ag->mq == SR_MQ_RELATIVE_HUMIDITY)
+               ag->packet.meaning->unit = SR_UNIT_HUMIDITY_293K;
+       else if (ag->mq == SR_MQ_TIME)
+               ag->packet.meaning->unit = SR_UNIT_SECOND;
+       else if (ag->mq == SR_MQ_WIND_SPEED)
+               ag->packet.meaning->unit = SR_UNIT_METER_SECOND;
+       else if (ag->mq == SR_MQ_PRESSURE)
+               ag->packet.meaning->unit = SR_UNIT_HECTOPASCAL;
+       else if (ag->mq == SR_MQ_PARALLEL_INDUCTANCE)
+               ag->packet.meaning->unit = SR_UNIT_HENRY;
+       else if (ag->mq == SR_MQ_PARALLEL_CAPACITANCE)
+               ag->packet.meaning->unit = SR_UNIT_FARAD;
+       else if (ag->mq == SR_MQ_PARALLEL_RESISTANCE)
+               ag->packet.meaning->unit = SR_UNIT_OHM;
+       else if (ag->mq == SR_MQ_SERIES_INDUCTANCE)
+               ag->packet.meaning->unit = SR_UNIT_HENRY;
+       else if (ag->mq == SR_MQ_SERIES_CAPACITANCE)
+               ag->packet.meaning->unit = SR_UNIT_FARAD;
+       else if (ag->mq == SR_MQ_SERIES_RESISTANCE)
+               ag->packet.meaning->unit = SR_UNIT_OHM;
+       else if (ag->mq == SR_MQ_DISSIPATION_FACTOR)
+               ag->packet.meaning->unit = SR_UNIT_UNITLESS;
+       else if (ag->mq == SR_MQ_QUALITY_FACTOR)
+               ag->packet.meaning->unit = SR_UNIT_UNITLESS;
+       else if (ag->mq == SR_MQ_PHASE_ANGLE)
+               ag->packet.meaning->unit = SR_UNIT_DEGREE;
+       else if (ag->mq == SR_MQ_DIFFERENCE)
+               ag->packet.meaning->unit = SR_UNIT_UNITLESS;
+       else if (ag->mq == SR_MQ_COUNT)
+               ag->packet.meaning->unit = SR_UNIT_PIECE;
+       else if (ag->mq == SR_MQ_POWER_FACTOR)
+               ag->packet.meaning->unit = SR_UNIT_UNITLESS;
+       else if (ag->mq == SR_MQ_APPARENT_POWER)
+               ag->packet.meaning->unit = SR_UNIT_VOLT_AMPERE;
+       else if (ag->mq == SR_MQ_MASS)
+               ag->packet.meaning->unit = SR_UNIT_GRAM;
+       else if (ag->mq == SR_MQ_HARMONIC_RATIO)
+               ag->packet.meaning->unit = SR_UNIT_UNITLESS;
+       else
+               ag->packet.meaning->unit = SR_UNIT_UNITLESS;
+
        if (!devc->avg) {
-               ag_pattern_pos = analog_pos % ag->num_samples;
-               sending_now = MIN(analog_todo, ag->num_samples - ag_pattern_pos);
-               ag->packet.data = ag->pattern_data + ag_pattern_pos;
+               ag_pattern_pos = analog_pos % pattern->num_samples;
+               sending_now = MIN(analog_todo, pattern->num_samples - ag_pattern_pos);
+               if (ag->amplitude != DEFAULT_ANALOG_AMPLITUDE ||
+                       ag->offset != DEFAULT_ANALOG_OFFSET) {
+
+                       /* Amplitude or offset changed, modify each sample. */
+                       amplitude = ag->amplitude / DEFAULT_ANALOG_AMPLITUDE;
+                       offset = ag->offset - DEFAULT_ANALOG_OFFSET;
+                       data = ag->packet.data;
+                       for (i = 0; i < sending_now; i++)
+                               data[i] = pattern->data[ag_pattern_pos + i] * amplitude + offset;
+               } else {
+                       /* Amplitude and offset unchanged, use the fast way. */
+                       ag->packet.data = pattern->data + ag_pattern_pos;
+               }
                ag->packet.num_samples = sending_now;
                sr_session_send(sdi, &packet);
 
                /* Whichever channel group gets there first. */
                *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);
+               ag_pattern_pos = analog_pos % pattern->num_samples;
+               to_avg = MIN(analog_todo, pattern->num_samples - ag_pattern_pos);
+               amplitude = ag->amplitude / DEFAULT_ANALOG_AMPLITUDE;
+               offset = ag->offset - DEFAULT_ANALOG_OFFSET;
 
                for (i = 0; i < to_avg; i++) {
-                       ag->avg_val = (ag->avg_val +
-                                       *(ag->pattern_data +
-                                         ag_pattern_pos + i)) / 2;
+                       value = *(pattern->data + ag_pattern_pos + i) * amplitude + offset;
+                       ag->avg_val = (ag->avg_val + value) / 2;
                        ag->num_avgs++;
                        /* Time to send averaged data? */
-                       if (devc->avg_samples > 0 &&
-                           ag->num_avgs >= devc->avg_samples)
+                       if ((devc->avg_samples > 0) && (ag->num_avgs >= devc->avg_samples))
                                goto do_send;
                }
 
                if (devc->avg_samples == 0) {
-                       /* We're averaging all the samples, so wait with
+                       /*
+                        * We're averaging all the samples, so wait with
                         * sending until the very end.
                         */
                        *analog_sent = ag->num_avgs;
@@ -456,6 +549,8 @@ SR_PRIV int demo_prepare_data(int fd, int revents, void *cb_data)
        void *value;
        uint64_t samples_todo, logic_done, analog_done, analog_sent, sending_now;
        int64_t elapsed_us, limit_us, todo_us;
+       int64_t trigger_offset;
+       int pre_trigger_samples;
 
        (void)fd;
        (void)revents;
@@ -510,6 +605,7 @@ SR_PRIV int demo_prepare_data(int fd, int revents, void *cb_data)
        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;
@@ -520,14 +616,47 @@ SR_PRIV int demo_prepare_data(int fd, int revents, void *cb_data)
                        sending_now = MIN(samples_todo - logic_done,
                                        LOGIC_BUFSIZE / devc->logic_unitsize);
                        logic_generator(sdi, sending_now * devc->logic_unitsize);
+                       /* Check for trigger and send pre-trigger data if needed */
+                       if (devc->stl && (!devc->trigger_fired)) {
+                               trigger_offset = soft_trigger_logic_check(devc->stl,
+                                               devc->logic_data, sending_now * devc->logic_unitsize,
+                                               &pre_trigger_samples);
+                               if (trigger_offset > -1) {
+                                       devc->trigger_fired = TRUE;
+                                       logic_done = pre_trigger_samples;
+                               }
+                       } else
+                               trigger_offset = 0;
+
+                       /* Send logic samples if needed */
                        packet.type = SR_DF_LOGIC;
                        packet.payload = &logic;
-                       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;
+
+                       if (devc->stl) {
+                               if (devc->trigger_fired && (trigger_offset < (int)sending_now)) {
+                                       /* Send after-trigger data */
+                                       logic.length = (sending_now - trigger_offset) * devc->logic_unitsize;
+                                       logic.data = devc->logic_data + trigger_offset * devc->logic_unitsize;
+                                       logic_fixup_feed(devc, &logic);
+                                       sr_session_send(sdi, &packet);
+                                       logic_done += sending_now - trigger_offset;
+                                       /* End acquisition */
+                                       sr_dbg("Triggered, stopping acquisition.");
+                                       sr_dev_acquisition_stop(sdi);
+                                       break;
+                               } else {
+                                       /* Send nothing */
+                                       logic_done += sending_now;
+                               }
+                       } else if (!devc->stl) {
+                               /* No trigger defined, send logic samples */
+                               logic.length = sending_now * devc->logic_unitsize;
+                               logic.data = devc->logic_data;
+                               logic_fixup_feed(devc, &logic);
+                               sr_session_send(sdi, &packet);
+                               logic_done += sending_now;
+                       }
                }
 
                /* Analog, one channel at a time */
@@ -543,15 +672,10 @@ SR_PRIV int demo_prepare_data(int fd, int revents, void *cb_data)
                        analog_done += analog_sent;
                }
        }
-       /* At this point, both logic_done and analog_done should be
-        * exactly equal to samples_todo, or else.
-        */
-       if (logic_done != samples_todo || analog_done != samples_todo) {
-               sr_err("BUG: Sample count mismatch.");
-               return G_SOURCE_REMOVE;
-       }
-       devc->sent_samples += samples_todo;
-       devc->sent_frame_samples += samples_todo;
+
+       uint64_t min = MIN(logic_done, analog_done);
+       devc->sent_samples += min;
+       devc->sent_frame_samples += min;
        devc->spent_us += todo_us;
 
        if (devc->limit_frames && devc->sent_frame_samples >= SAMPLES_PER_FRAME) {