]> sigrok.org Git - libsigrok.git/blame - src/output/analog.c
output/analog: track and free memory for float conversion buffer
[libsigrok.git] / src / output / analog.c
CommitLineData
161a8a27 1/*
50985c20 2 * This file is part of the libsigrok project.
161a8a27
BV
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
6ec6c43b 20#include <config.h>
161a8a27
BV
21#include <stdlib.h>
22#include <string.h>
a944a84b 23#include <math.h>
161a8a27 24#include <glib.h>
c1aae900 25#include <libsigrok/libsigrok.h>
161a8a27 26#include "libsigrok-internal.h"
a944a84b 27
3544f848 28#define LOG_PREFIX "output/analog"
161a8a27
BV
29
30struct context {
ba7dd8bb
UH
31 int num_enabled_channels;
32 GPtrArray *channellist;
820c48f8 33 int digits;
2dbe445d 34 float *fdata;
820c48f8
BV
35};
36
37enum {
38 DIGITS_ALL,
39 DIGITS_SPEC,
161a8a27
BV
40};
41
a755b0e1 42static int init(struct sr_output *o, GHashTable *options)
161a8a27
BV
43{
44 struct context *ctx;
ba7dd8bb 45 struct sr_channel *ch;
161a8a27 46 GSList *l;
820c48f8 47 const char *s;
a944a84b 48
161a8a27
BV
49 if (!o || !o->sdi)
50 return SR_ERR_ARG;
51
91219afc 52 o->priv = ctx = g_malloc0(sizeof(struct context));
820c48f8
BV
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;
161a8a27 58
ba7dd8bb
UH
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)
161a8a27 64 continue;
ba7dd8bb
UH
65 g_ptr_array_add(ctx->channellist, ch->name);
66 ctx->num_enabled_channels++;
161a8a27 67 }
2dbe445d 68 ctx->fdata = NULL;
161a8a27 69
161a8a27
BV
70 return SR_OK;
71}
72
2c240774 73static void si_printf(float value, GString *out, const char *unitstr)
161a8a27 74{
d713e561 75 float v;
161a8a27 76
d713e561
BV
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);
161a8a27
BV
96 else
97 g_string_append_printf(out, "%f %s", value, unitstr);
98
99}
100
ca7dbb56 101/* Please use the same order as in enum sr_unit (libsigrok.h). */
161a8a27
BV
102static void fancyprint(int unit, int mqflags, float value, GString *out)
103{
161a8a27 104 switch (unit) {
06c45a66
UH
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:
641d8f27 135 g_string_append_printf(out, "%f %%", value);
06c45a66
UH
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;
c984f2f9
UH
155 case SR_UNIT_UNITLESS:
156 si_printf(value, out, "");
157 break;
06c45a66
UH
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:
1a46cc62 180 g_string_append_printf(out, "%f ppm", value * (1000 * 1000));
06c45a66 181 break;
87532f23
AJ
182 case SR_UNIT_REVOLUTIONS_PER_MINUTE:
183 si_printf(value, out, "RPM");
d69d2642 184 break;
45315d04
AJ
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");
87532f23 193 break;
543d041d
BV
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;
02c7c482
JH
203 case SR_UNIT_DEGREE:
204 si_printf(value, out, "");
205 g_string_append_unichar(out, 0x00b0);
206 break;
c984f2f9
UH
207 case SR_UNIT_HENRY:
208 si_printf(value, out, "H");
209 break;
34eaf4bc
UH
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;
06c45a66
UH
243 default:
244 si_printf(value, out, "");
245 break;
161a8a27 246 }
e6523173 247
ca7dbb56 248 /* Please use the same order as in enum sr_mqflag (libsigrok.h). */
e6523173 249 if (mqflags & SR_MQFLAG_AC)
161a8a27 250 g_string_append_printf(out, " AC");
e6523173 251 if (mqflags & SR_MQFLAG_DC)
161a8a27 252 g_string_append_printf(out, " DC");
e6523173
UH
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");
c984f2f9
UH
267 /* Note: SR_MQFLAG_SPL_* is handled above. */
268 if (mqflags & SR_MQFLAG_DURATION)
269 g_string_append_printf(out, " DURATION");
f5027ca4
AJ
270 if (mqflags & SR_MQFLAG_AVG)
271 g_string_append_printf(out, " AVG");
02c7c482
JH
272 if (mqflags & SR_MQFLAG_REFERENCE)
273 g_string_append_printf(out, " REF");
34eaf4bc
UH
274 if (mqflags & SR_MQFLAG_UNSTABLE)
275 g_string_append_printf(out, " UNSTABLE");
161a8a27 276 g_string_append_c(out, '\n');
161a8a27
BV
277}
278
a755b0e1 279static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
dba3e682 280 GString **out)
161a8a27 281{
820c48f8 282 struct context *ctx;
5faebab2 283 const struct sr_datafeed_analog_old *analog_old;
edb691fc 284 const struct sr_datafeed_analog *analog;
ba7dd8bb 285 struct sr_channel *ch;
69e19dd7 286 GSList *l;
e02e9e6a
BV
287 float *fdata;
288 unsigned int i;
820c48f8 289 int num_channels, c, ret, si, digits;
a24da9a8 290 char *number, *suffix;
161a8a27 291
17f63de6 292 *out = NULL;
161a8a27 293 if (!o || !o->sdi)
17f63de6 294 return SR_ERR_ARG;
820c48f8 295 ctx = o->priv;
161a8a27 296
161a8a27 297 switch (packet->type) {
161a8a27 298 case SR_DF_FRAME_BEGIN:
17f63de6 299 *out = g_string_new("FRAME-BEGIN\n");
161a8a27
BV
300 break;
301 case SR_DF_FRAME_END:
17f63de6 302 *out = g_string_new("FRAME-END\n");
161a8a27 303 break;
5faebab2
UH
304 case SR_DF_ANALOG_OLD:
305 analog_old = packet->payload;
306 fdata = (float *)analog_old->data;
17f63de6 307 *out = g_string_sized_new(512);
5faebab2
UH
308 num_channels = g_slist_length(analog_old->channels);
309 for (si = 0; si < analog_old->num_samples; si++) {
310 for (l = analog_old->channels, c = 0; l; l = l->next, c++) {
ba7dd8bb
UH
311 ch = l->data;
312 g_string_append_printf(*out, "%s: ", ch->name);
5faebab2 313 fancyprint(analog_old->unit, analog_old->mqflags,
e02e9e6a
BV
314 fdata[si * num_channels + c], *out);
315 }
316 }
317 break;
edb691fc
UH
318 case SR_DF_ANALOG:
319 analog = packet->payload;
320 num_channels = g_slist_length(analog->meaning->channels);
2dbe445d 321 if (!(fdata = g_try_realloc(ctx->fdata,
edb691fc 322 analog->num_samples * num_channels * sizeof(float))))
e02e9e6a 323 return SR_ERR_MALLOC;
2dbe445d 324 ctx->fdata = fdata;
edb691fc 325 if ((ret = sr_analog_to_float(analog, fdata)) != SR_OK)
e02e9e6a
BV
326 return ret;
327 *out = g_string_sized_new(512);
edb691fc 328 if (analog->encoding->is_digits_decimal) {
820c48f8 329 if (ctx->digits == DIGITS_ALL)
edb691fc 330 digits = analog->encoding->digits;
820c48f8 331 else
edb691fc 332 digits = analog->spec->spec_digits;
820c48f8
BV
333 } else {
334 /* TODO we don't know how to print by number of bits yet. */
335 digits = 6;
336 }
edb691fc
UH
337 sr_analog_unit_to_string(analog, &suffix);
338 for (i = 0; i < analog->num_samples; i++) {
339 for (l = analog->meaning->channels, c = 0; l; l = l->next, c++) {
e02e9e6a
BV
340 ch = l->data;
341 g_string_append_printf(*out, "%s: ", ch->name);
222fdfd5
UH
342 number = g_strdup_printf("%.*f", digits,
343 fdata[i * num_channels + c]);
820c48f8 344 g_string_append(*out, number);
a24da9a8 345 g_free(number);
820c48f8
BV
346 g_string_append(*out, " ");
347 g_string_append(*out, suffix);
348 g_string_append(*out, "\n");
161a8a27
BV
349 }
350 }
a24da9a8 351 g_free(suffix);
161a8a27
BV
352 break;
353 }
354
17f63de6 355 return SR_OK;
161a8a27
BV
356}
357
358static int cleanup(struct sr_output *o)
359{
360 struct context *ctx;
361
362 if (!o || !o->sdi)
363 return SR_ERR_ARG;
d686c5ec 364 ctx = o->priv;
161a8a27 365
ba7dd8bb 366 g_ptr_array_free(ctx->channellist, 1);
2dbe445d 367 g_free(ctx->fdata);
161a8a27 368 g_free(ctx);
d686c5ec 369 o->priv = NULL;
161a8a27
BV
370
371 return SR_OK;
372}
373
820c48f8
BV
374static struct sr_option options[] = {
375 { "digits", "Digits", "Digits to show", NULL, NULL },
376 ALL_ZERO
377};
378
379static const struct sr_option *get_options(void)
380{
381 if (!options[0].def) {
382 options[0].def = g_variant_ref_sink(g_variant_new_string("all"));
383 options[0].values = g_slist_append(options[0].values,
384 g_variant_ref_sink(g_variant_new_string("all")));
385 options[0].values = g_slist_append(options[0].values,
386 g_variant_ref_sink(g_variant_new_string("spec")));
387 }
388
389 return options;
390}
391
a755b0e1 392SR_PRIV struct sr_output_module output_analog = {
161a8a27 393 .id = "analog",
a755b0e1
BV
394 .name = "Analog",
395 .desc = "Analog data and types",
8a174d23 396 .exts = NULL,
3cd4b381 397 .flags = 0,
820c48f8 398 .options = get_options,
161a8a27 399 .init = init,
17f63de6 400 .receive = receive,
161a8a27
BV
401 .cleanup = cleanup
402};