]> sigrok.org Git - libsigrok.git/blob - src/transform/scale.c
license: remove FSF postal address from boiler plate license text
[libsigrok.git] / src / transform / scale.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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include <string.h>
22 #include <libsigrok/libsigrok.h>
23 #include "libsigrok-internal.h"
24
25 #define LOG_PREFIX "transform/scale"
26
27 struct context {
28         struct sr_rational factor;
29 };
30
31 static int init(struct sr_transform *t, GHashTable *options)
32 {
33         struct context *ctx;
34
35         if (!t || !t->sdi || !options)
36                 return SR_ERR_ARG;
37
38         t->priv = ctx = g_malloc0(sizeof(struct context));
39
40         g_variant_get(g_hash_table_lookup(options, "factor"), "(xt)",
41                         &ctx->factor.p, &ctx->factor.q);
42
43         return SR_OK;
44 }
45
46 static int receive(const struct sr_transform *t,
47                 struct sr_datafeed_packet *packet_in,
48                 struct sr_datafeed_packet **packet_out)
49 {
50         struct context *ctx;
51         const struct sr_datafeed_analog *analog;
52
53         if (!t || !t->sdi || !packet_in || !packet_out)
54                 return SR_ERR_ARG;
55         ctx = t->priv;
56
57         switch (packet_in->type) {
58         case SR_DF_ANALOG:
59                 analog = packet_in->payload;
60                 analog->encoding->scale.p *= ctx->factor.p;
61                 analog->encoding->scale.q *= ctx->factor.q;
62                 break;
63         default:
64                 sr_spew("Unsupported packet type %d, ignoring.", packet_in->type);
65                 break;
66         }
67
68         /* Return the in-place-modified packet. */
69         *packet_out = packet_in;
70
71         return SR_OK;
72 }
73
74 static int cleanup(struct sr_transform *t)
75 {
76         struct context *ctx;
77
78         if (!t || !t->sdi)
79                 return SR_ERR_ARG;
80         ctx = t->priv;
81
82         g_free(ctx);
83         t->priv = NULL;
84
85         return SR_OK;
86 }
87
88 static struct sr_option options[] = {
89         { "factor", "Factor", "Factor by which to scale the analog values", NULL, NULL },
90         ALL_ZERO
91 };
92
93 static const struct sr_option *get_options(void)
94 {
95         int64_t p = 1;
96         uint64_t q = 1;
97
98         /* Default to a scaling factor of 1.0. */
99         if (!options[0].def)
100                 options[0].def = g_variant_ref_sink(g_variant_new("(xt)", &p, &q));
101
102         return options;
103 }
104
105 SR_PRIV struct sr_transform_module transform_scale = {
106         .id = "scale",
107         .name = "Scale",
108         .desc = "Scale analog values by a specified factor",
109         .options = get_options,
110         .init = init,
111         .receive = receive,
112         .cleanup = cleanup,
113 };