]> sigrok.org Git - libsigrok.git/blame - src/output/analog.c
uni-t-ut181a: silence compiler warning, use of uninitialized variable
[libsigrok.git] / src / output / analog.c
CommitLineData
161a8a27 1/*
50985c20 2 * This file is part of the libsigrok project.
161a8a27
BV
3 *
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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 3 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
6ec6c43b 20#include <config.h>
161a8a27
BV
21#include <stdlib.h>
22#include <string.h>
a944a84b 23#include <math.h>
161a8a27 24#include <glib.h>
c1aae900 25#include <libsigrok/libsigrok.h>
161a8a27 26#include "libsigrok-internal.h"
a944a84b 27
3544f848 28#define LOG_PREFIX "output/analog"
161a8a27 29
cd1e7fd2
ML
30#define BIN_TO_DEC_DIGITS (log(2) / log(10))
31
161a8a27 32struct context {
ba7dd8bb
UH
33 int num_enabled_channels;
34 GPtrArray *channellist;
820c48f8 35 int digits;
2dbe445d 36 float *fdata;
820c48f8
BV
37};
38
39enum {
40 DIGITS_ALL,
41 DIGITS_SPEC,
161a8a27
BV
42};
43
a755b0e1 44static int init(struct sr_output *o, GHashTable *options)
161a8a27
BV
45{
46 struct context *ctx;
ba7dd8bb 47 struct sr_channel *ch;
161a8a27 48 GSList *l;
820c48f8 49 const char *s;
a944a84b 50
161a8a27
BV
51 if (!o || !o->sdi)
52 return SR_ERR_ARG;
53
91219afc 54 o->priv = ctx = g_malloc0(sizeof(struct context));
820c48f8
BV
55 s = g_variant_get_string(g_hash_table_lookup(options, "digits"), NULL);
56 if (!strcmp(s, "all"))
57 ctx->digits = DIGITS_ALL;
58 else
59 ctx->digits = DIGITS_SPEC;
161a8a27 60
ba7dd8bb
UH
61 /* Get the number of channels and their names. */
62 ctx->channellist = g_ptr_array_new();
63 for (l = o->sdi->channels; l; l = l->next) {
64 ch = l->data;
65 if (!ch || !ch->enabled)
161a8a27 66 continue;
ba7dd8bb
UH
67 g_ptr_array_add(ctx->channellist, ch->name);
68 ctx->num_enabled_channels++;
161a8a27 69 }
2dbe445d 70 ctx->fdata = NULL;
161a8a27 71
161a8a27
BV
72 return SR_OK;
73}
74
a755b0e1 75static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
dba3e682 76 GString **out)
161a8a27 77{
820c48f8 78 struct context *ctx;
edb691fc 79 const struct sr_datafeed_analog *analog;
688e44ae
FS
80 const struct sr_datafeed_meta *meta;
81 const struct sr_config *src;
82 const struct sr_key_info *srci;
ba7dd8bb 83 struct sr_channel *ch;
69e19dd7 84 GSList *l;
e02e9e6a
BV
85 float *fdata;
86 unsigned int i;
6ca578fe 87 int num_channels, c, ret, digits, actual_digits;
a24da9a8 88 char *number, *suffix;
161a8a27 89
17f63de6 90 *out = NULL;
161a8a27 91 if (!o || !o->sdi)
17f63de6 92 return SR_ERR_ARG;
820c48f8 93 ctx = o->priv;
161a8a27 94
161a8a27 95 switch (packet->type) {
161a8a27 96 case SR_DF_FRAME_BEGIN:
17f63de6 97 *out = g_string_new("FRAME-BEGIN\n");
161a8a27
BV
98 break;
99 case SR_DF_FRAME_END:
17f63de6 100 *out = g_string_new("FRAME-END\n");
161a8a27 101 break;
688e44ae
FS
102 case SR_DF_META:
103 meta = packet->payload;
104 for (l = meta->config; l; l = l->next) {
105 src = l->data;
106 if (!(srci = sr_key_info_get(SR_KEY_CONFIG, src->key)))
107 return SR_ERR;
108 *out = g_string_sized_new(512);
109 g_string_append(*out, "META ");
110 g_string_append_printf(*out, "%s: ", srci->id);
111 if (srci->datatype == SR_T_BOOL) {
112 g_string_append_printf(*out, "%u",
113 g_variant_get_boolean(src->data));
114 } else if (srci->datatype == SR_T_FLOAT) {
115 g_string_append_printf(*out, "%f",
116 g_variant_get_double(src->data));
117 } else if (srci->datatype == SR_T_UINT64) {
374b0a94
UH
118 g_string_append_printf(*out, "%"
119 G_GUINT64_FORMAT,
688e44ae 120 g_variant_get_uint64(src->data));
e86cc12b
GS
121 } else if (srci->datatype == SR_T_STRING) {
122 g_string_append_printf(*out, "%s",
123 g_variant_get_string(src->data, NULL));
688e44ae
FS
124 }
125 g_string_append(*out, "\n");
126 }
127 break;
edb691fc
UH
128 case SR_DF_ANALOG:
129 analog = packet->payload;
130 num_channels = g_slist_length(analog->meaning->channels);
2dbe445d 131 if (!(fdata = g_try_realloc(ctx->fdata,
edb691fc 132 analog->num_samples * num_channels * sizeof(float))))
e02e9e6a 133 return SR_ERR_MALLOC;
2dbe445d 134 ctx->fdata = fdata;
edb691fc 135 if ((ret = sr_analog_to_float(analog, fdata)) != SR_OK)
e02e9e6a
BV
136 return ret;
137 *out = g_string_sized_new(512);
cd1e7fd2
ML
138 if (ctx->digits == DIGITS_ALL)
139 digits = analog->encoding->digits;
140 else
141 digits = analog->spec->spec_digits;
142 if (!analog->encoding->is_digits_decimal)
143 digits = copysign(ceil(abs(digits) * BIN_TO_DEC_DIGITS), digits);
5728718b 144 gboolean si_friendly = sr_analog_si_prefix_friendly(analog->meaning->unit);
edb691fc
UH
145 sr_analog_unit_to_string(analog, &suffix);
146 for (i = 0; i < analog->num_samples; i++) {
147 for (l = analog->meaning->channels, c = 0; l; l = l->next, c++) {
962172e4 148 float value = fdata[i * num_channels + c];
5728718b 149 const char *prefix = "";
6ca578fe 150 actual_digits = digits;
5728718b 151 if (si_friendly)
6ca578fe 152 prefix = sr_analog_si_prefix(&value, &actual_digits);
e02e9e6a
BV
153 ch = l->data;
154 g_string_append_printf(*out, "%s: ", ch->name);
6ca578fe 155 number = g_strdup_printf("%.*f", MAX(actual_digits, 0), value);
820c48f8 156 g_string_append(*out, number);
a24da9a8 157 g_free(number);
820c48f8 158 g_string_append(*out, " ");
962172e4 159 g_string_append(*out, prefix);
820c48f8
BV
160 g_string_append(*out, suffix);
161 g_string_append(*out, "\n");
161a8a27
BV
162 }
163 }
a24da9a8 164 g_free(suffix);
161a8a27
BV
165 break;
166 }
167
17f63de6 168 return SR_OK;
161a8a27
BV
169}
170
820c48f8
BV
171static struct sr_option options[] = {
172 { "digits", "Digits", "Digits to show", NULL, NULL },
173 ALL_ZERO
174};
175
176static const struct sr_option *get_options(void)
177{
178 if (!options[0].def) {
179 options[0].def = g_variant_ref_sink(g_variant_new_string("all"));
180 options[0].values = g_slist_append(options[0].values,
181 g_variant_ref_sink(g_variant_new_string("all")));
182 options[0].values = g_slist_append(options[0].values,
183 g_variant_ref_sink(g_variant_new_string("spec")));
184 }
185
186 return options;
187}
188
da3d141f
SB
189static int cleanup(struct sr_output *o)
190{
191 struct context *ctx;
192
193 if (!o || !o->sdi)
194 return SR_ERR_ARG;
195 ctx = o->priv;
196
197 g_ptr_array_free(ctx->channellist, 1);
588295b8 198 if (options[0].def) {
199 g_variant_unref(options[0].def);
200 options[0].def = NULL;
201 }
202 if (options[0].values) {
203 g_slist_free_full(options[0].values, (GDestroyNotify)g_variant_unref);
204 options[0].values = NULL;
205 }
da3d141f
SB
206 g_free(ctx->fdata);
207 g_free(ctx);
208 o->priv = NULL;
209
210 return SR_OK;
211}
212
a755b0e1 213SR_PRIV struct sr_output_module output_analog = {
161a8a27 214 .id = "analog",
a755b0e1 215 .name = "Analog",
b20eb520 216 .desc = "ASCII analog data values and units",
8a174d23 217 .exts = NULL,
3cd4b381 218 .flags = 0,
820c48f8 219 .options = get_options,
161a8a27 220 .init = init,
17f63de6 221 .receive = receive,
161a8a27
BV
222 .cleanup = cleanup
223};