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).
This commit is contained in:
Uwe Hermann 2015-02-11 09:34:38 +01:00
parent 43caa46623
commit d74d30bb14
3 changed files with 91 additions and 1 deletions

View File

@ -72,7 +72,8 @@ libsigrok_la_SOURCES += \
libsigrok_la_SOURCES += \ libsigrok_la_SOURCES += \
src/transform/transform.c \ src/transform/transform.c \
src/transform/nop.c \ src/transform/nop.c \
src/transform/scale.c src/transform/scale.c \
src/transform/invert.c
# SCPI support # SCPI support
libsigrok_la_SOURCES += \ libsigrok_la_SOURCES += \

87
src/transform/invert.c Normal file
View File

@ -0,0 +1,87 @@
/*
* This file is part of the libsigrok project.
*
* Copyright (C) 2015 Uwe Hermann <uwe@hermann-uwe.de>
*
* 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 <string.h>
#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,
};

View File

@ -41,11 +41,13 @@
/** @cond PRIVATE */ /** @cond PRIVATE */
extern SR_PRIV struct sr_transform_module transform_nop; 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_scale;
extern SR_PRIV struct sr_transform_module transform_invert;
/* @endcond */ /* @endcond */
static const struct sr_transform_module *transform_module_list[] = { static const struct sr_transform_module *transform_module_list[] = {
&transform_nop, &transform_nop,
&transform_scale, &transform_scale,
&transform_invert,
NULL, NULL,
}; };