]> sigrok.org Git - libsigrok.git/blob - src/output/analog.c
output/analog: Drop support for SR_DF_ANALOG_OLD.
[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 *analog;
286         struct sr_channel *ch;
287         GSList *l;
288         float *fdata;
289         unsigned int i;
290         int num_channels, c, ret, digits;
291         char *number, *suffix;
292
293         *out = NULL;
294         if (!o || !o->sdi)
295                 return SR_ERR_ARG;
296         ctx = o->priv;
297
298         switch (packet->type) {
299         case SR_DF_FRAME_BEGIN:
300                 *out = g_string_new("FRAME-BEGIN\n");
301                 break;
302         case SR_DF_FRAME_END:
303                 *out = g_string_new("FRAME-END\n");
304                 break;
305         case SR_DF_ANALOG:
306                 analog = packet->payload;
307                 num_channels = g_slist_length(analog->meaning->channels);
308                 if (!(fdata = g_try_realloc(ctx->fdata,
309                                                 analog->num_samples * num_channels * sizeof(float))))
310                         return SR_ERR_MALLOC;
311                 ctx->fdata = fdata;
312                 if ((ret = sr_analog_to_float(analog, fdata)) != SR_OK)
313                         return ret;
314                 *out = g_string_sized_new(512);
315                 if (analog->encoding->is_digits_decimal) {
316                         if (ctx->digits == DIGITS_ALL)
317                                 digits = analog->encoding->digits;
318                         else
319                                 digits = analog->spec->spec_digits;
320                 } else {
321                         /* TODO we don't know how to print by number of bits yet. */
322                         digits = 6;
323                 }
324                 sr_analog_unit_to_string(analog, &suffix);
325                 for (i = 0; i < analog->num_samples; i++) {
326                         for (l = analog->meaning->channels, c = 0; l; l = l->next, c++) {
327                                 ch = l->data;
328                                 g_string_append_printf(*out, "%s: ", ch->name);
329                                 number = g_strdup_printf("%.*f", digits,
330                                                 fdata[i * num_channels + c]);
331                                 g_string_append(*out, number);
332                                 g_free(number);
333                                 g_string_append(*out, " ");
334                                 g_string_append(*out, suffix);
335                                 g_string_append(*out, "\n");
336                         }
337                 }
338                 g_free(suffix);
339                 break;
340         }
341
342         return SR_OK;
343 }
344
345 static struct sr_option options[] = {
346         { "digits", "Digits", "Digits to show", NULL, NULL },
347         ALL_ZERO
348 };
349
350 static const struct sr_option *get_options(void)
351 {
352         if (!options[0].def) {
353                 options[0].def = g_variant_ref_sink(g_variant_new_string("all"));
354                 options[0].values = g_slist_append(options[0].values,
355                                 g_variant_ref_sink(g_variant_new_string("all")));
356                 options[0].values = g_slist_append(options[0].values,
357                                 g_variant_ref_sink(g_variant_new_string("spec")));
358         }
359
360         return options;
361 }
362
363 static int cleanup(struct sr_output *o)
364 {
365         struct context *ctx;
366
367         if (!o || !o->sdi)
368                 return SR_ERR_ARG;
369         ctx = o->priv;
370
371         g_ptr_array_free(ctx->channellist, 1);
372         g_variant_unref(options[0].def);
373         g_slist_free_full(options[0].values, (GDestroyNotify)g_variant_unref);
374         g_free(ctx->fdata);
375         g_free(ctx);
376         o->priv = NULL;
377
378         return SR_OK;
379 }
380
381 SR_PRIV struct sr_output_module output_analog = {
382         .id = "analog",
383         .name = "Analog",
384         .desc = "Analog data and types",
385         .exts = NULL,
386         .flags = 0,
387         .options = get_options,
388         .init = init,
389         .receive = receive,
390         .cleanup = cleanup
391 };