From: Miklos Marton Date: Wed, 10 Jul 2019 09:02:17 +0000 (+0200) Subject: demo: Add random analog signal generation X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=4d8338bb96568e316b425cb2aa0d32bce9f7726c;p=libsigrok.git demo: Add random analog signal generation --- diff --git a/src/hardware/demo/api.c b/src/hardware/demo/api.c index 4d4b891c..fcd40157 100644 --- a/src/hardware/demo/api.c +++ b/src/hardware/demo/api.c @@ -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[] = { diff --git a/src/hardware/demo/protocol.c b/src/hardware/demo/protocol.c index 05be1d31..305ff17c 100644 --- a/src/hardware/demo/protocol.c +++ b/src/hardware/demo/protocol.c @@ -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? */ diff --git a/src/hardware/demo/protocol.h b/src/hardware/demo/protocol.h index 9877f553..4156f5ee 100644 --- a/src/hardware/demo/protocol.h +++ b/src/hardware/demo/protocol.h @@ -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 {