]> sigrok.org Git - libsigrok.git/blob - filter.c
Change SIGROK_ prefix to SR_.
[libsigrok.git] / filter.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <stdlib.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include <sigrok.h>
24
25 /*
26  * Convert sample from maximum probes -- the way the hardware driver sent
27  * it -- to a sample taking up only as much space as required, with
28  * unused probes removed.
29  */
30 int filter_probes(int in_unitsize, int out_unitsize, int *probelist,
31                   char *data_in, uint64_t length_in, char **data_out,
32                   uint64_t *length_out)
33 {
34         unsigned int in_offset, out_offset;
35         int num_enabled_probes, out_bit, i;
36         uint64_t sample_in, sample_out;
37
38         if (!(*data_out = malloc(length_in)))
39                 return SR_ERR_MALLOC;
40
41         num_enabled_probes = 0;
42         for (i = 0; probelist[i]; i++)
43                 num_enabled_probes++;
44
45         if (num_enabled_probes == in_unitsize * 8) {
46                 /* All probes are used -- no need to compress anything. */
47                 memcpy(*data_out, data_in, length_in);
48                 *length_out = length_in;
49                 return SR_OK;
50         }
51
52         /* If we reached this point, not all probes are used, so "compress". */
53         in_offset = out_offset = 0;
54         while (in_offset <= length_in - in_unitsize) {
55                 memcpy(&sample_in, data_in + in_offset, in_unitsize);
56                 sample_out = out_bit = 0;
57                 for (i = 0; probelist[i]; i++) {
58                         if (sample_in & (1 << (probelist[i] - 1)))
59                                 sample_out |= (1 << out_bit);
60                         out_bit++;
61                 }
62                 memcpy((*data_out) + out_offset, &sample_out, out_unitsize);
63                 in_offset += in_unitsize;
64                 out_offset += out_unitsize;
65         }
66         *length_out = out_offset;
67
68         return SR_OK;
69 }