]> sigrok.org Git - libsigrok.git/blob - src/output/analog.c
9ff77da4b765cf7ae7d8ea4af9b42dd66b3fdf71
[libsigrok.git] / src / output / analog.c
1 /*
2  * This file is part of the libsigrok project.
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
20 #include <config.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <math.h>
24 #include <glib.h>
25 #include <libsigrok/libsigrok.h>
26 #include "libsigrok-internal.h"
27
28 #define LOG_PREFIX "output/analog"
29
30 struct context {
31         int num_enabled_channels;
32         GPtrArray *channellist;
33         int digits;
34         float *fdata;
35 };
36
37 enum {
38         DIGITS_ALL,
39         DIGITS_SPEC,
40 };
41
42 static int init(struct sr_output *o, GHashTable *options)
43 {
44         struct context *ctx;
45         struct sr_channel *ch;
46         GSList *l;
47         const char *s;
48
49         if (!o || !o->sdi)
50                 return SR_ERR_ARG;
51
52         o->priv = ctx = g_malloc0(sizeof(struct context));
53         s = g_variant_get_string(g_hash_table_lookup(options, "digits"), NULL);
54         if (!strcmp(s, "all"))
55                 ctx->digits = DIGITS_ALL;
56         else
57                 ctx->digits = DIGITS_SPEC;
58
59         /* Get the number of channels and their names. */
60         ctx->channellist = g_ptr_array_new();
61         for (l = o->sdi->channels; l; l = l->next) {
62                 ch = l->data;
63                 if (!ch || !ch->enabled)
64                         continue;
65                 g_ptr_array_add(ctx->channellist, ch->name);
66                 ctx->num_enabled_channels++;
67         }
68         ctx->fdata = NULL;
69
70         return SR_OK;
71 }
72
73 static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
74                 GString **out)
75 {
76         struct context *ctx;
77         const struct sr_datafeed_analog *analog;
78         struct sr_channel *ch;
79         GSList *l;
80         float *fdata;
81         unsigned int i;
82         int num_channels, c, ret, digits;
83         char *number, *suffix;
84
85         *out = NULL;
86         if (!o || !o->sdi)
87                 return SR_ERR_ARG;
88         ctx = o->priv;
89
90         switch (packet->type) {
91         case SR_DF_FRAME_BEGIN:
92                 *out = g_string_new("FRAME-BEGIN\n");
93                 break;
94         case SR_DF_FRAME_END:
95                 *out = g_string_new("FRAME-END\n");
96                 break;
97         case SR_DF_ANALOG:
98                 analog = packet->payload;
99                 num_channels = g_slist_length(analog->meaning->channels);
100                 if (!(fdata = g_try_realloc(ctx->fdata,
101                                                 analog->num_samples * num_channels * sizeof(float))))
102                         return SR_ERR_MALLOC;
103                 ctx->fdata = fdata;
104                 if ((ret = sr_analog_to_float(analog, fdata)) != SR_OK)
105                         return ret;
106                 *out = g_string_sized_new(512);
107                 if (analog->encoding->is_digits_decimal) {
108                         if (ctx->digits == DIGITS_ALL)
109                                 digits = analog->encoding->digits;
110                         else
111                                 digits = analog->spec->spec_digits;
112                 } else {
113                         /* TODO we don't know how to print by number of bits yet. */
114                         digits = 6;
115                 }
116                 gboolean si_friendly = sr_analog_si_prefix_friendly(analog->meaning->unit);
117                 sr_analog_unit_to_string(analog, &suffix);
118                 for (i = 0; i < analog->num_samples; i++) {
119                         for (l = analog->meaning->channels, c = 0; l; l = l->next, c++) {
120                                 float value = fdata[i * num_channels + c];
121                                 const char *prefix = "";
122                                 if (si_friendly)
123                                         prefix = sr_analog_si_prefix(&value, &digits);
124                                 ch = l->data;
125                                 g_string_append_printf(*out, "%s: ", ch->name);
126                                 number = g_strdup_printf("%.*f", MAX(digits, 0), value);
127                                 g_string_append(*out, number);
128                                 g_free(number);
129                                 g_string_append(*out, " ");
130                                 g_string_append(*out, prefix);
131                                 g_string_append(*out, suffix);
132                                 g_string_append(*out, "\n");
133                         }
134                 }
135                 g_free(suffix);
136                 break;
137         }
138
139         return SR_OK;
140 }
141
142 static struct sr_option options[] = {
143         { "digits", "Digits", "Digits to show", NULL, NULL },
144         ALL_ZERO
145 };
146
147 static const struct sr_option *get_options(void)
148 {
149         if (!options[0].def) {
150                 options[0].def = g_variant_ref_sink(g_variant_new_string("all"));
151                 options[0].values = g_slist_append(options[0].values,
152                                 g_variant_ref_sink(g_variant_new_string("all")));
153                 options[0].values = g_slist_append(options[0].values,
154                                 g_variant_ref_sink(g_variant_new_string("spec")));
155         }
156
157         return options;
158 }
159
160 static int cleanup(struct sr_output *o)
161 {
162         struct context *ctx;
163
164         if (!o || !o->sdi)
165                 return SR_ERR_ARG;
166         ctx = o->priv;
167
168         g_ptr_array_free(ctx->channellist, 1);
169         g_variant_unref(options[0].def);
170         g_slist_free_full(options[0].values, (GDestroyNotify)g_variant_unref);
171         g_free(ctx->fdata);
172         g_free(ctx);
173         o->priv = NULL;
174
175         return SR_OK;
176 }
177
178 SR_PRIV struct sr_output_module output_analog = {
179         .id = "analog",
180         .name = "Analog",
181         .desc = "Analog data and types",
182         .exts = NULL,
183         .flags = 0,
184         .options = get_options,
185         .init = init,
186         .receive = receive,
187         .cleanup = cleanup
188 };