]>
Commit | Line | Data |
---|---|---|
43caa466 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 | |
2ea1fdf1 | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
43caa466 UH |
18 | */ |
19 | ||
6ec6c43b | 20 | #include <config.h> |
43caa466 | 21 | #include <string.h> |
c1aae900 | 22 | #include <libsigrok/libsigrok.h> |
43caa466 UH |
23 | #include "libsigrok-internal.h" |
24 | ||
25 | #define LOG_PREFIX "transform/scale" | |
26 | ||
27 | struct context { | |
2d237f3c | 28 | struct sr_rational factor; |
43caa466 UH |
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 | ||
2d237f3c ML |
40 | g_variant_get(g_hash_table_lookup(options, "factor"), "(xt)", |
41 | &ctx->factor.p, &ctx->factor.q); | |
43caa466 UH |
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; | |
edb691fc | 51 | const struct sr_datafeed_analog *analog; |
43caa466 UH |
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) { | |
edb691fc UH |
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; | |
b1aa4f34 | 62 | break; |
43caa466 UH |
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 | { | |
2d237f3c ML |
95 | int64_t p = 1; |
96 | uint64_t q = 1; | |
97 | ||
43caa466 UH |
98 | /* Default to a scaling factor of 1.0. */ |
99 | if (!options[0].def) | |
a73665a3 | 100 | options[0].def = g_variant_ref_sink(g_variant_new("(xt)", &p, &q)); |
43caa466 UH |
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 | }; |