]> sigrok.org Git - libsigrok.git/blob - src/output/analog.c
4018ec888659f797231f48fbca4d97eb2df1be62
[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 };
35
36 enum {
37         DIGITS_ALL,
38         DIGITS_SPEC,
39 };
40
41 static int init(struct sr_output *o, GHashTable *options)
42 {
43         struct context *ctx;
44         struct sr_channel *ch;
45         GSList *l;
46         const char *s;
47
48         if (!o || !o->sdi)
49                 return SR_ERR_ARG;
50
51         o->priv = ctx = g_malloc0(sizeof(struct context));
52         s = g_variant_get_string(g_hash_table_lookup(options, "digits"), NULL);
53         if (!strcmp(s, "all"))
54                 ctx->digits = DIGITS_ALL;
55         else
56                 ctx->digits = DIGITS_SPEC;
57
58         /* Get the number of channels and their names. */
59         ctx->channellist = g_ptr_array_new();
60         for (l = o->sdi->channels; l; l = l->next) {
61                 ch = l->data;
62                 if (!ch || !ch->enabled)
63                         continue;
64                 g_ptr_array_add(ctx->channellist, ch->name);
65                 ctx->num_enabled_channels++;
66         }
67
68         return SR_OK;
69 }
70
71 static void si_printf(float value, GString *out, char *unitstr)
72 {
73         float v;
74
75         if (signbit(value))
76                 v = -(value);
77         else
78                 v = value;
79
80         if (v < 1e-12 || v > 1e+12)
81                 g_string_append_printf(out, "%f %s", value, unitstr);
82         else if (v > 1e+9)
83                 g_string_append_printf(out, "%f G%s", value / 1e+9, unitstr);
84         else if (v > 1e+6)
85                 g_string_append_printf(out, "%f M%s", value / 1e+6, unitstr);
86         else if (v > 1e+3)
87                 g_string_append_printf(out, "%f k%s", value / 1e+3, unitstr);
88         else if (v < 1e-9)
89                 g_string_append_printf(out, "%f n%s", value * 1e+9, unitstr);
90         else if (v < 1e-6)
91                 g_string_append_printf(out, "%f u%s", value * 1e+6, unitstr);
92         else if (v < 1e-3)
93                 g_string_append_printf(out, "%f m%s", value * 1e+3, unitstr);
94         else
95                 g_string_append_printf(out, "%f %s", value, unitstr);
96
97 }
98
99 /* Please use the same order as in enum sr_unit (libsigrok.h). */
100 static void fancyprint(int unit, int mqflags, float value, GString *out)
101 {
102         switch (unit) {
103         case SR_UNIT_VOLT:
104                 si_printf(value, out, "V");
105                 break;
106         case SR_UNIT_AMPERE:
107                 si_printf(value, out, "A");
108                 break;
109         case SR_UNIT_OHM:
110                 si_printf(value, out, "");
111                 g_string_append_unichar(out, 0x2126);
112                 break;
113         case SR_UNIT_FARAD:
114                 si_printf(value, out, "F");
115                 break;
116         case SR_UNIT_KELVIN:
117                 si_printf(value, out, "K");
118                 break;
119         case SR_UNIT_CELSIUS:
120                 si_printf(value, out, "");
121                 g_string_append_unichar(out, 0x00b0);
122                 g_string_append_c(out, 'C');
123                 break;
124         case SR_UNIT_FAHRENHEIT:
125                 si_printf(value, out, "");
126                 g_string_append_unichar(out, 0x00b0);
127                 g_string_append_c(out, 'F');
128                 break;
129         case SR_UNIT_HERTZ:
130                 si_printf(value, out, "Hz");
131                 break;
132         case SR_UNIT_PERCENTAGE:
133                 g_string_append_printf(out, "%f %%", value);
134                 break;
135         case SR_UNIT_BOOLEAN:
136                 if (value > 0)
137                         g_string_append_printf(out, "TRUE");
138                 else
139                         g_string_append_printf(out, "FALSE");
140                 break;
141         case SR_UNIT_SECOND:
142                 si_printf(value, out, "s");
143                 break;
144         case SR_UNIT_SIEMENS:
145                 si_printf(value, out, "S");
146                 break;
147         case SR_UNIT_DECIBEL_MW:
148                 si_printf(value, out, "dBu");
149                 break;
150         case SR_UNIT_DECIBEL_VOLT:
151                 si_printf(value, out, "dBV");
152                 break;
153         case SR_UNIT_UNITLESS:
154                 si_printf(value, out, "");
155                 break;
156         case SR_UNIT_DECIBEL_SPL:
157                 if (mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_A)
158                         si_printf(value, out, "dB(A)");
159                 else if (mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_C)
160                         si_printf(value, out, "dB(C)");
161                 else if (mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_Z)
162                         si_printf(value, out, "dB(Z)");
163                 else
164                         /* No frequency weighting, or non-standard "flat" */
165                         si_printf(value, out, "dB(SPL)");
166                 if (mqflags & SR_MQFLAG_SPL_TIME_WEIGHT_S)
167                         g_string_append(out, " S");
168                 else if (mqflags & SR_MQFLAG_SPL_TIME_WEIGHT_F)
169                         g_string_append(out, " F");
170                 if (mqflags & SR_MQFLAG_SPL_LAT)
171                         g_string_append(out, " LAT");
172                 else if (mqflags & SR_MQFLAG_SPL_PCT_OVER_ALARM)
173                         /* Not a standard function for SLMs, so this is
174                          * a made-up notation. */
175                         g_string_append(out, " %oA");
176                 break;
177         case SR_UNIT_CONCENTRATION:
178                 g_string_append_printf(out, "%f ppm", value * (1000 * 1000));
179                 break;
180         case SR_UNIT_REVOLUTIONS_PER_MINUTE:
181                 si_printf(value, out, "RPM");
182                 break;
183         case SR_UNIT_VOLT_AMPERE:
184                 si_printf(value, out, "VA");
185                 break;
186         case SR_UNIT_WATT:
187                 si_printf(value, out, "W");
188                 break;
189         case SR_UNIT_WATT_HOUR:
190                 si_printf(value, out, "Wh");
191                 break;
192         case SR_UNIT_METER_SECOND:
193                 si_printf(value, out, "m/s");
194                 break;
195         case SR_UNIT_HECTOPASCAL:
196                 si_printf(value, out, "hPa");
197                 break;
198         case SR_UNIT_HUMIDITY_293K:
199                 si_printf(value, out, "%rF");
200                 break;
201         case SR_UNIT_DEGREE:
202                 si_printf(value, out, "");
203                 g_string_append_unichar(out, 0x00b0);
204                 break;
205         case SR_UNIT_HENRY:
206                 si_printf(value, out, "H");
207                 break;
208         case SR_UNIT_GRAM:
209                 si_printf(value, out, "g");
210                 break;
211         case SR_UNIT_CARAT:
212                 si_printf(value, out, "ct");
213                 break;
214         case SR_UNIT_OUNCE:
215                 si_printf(value, out, "oz");
216                 break;
217         case SR_UNIT_TROY_OUNCE:
218                 si_printf(value, out, "oz t");
219                 break;
220         case SR_UNIT_POUND:
221                 si_printf(value, out, "lb");
222                 break;
223         case SR_UNIT_PENNYWEIGHT:
224                 si_printf(value, out, "dwt");
225                 break;
226         case SR_UNIT_GRAIN:
227                 si_printf(value, out, "gr");
228                 break;
229         case SR_UNIT_TAEL:
230                 si_printf(value, out, "tael");
231                 break;
232         case SR_UNIT_MOMME:
233                 si_printf(value, out, "momme");
234                 break;
235         case SR_UNIT_TOLA:
236                 si_printf(value, out, "tola");
237                 break;
238         case SR_UNIT_PIECE:
239                 si_printf(value, out, "pcs");
240                 break;
241         default:
242                 si_printf(value, out, "");
243                 break;
244         }
245
246         /* Please use the same order as in enum sr_mqflag (libsigrok.h). */
247         if (mqflags & SR_MQFLAG_AC)
248                 g_string_append_printf(out, " AC");
249         if (mqflags & SR_MQFLAG_DC)
250                 g_string_append_printf(out, " DC");
251         if (mqflags & SR_MQFLAG_RMS)
252                 g_string_append_printf(out, " RMS");
253         if (mqflags & SR_MQFLAG_DIODE)
254                 g_string_append_printf(out, " DIODE");
255         if (mqflags & SR_MQFLAG_HOLD)
256                 g_string_append_printf(out, " HOLD");
257         if (mqflags & SR_MQFLAG_MAX)
258                 g_string_append_printf(out, " MAX");
259         if (mqflags & SR_MQFLAG_MIN)
260                 g_string_append_printf(out, " MIN");
261         if (mqflags & SR_MQFLAG_AUTORANGE)
262                 g_string_append_printf(out, " AUTO");
263         if (mqflags & SR_MQFLAG_RELATIVE)
264                 g_string_append_printf(out, " REL");
265         /* Note: SR_MQFLAG_SPL_* is handled above. */
266         if (mqflags & SR_MQFLAG_DURATION)
267                 g_string_append_printf(out, " DURATION");
268         if (mqflags & SR_MQFLAG_AVG)
269                 g_string_append_printf(out, " AVG");
270         if (mqflags & SR_MQFLAG_REFERENCE)
271                 g_string_append_printf(out, " REF");
272         if (mqflags & SR_MQFLAG_UNSTABLE)
273                 g_string_append_printf(out, " UNSTABLE");
274         g_string_append_c(out, '\n');
275 }
276
277 static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
278                 GString **out)
279 {
280         struct context *ctx;
281         const struct sr_datafeed_analog *analog;
282         const struct sr_datafeed_analog2 *analog2;
283         struct sr_channel *ch;
284         GSList *l;
285         float *fdata;
286         unsigned int i;
287         int num_channels, c, ret, si, digits;
288         char *number, *suffix;
289
290         *out = NULL;
291         if (!o || !o->sdi)
292                 return SR_ERR_ARG;
293         ctx = o->priv;
294
295         switch (packet->type) {
296         case SR_DF_FRAME_BEGIN:
297                 *out = g_string_new("FRAME-BEGIN\n");
298                 break;
299         case SR_DF_FRAME_END:
300                 *out = g_string_new("FRAME-END\n");
301                 break;
302         case SR_DF_ANALOG:
303                 analog = packet->payload;
304                 fdata = (float *)analog->data;
305                 *out = g_string_sized_new(512);
306                 num_channels = g_slist_length(analog->channels);
307                 for (si = 0; si < analog->num_samples; si++) {
308                         for (l = analog->channels, c = 0; l; l = l->next, c++) {
309                                 ch = l->data;
310                                 g_string_append_printf(*out, "%s: ", ch->name);
311                                 fancyprint(analog->unit, analog->mqflags,
312                                                 fdata[si * num_channels + c], *out);
313                         }
314                 }
315                 break;
316         case SR_DF_ANALOG2:
317                 analog2 = packet->payload;
318                 num_channels = g_slist_length(analog2->meaning->channels);
319                 if (!(fdata = g_try_malloc(
320                                                 analog2->num_samples * num_channels * sizeof(float))))
321                         return SR_ERR_MALLOC;
322                 if ((ret = sr_analog_to_float(analog2, fdata)) != SR_OK)
323                         return ret;
324                 *out = g_string_sized_new(512);
325                 if (analog2->encoding->is_digits_decimal) {
326                         if (ctx->digits == DIGITS_ALL)
327                                 digits = analog2->encoding->digits;
328                         else
329                                 digits = analog2->spec->spec_digits;
330                 } else {
331                         /* TODO we don't know how to print by number of bits yet. */
332                         digits = 6;
333                 }
334                 sr_analog_unit_to_string(analog2, &suffix);
335                 for (i = 0; i < analog2->num_samples; i++) {
336                         for (l = analog2->meaning->channels, c = 0; l; l = l->next, c++) {
337                                 ch = l->data;
338                                 g_string_append_printf(*out, "%s: ", ch->name);
339                                 sr_analog_float_to_string(fdata[i * num_channels + c],
340                                                 digits, &number);
341                                 g_string_append(*out, number);
342                                 g_free(number);
343                                 g_string_append(*out, " ");
344                                 g_string_append(*out, suffix);
345                                 g_string_append(*out, "\n");
346                         }
347                 }
348                 g_free(suffix);
349                 break;
350         }
351
352         return SR_OK;
353 }
354
355 static int cleanup(struct sr_output *o)
356 {
357         struct context *ctx;
358
359         if (!o || !o->sdi)
360                 return SR_ERR_ARG;
361         ctx = o->priv;
362
363         g_ptr_array_free(ctx->channellist, 1);
364         g_free(ctx);
365         o->priv = NULL;
366
367         return SR_OK;
368 }
369
370 static struct sr_option options[] = {
371         { "digits", "Digits", "Digits to show", NULL, NULL },
372         ALL_ZERO
373 };
374
375 static const struct sr_option *get_options(void)
376 {
377         if (!options[0].def) {
378                 options[0].def = g_variant_ref_sink(g_variant_new_string("all"));
379                 options[0].values = g_slist_append(options[0].values,
380                                 g_variant_ref_sink(g_variant_new_string("all")));
381                 options[0].values = g_slist_append(options[0].values,
382                                 g_variant_ref_sink(g_variant_new_string("spec")));
383         }
384
385         return options;
386 }
387
388 SR_PRIV struct sr_output_module output_analog = {
389         .id = "analog",
390         .name = "Analog",
391         .desc = "Analog data and types",
392         .exts = NULL,
393         .flags = 0,
394         .options = get_options,
395         .init = init,
396         .receive = receive,
397         .cleanup = cleanup
398 };