]> sigrok.org Git - libsigrok.git/commitdiff
demo: introduce graycode generator mode
authorGerhard Sittig <redacted>
Sun, 13 May 2018 17:45:46 +0000 (19:45 +0200)
committerGerhard Sittig <redacted>
Sun, 13 May 2018 17:45:46 +0000 (19:45 +0200)
Introduce support for the "graycode" logic pattern. Generate up to
64 bits of graycode output (all logic lines, no repetition, not limited
by the generator's internal pattern buffer). The implementation was
tested with 16 channels.

src/hardware/demo/api.c
src/hardware/demo/protocol.c
src/hardware/demo/protocol.h

index 7f2971db06c1a7992f12503eccb545790441b2b9..d0abe4ff92e053f111c6cb44b7cc9e66f9aa0f50 100644 (file)
@@ -44,6 +44,7 @@ static const char *logic_pattern_str[] = {
        "all-low",
        "all-high",
        "squid",
+       "graycode",
 };
 
 static const uint32_t scanopts[] = {
index 53ae247726ab9af2382a084af10977b029f17c7a..05f3f9d33fd3f62715d83e674b3f6f829d1d0de0 100644 (file)
@@ -245,6 +245,19 @@ SR_PRIV void demo_generate_analog_pattern(struct analog_gen *ag, uint64_t sample
        }
 }
 
+static uint64_t encode_number_to_gray(uint64_t nr)
+{
+       return nr ^ (nr >> 1);
+}
+
+static void set_logic_data(uint64_t bits, uint8_t *data, size_t len)
+{
+       while (len--) {
+               *data++ = bits & 0xff;
+               bits >>= 8;
+       }
+}
+
 static void logic_generator(struct sr_dev_inst *sdi, uint64_t size)
 {
        struct dev_context *devc;
@@ -253,6 +266,7 @@ static void logic_generator(struct sr_dev_inst *sdi, uint64_t size)
        uint8_t *sample;
        const uint8_t *image_col;
        size_t col_count, col_height;
+       uint64_t gray;
 
        devc = sdi->priv;
 
@@ -326,6 +340,15 @@ static void logic_generator(struct sr_dev_inst *sdi, uint64_t size)
                        devc->step %= col_count;
                }
                break;
+       case PATTERN_GRAYCODE:
+               for (i = 0; i < size; i += devc->logic_unitsize) {
+                       devc->step++;
+                       devc->step &= devc->all_logic_channels_mask;
+                       gray = encode_number_to_gray(devc->step);
+                       gray &= devc->all_logic_channels_mask;
+                       set_logic_data(gray, &devc->logic_data[i], devc->logic_unitsize);
+               }
+               break;
        default:
                sr_err("Unknown pattern: %d.", devc->logic_pattern);
                break;
index c35ea2d4c618db1a19da649f737e91c26d30e40e..285a127dd02f6fef7a1aea3bc4be98cb6c53e37a 100644 (file)
@@ -77,6 +77,9 @@ enum logic_pattern_type {
         * something that can get recognized.
         */
        PATTERN_SQUID,
+
+       /** Gray encoded data, like rotary encoder signals. */
+       PATTERN_GRAYCODE,
 };
 
 /* Analog patterns we can generate. */