]> sigrok.org Git - libsigrok.git/blob - src/hardware/demo/protocol.c
ce897ad903bc0790223bc5e82182b8f41cd6695b
[libsigrok.git] / src / hardware / demo / protocol.c
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 #include <config.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <math.h>
27 #include <libsigrok/libsigrok.h>
28 #include "libsigrok-internal.h"
29 #include "protocol.h"
30
31 #define ANALOG_SAMPLES_PER_PERIOD 20
32
33 static const uint8_t pattern_sigrok[] = {
34         0x4c, 0x92, 0x92, 0x92, 0x64, 0x00, 0x00, 0x00,
35         0x82, 0xfe, 0xfe, 0x82, 0x00, 0x00, 0x00, 0x00,
36         0x7c, 0x82, 0x82, 0x92, 0x74, 0x00, 0x00, 0x00,
37         0xfe, 0x12, 0x12, 0x32, 0xcc, 0x00, 0x00, 0x00,
38         0x7c, 0x82, 0x82, 0x82, 0x7c, 0x00, 0x00, 0x00,
39         0xfe, 0x10, 0x28, 0x44, 0x82, 0x00, 0x00, 0x00,
40         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41         0xbe, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
42 };
43
44 static const uint8_t pattern_squid[128][128 / 8] = {
45         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
46         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
47         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
48         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
49         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
50         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
51         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
52         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
53         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
54         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
55         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
56         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0xe0, 0x00, },
57         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xe1, 0x01, },
58         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xe1, 0x01, },
59         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xe3, 0x03, },
60         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xe3, 0x03, },
61         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc3, 0x03, },
62         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcf, 0xc7, 0x03, },
63         { 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0xc7, 0x03, },
64         { 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x87, 0x03, },
65         { 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0xc7, 0x03, },
66         { 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0xcf, 0x03, },
67         { 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xcf, 0x03, },
68         { 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0x03, },
69         { 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0xff, 0x03, },
70         { 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0xfe, 0x01, },
71         { 0x00, 0x00, 0x00, 0x80, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0xfe, 0x01, },
72         { 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0xfc, 0x00, },
73         { 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
74         { 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
75         { 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
76         { 0x00, 0xc0, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
77         { 0x00, 0x80, 0x01, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0xfe, 0xff, 0x03, },
78         { 0x00, 0x00, 0x07, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0xfe, 0xff, 0x03, },
79         { 0x00, 0x00, 0x1c, 0x00, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0xfe, 0xff, 0x03, },
80         { 0x00, 0x00, 0x78, 0x00, 0xc0, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0xfe, 0xff, 0x03, },
81         { 0x00, 0x00, 0xe0, 0x00, 0x80, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0xfe, 0xff, 0x03, },
82         { 0x00, 0x00, 0xc0, 0x03, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
83         { 0x00, 0x00, 0x00, 0x07, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
84         { 0x00, 0x00, 0x00, 0x1c, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
85         { 0x00, 0x00, 0x00, 0xf8, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x03, 0x00, },
86         { 0x00, 0x00, 0x00, 0xf0, 0x01, 0x38, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x01, 0x00, 0xf0, 0x1f, 0x1c, },
87         { 0x00, 0x00, 0x00, 0xe0, 0x07, 0x70, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x0f, 0x00, 0xfc, 0x3f, 0x3c, },
88         { 0x80, 0x03, 0x00, 0xc0, 0x0f, 0xe0, 0x00, 0x00, 0x80, 0xff, 0xff, 0x3f, 0x00, 0xfc, 0x7f, 0x7c, },
89         { 0x00, 0x1e, 0x00, 0x00, 0x1f, 0xc0, 0x01, 0x00, 0xc0, 0xff, 0xff, 0x7f, 0x00, 0xfe, 0xff, 0x7c, },
90         { 0x00, 0xf0, 0x01, 0x00, 0x7c, 0x80, 0x03, 0x00, 0xc0, 0xff, 0xff, 0xff, 0x00, 0xfe, 0xff, 0x7c, },
91         { 0x00, 0xc0, 0x0f, 0x00, 0xf0, 0x00, 0x07, 0x00, 0xc0, 0xff, 0xff, 0xff, 0x00, 0x3f, 0xf8, 0x78, },
92         { 0x00, 0x00, 0x3e, 0x00, 0xc0, 0x03, 0x0e, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x00, 0x0f, 0xf0, 0xf0, },
93         { 0x00, 0x00, 0xf0, 0x07, 0x80, 0x07, 0x3c, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x00, 0x0f, 0xf0, 0xf0, },
94         { 0x00, 0x00, 0x80, 0x3f, 0x00, 0x1e, 0x78, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x00, 0x0f, 0xe0, 0xf0, },
95         { 0x00, 0x00, 0x00, 0xff, 0x00, 0x7c, 0xe0, 0x01, 0xe0, 0xff, 0xff, 0xff, 0x00, 0x0f, 0xe0, 0xf0, },
96         { 0x00, 0x00, 0x00, 0xfc, 0x03, 0xf0, 0xc1, 0x07, 0xf0, 0xff, 0xff, 0xff, 0x00, 0x0f, 0xf0, 0xf0, },
97         { 0x00, 0x00, 0x00, 0xe0, 0x1f, 0xc0, 0x03, 0x1f, 0xe0, 0xff, 0xff, 0xff, 0x00, 0x0f, 0xf0, 0x78, },
98         { 0x00, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x1f, 0xfc, 0xe0, 0xff, 0xff, 0xff, 0x01, 0xff, 0xff, 0x7f, },
99         { 0xf8, 0x03, 0x00, 0x00, 0xf0, 0x07, 0xfe, 0xf0, 0xff, 0xff, 0xff, 0xff, 0x01, 0xff, 0xff, 0x7f, },
100         { 0x80, 0xff, 0x01, 0x00, 0x80, 0x3f, 0xf0, 0x8f, 0xff, 0xff, 0xff, 0xff, 0x00, 0xfe, 0xff, 0x3f, },
101         { 0x00, 0xf0, 0xff, 0x07, 0x00, 0xfc, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xfe, 0xff, 0x3f, },
102         { 0x00, 0x00, 0xfc, 0x7f, 0x00, 0xe0, 0x7f, 0xfc, 0xff, 0xff, 0xff, 0xff, 0x00, 0xfe, 0xff, 0x1f, },
103         { 0x00, 0x00, 0xc0, 0xff, 0x1f, 0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xfc, 0xff, 0x03, },
104         { 0x00, 0x00, 0x00, 0xf8, 0xff, 0x07, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, },
105         { 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0x0f, 0xe0, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, },
106         { 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, },
107         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, },
108         { 0x00, 0x00, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0xff, 0xff, 0x03, },
109         { 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0xff, 0xff, 0x03, },
110         { 0x00, 0x10, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0x03, },
111         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0x03, },
112         { 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0x03, },
113         { 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0x0f, 0xe0, 0xff, 0xff, 0xff, 0xff, 0x00, 0x7c, 0x00, 0x00, },
114         { 0x00, 0x00, 0x00, 0xf8, 0xff, 0x07, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x3c, 0x00, 0x00, },
115         { 0x00, 0x00, 0xc0, 0xff, 0x1f, 0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x1e, 0x00, 0x00, },
116         { 0x00, 0x00, 0xf8, 0x7f, 0x00, 0xe0, 0x7f, 0xfc, 0xff, 0xff, 0xff, 0xff, 0x00, 0x1e, 0x00, 0x00, },
117         { 0x00, 0xf0, 0xff, 0x07, 0x00, 0xfc, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x0f, 0x00, 0x00, },
118         { 0x80, 0xff, 0x01, 0x00, 0x80, 0x3f, 0xf0, 0x8f, 0xff, 0xff, 0xff, 0xff, 0x01, 0x0f, 0x00, 0x00, },
119         { 0xf8, 0x03, 0x00, 0x00, 0xf0, 0x07, 0xfe, 0xf0, 0xff, 0xff, 0xff, 0xff, 0x00, 0x0f, 0x00, 0x00, },
120         { 0x00, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x1f, 0xfc, 0xe0, 0xff, 0xff, 0xff, 0x00, 0x0f, 0x00, 0x00, },
121         { 0x00, 0x00, 0x00, 0xe0, 0x1f, 0xc0, 0x07, 0x1f, 0xf0, 0xff, 0xff, 0xff, 0x00, 0x06, 0x00, 0x00, },
122         { 0x00, 0x00, 0x00, 0xfc, 0x03, 0xf0, 0xc1, 0x07, 0xf0, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, },
123         { 0x00, 0x00, 0x00, 0xff, 0x00, 0x7c, 0xe0, 0x01, 0xf0, 0xff, 0xff, 0xff, 0x01, 0xe0, 0x1f, 0x00, },
124         { 0x00, 0x00, 0x80, 0x3f, 0x00, 0x1e, 0x78, 0x00, 0xf0, 0xff, 0xff, 0xff, 0x01, 0xf0, 0x7f, 0x00, },
125         { 0x00, 0x00, 0xf0, 0x0f, 0x80, 0x07, 0x3c, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x01, 0xfc, 0xff, 0x00, },
126         { 0x00, 0x00, 0x3e, 0x00, 0xc0, 0x03, 0x0e, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x01, 0xfc, 0xff, 0x01, },
127         { 0x00, 0xc0, 0x0f, 0x00, 0xf0, 0x00, 0x07, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x01, 0xfe, 0xff, 0x01, },
128         { 0x00, 0xf0, 0x01, 0x00, 0x7c, 0x80, 0x03, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0x03, },
129         { 0x00, 0x3e, 0x00, 0x00, 0x1f, 0xc0, 0x01, 0x00, 0x80, 0xff, 0xff, 0xff, 0x00, 0x1f, 0xe0, 0x03, },
130         { 0x80, 0x03, 0x00, 0xc0, 0x0f, 0xe0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x0f, 0xc0, 0x03, },
131         { 0x00, 0x00, 0x00, 0xe0, 0x07, 0x70, 0x00, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x00, 0x0f, 0xc0, 0x03, },
132         { 0x00, 0x00, 0x00, 0xf0, 0x01, 0x38, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x01, 0x00, 0x0f, 0x80, 0x03, },
133         { 0x00, 0x00, 0x00, 0xf8, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xc0, 0x03, },
134         { 0x00, 0x00, 0x00, 0x1c, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xc0, 0x03, },
135         { 0x00, 0x00, 0x00, 0x07, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xe0, 0x03, },
136         { 0x00, 0x00, 0xc0, 0x03, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xf8, 0x03, },
137         { 0x00, 0x00, 0xe0, 0x00, 0x80, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x01, },
138         { 0x00, 0x00, 0x78, 0x00, 0xc0, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x01, },
139         { 0x00, 0x00, 0x1c, 0x00, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff, 0x00, },
140         { 0x00, 0x00, 0x06, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x7f, 0x00, },
141         { 0x00, 0x80, 0x03, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x1f, 0x00, },
142         { 0x00, 0xc0, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
143         { 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
144         { 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
145         { 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x01, },
146         { 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x03, },
147         { 0x00, 0x00, 0x00, 0x80, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x03, },
148         { 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x03, },
149         { 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x03, },
150         { 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x03, },
151         { 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x07, 0x00, },
152         { 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x07, 0x00, },
153         { 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x0f, 0x00, },
154         { 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x1f, 0x00, },
155         { 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0x00, },
156         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff, 0x00, },
157         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x01, },
158         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xfc, 0x03, },
159         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xf8, 0x03, },
160         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xf0, 0x03, },
161         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x03, },
162         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, },
163         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
164         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
165         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
166         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
167         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
168         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
169         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
170         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
171         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
172         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
173 };
174
175 SR_PRIV void demo_generate_analog_pattern(struct analog_gen *ag, uint64_t sample_rate)
176 {
177         double t, frequency;
178         float value;
179         unsigned int num_samples, i;
180         int last_end;
181
182         sr_dbg("Generating %s pattern.", analog_pattern_str[ag->pattern]);
183
184         num_samples = ANALOG_BUFSIZE / sizeof(float);
185
186         switch (ag->pattern) {
187         case PATTERN_SQUARE:
188                 value = ag->amplitude;
189                 last_end = 0;
190                 for (i = 0; i < num_samples; i++) {
191                         if (i % 5 == 0)
192                                 value = -value;
193                         if (i % 10 == 0)
194                                 last_end = i;
195                         ag->pattern_data[i] = value;
196                 }
197                 ag->num_samples = last_end;
198                 break;
199         case PATTERN_SINE:
200                 frequency = (double) sample_rate / ANALOG_SAMPLES_PER_PERIOD;
201
202                 /* Make sure the number of samples we put out is an integer
203                  * multiple of our period size */
204                 /* FIXME we actually need only one period. A ringbuffer would be
205                  * useful here. */
206                 while (num_samples % ANALOG_SAMPLES_PER_PERIOD != 0)
207                         num_samples--;
208
209                 for (i = 0; i < num_samples; i++) {
210                         t = (double) i / (double) sample_rate;
211                         ag->pattern_data[i] = ag->amplitude *
212                                                 sin(2 * G_PI * frequency * t);
213                 }
214
215                 ag->num_samples = num_samples;
216                 break;
217         case PATTERN_TRIANGLE:
218                 frequency = (double) sample_rate / ANALOG_SAMPLES_PER_PERIOD;
219
220                 while (num_samples % ANALOG_SAMPLES_PER_PERIOD != 0)
221                         num_samples--;
222
223                 for (i = 0; i < num_samples; i++) {
224                         t = (double) i / (double) sample_rate;
225                         ag->pattern_data[i] = (2 * ag->amplitude / G_PI) *
226                                                 asin(sin(2 * G_PI * frequency * t));
227                 }
228
229                 ag->num_samples = num_samples;
230                 break;
231         case PATTERN_SAWTOOTH:
232                 frequency = (double) sample_rate / ANALOG_SAMPLES_PER_PERIOD;
233
234                 while (num_samples % ANALOG_SAMPLES_PER_PERIOD != 0)
235                         num_samples--;
236
237                 for (i = 0; i < num_samples; i++) {
238                         t = (double) i / (double) sample_rate;
239                         ag->pattern_data[i] = 2 * ag->amplitude *
240                                                 ((t * frequency) - floor(0.5f + t * frequency));
241                 }
242
243                 ag->num_samples = num_samples;
244                 break;
245         }
246 }
247
248 static uint64_t encode_number_to_gray(uint64_t nr)
249 {
250         return nr ^ (nr >> 1);
251 }
252
253 static void set_logic_data(uint64_t bits, uint8_t *data, size_t len)
254 {
255         while (len--) {
256                 *data++ = bits & 0xff;
257                 bits >>= 8;
258         }
259 }
260
261 static void logic_generator(struct sr_dev_inst *sdi, uint64_t size)
262 {
263         struct dev_context *devc;
264         uint64_t i, j;
265         uint8_t pat;
266         uint8_t *sample;
267         const uint8_t *image_col;
268         size_t col_count, col_height;
269         uint64_t gray;
270
271         devc = sdi->priv;
272
273         switch (devc->logic_pattern) {
274         case PATTERN_SIGROK:
275                 memset(devc->logic_data, 0x00, size);
276                 for (i = 0; i < size; i += devc->logic_unitsize) {
277                         for (j = 0; j < devc->logic_unitsize; j++) {
278                                 pat = pattern_sigrok[(devc->step + j) % sizeof(pattern_sigrok)] >> 1;
279                                 devc->logic_data[i + j] = ~pat;
280                         }
281                         devc->step++;
282                 }
283                 break;
284         case PATTERN_RANDOM:
285                 for (i = 0; i < size; i++)
286                         devc->logic_data[i] = (uint8_t)(rand() & 0xff);
287                 break;
288         case PATTERN_INC:
289                 for (i = 0; i < size; i++) {
290                         for (j = 0; j < devc->logic_unitsize; j++)
291                                 devc->logic_data[i + j] = devc->step;
292                         devc->step++;
293                 }
294                 break;
295         case PATTERN_WALKING_ONE:
296                 /* j contains the value of the highest bit */
297                 j = 1 << (devc->num_logic_channels - 1);
298                 for (i = 0; i < size; i++) {
299                         devc->logic_data[i] = devc->step;
300                         if (devc->step == 0)
301                                 devc->step = 1;
302                         else
303                                 if (devc->step == j)
304                                         devc->step = 0;
305                                 else
306                                         devc->step <<= 1;
307                 }
308                 break;
309         case PATTERN_WALKING_ZERO:
310                 /* Same as walking one, only with inverted output */
311                 /* j contains the value of the highest bit */
312                 j = 1 << (devc->num_logic_channels - 1);
313                 for (i = 0; i < size; i++) {
314                         devc->logic_data[i] = ~devc->step;
315                         if (devc->step == 0)
316                                 devc->step = 1;
317                         else
318                                 if (devc->step == j)
319                                         devc->step = 0;
320                                 else
321                                         devc->step <<= 1;
322                 }
323                 break;
324         case PATTERN_ALL_LOW:
325         case PATTERN_ALL_HIGH:
326                 /* These were set when the pattern mode was selected. */
327                 break;
328         case PATTERN_SQUID:
329                 memset(devc->logic_data, 0x00, size);
330                 col_count = ARRAY_SIZE(pattern_squid);
331                 col_height = ARRAY_SIZE(pattern_squid[0]);
332                 for (i = 0; i < size; i += devc->logic_unitsize) {
333                         sample = &devc->logic_data[i];
334                         image_col = pattern_squid[devc->step];
335                         for (j = 0; j < devc->logic_unitsize; j++) {
336                                 pat = image_col[j % col_height];
337                                 sample[j] = pat;
338                         }
339                         devc->step++;
340                         devc->step %= col_count;
341                 }
342                 break;
343         case PATTERN_GRAYCODE:
344                 for (i = 0; i < size; i += devc->logic_unitsize) {
345                         devc->step++;
346                         devc->step &= devc->all_logic_channels_mask;
347                         gray = encode_number_to_gray(devc->step);
348                         gray &= devc->all_logic_channels_mask;
349                         set_logic_data(gray, &devc->logic_data[i], devc->logic_unitsize);
350                 }
351                 break;
352         default:
353                 sr_err("Unknown pattern: %d.", devc->logic_pattern);
354                 break;
355         }
356 }
357
358 /*
359  * Fixup a memory image of generated logic data before it gets sent to
360  * the session's datafeed. Mask out content from disabled channels.
361  *
362  * TODO: Need we apply a channel map, and enforce a dense representation
363  * of the enabled channels' data?
364  */
365 static void logic_fixup_feed(struct dev_context *devc,
366                 struct sr_datafeed_logic *logic)
367 {
368         size_t fp_off;
369         uint8_t fp_mask;
370         size_t off, idx;
371         uint8_t *sample;
372
373         fp_off = devc->first_partial_logic_index;
374         fp_mask = devc->first_partial_logic_mask;
375         if (fp_off == logic->unitsize)
376                 return;
377
378         for (off = 0; off < logic->length; off += logic->unitsize) {
379                 sample = logic->data + off;
380                 sample[fp_off] &= fp_mask;
381                 for (idx = fp_off + 1; idx < logic->unitsize; idx++)
382                         sample[idx] = 0x00;
383         }
384 }
385
386 static void send_analog_packet(struct analog_gen *ag,
387                 struct sr_dev_inst *sdi, uint64_t *analog_sent,
388                 uint64_t analog_pos, uint64_t analog_todo)
389 {
390         struct sr_datafeed_packet packet;
391         struct dev_context *devc;
392         uint64_t sending_now, to_avg;
393         int ag_pattern_pos;
394         unsigned int i;
395
396         if (!ag->ch || !ag->ch->enabled)
397                 return;
398
399         devc = sdi->priv;
400         packet.type = SR_DF_ANALOG;
401         packet.payload = &ag->packet;
402
403         if (!devc->avg) {
404                 ag_pattern_pos = analog_pos % ag->num_samples;
405                 sending_now = MIN(analog_todo, ag->num_samples - ag_pattern_pos);
406                 ag->packet.data = ag->pattern_data + ag_pattern_pos;
407                 ag->packet.num_samples = sending_now;
408                 sr_session_send(sdi, &packet);
409
410                 /* Whichever channel group gets there first. */
411                 *analog_sent = MAX(*analog_sent, sending_now);
412         } else {
413                 ag_pattern_pos = analog_pos % ag->num_samples;
414                 to_avg = MIN(analog_todo, ag->num_samples - ag_pattern_pos);
415
416                 for (i = 0; i < to_avg; i++) {
417                         ag->avg_val = (ag->avg_val +
418                                         *(ag->pattern_data +
419                                           ag_pattern_pos + i)) / 2;
420                         ag->num_avgs++;
421                         /* Time to send averaged data? */
422                         if ((devc->avg_samples > 0) && (ag->num_avgs >= devc->avg_samples))
423                                 goto do_send;
424                 }
425
426                 if (devc->avg_samples == 0) {
427                         /* We're averaging all the samples, so wait with
428                          * sending until the very end.
429                          */
430                         *analog_sent = ag->num_avgs;
431                         return;
432                 }
433
434 do_send:
435                 ag->packet.data = &ag->avg_val;
436                 ag->packet.num_samples = 1;
437
438                 sr_session_send(sdi, &packet);
439                 *analog_sent = ag->num_avgs;
440
441                 ag->num_avgs = 0;
442                 ag->avg_val = 0.0f;
443         }
444 }
445
446 /* Callback handling data */
447 SR_PRIV int demo_prepare_data(int fd, int revents, void *cb_data)
448 {
449         struct sr_dev_inst *sdi;
450         struct dev_context *devc;
451         struct sr_datafeed_packet packet;
452         struct sr_datafeed_logic logic;
453         struct analog_gen *ag;
454         GHashTableIter iter;
455         void *value;
456         uint64_t samples_todo, logic_done, analog_done, analog_sent, sending_now;
457         int64_t elapsed_us, limit_us, todo_us;
458         int64_t trigger_offset;
459         int pre_trigger_samples;
460
461         (void)fd;
462         (void)revents;
463
464         sdi = cb_data;
465         devc = sdi->priv;
466
467         /* Just in case. */
468         if (devc->cur_samplerate <= 0
469                         || (devc->num_logic_channels <= 0
470                         && devc->num_analog_channels <= 0)) {
471                 sr_dev_acquisition_stop(sdi);
472                 return G_SOURCE_CONTINUE;
473         }
474
475         /* What time span should we send samples for? */
476         elapsed_us = g_get_monotonic_time() - devc->start_us;
477         limit_us = 1000 * devc->limit_msec;
478         if (limit_us > 0 && limit_us < elapsed_us)
479                 todo_us = MAX(0, limit_us - devc->spent_us);
480         else
481                 todo_us = MAX(0, elapsed_us - devc->spent_us);
482
483         /* How many samples are outstanding since the last round? */
484         samples_todo = (todo_us * devc->cur_samplerate + G_USEC_PER_SEC - 1)
485                         / G_USEC_PER_SEC;
486
487         if (devc->limit_samples > 0) {
488                 if (devc->limit_samples < devc->sent_samples)
489                         samples_todo = 0;
490                 else if (devc->limit_samples - devc->sent_samples < samples_todo)
491                         samples_todo = devc->limit_samples - devc->sent_samples;
492         }
493
494         if (samples_todo == 0)
495                 return G_SOURCE_CONTINUE;
496
497         if (devc->limit_frames) {
498                 /* Never send more samples than a frame can fit... */
499                 samples_todo = MIN(samples_todo, SAMPLES_PER_FRAME);
500                 /* ...or than we need to finish the current frame. */
501                 samples_todo = MIN(samples_todo,
502                         SAMPLES_PER_FRAME - devc->sent_frame_samples);
503         }
504
505         /* Calculate the actual time covered by this run back from the sample
506          * count, rounded towards zero. This avoids getting stuck on a too-low
507          * time delta with no samples being sent due to round-off.
508          */
509         todo_us = samples_todo * G_USEC_PER_SEC / devc->cur_samplerate;
510
511         logic_done = devc->num_logic_channels > 0 ? 0 : samples_todo;
512         if (!devc->enabled_logic_channels)
513                 logic_done = samples_todo;
514
515         analog_done = devc->num_analog_channels > 0 ? 0 : samples_todo;
516         if (!devc->enabled_analog_channels)
517                 analog_done = samples_todo;
518
519         while (logic_done < samples_todo || analog_done < samples_todo) {
520                 /* Logic */
521                 if (logic_done < samples_todo) {
522                         sending_now = MIN(samples_todo - logic_done,
523                                         LOGIC_BUFSIZE / devc->logic_unitsize);
524                         logic_generator(sdi, sending_now * devc->logic_unitsize);
525                         /* Check for trigger and send pre-trigger data if needed */
526                         if (devc->stl && (!devc->trigger_fired)) {
527                                 trigger_offset = soft_trigger_logic_check(devc->stl,
528                                                 devc->logic_data, sending_now * devc->logic_unitsize,
529                                                 &pre_trigger_samples);
530                                 if (trigger_offset > -1) {
531                                         devc->trigger_fired = TRUE;
532                                         logic_done = pre_trigger_samples;
533                                 }
534                         } else
535                                 trigger_offset = 0;
536
537                         /* Send logic samples if needed */
538                         packet.type = SR_DF_LOGIC;
539                         packet.payload = &logic;
540                         logic.unitsize = devc->logic_unitsize;
541
542                         if (devc->stl) {
543                                 if (devc->trigger_fired && (trigger_offset < (int)sending_now)) {
544                                         /* Send after-trigger data */
545                                         logic.length = (sending_now - trigger_offset) * devc->logic_unitsize;
546                                         logic.data = devc->logic_data + trigger_offset * devc->logic_unitsize;
547                                         logic_fixup_feed(devc, &logic);
548                                         sr_session_send(sdi, &packet);
549                                         logic_done += sending_now - trigger_offset;
550                                         /* End acquisition */
551                                         sr_dbg("Triggered, stopping acquisition.");
552                                         sr_dev_acquisition_stop(sdi);
553                                         break;
554                                 } else {
555                                         /* Send nothing */
556                                         logic_done += sending_now;
557                                 }
558                         } else if (!devc->stl) {
559                                 /* No trigger defined, send logic samples */
560                                 logic.length = sending_now * devc->logic_unitsize;
561                                 logic.data = devc->logic_data;
562                                 logic_fixup_feed(devc, &logic);
563                                 sr_session_send(sdi, &packet);
564                                 logic_done += sending_now;
565                         }
566                 }
567
568                 /* Analog, one channel at a time */
569                 if (analog_done < samples_todo) {
570                         analog_sent = 0;
571
572                         g_hash_table_iter_init(&iter, devc->ch_ag);
573                         while (g_hash_table_iter_next(&iter, NULL, &value)) {
574                                 send_analog_packet(value, sdi, &analog_sent,
575                                                 devc->sent_samples + analog_done,
576                                                 samples_todo - analog_done);
577                         }
578                         analog_done += analog_sent;
579                 }
580         }
581
582         uint64_t min = MIN(logic_done, analog_done);
583         devc->sent_samples += min;
584         devc->sent_frame_samples += min;
585         devc->spent_us += todo_us;
586
587         if (devc->limit_frames && devc->sent_frame_samples >= SAMPLES_PER_FRAME) {
588                 std_session_send_frame_end(sdi);
589                 devc->sent_frame_samples = 0;
590                 devc->limit_frames--;
591                 if (!devc->limit_frames) {
592                         sr_dbg("Requested number of frames reached.");
593                         sr_dev_acquisition_stop(sdi);
594                 }
595         }
596
597         if ((devc->limit_samples > 0 && devc->sent_samples >= devc->limit_samples)
598                         || (limit_us > 0 && devc->spent_us >= limit_us)) {
599
600                 /* If we're averaging everything - now is the time to send data */
601                 if (devc->avg && devc->avg_samples == 0) {
602                         g_hash_table_iter_init(&iter, devc->ch_ag);
603                         while (g_hash_table_iter_next(&iter, NULL, &value)) {
604                                 ag = value;
605                                 packet.type = SR_DF_ANALOG;
606                                 packet.payload = &ag->packet;
607                                 ag->packet.data = &ag->avg_val;
608                                 ag->packet.num_samples = 1;
609                                 sr_session_send(sdi, &packet);
610                         }
611                 }
612                 sr_dbg("Requested number of samples reached.");
613                 sr_dev_acquisition_stop(sdi);
614         } else if (devc->limit_frames) {
615                 if (devc->sent_frame_samples == 0)
616                         std_session_send_frame_begin(sdi);
617         }
618
619         return G_SOURCE_CONTINUE;
620 }