]>
Commit | Line | Data |
---|---|---|
d74d30bb UH |
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 | ||
6ec6c43b | 21 | #include <config.h> |
d74d30bb | 22 | #include <string.h> |
c1aae900 | 23 | #include <libsigrok/libsigrok.h> |
d74d30bb UH |
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; | |
5faebab2 | 33 | const struct sr_datafeed_analog_old *analog_old; |
edb691fc | 34 | const struct sr_datafeed_analog *analog; |
d74d30bb UH |
35 | struct sr_channel *ch; |
36 | GSList *l; | |
37 | float *fdata, *f; | |
38 | int si, num_channels, c; | |
39 | uint8_t *b; | |
0662a7d0 ML |
40 | int64_t p; |
41 | uint64_t i, j, q; | |
d74d30bb UH |
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; | |
5faebab2 UH |
57 | case SR_DF_ANALOG_OLD: |
58 | analog_old = packet_in->payload; | |
59 | fdata = (float *)analog_old->data; | |
60 | num_channels = g_slist_length(analog_old->channels); | |
61 | for (si = 0; si < analog_old->num_samples; si++) { | |
d74d30bb | 62 | /* For now invert all values in all channels. */ |
5faebab2 | 63 | for (l = analog_old->channels, c = 0; l; l = l->next, c++) { |
d74d30bb UH |
64 | ch = l->data; |
65 | (void)ch; | |
66 | f = &fdata[si * num_channels + c]; | |
67 | *f = 1.0 / *f; | |
68 | } | |
69 | } | |
70 | break; | |
edb691fc UH |
71 | case SR_DF_ANALOG: |
72 | analog = packet_in->payload; | |
73 | p = analog->encoding->scale.p; | |
74 | q = analog->encoding->scale.q; | |
0662a7d0 ML |
75 | if (q > INT64_MAX) |
76 | return SR_ERR; | |
edb691fc UH |
77 | analog->encoding->scale.p = (p < 0) ? -q : q; |
78 | analog->encoding->scale.q = (p < 0) ? -p : p; | |
0662a7d0 | 79 | break; |
d74d30bb UH |
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 | }; |