]> sigrok.org Git - libsigrok.git/blob - src/hardware/demo/protocol.h
demo: Properly handle low samplerates
[libsigrok.git] / src / hardware / demo / protocol.h
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  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #ifndef LIBSIGROK_HARDWARE_DEMO_PROTOCOL_H
24 #define LIBSIGROK_HARDWARE_DEMO_PROTOCOL_H
25
26 #include <stdint.h>
27 #include <libsigrok/libsigrok.h>
28 #include "libsigrok-internal.h"
29
30 #define LOG_PREFIX "demo"
31
32 /* The size in bytes of chunks to send through the session bus. */
33 #define LOGIC_BUFSIZE                   4096
34 /* Size of the analog pattern space per channel. */
35 #define ANALOG_BUFSIZE                  4096
36 /* This is a development feature: it starts a new frame every n samples. */
37 #define SAMPLES_PER_FRAME               0
38
39 struct dev_context {
40         uint64_t cur_samplerate;
41         uint64_t limit_samples;
42         uint64_t limit_msec;
43         uint64_t sent_samples;
44         uint64_t sent_frame_samples; /* Number of samples that were sent for current frame. */
45         int64_t start_us;
46         int64_t spent_us;
47         uint64_t step;
48         /* Logic */
49         int32_t num_logic_channels;
50         unsigned int logic_unitsize;
51         /* There is only ever one logic channel group, so its pattern goes here. */
52         uint8_t logic_pattern;
53         unsigned char logic_data[LOGIC_BUFSIZE];
54         /* Analog */
55         int32_t num_analog_channels;
56         GHashTable *ch_ag;
57         gboolean avg; /* True if averaging is enabled */
58         uint64_t avg_samples;
59         size_t enabled_logic_channels;
60         size_t enabled_analog_channels;
61         size_t first_partial_logic_index;
62         uint8_t first_partial_logic_mask;
63 };
64
65 /* Logic patterns we can generate. */
66 enum {
67         /**
68          * Spells "sigrok" across 8 channels using '0's (with '1's as
69          * "background") when displayed using the 'bits' output format.
70          * The pattern is repeated every 8 channels, shifted to the right
71          * in time by one bit.
72          */
73         PATTERN_SIGROK,
74
75         /** Pseudo-random values on all channels. */
76         PATTERN_RANDOM,
77
78         /**
79          * Incrementing number across 8 channels. The pattern is repeated
80          * every 8 channels, shifted to the right in time by one bit.
81          */
82         PATTERN_INC,
83
84         /**
85          * Single bit "walking" across all logic channels by being
86          * shifted across data lines, restarting after the last line
87          * was used. An all-zero (all-one) state is inserted to prevent
88          * repetitive patterns (e.g. with 8 data lines, every 8th state
89          * would show the same line state)
90          */
91         PATTERN_WALKING_ONE,
92         PATTERN_WALKING_ZERO,
93
94         /** All channels have a low logic state. */
95         PATTERN_ALL_LOW,
96
97         /** All channels have a high logic state. */
98         PATTERN_ALL_HIGH,
99
100         /**
101          * Mimics a cable squid. Derived from the "works with" logo
102          * to occupy a larger number of channels yet "painting"
103          * something that can get recognized.
104          */
105         PATTERN_SQUID,
106 };
107
108 /* Analog patterns we can generate. */
109 enum {
110         PATTERN_SQUARE,
111         PATTERN_SINE,
112         PATTERN_TRIANGLE,
113         PATTERN_SAWTOOTH,
114 };
115
116 static const char *analog_pattern_str[] = {
117         "square",
118         "sine",
119         "triangle",
120         "sawtooth",
121 };
122
123 struct analog_gen {
124         struct sr_channel *ch;
125         int pattern;
126         float amplitude;
127         float pattern_data[ANALOG_BUFSIZE];
128         unsigned int num_samples;
129         struct sr_datafeed_analog packet;
130         struct sr_analog_encoding encoding;
131         struct sr_analog_meaning meaning;
132         struct sr_analog_spec spec;
133         float avg_val; /* Average value */
134         unsigned num_avgs; /* Number of samples averaged */
135 };
136
137 SR_PRIV void demo_generate_analog_pattern(struct analog_gen *ag, uint64_t sample_rate);
138 SR_PRIV int demo_prepare_data(int fd, int revents, void *cb_data);
139
140 #endif