1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5  * Copyright (C) 2011 Olivier Fauchon <olivier@aixmarseille.com>
6  * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
7  * Copyright (C) 2015 Bartosz Golaszewski <bgolaszewski@baylibre.com>
8  * Copyright (C) 2019 Frank Stettner <frank-stettner@gmx.net>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef LIBSIGROK_HARDWARE_DEMO_PROTOCOL_H
25 #define LIBSIGROK_HARDWARE_DEMO_PROTOCOL_H
26 
27 #include <stdint.h>
28 #include <libsigrok/libsigrok.h>
29 #include "libsigrok-internal.h"
30 
31 #define LOG_PREFIX "demo"
32 
33 /* The size in bytes of chunks to send through the session bus. */
34 #define LOGIC_BUFSIZE			4096
35 /* Size of the analog pattern space per channel. */
36 #define ANALOG_BUFSIZE			4096
37 /* This is a development feature: it starts a new frame every n samples. */
38 #define SAMPLES_PER_FRAME		1000UL
39 #define DEFAULT_LIMIT_FRAMES		0
40 
41 #define DEFAULT_ANALOG_ENCODING_DIGITS	4
42 #define DEFAULT_ANALOG_SPEC_DIGITS		4
43 #define DEFAULT_ANALOG_AMPLITUDE		10
44 #define DEFAULT_ANALOG_OFFSET			0.
45 
46 /* Logic patterns we can generate. */
47 enum logic_pattern_type {
48 	/**
49 	 * Spells "sigrok" across 8 channels using '0's (with '1's as
50 	 * "background") when displayed using the 'bits' output format.
51 	 * The pattern is repeated every 8 channels, shifted to the right
52 	 * in time by one bit.
53 	 */
54 	PATTERN_SIGROK,
55 
56 	/** Pseudo-random values on all channels. */
57 	PATTERN_RANDOM,
58 
59 	/**
60 	 * Incrementing number across 8 channels. The pattern is repeated
61 	 * every 8 channels, shifted to the right in time by one bit.
62 	 */
63 	PATTERN_INC,
64 
65 	/**
66 	 * Single bit "walking" across all logic channels by being
67 	 * shifted across data lines, restarting after the last line
68 	 * was used. An all-zero (all-one) state is inserted to prevent
69 	 * repetitive patterns (e.g. with 8 data lines, every 8th state
70 	 * would show the same line state)
71 	 */
72 	PATTERN_WALKING_ONE,
73 	PATTERN_WALKING_ZERO,
74 
75 	/** All channels have a low logic state. */
76 	PATTERN_ALL_LOW,
77 
78 	/** All channels have a high logic state. */
79 	PATTERN_ALL_HIGH,
80 
81 	/**
82 	 * Mimics a cable squid. Derived from the "works with" logo
83 	 * to occupy a larger number of channels yet "painting"
84 	 * something that can get recognized.
85 	 */
86 	PATTERN_SQUID,
87 
88 	/** Gray encoded data, like rotary encoder signals. */
89 	PATTERN_GRAYCODE,
90 };
91 
92 /* Analog patterns we can generate. */
93 enum analog_pattern_type {
94 	PATTERN_SQUARE,
95 	PATTERN_SINE,
96 	PATTERN_TRIANGLE,
97 	PATTERN_SAWTOOTH,
98 	PATTERN_ANALOG_RANDOM,
99 };
100 
101 static const char *analog_pattern_str[] = {
102 	"square",
103 	"sine",
104 	"triangle",
105 	"sawtooth",
106 	"random",
107 };
108 
109 struct analog_pattern {
110 	float data[ANALOG_BUFSIZE];
111 	unsigned int num_samples;
112 };
113 
114 struct dev_context {
115 	uint64_t cur_samplerate;
116 	uint64_t limit_samples;
117 	uint64_t limit_msec;
118 	uint64_t limit_frames;
119 	uint64_t sent_samples;
120 	uint64_t sent_frame_samples; /* Number of samples that were sent for current frame. */
121 	int64_t start_us;
122 	int64_t spent_us;
123 	uint64_t step;
124 	/* Logic */
125 	int32_t num_logic_channels;
126 	size_t logic_unitsize;
127 	uint64_t all_logic_channels_mask;
128 	/* There is only ever one logic channel group, so its pattern goes here. */
129 	enum logic_pattern_type logic_pattern;
130 	uint8_t logic_data[LOGIC_BUFSIZE];
131 	/* Analog */
132 	struct analog_pattern *analog_patterns[ARRAY_SIZE(analog_pattern_str)];
133 	int32_t num_analog_channels;
134 	GHashTable *ch_ag;
135 	gboolean avg; /* True if averaging is enabled */
136 	uint64_t avg_samples;
137 	size_t enabled_logic_channels;
138 	size_t enabled_analog_channels;
139 	size_t first_partial_logic_index;
140 	uint8_t first_partial_logic_mask;
141 	/* Triggers */
142 	uint64_t capture_ratio;
143 	gboolean trigger_fired;
144 	struct soft_trigger_logic *stl;
145 };
146 
147 struct analog_gen {
148 	struct sr_channel *ch;
149 	enum sr_mq mq;
150 	enum sr_mqflag mq_flags;
151 	enum sr_unit unit;
152 	enum analog_pattern_type pattern;
153 	float amplitude;
154 	float offset;
155 	struct sr_datafeed_analog packet;
156 	struct sr_analog_encoding encoding;
157 	struct sr_analog_meaning meaning;
158 	struct sr_analog_spec spec;
159 	float avg_val; /* Average value */
160 	unsigned int num_avgs; /* Number of samples averaged */
161 };
162 
163 SR_PRIV void demo_generate_analog_pattern(struct dev_context *devc);
164 SR_PRIV void demo_free_analog_pattern(struct dev_context *devc);
165 SR_PRIV int demo_prepare_data(int fd, int revents, void *cb_data);
166 
167 #endif
168