From: Uwe Hermann Date: Wed, 11 Feb 2015 08:34:38 +0000 (+0100) Subject: transform: Add an "invert" transform module. X-Git-Tag: libsigrok-0.4.0~653 X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=d74d30bb14b9cb240e244caff7179926c72630e6 transform: Add an "invert" transform module. This inverts the data values: - An analog value of x becomes 1/x. - A digital value of 0 becomes 1 (and vice versa). --- diff --git a/Makefile.am b/Makefile.am index c68ceeb3..16cd288d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -72,7 +72,8 @@ libsigrok_la_SOURCES += \ libsigrok_la_SOURCES += \ src/transform/transform.c \ src/transform/nop.c \ - src/transform/scale.c + src/transform/scale.c \ + src/transform/invert.c # SCPI support libsigrok_la_SOURCES += \ diff --git a/src/transform/invert.c b/src/transform/invert.c new file mode 100644 index 00000000..9f4dcea8 --- /dev/null +++ b/src/transform/invert.c @@ -0,0 +1,87 @@ +/* + * This file is part of the libsigrok project. + * + * Copyright (C) 2015 Uwe Hermann + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include "libsigrok.h" +#include "libsigrok-internal.h" + +#define LOG_PREFIX "transform/invert" + +static int receive(const struct sr_transform *t, + struct sr_datafeed_packet *packet_in, + struct sr_datafeed_packet **packet_out) +{ + const struct sr_datafeed_logic *logic; + const struct sr_datafeed_analog *analog; + struct sr_channel *ch; + GSList *l; + float *fdata, *f; + int si, num_channels, c; + uint8_t *b; + uint64_t i, j; + + if (!t || !t->sdi || !packet_in || !packet_out) + return SR_ERR_ARG; + + switch (packet_in->type) { + case SR_DF_LOGIC: + logic = packet_in->payload; + for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) { + for (j = 0; j < logic->unitsize; j++) { + /* For now invert every bit in every byte. */ + b = (uint8_t *)logic->data + i + logic->unitsize - 1 - j; + *b = ~(*b); + } + } + break; + case SR_DF_ANALOG: + analog = packet_in->payload; + fdata = (float *)analog->data; + num_channels = g_slist_length(analog->channels); + for (si = 0; si < analog->num_samples; si++) { + /* For now invert all values in all channels. */ + for (l = analog->channels, c = 0; l; l = l->next, c++) { + ch = l->data; + (void)ch; + f = &fdata[si * num_channels + c]; + *f = 1.0 / *f; + } + } + break; + default: + sr_spew("Unsupported packet type %d, ignoring.", packet_in->type); + break; + } + + /* Return the in-place-modified packet. */ + *packet_out = packet_in; + + return SR_OK; +} + +SR_PRIV struct sr_transform_module transform_invert = { + .id = "invert", + .name = "Invert", + .desc = "Invert values", + .options = NULL, + .init = NULL, + .receive = receive, + .cleanup = NULL, +}; diff --git a/src/transform/transform.c b/src/transform/transform.c index f4166dde..3a422597 100644 --- a/src/transform/transform.c +++ b/src/transform/transform.c @@ -41,11 +41,13 @@ /** @cond PRIVATE */ extern SR_PRIV struct sr_transform_module transform_nop; extern SR_PRIV struct sr_transform_module transform_scale; +extern SR_PRIV struct sr_transform_module transform_invert; /* @endcond */ static const struct sr_transform_module *transform_module_list[] = { &transform_nop, &transform_scale, + &transform_invert, NULL, };