]> sigrok.org Git - libsigrok.git/commitdiff
demo: Add random analog signal generation
authorMiklos Marton <redacted>
Wed, 10 Jul 2019 09:02:17 +0000 (11:02 +0200)
committerUwe Hermann <redacted>
Wed, 31 Jul 2019 21:15:18 +0000 (23:15 +0200)
src/hardware/demo/api.c
src/hardware/demo/protocol.c
src/hardware/demo/protocol.h

index 4d4b891c651a1886daf01e74757703d7fd05976b..fcd40157ea5a2ecd5aa57e547fa2175cf203fc63 100644 (file)
@@ -32,7 +32,7 @@
 #define DEFAULT_NUM_LOGIC_CHANNELS             8
 #define DEFAULT_LOGIC_PATTERN                  PATTERN_SIGROK
 
-#define DEFAULT_NUM_ANALOG_CHANNELS            4
+#define DEFAULT_NUM_ANALOG_CHANNELS            5
 
 /* Note: No spaces allowed because of sigrok-cli. */
 static const char *logic_pattern_str[] = {
index 05be1d31682a309b1514957bacfee4941877e522..305ff17cd85d26430d926459133a063b3f32bfd8 100644 (file)
@@ -244,6 +244,12 @@ SR_PRIV void demo_generate_analog_pattern(struct dev_context *devc)
        }
        pattern->num_samples = last_end;
        devc->analog_patterns[PATTERN_SAWTOOTH] = pattern;
+
+       /* PATTERN_ANALOG_RANDOM */
+       /* Data not filled here, will be generated in send_analog_packet(). */
+       pattern = g_malloc(sizeof(struct analog_pattern));
+       pattern->num_samples = last_end;
+       devc->analog_patterns[PATTERN_ANALOG_RANDOM] = pattern;
 }
 
 static uint64_t encode_number_to_gray(uint64_t nr)
@@ -484,14 +490,26 @@ static void send_analog_packet(struct analog_gen *ag,
                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;
+                       ag->offset != DEFAULT_ANALOG_OFFSET ||
+                       ag->pattern == PATTERN_ANALOG_RANDOM) {
+                       /*
+                        * Amplitude or offset changed (or we are generating
+                        * random data), modify each sample.
+                        */
+                       if (ag->pattern == PATTERN_ANALOG_RANDOM) {
+                               amplitude = ag->amplitude / 500.0;
+                               offset = ag->offset - DEFAULT_ANALOG_OFFSET - ag->amplitude;
+                       } else {
+                               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;
+                       for (i = 0; i < sending_now; i++) {
+                               if (ag->pattern == PATTERN_ANALOG_RANDOM)
+                                       data[i] = (rand() % 1000) * amplitude + offset;
+                               else
+                                       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;
@@ -504,11 +522,19 @@ static void send_analog_packet(struct analog_gen *ag,
        } else {
                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;
+               if (ag->pattern == PATTERN_ANALOG_RANDOM) {
+                       amplitude = ag->amplitude / 500.0;
+                       offset = ag->offset - DEFAULT_ANALOG_OFFSET - ag->amplitude;
+               } else {
+                       amplitude = ag->amplitude / DEFAULT_ANALOG_AMPLITUDE;
+                       offset = ag->offset - DEFAULT_ANALOG_OFFSET;
+               }
 
                for (i = 0; i < to_avg; i++) {
-                       value = *(pattern->data + ag_pattern_pos + i) * amplitude + offset;
+                       if (ag->pattern == PATTERN_ANALOG_RANDOM)
+                               value = (rand() % 1000) * amplitude + offset;
+                       else
+                               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? */
index 9877f55367b2a84f16374bc52a64deba5c8cfee2..4156f5ee539847d6c3113195061b89cd081f1e2c 100644 (file)
@@ -95,6 +95,7 @@ enum analog_pattern_type {
        PATTERN_SINE,
        PATTERN_TRIANGLE,
        PATTERN_SAWTOOTH,
+       PATTERN_ANALOG_RANDOM,
 };
 
 static const char *analog_pattern_str[] = {
@@ -102,6 +103,7 @@ static const char *analog_pattern_str[] = {
        "sine",
        "triangle",
        "sawtooth",
+       "random",
 };
 
 struct analog_pattern {