demo: Add random analog signal generation

This commit is contained in:
Miklos Marton 2019-07-10 11:02:17 +02:00 committed by Uwe Hermann
parent b1b8a7d079
commit 4d8338bb96
3 changed files with 39 additions and 11 deletions

View File

@ -32,7 +32,7 @@
#define DEFAULT_NUM_LOGIC_CHANNELS 8 #define DEFAULT_NUM_LOGIC_CHANNELS 8
#define DEFAULT_LOGIC_PATTERN PATTERN_SIGROK #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. */ /* Note: No spaces allowed because of sigrok-cli. */
static const char *logic_pattern_str[] = { static const char *logic_pattern_str[] = {

View File

@ -244,6 +244,12 @@ SR_PRIV void demo_generate_analog_pattern(struct dev_context *devc)
} }
pattern->num_samples = last_end; pattern->num_samples = last_end;
devc->analog_patterns[PATTERN_SAWTOOTH] = pattern; 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) 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; ag_pattern_pos = analog_pos % pattern->num_samples;
sending_now = MIN(analog_todo, pattern->num_samples - ag_pattern_pos); sending_now = MIN(analog_todo, pattern->num_samples - ag_pattern_pos);
if (ag->amplitude != DEFAULT_ANALOG_AMPLITUDE || if (ag->amplitude != DEFAULT_ANALOG_AMPLITUDE ||
ag->offset != DEFAULT_ANALOG_OFFSET) { ag->offset != DEFAULT_ANALOG_OFFSET ||
ag->pattern == PATTERN_ANALOG_RANDOM) {
/* Amplitude or offset changed, modify each sample. */ /*
amplitude = ag->amplitude / DEFAULT_ANALOG_AMPLITUDE; * Amplitude or offset changed (or we are generating
offset = ag->offset - DEFAULT_ANALOG_OFFSET; * 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; data = ag->packet.data;
for (i = 0; i < sending_now; i++) for (i = 0; i < sending_now; i++) {
data[i] = pattern->data[ag_pattern_pos + i] * amplitude + offset; if (ag->pattern == PATTERN_ANALOG_RANDOM)
data[i] = (rand() % 1000) * amplitude + offset;
else
data[i] = pattern->data[ag_pattern_pos + i] * amplitude + offset;
}
} else { } else {
/* Amplitude and offset unchanged, use the fast way. */ /* Amplitude and offset unchanged, use the fast way. */
ag->packet.data = pattern->data + ag_pattern_pos; ag->packet.data = pattern->data + ag_pattern_pos;
@ -504,11 +522,19 @@ static void send_analog_packet(struct analog_gen *ag,
} else { } else {
ag_pattern_pos = analog_pos % pattern->num_samples; ag_pattern_pos = analog_pos % pattern->num_samples;
to_avg = MIN(analog_todo, pattern->num_samples - ag_pattern_pos); to_avg = MIN(analog_todo, pattern->num_samples - ag_pattern_pos);
amplitude = ag->amplitude / DEFAULT_ANALOG_AMPLITUDE; if (ag->pattern == PATTERN_ANALOG_RANDOM) {
offset = ag->offset - DEFAULT_ANALOG_OFFSET; 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++) { 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->avg_val = (ag->avg_val + value) / 2;
ag->num_avgs++; ag->num_avgs++;
/* Time to send averaged data? */ /* Time to send averaged data? */

View File

@ -95,6 +95,7 @@ enum analog_pattern_type {
PATTERN_SINE, PATTERN_SINE,
PATTERN_TRIANGLE, PATTERN_TRIANGLE,
PATTERN_SAWTOOTH, PATTERN_SAWTOOTH,
PATTERN_ANALOG_RANDOM,
}; };
static const char *analog_pattern_str[] = { static const char *analog_pattern_str[] = {
@ -102,6 +103,7 @@ static const char *analog_pattern_str[] = {
"sine", "sine",
"triangle", "triangle",
"sawtooth", "sawtooth",
"random",
}; };
struct analog_pattern { struct analog_pattern {