2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
24 #include <libsigrok/libsigrok.h>
25 #include "libsigrok-internal.h"
27 #define LOG_PREFIX "output/analog"
30 int num_enabled_channels;
31 GPtrArray *channellist;
40 static int init(struct sr_output *o, GHashTable *options)
43 struct sr_channel *ch;
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;
55 ctx->digits = DIGITS_SPEC;
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) {
61 if (!ch || !ch->enabled)
63 g_ptr_array_add(ctx->channellist, ch->name);
64 ctx->num_enabled_channels++;
70 static void si_printf(float value, GString *out, char *unitstr)
79 if (v < 1e-12 || v > 1e+12)
80 g_string_append_printf(out, "%f %s", value, unitstr);
82 g_string_append_printf(out, "%f G%s", value / 1e+9, unitstr);
84 g_string_append_printf(out, "%f M%s", value / 1e+6, unitstr);
86 g_string_append_printf(out, "%f k%s", value / 1e+3, unitstr);
88 g_string_append_printf(out, "%f n%s", value * 1e+9, unitstr);
90 g_string_append_printf(out, "%f u%s", value * 1e+6, unitstr);
92 g_string_append_printf(out, "%f m%s", value * 1e+3, unitstr);
94 g_string_append_printf(out, "%f %s", value, unitstr);
98 static void fancyprint(int unit, int mqflags, float value, GString *out)
102 si_printf(value, out, "V");
105 si_printf(value, out, "A");
108 si_printf(value, out, "");
109 g_string_append_unichar(out, 0x2126);
112 si_printf(value, out, "F");
115 si_printf(value, out, "K");
117 case SR_UNIT_CELSIUS:
118 si_printf(value, out, "");
119 g_string_append_unichar(out, 0x00b0);
120 g_string_append_c(out, 'C');
122 case SR_UNIT_FAHRENHEIT:
123 si_printf(value, out, "");
124 g_string_append_unichar(out, 0x00b0);
125 g_string_append_c(out, 'F');
128 si_printf(value, out, "Hz");
130 case SR_UNIT_PERCENTAGE:
131 g_string_append_printf(out, "%f %%", value);
133 case SR_UNIT_BOOLEAN:
135 g_string_append_printf(out, "TRUE");
137 g_string_append_printf(out, "FALSE");
140 si_printf(value, out, "s");
142 case SR_UNIT_SIEMENS:
143 si_printf(value, out, "S");
145 case SR_UNIT_DECIBEL_MW:
146 si_printf(value, out, "dBu");
148 case SR_UNIT_DECIBEL_VOLT:
149 si_printf(value, out, "dBV");
151 case SR_UNIT_UNITLESS:
152 si_printf(value, out, "");
154 case SR_UNIT_DECIBEL_SPL:
155 if (mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_A)
156 si_printf(value, out, "dB(A)");
157 else if (mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_C)
158 si_printf(value, out, "dB(C)");
159 else if (mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_Z)
160 si_printf(value, out, "dB(Z)");
162 /* No frequency weighting, or non-standard "flat" */
163 si_printf(value, out, "dB(SPL)");
164 if (mqflags & SR_MQFLAG_SPL_TIME_WEIGHT_S)
165 g_string_append(out, " S");
166 else if (mqflags & SR_MQFLAG_SPL_TIME_WEIGHT_F)
167 g_string_append(out, " F");
168 if (mqflags & SR_MQFLAG_SPL_LAT)
169 g_string_append(out, " LAT");
170 else if (mqflags & SR_MQFLAG_SPL_PCT_OVER_ALARM)
171 /* Not a standard function for SLMs, so this is
172 * a made-up notation. */
173 g_string_append(out, " %oA");
175 case SR_UNIT_CONCENTRATION:
176 g_string_append_printf(out, "%f ppm", value * (1000 * 1000));
178 case SR_UNIT_REVOLUTIONS_PER_MINUTE:
179 si_printf(value, out, "RPM");
181 case SR_UNIT_VOLT_AMPERE:
182 si_printf(value, out, "VA");
185 si_printf(value, out, "W");
187 case SR_UNIT_WATT_HOUR:
188 si_printf(value, out, "Wh");
190 case SR_UNIT_METER_SECOND:
191 si_printf(value, out, "m/s");
193 case SR_UNIT_HECTOPASCAL:
194 si_printf(value, out, "hPa");
196 case SR_UNIT_HUMIDITY_293K:
197 si_printf(value, out, "%rF");
200 si_printf(value, out, "");
201 g_string_append_unichar(out, 0x00b0);
204 si_printf(value, out, "H");
207 si_printf(value, out, "g");
210 si_printf(value, out, "ct");
213 si_printf(value, out, "oz");
215 case SR_UNIT_TROY_OUNCE:
216 si_printf(value, out, "oz t");
219 si_printf(value, out, "lb");
221 case SR_UNIT_PENNYWEIGHT:
222 si_printf(value, out, "dwt");
225 si_printf(value, out, "gr");
228 si_printf(value, out, "tael");
231 si_printf(value, out, "momme");
234 si_printf(value, out, "tola");
237 si_printf(value, out, "pcs");
240 si_printf(value, out, "");
244 if (mqflags & SR_MQFLAG_AC)
245 g_string_append_printf(out, " AC");
246 if (mqflags & SR_MQFLAG_DC)
247 g_string_append_printf(out, " DC");
248 if (mqflags & SR_MQFLAG_RMS)
249 g_string_append_printf(out, " RMS");
250 if (mqflags & SR_MQFLAG_DIODE)
251 g_string_append_printf(out, " DIODE");
252 if (mqflags & SR_MQFLAG_HOLD)
253 g_string_append_printf(out, " HOLD");
254 if (mqflags & SR_MQFLAG_MAX)
255 g_string_append_printf(out, " MAX");
256 if (mqflags & SR_MQFLAG_MIN)
257 g_string_append_printf(out, " MIN");
258 if (mqflags & SR_MQFLAG_AUTORANGE)
259 g_string_append_printf(out, " AUTO");
260 if (mqflags & SR_MQFLAG_RELATIVE)
261 g_string_append_printf(out, " REL");
262 /* Note: SR_MQFLAG_SPL_* is handled above. */
263 if (mqflags & SR_MQFLAG_DURATION)
264 g_string_append_printf(out, " DURATION");
265 if (mqflags & SR_MQFLAG_AVG)
266 g_string_append_printf(out, " AVG");
267 if (mqflags & SR_MQFLAG_REFERENCE)
268 g_string_append_printf(out, " REF");
269 if (mqflags & SR_MQFLAG_UNSTABLE)
270 g_string_append_printf(out, " UNSTABLE");
271 g_string_append_c(out, '\n');
274 static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
278 const struct sr_datafeed_analog *analog;
279 const struct sr_datafeed_analog2 *analog2;
280 struct sr_channel *ch;
284 int num_channels, c, ret, si, digits;
285 char *number, *suffix;
292 switch (packet->type) {
293 case SR_DF_FRAME_BEGIN:
294 *out = g_string_new("FRAME-BEGIN\n");
296 case SR_DF_FRAME_END:
297 *out = g_string_new("FRAME-END\n");
300 analog = packet->payload;
301 fdata = (float *)analog->data;
302 *out = g_string_sized_new(512);
303 num_channels = g_slist_length(analog->channels);
304 for (si = 0; si < analog->num_samples; si++) {
305 for (l = analog->channels, c = 0; l; l = l->next, c++) {
307 g_string_append_printf(*out, "%s: ", ch->name);
308 fancyprint(analog->unit, analog->mqflags,
309 fdata[si * num_channels + c], *out);
314 analog2 = packet->payload;
315 if (!(fdata = g_try_malloc(analog2->num_samples * sizeof(float))))
316 return SR_ERR_MALLOC;
317 if ((ret = sr_analog_to_float(analog2, fdata)) != SR_OK)
319 *out = g_string_sized_new(512);
320 if (analog2->encoding->is_digits_decimal) {
321 if (ctx->digits == DIGITS_ALL)
322 digits = analog2->encoding->digits;
324 digits = analog2->spec->spec_digits;
326 /* TODO we don't know how to print by number of bits yet. */
329 sr_analog_unit_to_string(analog2, &suffix);
330 num_channels = g_slist_length(analog2->meaning->channels);
331 for (i = 0; i < analog2->num_samples; i++) {
332 for (l = analog2->meaning->channels, c = 0; l; l = l->next, c++) {
334 g_string_append_printf(*out, "%s: ", ch->name);
335 sr_analog_float_to_string(fdata[i * num_channels + c],
337 g_string_append(*out, number);
339 g_string_append(*out, " ");
340 g_string_append(*out, suffix);
341 g_string_append(*out, "\n");
351 static int cleanup(struct sr_output *o)
359 g_ptr_array_free(ctx->channellist, 1);
366 static struct sr_option options[] = {
367 { "digits", "Digits", "Digits to show", NULL, NULL },
371 static const struct sr_option *get_options(void)
373 if (!options[0].def) {
374 options[0].def = g_variant_ref_sink(g_variant_new_string("all"));
375 options[0].values = g_slist_append(options[0].values,
376 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("spec")));
384 SR_PRIV struct sr_output_module output_analog = {
387 .desc = "Analog data and types",
390 .options = get_options,