]> sigrok.org Git - libsigrok.git/blob - src/output/analog.c
45df4003b8d17bc92d8fb75bb4e04d7ae1d9c8cc
[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         const struct sr_datafeed_meta *meta;
79         const struct sr_config *src;
80         const struct sr_key_info *srci;
81         struct sr_channel *ch;
82         GSList *l;
83         float *fdata;
84         unsigned int i;
85         int num_channels, c, ret, digits, actual_digits;
86         char *number, *suffix;
87
88         *out = NULL;
89         if (!o || !o->sdi)
90                 return SR_ERR_ARG;
91         ctx = o->priv;
92
93         switch (packet->type) {
94         case SR_DF_FRAME_BEGIN:
95                 *out = g_string_new("FRAME-BEGIN\n");
96                 break;
97         case SR_DF_FRAME_END:
98                 *out = g_string_new("FRAME-END\n");
99                 break;
100         case SR_DF_META:
101                 meta = packet->payload;
102                 for (l = meta->config; l; l = l->next) {
103                         src = l->data;
104                         if (!(srci = sr_key_info_get(SR_KEY_CONFIG, src->key)))
105                                 return SR_ERR;
106                         *out = g_string_sized_new(512);
107                         g_string_append(*out, "META ");
108                         g_string_append_printf(*out, "%s: ", srci->id);
109                         if (srci->datatype == SR_T_BOOL) {
110                                 g_string_append_printf(*out, "%u",
111                                         g_variant_get_boolean(src->data));
112                         } else if (srci->datatype == SR_T_FLOAT) {
113                                 g_string_append_printf(*out, "%f",
114                                         g_variant_get_double(src->data));
115                         } else if (srci->datatype == SR_T_UINT64) {
116                                 g_string_append_printf(*out, "%"
117                                         G_GUINT64_FORMAT,
118                                         g_variant_get_uint64(src->data));
119                         }
120                         g_string_append(*out, "\n");
121                 }
122                 break;
123         case SR_DF_ANALOG:
124                 analog = packet->payload;
125                 num_channels = g_slist_length(analog->meaning->channels);
126                 if (!(fdata = g_try_realloc(ctx->fdata,
127                                                 analog->num_samples * num_channels * sizeof(float))))
128                         return SR_ERR_MALLOC;
129                 ctx->fdata = fdata;
130                 if ((ret = sr_analog_to_float(analog, fdata)) != SR_OK)
131                         return ret;
132                 *out = g_string_sized_new(512);
133                 if (analog->encoding->is_digits_decimal) {
134                         if (ctx->digits == DIGITS_ALL)
135                                 digits = analog->encoding->digits;
136                         else
137                                 digits = analog->spec->spec_digits;
138                 } else {
139                         /* TODO we don't know how to print by number of bits yet. */
140                         digits = 6;
141                 }
142                 gboolean si_friendly = sr_analog_si_prefix_friendly(analog->meaning->unit);
143                 sr_analog_unit_to_string(analog, &suffix);
144                 for (i = 0; i < analog->num_samples; i++) {
145                         for (l = analog->meaning->channels, c = 0; l; l = l->next, c++) {
146                                 float value = fdata[i * num_channels + c];
147                                 const char *prefix = "";
148                                 actual_digits = digits;
149                                 if (si_friendly)
150                                         prefix = sr_analog_si_prefix(&value, &actual_digits);
151                                 ch = l->data;
152                                 g_string_append_printf(*out, "%s: ", ch->name);
153                                 number = g_strdup_printf("%.*f", MAX(actual_digits, 0), value);
154                                 g_string_append(*out, number);
155                                 g_free(number);
156                                 g_string_append(*out, " ");
157                                 g_string_append(*out, prefix);
158                                 g_string_append(*out, suffix);
159                                 g_string_append(*out, "\n");
160                         }
161                 }
162                 g_free(suffix);
163                 break;
164         }
165
166         return SR_OK;
167 }
168
169 static struct sr_option options[] = {
170         { "digits", "Digits", "Digits to show", NULL, NULL },
171         ALL_ZERO
172 };
173
174 static const struct sr_option *get_options(void)
175 {
176         if (!options[0].def) {
177                 options[0].def = g_variant_ref_sink(g_variant_new_string("all"));
178                 options[0].values = g_slist_append(options[0].values,
179                                 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("spec")));
182         }
183
184         return options;
185 }
186
187 static int cleanup(struct sr_output *o)
188 {
189         struct context *ctx;
190
191         if (!o || !o->sdi)
192                 return SR_ERR_ARG;
193         ctx = o->priv;
194
195         g_ptr_array_free(ctx->channellist, 1);
196         g_variant_unref(options[0].def);
197         g_slist_free_full(options[0].values, (GDestroyNotify)g_variant_unref);
198         g_free(ctx->fdata);
199         g_free(ctx);
200         o->priv = NULL;
201
202         return SR_OK;
203 }
204
205 SR_PRIV struct sr_output_module output_analog = {
206         .id = "analog",
207         .name = "Analog",
208         .desc = "Analog data and types",
209         .exts = NULL,
210         .flags = 0,
211         .options = get_options,
212         .init = init,
213         .receive = receive,
214         .cleanup = cleanup
215 };