28 #define pipe(fds) _pipe(fds, 4096, _O_BINARY)
31 #include "sigrok-internal.h"
36 #define DEMONAME "Demo device"
79 static int hwcaps[] = {
96 static const char *pattern_strings[] = {
106 static const char *probe_names[
NUM_PROBES + 1] = {
118 static uint8_t pattern_sigrok[] = {
119 0x4c, 0x92, 0x92, 0x92, 0x64, 0x00, 0x00, 0x00,
120 0x82, 0xfe, 0xfe, 0x82, 0x00, 0x00, 0x00, 0x00,
121 0x7c, 0x82, 0x82, 0x92, 0x74, 0x00, 0x00, 0x00,
122 0xfe, 0x12, 0x12, 0x32, 0xcc, 0x00, 0x00, 0x00,
123 0x7c, 0x82, 0x82, 0x82, 0x7c, 0x00, 0x00, 0x00,
124 0xfe, 0x10, 0x28, 0x44, 0x82, 0x00, 0x00, 0x00,
125 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
126 0xbe, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
133 static GSList *dev_insts = NULL;
134 static uint64_t cur_samplerate =
SR_KHZ(200);
135 static uint64_t limit_samples = 0;
136 static uint64_t limit_msec = 0;
138 static GThread *my_thread;
139 static int thread_running;
141 static int hw_dev_acquisition_stop(
int dev_index,
void *cb_data);
143 static int hw_init(
const char *devinfo)
152 sr_err(
"demo: %s: sr_dev_inst_new failed", __func__);
156 dev_insts = g_slist_append(dev_insts, sdi);
161 static int hw_dev_open(
int dev_index)
171 static int hw_dev_close(
int dev_index)
181 static int hw_cleanup(
void)
187 static void *hw_dev_info_get(
int dev_index,
int dev_info_id)
193 sr_err(
"demo: %s: sdi was NULL", __func__);
197 switch (dev_info_id) {
211 info = &cur_samplerate;
214 info = &pattern_strings;
221 static int hw_dev_status_get(
int dev_index)
229 static int *hw_hwcap_get_all(
void)
234 static int hw_dev_config_set(
int dev_index,
int hwcap,
void *value)
246 cur_samplerate = *(uint64_t *)value;
247 sr_dbg(
"demo: %s: setting samplerate to %" PRIu64, __func__,
251 limit_samples = *(uint64_t *)value;
252 sr_dbg(
"demo: %s: setting limit_samples to %" PRIu64, __func__,
256 limit_msec = *(uint64_t *)value;
257 sr_dbg(
"demo: %s: setting limit_msec to %" PRIu64, __func__,
263 if (!strcmp(stropt,
"sigrok")) {
265 }
else if (!strcmp(stropt,
"random")) {
267 }
else if (!strcmp(stropt,
"incremental")) {
269 }
else if (!strcmp(stropt,
"all-low")) {
271 }
else if (!strcmp(stropt,
"all-high")) {
276 sr_dbg(
"demo: %s: setting pattern to %d", __func__,
285 static void samples_generator(uint8_t *buf, uint64_t size,
void *data)
287 static uint64_t p = 0;
292 memset(buf, 0, size);
296 for (i = 0; i < size; i++) {
297 *(buf + i) = ~(pattern_sigrok[p] >> 1);
303 for (i = 0; i < size; i++)
304 *(buf + i) = (uint8_t)(rand() & 0xff);
307 for (i = 0; i < size; i++)
311 memset(buf, 0x00, size);
314 memset(buf, 0xff, size);
317 sr_err(
"demo: %s: unknown pattern %d", __func__,
324 static void thread_func(
void *data)
328 uint64_t nb_to_send = 0;
330 double time_cur, time_last, time_diff;
332 time_last = g_timer_elapsed(ctx->
timer, NULL);
336 time_cur = g_timer_elapsed(ctx->
timer, NULL);
338 time_diff = time_cur - time_last;
339 time_last = time_cur;
344 nb_to_send = MIN(nb_to_send,
349 nb_to_send = MIN(nb_to_send,
BUFSIZE);
352 samples_generator(buf, nb_to_send, data);
355 g_io_channel_write_chars(
channels[1], (gchar *)&buf,
356 nb_to_send, (gsize *)&bytes_written, NULL);
372 static int receive_data(
int fd,
int revents,
void *cb_data)
376 static uint64_t samples_received = 0;
385 g_io_channel_read_chars(
channels[0],
386 (gchar *)&c,
BUFSIZE, &z, NULL);
390 packet.payload = &logic;
395 samples_received += z;
399 if (!thread_running && z <= 0) {
401 g_io_channel_shutdown(
channels[0], FALSE, NULL);
413 static int hw_dev_acquisition_start(
int dev_index,
void *cb_data)
420 if (!(ctx = g_try_malloc(
sizeof(
struct context)))) {
421 sr_err(
"demo: %s: ctx malloc failed", __func__);
432 sr_err(
"demo: %s: pipe() failed", __func__);
440 g_io_channel_set_encoding(
channels[0], NULL, NULL);
441 g_io_channel_set_encoding(
channels[1], NULL, NULL);
444 g_io_channel_set_buffered(
channels[0], FALSE);
445 g_io_channel_set_buffered(
channels[1], FALSE);
453 ctx->
timer = g_timer_new();
456 g_thread_create((GThreadFunc)thread_func, ctx, TRUE, NULL);
458 sr_err(
"demo: %s: g_thread_create failed", __func__);
463 sr_err(
"demo: %s: packet malloc failed", __func__);
468 sr_err(
"demo: %s: header malloc failed", __func__);
486 static int hw_dev_acquisition_stop(
int dev_index,
void *cb_data)
500 .longname =
"Demo driver and pattern generator",
503 .cleanup = hw_cleanup,
504 .dev_open = hw_dev_open,
505 .dev_close = hw_dev_close,
506 .dev_info_get = hw_dev_info_get,
507 .dev_status_get = hw_dev_status_get,
508 .hwcap_get_all = hw_hwcap_get_all,
509 .dev_config_set = hw_dev_config_set,
510 .dev_acquisition_start = hw_dev_acquisition_start,
511 .dev_acquisition_stop = hw_dev_acquisition_stop,