2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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.
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/>.
25 #include <libsigrok/libsigrok.h>
26 #include "libsigrok-internal.h"
28 #define LOG_PREFIX "output/analog"
30 #define BIN_TO_DEC_DIGITS (log(2) / log(10))
33 int num_enabled_channels;
34 GPtrArray *channellist;
44 static int init(struct sr_output *o, GHashTable *options)
47 struct sr_channel *ch;
54 o->priv = ctx = g_malloc0(sizeof(struct context));
55 s = g_variant_get_string(g_hash_table_lookup(options, "digits"), NULL);
56 if (!strcmp(s, "all"))
57 ctx->digits = DIGITS_ALL;
59 ctx->digits = DIGITS_SPEC;
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) {
65 if (!ch || !ch->enabled)
67 g_ptr_array_add(ctx->channellist, ch->name);
68 ctx->num_enabled_channels++;
75 static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
79 const struct sr_datafeed_analog *analog;
80 const struct sr_datafeed_meta *meta;
81 const struct sr_config *src;
82 const struct sr_key_info *srci;
83 struct sr_channel *ch;
87 int num_channels, c, ret, digits, actual_digits;
88 char *number, *suffix;
95 switch (packet->type) {
96 case SR_DF_FRAME_BEGIN:
97 *out = g_string_new("FRAME-BEGIN\n");
100 *out = g_string_new("FRAME-END\n");
103 meta = packet->payload;
104 for (l = meta->config; l; l = l->next) {
106 if (!(srci = sr_key_info_get(SR_KEY_CONFIG, src->key)))
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) {
118 g_string_append_printf(*out, "%"
120 g_variant_get_uint64(src->data));
122 g_string_append(*out, "\n");
126 analog = packet->payload;
127 num_channels = g_slist_length(analog->meaning->channels);
128 if (!(fdata = g_try_realloc(ctx->fdata,
129 analog->num_samples * num_channels * sizeof(float))))
130 return SR_ERR_MALLOC;
132 if ((ret = sr_analog_to_float(analog, fdata)) != SR_OK)
134 *out = g_string_sized_new(512);
135 if (ctx->digits == DIGITS_ALL)
136 digits = analog->encoding->digits;
138 digits = analog->spec->spec_digits;
139 if (!analog->encoding->is_digits_decimal)
140 digits = copysign(ceil(abs(digits) * BIN_TO_DEC_DIGITS), digits);
141 gboolean si_friendly = sr_analog_si_prefix_friendly(analog->meaning->unit);
142 sr_analog_unit_to_string(analog, &suffix);
143 for (i = 0; i < analog->num_samples; i++) {
144 for (l = analog->meaning->channels, c = 0; l; l = l->next, c++) {
145 float value = fdata[i * num_channels + c];
146 const char *prefix = "";
147 actual_digits = digits;
149 prefix = sr_analog_si_prefix(&value, &actual_digits);
151 g_string_append_printf(*out, "%s: ", ch->name);
152 number = g_strdup_printf("%.*f", MAX(actual_digits, 0), value);
153 g_string_append(*out, number);
155 g_string_append(*out, " ");
156 g_string_append(*out, prefix);
157 g_string_append(*out, suffix);
158 g_string_append(*out, "\n");
168 static struct sr_option options[] = {
169 { "digits", "Digits", "Digits to show", NULL, NULL },
173 static const struct sr_option *get_options(void)
175 if (!options[0].def) {
176 options[0].def = g_variant_ref_sink(g_variant_new_string("all"));
177 options[0].values = g_slist_append(options[0].values,
178 g_variant_ref_sink(g_variant_new_string("all")));
179 options[0].values = g_slist_append(options[0].values,
180 g_variant_ref_sink(g_variant_new_string("spec")));
186 static int cleanup(struct sr_output *o)
194 g_ptr_array_free(ctx->channellist, 1);
195 g_variant_unref(options[0].def);
196 g_slist_free_full(options[0].values, (GDestroyNotify)g_variant_unref);
204 SR_PRIV struct sr_output_module output_analog = {
207 .desc = "ASCII analog data values and units",
210 .options = get_options,