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