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