]> sigrok.org Git - libsigrok.git/blob - src/hardware/demo/protocol.c
output/csv: use intermediate time_t var, silence compiler warning
[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  * 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 #include <config.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <math.h>
28 #include <libsigrok/libsigrok.h>
29 #include "libsigrok-internal.h"
30 #include "protocol.h"
31
32 #define ANALOG_SAMPLES_PER_PERIOD 20
33
34 static const uint8_t pattern_sigrok[] = {
35         0x4c, 0x92, 0x92, 0x92, 0x64, 0x00, 0x00, 0x00,
36         0x82, 0xfe, 0xfe, 0x82, 0x00, 0x00, 0x00, 0x00,
37         0x7c, 0x82, 0x82, 0x92, 0x74, 0x00, 0x00, 0x00,
38         0xfe, 0x12, 0x12, 0x32, 0xcc, 0x00, 0x00, 0x00,
39         0x7c, 0x82, 0x82, 0x82, 0x7c, 0x00, 0x00, 0x00,
40         0xfe, 0x10, 0x28, 0x44, 0x82, 0x00, 0x00, 0x00,
41         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
42         0xbe, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
43 };
44
45 static const uint8_t pattern_squid[128][128 / 8] = {
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, 0x00, 0x00, 0x00, },
57         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0xe0, 0x00, },
58         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xe1, 0x01, },
59         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xe1, 0x01, },
60         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xe3, 0x03, },
61         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xe3, 0x03, },
62         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xc3, 0x03, },
63         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcf, 0xc7, 0x03, },
64         { 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0xc7, 0x03, },
65         { 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0x87, 0x03, },
66         { 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0xc7, 0x03, },
67         { 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8f, 0xcf, 0x03, },
68         { 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xcf, 0x03, },
69         { 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0x03, },
70         { 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0xff, 0x03, },
71         { 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0xfe, 0x01, },
72         { 0x00, 0x00, 0x00, 0x80, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0xfe, 0x01, },
73         { 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0xfc, 0x00, },
74         { 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
75         { 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
76         { 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
77         { 0x00, 0xc0, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
78         { 0x00, 0x80, 0x01, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0xfe, 0xff, 0x03, },
79         { 0x00, 0x00, 0x07, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0xfe, 0xff, 0x03, },
80         { 0x00, 0x00, 0x1c, 0x00, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0xfe, 0xff, 0x03, },
81         { 0x00, 0x00, 0x78, 0x00, 0xc0, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0xfe, 0xff, 0x03, },
82         { 0x00, 0x00, 0xe0, 0x00, 0x80, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0xfe, 0xff, 0x03, },
83         { 0x00, 0x00, 0xc0, 0x03, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
84         { 0x00, 0x00, 0x00, 0x07, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
85         { 0x00, 0x00, 0x00, 0x1c, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
86         { 0x00, 0x00, 0x00, 0xf8, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x03, 0x00, },
87         { 0x00, 0x00, 0x00, 0xf0, 0x01, 0x38, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x01, 0x00, 0xf0, 0x1f, 0x1c, },
88         { 0x00, 0x00, 0x00, 0xe0, 0x07, 0x70, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x0f, 0x00, 0xfc, 0x3f, 0x3c, },
89         { 0x80, 0x03, 0x00, 0xc0, 0x0f, 0xe0, 0x00, 0x00, 0x80, 0xff, 0xff, 0x3f, 0x00, 0xfc, 0x7f, 0x7c, },
90         { 0x00, 0x1e, 0x00, 0x00, 0x1f, 0xc0, 0x01, 0x00, 0xc0, 0xff, 0xff, 0x7f, 0x00, 0xfe, 0xff, 0x7c, },
91         { 0x00, 0xf0, 0x01, 0x00, 0x7c, 0x80, 0x03, 0x00, 0xc0, 0xff, 0xff, 0xff, 0x00, 0xfe, 0xff, 0x7c, },
92         { 0x00, 0xc0, 0x0f, 0x00, 0xf0, 0x00, 0x07, 0x00, 0xc0, 0xff, 0xff, 0xff, 0x00, 0x3f, 0xf8, 0x78, },
93         { 0x00, 0x00, 0x3e, 0x00, 0xc0, 0x03, 0x0e, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x00, 0x0f, 0xf0, 0xf0, },
94         { 0x00, 0x00, 0xf0, 0x07, 0x80, 0x07, 0x3c, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x00, 0x0f, 0xf0, 0xf0, },
95         { 0x00, 0x00, 0x80, 0x3f, 0x00, 0x1e, 0x78, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x00, 0x0f, 0xe0, 0xf0, },
96         { 0x00, 0x00, 0x00, 0xff, 0x00, 0x7c, 0xe0, 0x01, 0xe0, 0xff, 0xff, 0xff, 0x00, 0x0f, 0xe0, 0xf0, },
97         { 0x00, 0x00, 0x00, 0xfc, 0x03, 0xf0, 0xc1, 0x07, 0xf0, 0xff, 0xff, 0xff, 0x00, 0x0f, 0xf0, 0xf0, },
98         { 0x00, 0x00, 0x00, 0xe0, 0x1f, 0xc0, 0x03, 0x1f, 0xe0, 0xff, 0xff, 0xff, 0x00, 0x0f, 0xf0, 0x78, },
99         { 0x00, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x1f, 0xfc, 0xe0, 0xff, 0xff, 0xff, 0x01, 0xff, 0xff, 0x7f, },
100         { 0xf8, 0x03, 0x00, 0x00, 0xf0, 0x07, 0xfe, 0xf0, 0xff, 0xff, 0xff, 0xff, 0x01, 0xff, 0xff, 0x7f, },
101         { 0x80, 0xff, 0x01, 0x00, 0x80, 0x3f, 0xf0, 0x8f, 0xff, 0xff, 0xff, 0xff, 0x00, 0xfe, 0xff, 0x3f, },
102         { 0x00, 0xf0, 0xff, 0x07, 0x00, 0xfc, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xfe, 0xff, 0x3f, },
103         { 0x00, 0x00, 0xfc, 0x7f, 0x00, 0xe0, 0x7f, 0xfc, 0xff, 0xff, 0xff, 0xff, 0x00, 0xfe, 0xff, 0x1f, },
104         { 0x00, 0x00, 0xc0, 0xff, 0x1f, 0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xfc, 0xff, 0x03, },
105         { 0x00, 0x00, 0x00, 0xf8, 0xff, 0x07, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, },
106         { 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0x0f, 0xe0, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, },
107         { 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, },
108         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, },
109         { 0x00, 0x00, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0xff, 0xff, 0x03, },
110         { 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0xff, 0xff, 0x03, },
111         { 0x00, 0x10, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0x03, },
112         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0x03, },
113         { 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0x03, },
114         { 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0x0f, 0xe0, 0xff, 0xff, 0xff, 0xff, 0x00, 0x7c, 0x00, 0x00, },
115         { 0x00, 0x00, 0x00, 0xf8, 0xff, 0x07, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x3c, 0x00, 0x00, },
116         { 0x00, 0x00, 0xc0, 0xff, 0x1f, 0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x1e, 0x00, 0x00, },
117         { 0x00, 0x00, 0xf8, 0x7f, 0x00, 0xe0, 0x7f, 0xfc, 0xff, 0xff, 0xff, 0xff, 0x00, 0x1e, 0x00, 0x00, },
118         { 0x00, 0xf0, 0xff, 0x07, 0x00, 0xfc, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x0f, 0x00, 0x00, },
119         { 0x80, 0xff, 0x01, 0x00, 0x80, 0x3f, 0xf0, 0x8f, 0xff, 0xff, 0xff, 0xff, 0x01, 0x0f, 0x00, 0x00, },
120         { 0xf8, 0x03, 0x00, 0x00, 0xf0, 0x07, 0xfe, 0xf0, 0xff, 0xff, 0xff, 0xff, 0x00, 0x0f, 0x00, 0x00, },
121         { 0x00, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x1f, 0xfc, 0xe0, 0xff, 0xff, 0xff, 0x00, 0x0f, 0x00, 0x00, },
122         { 0x00, 0x00, 0x00, 0xe0, 0x1f, 0xc0, 0x07, 0x1f, 0xf0, 0xff, 0xff, 0xff, 0x00, 0x06, 0x00, 0x00, },
123         { 0x00, 0x00, 0x00, 0xfc, 0x03, 0xf0, 0xc1, 0x07, 0xf0, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, },
124         { 0x00, 0x00, 0x00, 0xff, 0x00, 0x7c, 0xe0, 0x01, 0xf0, 0xff, 0xff, 0xff, 0x01, 0xe0, 0x1f, 0x00, },
125         { 0x00, 0x00, 0x80, 0x3f, 0x00, 0x1e, 0x78, 0x00, 0xf0, 0xff, 0xff, 0xff, 0x01, 0xf0, 0x7f, 0x00, },
126         { 0x00, 0x00, 0xf0, 0x0f, 0x80, 0x07, 0x3c, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x01, 0xfc, 0xff, 0x00, },
127         { 0x00, 0x00, 0x3e, 0x00, 0xc0, 0x03, 0x0e, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x01, 0xfc, 0xff, 0x01, },
128         { 0x00, 0xc0, 0x0f, 0x00, 0xf0, 0x00, 0x07, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x01, 0xfe, 0xff, 0x01, },
129         { 0x00, 0xf0, 0x01, 0x00, 0x7c, 0x80, 0x03, 0x00, 0xe0, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0x03, },
130         { 0x00, 0x3e, 0x00, 0x00, 0x1f, 0xc0, 0x01, 0x00, 0x80, 0xff, 0xff, 0xff, 0x00, 0x1f, 0xe0, 0x03, },
131         { 0x80, 0x03, 0x00, 0xc0, 0x0f, 0xe0, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x00, 0x0f, 0xc0, 0x03, },
132         { 0x00, 0x00, 0x00, 0xe0, 0x07, 0x70, 0x00, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x00, 0x0f, 0xc0, 0x03, },
133         { 0x00, 0x00, 0x00, 0xf0, 0x01, 0x38, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x01, 0x00, 0x0f, 0x80, 0x03, },
134         { 0x00, 0x00, 0x00, 0xf8, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xc0, 0x03, },
135         { 0x00, 0x00, 0x00, 0x1c, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xc0, 0x03, },
136         { 0x00, 0x00, 0x00, 0x07, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xe0, 0x03, },
137         { 0x00, 0x00, 0xc0, 0x03, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xf8, 0x03, },
138         { 0x00, 0x00, 0xe0, 0x00, 0x80, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x01, },
139         { 0x00, 0x00, 0x78, 0x00, 0xc0, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x01, },
140         { 0x00, 0x00, 0x1c, 0x00, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff, 0x00, },
141         { 0x00, 0x00, 0x06, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x7f, 0x00, },
142         { 0x00, 0x80, 0x03, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x1f, 0x00, },
143         { 0x00, 0xc0, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
144         { 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
145         { 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
146         { 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x01, },
147         { 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x03, },
148         { 0x00, 0x00, 0x00, 0x80, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x03, },
149         { 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x03, },
150         { 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x03, },
151         { 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x03, },
152         { 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x07, 0x00, },
153         { 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x07, 0x00, },
154         { 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x0f, 0x00, },
155         { 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x1f, 0x00, },
156         { 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0x00, },
157         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff, 0x00, },
158         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0x01, },
159         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xfc, 0x03, },
160         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xf8, 0x03, },
161         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xf0, 0x03, },
162         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x03, },
163         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, },
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         { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, },
174 };
175
176 SR_PRIV void demo_generate_analog_pattern(struct dev_context *devc)
177 {
178         double t, frequency;
179         float amplitude, offset;
180         struct analog_pattern *pattern;
181         unsigned int num_samples, i;
182         float value;
183         int last_end;
184
185         num_samples = ANALOG_BUFSIZE / sizeof(float);
186         frequency = (double) devc->cur_samplerate / ANALOG_SAMPLES_PER_PERIOD;
187         amplitude = DEFAULT_ANALOG_AMPLITUDE;
188         offset = DEFAULT_ANALOG_OFFSET;
189
190         /*
191          * FIXME: We actually need only one period. A ringbuffer would be
192          * useful here.
193          * Make sure the number of samples we put out is an integer
194          * multiple of our period size.
195          */
196
197         /* PATTERN_SQUARE: */
198         sr_dbg("Generating %s pattern.", analog_pattern_str[PATTERN_SQUARE]);
199         pattern = g_malloc(sizeof(struct analog_pattern));
200         value = amplitude;
201         last_end = 0;
202         for (i = 0; i < num_samples; i++) {
203                 if (i % 5 == 0)
204                         value = -value;
205                 if (i % 10 == 0)
206                         last_end = i;
207                 pattern->data[i] = value + offset;
208         }
209         pattern->num_samples = last_end;
210         devc->analog_patterns[PATTERN_SQUARE] = pattern;
211
212         /* Readjusting num_samples for all other patterns. */
213         while (num_samples % ANALOG_SAMPLES_PER_PERIOD != 0)
214                 num_samples--;
215
216         /* PATTERN_SINE: */
217         sr_dbg("Generating %s pattern.", analog_pattern_str[PATTERN_SINE]);
218         pattern = g_malloc(sizeof(struct analog_pattern));
219         for (i = 0; i < num_samples; i++) {
220                 t = (double) i / (double) devc->cur_samplerate;
221                 pattern->data[i] = sin(2 * G_PI * frequency * t) * amplitude + offset;
222         }
223         pattern->num_samples = last_end;
224         devc->analog_patterns[PATTERN_SINE] = pattern;
225
226         /* PATTERN_TRIANGLE: */
227         sr_dbg("Generating %s pattern.", analog_pattern_str[PATTERN_TRIANGLE]);
228         pattern = g_malloc(sizeof(struct analog_pattern));
229         for (i = 0; i < num_samples; i++) {
230                 t = (double) i / (double) devc->cur_samplerate;
231                 pattern->data[i] = (2 / G_PI) * asin(sin(2 * G_PI * frequency * t)) *
232                         amplitude + offset;
233         }
234         pattern->num_samples = last_end;
235         devc->analog_patterns[PATTERN_TRIANGLE] = pattern;
236
237         /* PATTERN_SAWTOOTH: */
238         sr_dbg("Generating %s pattern.", analog_pattern_str[PATTERN_SAWTOOTH]);
239         pattern = g_malloc(sizeof(struct analog_pattern));
240         for (i = 0; i < num_samples; i++) {
241                 t = (double) i / (double) devc->cur_samplerate;
242                 pattern->data[i] = 2 * ((t * frequency) - floor(0.5f + t * frequency)) *
243                         amplitude + offset;
244         }
245         pattern->num_samples = last_end;
246         devc->analog_patterns[PATTERN_SAWTOOTH] = pattern;
247
248         /* PATTERN_ANALOG_RANDOM */
249         /* Data not filled here, will be generated in send_analog_packet(). */
250         pattern = g_malloc(sizeof(struct analog_pattern));
251         pattern->num_samples = last_end;
252         devc->analog_patterns[PATTERN_ANALOG_RANDOM] = pattern;
253 }
254
255 SR_PRIV void demo_free_analog_pattern(struct dev_context *devc)
256 {
257         g_free(devc->analog_patterns[PATTERN_SQUARE]);
258         g_free(devc->analog_patterns[PATTERN_SINE]);
259         g_free(devc->analog_patterns[PATTERN_TRIANGLE]);
260         g_free(devc->analog_patterns[PATTERN_SAWTOOTH]);
261         g_free(devc->analog_patterns[PATTERN_ANALOG_RANDOM]);
262 }
263
264 static uint64_t encode_number_to_gray(uint64_t nr)
265 {
266         return nr ^ (nr >> 1);
267 }
268
269 static void set_logic_data(uint64_t bits, uint8_t *data, size_t len)
270 {
271         while (len--) {
272                 *data++ = bits & 0xff;
273                 bits >>= 8;
274         }
275 }
276
277 static void logic_generator(struct sr_dev_inst *sdi, uint64_t size)
278 {
279         struct dev_context *devc;
280         uint64_t i, j;
281         uint8_t pat;
282         uint8_t *sample;
283         const uint8_t *image_col;
284         size_t col_count, col_height;
285         uint64_t gray;
286
287         devc = sdi->priv;
288
289         switch (devc->logic_pattern) {
290         case PATTERN_SIGROK:
291                 memset(devc->logic_data, 0x00, size);
292                 for (i = 0; i < size; i += devc->logic_unitsize) {
293                         for (j = 0; j < devc->logic_unitsize; j++) {
294                                 pat = pattern_sigrok[(devc->step + j) % sizeof(pattern_sigrok)] >> 1;
295                                 devc->logic_data[i + j] = ~pat;
296                         }
297                         devc->step++;
298                 }
299                 break;
300         case PATTERN_RANDOM:
301                 for (i = 0; i < size; i++)
302                         devc->logic_data[i] = (uint8_t)(rand() & 0xff);
303                 break;
304         case PATTERN_INC:
305                 for (i = 0; i < size; i++) {
306                         for (j = 0; j < devc->logic_unitsize; j++)
307                                 devc->logic_data[i + j] = devc->step;
308                         devc->step++;
309                 }
310                 break;
311         case PATTERN_WALKING_ONE:
312                 /* j contains the value of the highest bit */
313                 j = 1 << (devc->num_logic_channels - 1);
314                 for (i = 0; i < size; i++) {
315                         devc->logic_data[i] = devc->step;
316                         if (devc->step == 0)
317                                 devc->step = 1;
318                         else
319                                 if (devc->step == j)
320                                         devc->step = 0;
321                                 else
322                                         devc->step <<= 1;
323                 }
324                 break;
325         case PATTERN_WALKING_ZERO:
326                 /* Same as walking one, only with inverted output */
327                 /* j contains the value of the highest bit */
328                 j = 1 << (devc->num_logic_channels - 1);
329                 for (i = 0; i < size; i++) {
330                         devc->logic_data[i] = ~devc->step;
331                         if (devc->step == 0)
332                                 devc->step = 1;
333                         else
334                                 if (devc->step == j)
335                                         devc->step = 0;
336                                 else
337                                         devc->step <<= 1;
338                 }
339                 break;
340         case PATTERN_ALL_LOW:
341         case PATTERN_ALL_HIGH:
342                 /* These were set when the pattern mode was selected. */
343                 break;
344         case PATTERN_SQUID:
345                 memset(devc->logic_data, 0x00, size);
346                 col_count = ARRAY_SIZE(pattern_squid);
347                 col_height = ARRAY_SIZE(pattern_squid[0]);
348                 for (i = 0; i < size; i += devc->logic_unitsize) {
349                         sample = &devc->logic_data[i];
350                         image_col = pattern_squid[devc->step];
351                         for (j = 0; j < devc->logic_unitsize; j++) {
352                                 pat = image_col[j % col_height];
353                                 sample[j] = pat;
354                         }
355                         devc->step++;
356                         devc->step %= col_count;
357                 }
358                 break;
359         case PATTERN_GRAYCODE:
360                 for (i = 0; i < size; i += devc->logic_unitsize) {
361                         devc->step++;
362                         devc->step &= devc->all_logic_channels_mask;
363                         gray = encode_number_to_gray(devc->step);
364                         gray &= devc->all_logic_channels_mask;
365                         set_logic_data(gray, &devc->logic_data[i], devc->logic_unitsize);
366                 }
367                 break;
368         default:
369                 sr_err("Unknown pattern: %d.", devc->logic_pattern);
370                 break;
371         }
372 }
373
374 /*
375  * Fixup a memory image of generated logic data before it gets sent to
376  * the session's datafeed. Mask out content from disabled channels.
377  *
378  * TODO: Need we apply a channel map, and enforce a dense representation
379  * of the enabled channels' data?
380  */
381 static void logic_fixup_feed(struct dev_context *devc,
382                 struct sr_datafeed_logic *logic)
383 {
384         size_t fp_off;
385         uint8_t fp_mask;
386         size_t off, idx;
387         uint8_t *sample;
388
389         fp_off = devc->first_partial_logic_index;
390         fp_mask = devc->first_partial_logic_mask;
391         if (fp_off == logic->unitsize)
392                 return;
393
394         for (off = 0; off < logic->length; off += logic->unitsize) {
395                 sample = logic->data + off;
396                 sample[fp_off] &= fp_mask;
397                 for (idx = fp_off + 1; idx < logic->unitsize; idx++)
398                         sample[idx] = 0x00;
399         }
400 }
401
402 static void send_analog_packet(struct analog_gen *ag,
403                 struct sr_dev_inst *sdi, uint64_t *analog_sent,
404                 uint64_t analog_pos, uint64_t analog_todo)
405 {
406         struct sr_datafeed_packet packet;
407         struct dev_context *devc;
408         struct analog_pattern *pattern;
409         uint64_t sending_now, to_avg;
410         int ag_pattern_pos;
411         unsigned int i;
412         float amplitude, offset, value;
413         float *data;
414
415         if (!ag->ch || !ag->ch->enabled)
416                 return;
417
418         devc = sdi->priv;
419         packet.type = SR_DF_ANALOG;
420         packet.payload = &ag->packet;
421
422         pattern = devc->analog_patterns[ag->pattern];
423
424         ag->packet.meaning->channels = g_slist_append(NULL, ag->ch);
425         ag->packet.meaning->mq = ag->mq;
426         ag->packet.meaning->mqflags = ag->mq_flags;
427
428         /* Set a unit for the given quantity. */
429         if (ag->mq == SR_MQ_VOLTAGE)
430                 ag->packet.meaning->unit = SR_UNIT_VOLT;
431         else if (ag->mq == SR_MQ_CURRENT)
432                 ag->packet.meaning->unit = SR_UNIT_AMPERE;
433         else if (ag->mq == SR_MQ_RESISTANCE)
434                 ag->packet.meaning->unit = SR_UNIT_OHM;
435         else if (ag->mq == SR_MQ_CAPACITANCE)
436                 ag->packet.meaning->unit = SR_UNIT_FARAD;
437         else if (ag->mq == SR_MQ_TEMPERATURE)
438                 ag->packet.meaning->unit = SR_UNIT_CELSIUS;
439         else if (ag->mq == SR_MQ_FREQUENCY)
440                 ag->packet.meaning->unit = SR_UNIT_HERTZ;
441         else if (ag->mq == SR_MQ_DUTY_CYCLE)
442                 ag->packet.meaning->unit = SR_UNIT_PERCENTAGE;
443         else if (ag->mq == SR_MQ_CONTINUITY)
444                 ag->packet.meaning->unit = SR_UNIT_OHM;
445         else if (ag->mq == SR_MQ_PULSE_WIDTH)
446                 ag->packet.meaning->unit = SR_UNIT_PERCENTAGE;
447         else if (ag->mq == SR_MQ_CONDUCTANCE)
448                 ag->packet.meaning->unit = SR_UNIT_SIEMENS;
449         else if (ag->mq == SR_MQ_POWER)
450                 ag->packet.meaning->unit = SR_UNIT_WATT;
451         else if (ag->mq == SR_MQ_GAIN)
452                 ag->packet.meaning->unit = SR_UNIT_UNITLESS;
453         else if (ag->mq == SR_MQ_SOUND_PRESSURE_LEVEL)
454                 ag->packet.meaning->unit = SR_UNIT_DECIBEL_SPL;
455         else if (ag->mq == SR_MQ_CARBON_MONOXIDE)
456                 ag->packet.meaning->unit = SR_UNIT_CONCENTRATION;
457         else if (ag->mq == SR_MQ_RELATIVE_HUMIDITY)
458                 ag->packet.meaning->unit = SR_UNIT_HUMIDITY_293K;
459         else if (ag->mq == SR_MQ_TIME)
460                 ag->packet.meaning->unit = SR_UNIT_SECOND;
461         else if (ag->mq == SR_MQ_WIND_SPEED)
462                 ag->packet.meaning->unit = SR_UNIT_METER_SECOND;
463         else if (ag->mq == SR_MQ_PRESSURE)
464                 ag->packet.meaning->unit = SR_UNIT_HECTOPASCAL;
465         else if (ag->mq == SR_MQ_PARALLEL_INDUCTANCE)
466                 ag->packet.meaning->unit = SR_UNIT_HENRY;
467         else if (ag->mq == SR_MQ_PARALLEL_CAPACITANCE)
468                 ag->packet.meaning->unit = SR_UNIT_FARAD;
469         else if (ag->mq == SR_MQ_PARALLEL_RESISTANCE)
470                 ag->packet.meaning->unit = SR_UNIT_OHM;
471         else if (ag->mq == SR_MQ_SERIES_INDUCTANCE)
472                 ag->packet.meaning->unit = SR_UNIT_HENRY;
473         else if (ag->mq == SR_MQ_SERIES_CAPACITANCE)
474                 ag->packet.meaning->unit = SR_UNIT_FARAD;
475         else if (ag->mq == SR_MQ_SERIES_RESISTANCE)
476                 ag->packet.meaning->unit = SR_UNIT_OHM;
477         else if (ag->mq == SR_MQ_DISSIPATION_FACTOR)
478                 ag->packet.meaning->unit = SR_UNIT_UNITLESS;
479         else if (ag->mq == SR_MQ_QUALITY_FACTOR)
480                 ag->packet.meaning->unit = SR_UNIT_UNITLESS;
481         else if (ag->mq == SR_MQ_PHASE_ANGLE)
482                 ag->packet.meaning->unit = SR_UNIT_DEGREE;
483         else if (ag->mq == SR_MQ_DIFFERENCE)
484                 ag->packet.meaning->unit = SR_UNIT_UNITLESS;
485         else if (ag->mq == SR_MQ_COUNT)
486                 ag->packet.meaning->unit = SR_UNIT_PIECE;
487         else if (ag->mq == SR_MQ_POWER_FACTOR)
488                 ag->packet.meaning->unit = SR_UNIT_UNITLESS;
489         else if (ag->mq == SR_MQ_APPARENT_POWER)
490                 ag->packet.meaning->unit = SR_UNIT_VOLT_AMPERE;
491         else if (ag->mq == SR_MQ_MASS)
492                 ag->packet.meaning->unit = SR_UNIT_GRAM;
493         else if (ag->mq == SR_MQ_HARMONIC_RATIO)
494                 ag->packet.meaning->unit = SR_UNIT_UNITLESS;
495         else
496                 ag->packet.meaning->unit = SR_UNIT_UNITLESS;
497
498         if (!devc->avg) {
499                 ag_pattern_pos = analog_pos % pattern->num_samples;
500                 sending_now = MIN(analog_todo, pattern->num_samples - ag_pattern_pos);
501                 if (ag->amplitude != DEFAULT_ANALOG_AMPLITUDE ||
502                         ag->offset != DEFAULT_ANALOG_OFFSET ||
503                         ag->pattern == PATTERN_ANALOG_RANDOM) {
504                         /*
505                          * Amplitude or offset changed (or we are generating
506                          * random data), modify each sample.
507                          */
508                         if (ag->pattern == PATTERN_ANALOG_RANDOM) {
509                                 amplitude = ag->amplitude / 500.0;
510                                 offset = ag->offset - DEFAULT_ANALOG_OFFSET - ag->amplitude;
511                         } else {
512                                 amplitude = ag->amplitude / DEFAULT_ANALOG_AMPLITUDE;
513                                 offset = ag->offset - DEFAULT_ANALOG_OFFSET;
514                         }
515                         data = ag->packet.data;
516                         for (i = 0; i < sending_now; i++) {
517                                 if (ag->pattern == PATTERN_ANALOG_RANDOM)
518                                         data[i] = (rand() % 1000) * amplitude + offset;
519                                 else
520                                         data[i] = pattern->data[ag_pattern_pos + i] * amplitude + offset;
521                         }
522                 } else {
523                         /* Amplitude and offset unchanged, use the fast way. */
524                         ag->packet.data = pattern->data + ag_pattern_pos;
525                 }
526                 ag->packet.num_samples = sending_now;
527                 sr_session_send(sdi, &packet);
528
529                 /* Whichever channel group gets there first. */
530                 *analog_sent = MAX(*analog_sent, sending_now);
531         } else {
532                 ag_pattern_pos = analog_pos % pattern->num_samples;
533                 to_avg = MIN(analog_todo, pattern->num_samples - ag_pattern_pos);
534                 if (ag->pattern == PATTERN_ANALOG_RANDOM) {
535                         amplitude = ag->amplitude / 500.0;
536                         offset = ag->offset - DEFAULT_ANALOG_OFFSET - ag->amplitude;
537                 } else {
538                         amplitude = ag->amplitude / DEFAULT_ANALOG_AMPLITUDE;
539                         offset = ag->offset - DEFAULT_ANALOG_OFFSET;
540                 }
541
542                 for (i = 0; i < to_avg; i++) {
543                         if (ag->pattern == PATTERN_ANALOG_RANDOM)
544                                 value = (rand() % 1000) * amplitude + offset;
545                         else
546                                 value = *(pattern->data + ag_pattern_pos + i) * amplitude + offset;
547                         ag->avg_val = (ag->avg_val + value) / 2;
548                         ag->num_avgs++;
549                         /* Time to send averaged data? */
550                         if ((devc->avg_samples > 0) && (ag->num_avgs >= devc->avg_samples))
551                                 goto do_send;
552                 }
553
554                 if (devc->avg_samples == 0) {
555                         /*
556                          * We're averaging all the samples, so wait with
557                          * sending until the very end.
558                          */
559                         *analog_sent = ag->num_avgs;
560                         return;
561                 }
562
563 do_send:
564                 ag->packet.data = &ag->avg_val;
565                 ag->packet.num_samples = 1;
566
567                 sr_session_send(sdi, &packet);
568                 *analog_sent = ag->num_avgs;
569
570                 ag->num_avgs = 0;
571                 ag->avg_val = 0.0f;
572         }
573 }
574
575 /* Callback handling data */
576 SR_PRIV int demo_prepare_data(int fd, int revents, void *cb_data)
577 {
578         struct sr_dev_inst *sdi;
579         struct dev_context *devc;
580         struct sr_datafeed_packet packet;
581         struct sr_datafeed_logic logic;
582         struct analog_gen *ag;
583         GHashTableIter iter;
584         void *value;
585         uint64_t samples_todo, logic_done, analog_done, analog_sent, sending_now;
586         int64_t elapsed_us, limit_us, todo_us;
587         int64_t trigger_offset;
588         int pre_trigger_samples;
589
590         (void)fd;
591         (void)revents;
592
593         sdi = cb_data;
594         devc = sdi->priv;
595
596         /* Just in case. */
597         if (devc->cur_samplerate <= 0
598                         || (devc->num_logic_channels <= 0
599                         && devc->num_analog_channels <= 0)) {
600                 sr_dev_acquisition_stop(sdi);
601                 return G_SOURCE_CONTINUE;
602         }
603
604         /* What time span should we send samples for? */
605         elapsed_us = g_get_monotonic_time() - devc->start_us;
606         limit_us = 1000 * devc->limit_msec;
607         if (limit_us > 0 && limit_us < elapsed_us)
608                 todo_us = MAX(0, limit_us - devc->spent_us);
609         else
610                 todo_us = MAX(0, elapsed_us - devc->spent_us);
611
612         /* How many samples are outstanding since the last round? */
613         samples_todo = (todo_us * devc->cur_samplerate + G_USEC_PER_SEC - 1)
614                         / G_USEC_PER_SEC;
615
616         if (devc->limit_samples > 0) {
617                 if (devc->limit_samples < devc->sent_samples)
618                         samples_todo = 0;
619                 else if (devc->limit_samples - devc->sent_samples < samples_todo)
620                         samples_todo = devc->limit_samples - devc->sent_samples;
621         }
622
623         if (samples_todo == 0)
624                 return G_SOURCE_CONTINUE;
625
626         if (devc->limit_frames) {
627                 /* Never send more samples than a frame can fit... */
628                 samples_todo = MIN(samples_todo, SAMPLES_PER_FRAME);
629                 /* ...or than we need to finish the current frame. */
630                 samples_todo = MIN(samples_todo,
631                         SAMPLES_PER_FRAME - devc->sent_frame_samples);
632         }
633
634         /* Calculate the actual time covered by this run back from the sample
635          * count, rounded towards zero. This avoids getting stuck on a too-low
636          * time delta with no samples being sent due to round-off.
637          */
638         todo_us = samples_todo * G_USEC_PER_SEC / devc->cur_samplerate;
639
640         logic_done = devc->num_logic_channels > 0 ? 0 : samples_todo;
641         if (!devc->enabled_logic_channels)
642                 logic_done = samples_todo;
643
644         analog_done = devc->num_analog_channels > 0 ? 0 : samples_todo;
645         if (!devc->enabled_analog_channels)
646                 analog_done = samples_todo;
647
648         while (logic_done < samples_todo || analog_done < samples_todo) {
649                 /* Logic */
650                 if (logic_done < samples_todo) {
651                         sending_now = MIN(samples_todo - logic_done,
652                                         LOGIC_BUFSIZE / devc->logic_unitsize);
653                         logic_generator(sdi, sending_now * devc->logic_unitsize);
654                         /* Check for trigger and send pre-trigger data if needed */
655                         if (devc->stl && (!devc->trigger_fired)) {
656                                 trigger_offset = soft_trigger_logic_check(devc->stl,
657                                                 devc->logic_data, sending_now * devc->logic_unitsize,
658                                                 &pre_trigger_samples);
659                                 if (trigger_offset > -1) {
660                                         devc->trigger_fired = TRUE;
661                                         logic_done = pre_trigger_samples;
662                                 }
663                         } else
664                                 trigger_offset = 0;
665
666                         /* Send logic samples if needed */
667                         packet.type = SR_DF_LOGIC;
668                         packet.payload = &logic;
669                         logic.unitsize = devc->logic_unitsize;
670
671                         if (devc->stl) {
672                                 if (devc->trigger_fired && (trigger_offset < (int)sending_now)) {
673                                         /* Send after-trigger data */
674                                         logic.length = (sending_now - trigger_offset) * devc->logic_unitsize;
675                                         logic.data = devc->logic_data + trigger_offset * devc->logic_unitsize;
676                                         logic_fixup_feed(devc, &logic);
677                                         sr_session_send(sdi, &packet);
678                                         logic_done += sending_now - trigger_offset;
679                                         /* End acquisition */
680                                         sr_dbg("Triggered, stopping acquisition.");
681                                         sr_dev_acquisition_stop(sdi);
682                                         break;
683                                 } else {
684                                         /* Send nothing */
685                                         logic_done += sending_now;
686                                 }
687                         } else if (!devc->stl) {
688                                 /* No trigger defined, send logic samples */
689                                 logic.length = sending_now * devc->logic_unitsize;
690                                 logic.data = devc->logic_data;
691                                 logic_fixup_feed(devc, &logic);
692                                 sr_session_send(sdi, &packet);
693                                 logic_done += sending_now;
694                         }
695                 }
696
697                 /* Analog, one channel at a time */
698                 if (analog_done < samples_todo) {
699                         analog_sent = 0;
700
701                         g_hash_table_iter_init(&iter, devc->ch_ag);
702                         while (g_hash_table_iter_next(&iter, NULL, &value)) {
703                                 send_analog_packet(value, sdi, &analog_sent,
704                                                 devc->sent_samples + analog_done,
705                                                 samples_todo - analog_done);
706                         }
707                         analog_done += analog_sent;
708                 }
709         }
710
711         uint64_t min = MIN(logic_done, analog_done);
712         devc->sent_samples += min;
713         devc->sent_frame_samples += min;
714         devc->spent_us += todo_us;
715
716         if (devc->limit_frames && devc->sent_frame_samples >= SAMPLES_PER_FRAME) {
717                 std_session_send_df_frame_end(sdi);
718                 devc->sent_frame_samples = 0;
719                 devc->limit_frames--;
720                 if (!devc->limit_frames) {
721                         sr_dbg("Requested number of frames reached.");
722                         sr_dev_acquisition_stop(sdi);
723                 }
724         }
725
726         if ((devc->limit_samples > 0 && devc->sent_samples >= devc->limit_samples)
727                         || (limit_us > 0 && devc->spent_us >= limit_us)) {
728
729                 /* If we're averaging everything - now is the time to send data */
730                 if (devc->avg && devc->avg_samples == 0) {
731                         g_hash_table_iter_init(&iter, devc->ch_ag);
732                         while (g_hash_table_iter_next(&iter, NULL, &value)) {
733                                 ag = value;
734                                 packet.type = SR_DF_ANALOG;
735                                 packet.payload = &ag->packet;
736                                 ag->packet.data = &ag->avg_val;
737                                 ag->packet.num_samples = 1;
738                                 sr_session_send(sdi, &packet);
739                         }
740                 }
741                 sr_dbg("Requested number of samples reached.");
742                 sr_dev_acquisition_stop(sdi);
743         } else if (devc->limit_frames) {
744                 if (devc->sent_frame_samples == 0)
745                         std_session_send_df_frame_begin(sdi);
746         }
747
748         return G_SOURCE_CONTINUE;
749 }