2 * This file is part of the libsigrok project.
4 * Copyright (C) 2015 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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/>.
22 #include <libsigrok/libsigrok.h>
23 #include "libsigrok-internal.h"
25 #define LOG_PREFIX "transform/scale"
28 struct sr_rational factor;
31 static int init(struct sr_transform *t, GHashTable *options)
35 if (!t || !t->sdi || !options)
38 t->priv = ctx = g_malloc0(sizeof(struct context));
40 g_variant_get(g_hash_table_lookup(options, "factor"), "(xt)",
41 &ctx->factor.p, &ctx->factor.q);
46 static int receive(const struct sr_transform *t,
47 struct sr_datafeed_packet *packet_in,
48 struct sr_datafeed_packet **packet_out)
51 const struct sr_datafeed_analog *analog;
53 if (!t || !t->sdi || !packet_in || !packet_out)
57 switch (packet_in->type) {
59 analog = packet_in->payload;
60 analog->encoding->scale.p *= ctx->factor.p;
61 analog->encoding->scale.q *= ctx->factor.q;
64 sr_spew("Unsupported packet type %d, ignoring.", packet_in->type);
68 /* Return the in-place-modified packet. */
69 *packet_out = packet_in;
74 static int cleanup(struct sr_transform *t)
88 static struct sr_option options[] = {
89 { "factor", "Factor", "Factor by which to scale the analog values", NULL, NULL },
93 static const struct sr_option *get_options(void)
98 /* Default to a scaling factor of 1.0. */
100 options[0].def = g_variant_ref_sink(g_variant_new("(xt)", &p, &q));
105 SR_PRIV struct sr_transform_module transform_scale = {
108 .desc = "Scale analog values by a specified factor",
109 .options = get_options,