]>
Commit | Line | Data |
---|---|---|
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 | |
30 | struct context { | |
ba7dd8bb UH |
31 | int num_enabled_channels; |
32 | GPtrArray *channellist; | |
820c48f8 | 33 | int digits; |
2dbe445d | 34 | float *fdata; |
820c48f8 BV |
35 | }; |
36 | ||
37 | enum { | |
38 | DIGITS_ALL, | |
39 | DIGITS_SPEC, | |
161a8a27 BV |
40 | }; |
41 | ||
a755b0e1 | 42 | static 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 | 73 | static 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 |
102 | static 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"); | |
6d5cd3bd AG |
276 | if (mqflags & SR_MQFLAG_FOUR_WIRE) |
277 | g_string_append_printf(out, " 4-WIRE"); | |
161a8a27 | 278 | g_string_append_c(out, '\n'); |
161a8a27 BV |
279 | } |
280 | ||
a755b0e1 | 281 | static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet, |
dba3e682 | 282 | GString **out) |
161a8a27 | 283 | { |
820c48f8 | 284 | struct context *ctx; |
5faebab2 | 285 | const struct sr_datafeed_analog_old *analog_old; |
edb691fc | 286 | const struct sr_datafeed_analog *analog; |
ba7dd8bb | 287 | struct sr_channel *ch; |
69e19dd7 | 288 | GSList *l; |
e02e9e6a BV |
289 | float *fdata; |
290 | unsigned int i; | |
820c48f8 | 291 | int num_channels, c, ret, si, digits; |
a24da9a8 | 292 | char *number, *suffix; |
161a8a27 | 293 | |
17f63de6 | 294 | *out = NULL; |
161a8a27 | 295 | if (!o || !o->sdi) |
17f63de6 | 296 | return SR_ERR_ARG; |
820c48f8 | 297 | ctx = o->priv; |
161a8a27 | 298 | |
161a8a27 | 299 | switch (packet->type) { |
161a8a27 | 300 | case SR_DF_FRAME_BEGIN: |
17f63de6 | 301 | *out = g_string_new("FRAME-BEGIN\n"); |
161a8a27 BV |
302 | break; |
303 | case SR_DF_FRAME_END: | |
17f63de6 | 304 | *out = g_string_new("FRAME-END\n"); |
161a8a27 | 305 | break; |
5faebab2 UH |
306 | case SR_DF_ANALOG_OLD: |
307 | analog_old = packet->payload; | |
308 | fdata = (float *)analog_old->data; | |
17f63de6 | 309 | *out = g_string_sized_new(512); |
5faebab2 UH |
310 | num_channels = g_slist_length(analog_old->channels); |
311 | for (si = 0; si < analog_old->num_samples; si++) { | |
312 | for (l = analog_old->channels, c = 0; l; l = l->next, c++) { | |
ba7dd8bb UH |
313 | ch = l->data; |
314 | g_string_append_printf(*out, "%s: ", ch->name); | |
5faebab2 | 315 | fancyprint(analog_old->unit, analog_old->mqflags, |
e02e9e6a BV |
316 | fdata[si * num_channels + c], *out); |
317 | } | |
318 | } | |
319 | break; | |
edb691fc UH |
320 | case SR_DF_ANALOG: |
321 | analog = packet->payload; | |
322 | num_channels = g_slist_length(analog->meaning->channels); | |
2dbe445d | 323 | if (!(fdata = g_try_realloc(ctx->fdata, |
edb691fc | 324 | analog->num_samples * num_channels * sizeof(float)))) |
e02e9e6a | 325 | return SR_ERR_MALLOC; |
2dbe445d | 326 | ctx->fdata = fdata; |
edb691fc | 327 | if ((ret = sr_analog_to_float(analog, fdata)) != SR_OK) |
e02e9e6a BV |
328 | return ret; |
329 | *out = g_string_sized_new(512); | |
edb691fc | 330 | if (analog->encoding->is_digits_decimal) { |
820c48f8 | 331 | if (ctx->digits == DIGITS_ALL) |
edb691fc | 332 | digits = analog->encoding->digits; |
820c48f8 | 333 | else |
edb691fc | 334 | digits = analog->spec->spec_digits; |
820c48f8 BV |
335 | } else { |
336 | /* TODO we don't know how to print by number of bits yet. */ | |
337 | digits = 6; | |
338 | } | |
edb691fc UH |
339 | sr_analog_unit_to_string(analog, &suffix); |
340 | for (i = 0; i < analog->num_samples; i++) { | |
341 | for (l = analog->meaning->channels, c = 0; l; l = l->next, c++) { | |
e02e9e6a BV |
342 | ch = l->data; |
343 | g_string_append_printf(*out, "%s: ", ch->name); | |
222fdfd5 UH |
344 | number = g_strdup_printf("%.*f", digits, |
345 | fdata[i * num_channels + c]); | |
820c48f8 | 346 | g_string_append(*out, number); |
a24da9a8 | 347 | g_free(number); |
820c48f8 BV |
348 | g_string_append(*out, " "); |
349 | g_string_append(*out, suffix); | |
350 | g_string_append(*out, "\n"); | |
161a8a27 BV |
351 | } |
352 | } | |
a24da9a8 | 353 | g_free(suffix); |
161a8a27 BV |
354 | break; |
355 | } | |
356 | ||
17f63de6 | 357 | return SR_OK; |
161a8a27 BV |
358 | } |
359 | ||
820c48f8 BV |
360 | static struct sr_option options[] = { |
361 | { "digits", "Digits", "Digits to show", NULL, NULL }, | |
362 | ALL_ZERO | |
363 | }; | |
364 | ||
365 | static const struct sr_option *get_options(void) | |
366 | { | |
367 | if (!options[0].def) { | |
368 | options[0].def = g_variant_ref_sink(g_variant_new_string("all")); | |
369 | options[0].values = g_slist_append(options[0].values, | |
370 | g_variant_ref_sink(g_variant_new_string("all"))); | |
371 | options[0].values = g_slist_append(options[0].values, | |
372 | g_variant_ref_sink(g_variant_new_string("spec"))); | |
373 | } | |
374 | ||
375 | return options; | |
376 | } | |
377 | ||
da3d141f SB |
378 | static int cleanup(struct sr_output *o) |
379 | { | |
380 | struct context *ctx; | |
381 | ||
382 | if (!o || !o->sdi) | |
383 | return SR_ERR_ARG; | |
384 | ctx = o->priv; | |
385 | ||
386 | g_ptr_array_free(ctx->channellist, 1); | |
387 | g_variant_unref(options[0].def); | |
388 | g_slist_free_full(options[0].values, (GDestroyNotify)g_variant_unref); | |
389 | g_free(ctx->fdata); | |
390 | g_free(ctx); | |
391 | o->priv = NULL; | |
392 | ||
393 | return SR_OK; | |
394 | } | |
395 | ||
a755b0e1 | 396 | SR_PRIV struct sr_output_module output_analog = { |
161a8a27 | 397 | .id = "analog", |
a755b0e1 BV |
398 | .name = "Analog", |
399 | .desc = "Analog data and types", | |
8a174d23 | 400 | .exts = NULL, |
3cd4b381 | 401 | .flags = 0, |
820c48f8 | 402 | .options = get_options, |
161a8a27 | 403 | .init = init, |
17f63de6 | 404 | .receive = receive, |
161a8a27 BV |
405 | .cleanup = cleanup |
406 | }; |