]> sigrok.org Git - libsigrok.git/blob - src/transform/invert.c
98d2240ed961376bdc8ef577fc5a3a52355f66e2
[libsigrok.git] / src / transform / invert.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2015 Uwe Hermann <uwe@hermann-uwe.de>
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 2 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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <config.h>
22 #include <string.h>
23 #include <libsigrok/libsigrok.h>
24 #include "libsigrok-internal.h"
25
26 #define LOG_PREFIX "transform/invert"
27
28 static int receive(const struct sr_transform *t,
29                 struct sr_datafeed_packet *packet_in,
30                 struct sr_datafeed_packet **packet_out)
31 {
32         const struct sr_datafeed_logic *logic;
33         const struct sr_datafeed_analog *analog;
34         const struct sr_datafeed_analog2 *analog2;
35         struct sr_channel *ch;
36         GSList *l;
37         float *fdata, *f;
38         int si, num_channels, c;
39         uint8_t *b;
40         int64_t p;
41         uint64_t i, j, q;
42
43         if (!t || !t->sdi || !packet_in || !packet_out)
44                 return SR_ERR_ARG;
45
46         switch (packet_in->type) {
47         case SR_DF_LOGIC:
48                 logic = packet_in->payload;
49                 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
50                         for (j = 0; j < logic->unitsize; j++) {
51                                 /* For now invert every bit in every byte. */
52                                 b = (uint8_t *)logic->data + i + logic->unitsize - 1 - j;
53                                 *b = ~(*b);
54                         }
55                 }
56                 break;
57         case SR_DF_ANALOG:
58                 analog = packet_in->payload;
59                 fdata = (float *)analog->data;
60                 num_channels = g_slist_length(analog->channels);
61                 for (si = 0; si < analog->num_samples; si++) {
62                         /* For now invert all values in all channels. */
63                         for (l = analog->channels, c = 0; l; l = l->next, c++) {
64                                 ch = l->data;
65                                 (void)ch;
66                                 f = &fdata[si * num_channels + c];
67                                 *f = 1.0 / *f;
68                         }
69                 }
70                 break;
71         case SR_DF_ANALOG2:
72                 analog2 = packet_in->payload;
73                 p = analog2->encoding->scale.p;
74                 q = analog2->encoding->scale.q;
75                 if (q > INT64_MAX)
76                         return SR_ERR;
77                 analog2->encoding->scale.p = (p < 0) ? -q : q;
78                 analog2->encoding->scale.q = (p < 0) ? -p : p;
79                 break;
80         default:
81                 sr_spew("Unsupported packet type %d, ignoring.", packet_in->type);
82                 break;
83         }
84
85         /* Return the in-place-modified packet. */
86         *packet_out = packet_in;
87
88         return SR_OK;
89 }
90
91 SR_PRIV struct sr_transform_module transform_invert = {
92         .id = "invert",
93         .name = "Invert",
94         .desc = "Invert values",
95         .options = NULL,
96         .init = NULL,
97         .receive = receive,
98         .cleanup = NULL,
99 };