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/>.
25 #include <libsigrok/libsigrok.h>
26 #include "libsigrok-internal.h"
28 #define LOG_PREFIX "output/analog"
31 int num_enabled_channels;
32 GPtrArray *channellist;
41 static int init(struct sr_output *o, GHashTable *options)
44 struct sr_channel *ch;
51 o->priv = ctx = g_malloc0(sizeof(struct context));
52 s = g_variant_get_string(g_hash_table_lookup(options, "digits"), NULL);
53 if (!strcmp(s, "all"))
54 ctx->digits = DIGITS_ALL;
56 ctx->digits = DIGITS_SPEC;
58 /* Get the number of channels and their names. */
59 ctx->channellist = g_ptr_array_new();
60 for (l = o->sdi->channels; l; l = l->next) {
62 if (!ch || !ch->enabled)
64 g_ptr_array_add(ctx->channellist, ch->name);
65 ctx->num_enabled_channels++;
71 static void si_printf(float value, GString *out, char *unitstr)
80 if (v < 1e-12 || v > 1e+12)
81 g_string_append_printf(out, "%f %s", value, unitstr);
83 g_string_append_printf(out, "%f G%s", value / 1e+9, unitstr);
85 g_string_append_printf(out, "%f M%s", value / 1e+6, unitstr);
87 g_string_append_printf(out, "%f k%s", value / 1e+3, unitstr);
89 g_string_append_printf(out, "%f n%s", value * 1e+9, unitstr);
91 g_string_append_printf(out, "%f u%s", value * 1e+6, unitstr);
93 g_string_append_printf(out, "%f m%s", value * 1e+3, unitstr);
95 g_string_append_printf(out, "%f %s", value, unitstr);
99 /* Please use the same order as in enum sr_unit (libsigrok.h). */
100 static void fancyprint(int unit, int mqflags, float value, GString *out)
104 si_printf(value, out, "V");
107 si_printf(value, out, "A");
110 si_printf(value, out, "");
111 g_string_append_unichar(out, 0x2126);
114 si_printf(value, out, "F");
117 si_printf(value, out, "K");
119 case SR_UNIT_CELSIUS:
120 si_printf(value, out, "");
121 g_string_append_unichar(out, 0x00b0);
122 g_string_append_c(out, 'C');
124 case SR_UNIT_FAHRENHEIT:
125 si_printf(value, out, "");
126 g_string_append_unichar(out, 0x00b0);
127 g_string_append_c(out, 'F');
130 si_printf(value, out, "Hz");
132 case SR_UNIT_PERCENTAGE:
133 g_string_append_printf(out, "%f %%", value);
135 case SR_UNIT_BOOLEAN:
137 g_string_append_printf(out, "TRUE");
139 g_string_append_printf(out, "FALSE");
142 si_printf(value, out, "s");
144 case SR_UNIT_SIEMENS:
145 si_printf(value, out, "S");
147 case SR_UNIT_DECIBEL_MW:
148 si_printf(value, out, "dBu");
150 case SR_UNIT_DECIBEL_VOLT:
151 si_printf(value, out, "dBV");
153 case SR_UNIT_UNITLESS:
154 si_printf(value, out, "");
156 case SR_UNIT_DECIBEL_SPL:
157 if (mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_A)
158 si_printf(value, out, "dB(A)");
159 else if (mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_C)
160 si_printf(value, out, "dB(C)");
161 else if (mqflags & SR_MQFLAG_SPL_FREQ_WEIGHT_Z)
162 si_printf(value, out, "dB(Z)");
164 /* No frequency weighting, or non-standard "flat" */
165 si_printf(value, out, "dB(SPL)");
166 if (mqflags & SR_MQFLAG_SPL_TIME_WEIGHT_S)
167 g_string_append(out, " S");
168 else if (mqflags & SR_MQFLAG_SPL_TIME_WEIGHT_F)
169 g_string_append(out, " F");
170 if (mqflags & SR_MQFLAG_SPL_LAT)
171 g_string_append(out, " LAT");
172 else if (mqflags & SR_MQFLAG_SPL_PCT_OVER_ALARM)
173 /* Not a standard function for SLMs, so this is
174 * a made-up notation. */
175 g_string_append(out, " %oA");
177 case SR_UNIT_CONCENTRATION:
178 g_string_append_printf(out, "%f ppm", value * (1000 * 1000));
180 case SR_UNIT_REVOLUTIONS_PER_MINUTE:
181 si_printf(value, out, "RPM");
183 case SR_UNIT_VOLT_AMPERE:
184 si_printf(value, out, "VA");
187 si_printf(value, out, "W");
189 case SR_UNIT_WATT_HOUR:
190 si_printf(value, out, "Wh");
192 case SR_UNIT_METER_SECOND:
193 si_printf(value, out, "m/s");
195 case SR_UNIT_HECTOPASCAL:
196 si_printf(value, out, "hPa");
198 case SR_UNIT_HUMIDITY_293K:
199 si_printf(value, out, "%rF");
202 si_printf(value, out, "");
203 g_string_append_unichar(out, 0x00b0);
206 si_printf(value, out, "H");
209 si_printf(value, out, "g");
212 si_printf(value, out, "ct");
215 si_printf(value, out, "oz");
217 case SR_UNIT_TROY_OUNCE:
218 si_printf(value, out, "oz t");
221 si_printf(value, out, "lb");
223 case SR_UNIT_PENNYWEIGHT:
224 si_printf(value, out, "dwt");
227 si_printf(value, out, "gr");
230 si_printf(value, out, "tael");
233 si_printf(value, out, "momme");
236 si_printf(value, out, "tola");
239 si_printf(value, out, "pcs");
242 si_printf(value, out, "");
246 /* Please use the same order as in enum sr_mqflag (libsigrok.h). */
247 if (mqflags & SR_MQFLAG_AC)
248 g_string_append_printf(out, " AC");
249 if (mqflags & SR_MQFLAG_DC)
250 g_string_append_printf(out, " DC");
251 if (mqflags & SR_MQFLAG_RMS)
252 g_string_append_printf(out, " RMS");
253 if (mqflags & SR_MQFLAG_DIODE)
254 g_string_append_printf(out, " DIODE");
255 if (mqflags & SR_MQFLAG_HOLD)
256 g_string_append_printf(out, " HOLD");
257 if (mqflags & SR_MQFLAG_MAX)
258 g_string_append_printf(out, " MAX");
259 if (mqflags & SR_MQFLAG_MIN)
260 g_string_append_printf(out, " MIN");
261 if (mqflags & SR_MQFLAG_AUTORANGE)
262 g_string_append_printf(out, " AUTO");
263 if (mqflags & SR_MQFLAG_RELATIVE)
264 g_string_append_printf(out, " REL");
265 /* Note: SR_MQFLAG_SPL_* is handled above. */
266 if (mqflags & SR_MQFLAG_DURATION)
267 g_string_append_printf(out, " DURATION");
268 if (mqflags & SR_MQFLAG_AVG)
269 g_string_append_printf(out, " AVG");
270 if (mqflags & SR_MQFLAG_REFERENCE)
271 g_string_append_printf(out, " REF");
272 if (mqflags & SR_MQFLAG_UNSTABLE)
273 g_string_append_printf(out, " UNSTABLE");
274 g_string_append_c(out, '\n');
277 static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
281 const struct sr_datafeed_analog *analog;
282 const struct sr_datafeed_analog2 *analog2;
283 struct sr_channel *ch;
287 int num_channels, c, ret, si, digits;
288 char *number, *suffix;
295 switch (packet->type) {
296 case SR_DF_FRAME_BEGIN:
297 *out = g_string_new("FRAME-BEGIN\n");
299 case SR_DF_FRAME_END:
300 *out = g_string_new("FRAME-END\n");
303 analog = packet->payload;
304 fdata = (float *)analog->data;
305 *out = g_string_sized_new(512);
306 num_channels = g_slist_length(analog->channels);
307 for (si = 0; si < analog->num_samples; si++) {
308 for (l = analog->channels, c = 0; l; l = l->next, c++) {
310 g_string_append_printf(*out, "%s: ", ch->name);
311 fancyprint(analog->unit, analog->mqflags,
312 fdata[si * num_channels + c], *out);
317 analog2 = packet->payload;
318 if (!(fdata = g_try_malloc(analog2->num_samples * sizeof(float))))
319 return SR_ERR_MALLOC;
320 if ((ret = sr_analog_to_float(analog2, fdata)) != SR_OK)
322 *out = g_string_sized_new(512);
323 if (analog2->encoding->is_digits_decimal) {
324 if (ctx->digits == DIGITS_ALL)
325 digits = analog2->encoding->digits;
327 digits = analog2->spec->spec_digits;
329 /* TODO we don't know how to print by number of bits yet. */
332 sr_analog_unit_to_string(analog2, &suffix);
333 num_channels = g_slist_length(analog2->meaning->channels);
334 for (i = 0; i < analog2->num_samples; i++) {
335 for (l = analog2->meaning->channels, c = 0; l; l = l->next, c++) {
337 g_string_append_printf(*out, "%s: ", ch->name);
338 sr_analog_float_to_string(fdata[i * num_channels + c],
340 g_string_append(*out, number);
342 g_string_append(*out, " ");
343 g_string_append(*out, suffix);
344 g_string_append(*out, "\n");
354 static int cleanup(struct sr_output *o)
362 g_ptr_array_free(ctx->channellist, 1);
369 static struct sr_option options[] = {
370 { "digits", "Digits", "Digits to show", NULL, NULL },
374 static const struct sr_option *get_options(void)
376 if (!options[0].def) {
377 options[0].def = g_variant_ref_sink(g_variant_new_string("all"));
378 options[0].values = g_slist_append(options[0].values,
379 g_variant_ref_sink(g_variant_new_string("all")));
380 options[0].values = g_slist_append(options[0].values,
381 g_variant_ref_sink(g_variant_new_string("spec")));
387 SR_PRIV struct sr_output_module output_analog = {
390 .desc = "Analog data and types",
393 .options = get_options,