]>
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 BV |
33 | int digits; |
34 | }; | |
35 | ||
36 | enum { | |
37 | DIGITS_ALL, | |
38 | DIGITS_SPEC, | |
161a8a27 BV |
39 | }; |
40 | ||
a755b0e1 | 41 | static int init(struct sr_output *o, GHashTable *options) |
161a8a27 BV |
42 | { |
43 | struct context *ctx; | |
ba7dd8bb | 44 | struct sr_channel *ch; |
161a8a27 | 45 | GSList *l; |
820c48f8 | 46 | const char *s; |
a944a84b | 47 | |
161a8a27 BV |
48 | if (!o || !o->sdi) |
49 | return SR_ERR_ARG; | |
50 | ||
91219afc | 51 | o->priv = ctx = g_malloc0(sizeof(struct context)); |
820c48f8 BV |
52 | s = g_variant_get_string(g_hash_table_lookup(options, "digits"), NULL); |
53 | if (!strcmp(s, "all")) | |
54 | ctx->digits = DIGITS_ALL; | |
55 | else | |
56 | ctx->digits = DIGITS_SPEC; | |
161a8a27 | 57 | |
ba7dd8bb UH |
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) { | |
61 | ch = l->data; | |
62 | if (!ch || !ch->enabled) | |
161a8a27 | 63 | continue; |
ba7dd8bb UH |
64 | g_ptr_array_add(ctx->channellist, ch->name); |
65 | ctx->num_enabled_channels++; | |
161a8a27 BV |
66 | } |
67 | ||
161a8a27 BV |
68 | return SR_OK; |
69 | } | |
70 | ||
71 | static void si_printf(float value, GString *out, char *unitstr) | |
72 | { | |
d713e561 | 73 | float v; |
161a8a27 | 74 | |
d713e561 BV |
75 | if (signbit(value)) |
76 | v = -(value); | |
77 | else | |
78 | v = value; | |
79 | ||
80 | if (v < 1e-12 || v > 1e+12) | |
81 | g_string_append_printf(out, "%f %s", value, unitstr); | |
82 | else if (v > 1e+9) | |
83 | g_string_append_printf(out, "%f G%s", value / 1e+9, unitstr); | |
84 | else if (v > 1e+6) | |
85 | g_string_append_printf(out, "%f M%s", value / 1e+6, unitstr); | |
86 | else if (v > 1e+3) | |
87 | g_string_append_printf(out, "%f k%s", value / 1e+3, unitstr); | |
88 | else if (v < 1e-9) | |
89 | g_string_append_printf(out, "%f n%s", value * 1e+9, unitstr); | |
90 | else if (v < 1e-6) | |
91 | g_string_append_printf(out, "%f u%s", value * 1e+6, unitstr); | |
92 | else if (v < 1e-3) | |
93 | g_string_append_printf(out, "%f m%s", value * 1e+3, unitstr); | |
161a8a27 BV |
94 | else |
95 | g_string_append_printf(out, "%f %s", value, unitstr); | |
96 | ||
97 | } | |
98 | ||
ca7dbb56 | 99 | /* Please use the same order as in enum sr_unit (libsigrok.h). */ |
161a8a27 BV |
100 | static void fancyprint(int unit, int mqflags, float value, GString *out) |
101 | { | |
161a8a27 | 102 | switch (unit) { |
06c45a66 UH |
103 | case SR_UNIT_VOLT: |
104 | si_printf(value, out, "V"); | |
105 | break; | |
106 | case SR_UNIT_AMPERE: | |
107 | si_printf(value, out, "A"); | |
108 | break; | |
109 | case SR_UNIT_OHM: | |
110 | si_printf(value, out, ""); | |
111 | g_string_append_unichar(out, 0x2126); | |
112 | break; | |
113 | case SR_UNIT_FARAD: | |
114 | si_printf(value, out, "F"); | |
115 | break; | |
116 | case SR_UNIT_KELVIN: | |
117 | si_printf(value, out, "K"); | |
118 | break; | |
119 | case SR_UNIT_CELSIUS: | |
120 | si_printf(value, out, ""); | |
121 | g_string_append_unichar(out, 0x00b0); | |
122 | g_string_append_c(out, 'C'); | |
123 | break; | |
124 | case SR_UNIT_FAHRENHEIT: | |
125 | si_printf(value, out, ""); | |
126 | g_string_append_unichar(out, 0x00b0); | |
127 | g_string_append_c(out, 'F'); | |
128 | break; | |
129 | case SR_UNIT_HERTZ: | |
130 | si_printf(value, out, "Hz"); | |
131 | break; | |
132 | case SR_UNIT_PERCENTAGE: | |
641d8f27 | 133 | g_string_append_printf(out, "%f %%", value); |
06c45a66 UH |
134 | break; |
135 | case SR_UNIT_BOOLEAN: | |
136 | if (value > 0) | |
137 | g_string_append_printf(out, "TRUE"); | |
138 | else | |
139 | g_string_append_printf(out, "FALSE"); | |
140 | break; | |
141 | case SR_UNIT_SECOND: | |
142 | si_printf(value, out, "s"); | |
143 | break; | |
144 | case SR_UNIT_SIEMENS: | |
145 | si_printf(value, out, "S"); | |
146 | break; | |
147 | case SR_UNIT_DECIBEL_MW: | |
148 | si_printf(value, out, "dBu"); | |
149 | break; | |
150 | case SR_UNIT_DECIBEL_VOLT: | |
151 | si_printf(value, out, "dBV"); | |
152 | break; | |
c984f2f9 UH |
153 | case SR_UNIT_UNITLESS: |
154 | si_printf(value, out, ""); | |
155 | break; | |
06c45a66 UH |
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)"); | |
163 | else | |
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"); | |
176 | break; | |
177 | case SR_UNIT_CONCENTRATION: | |
1a46cc62 | 178 | g_string_append_printf(out, "%f ppm", value * (1000 * 1000)); |
06c45a66 | 179 | break; |
87532f23 AJ |
180 | case SR_UNIT_REVOLUTIONS_PER_MINUTE: |
181 | si_printf(value, out, "RPM"); | |
d69d2642 | 182 | break; |
45315d04 AJ |
183 | case SR_UNIT_VOLT_AMPERE: |
184 | si_printf(value, out, "VA"); | |
185 | break; | |
186 | case SR_UNIT_WATT: | |
187 | si_printf(value, out, "W"); | |
188 | break; | |
189 | case SR_UNIT_WATT_HOUR: | |
190 | si_printf(value, out, "Wh"); | |
87532f23 | 191 | break; |
543d041d BV |
192 | case SR_UNIT_METER_SECOND: |
193 | si_printf(value, out, "m/s"); | |
194 | break; | |
195 | case SR_UNIT_HECTOPASCAL: | |
196 | si_printf(value, out, "hPa"); | |
197 | break; | |
198 | case SR_UNIT_HUMIDITY_293K: | |
199 | si_printf(value, out, "%rF"); | |
200 | break; | |
02c7c482 JH |
201 | case SR_UNIT_DEGREE: |
202 | si_printf(value, out, ""); | |
203 | g_string_append_unichar(out, 0x00b0); | |
204 | break; | |
c984f2f9 UH |
205 | case SR_UNIT_HENRY: |
206 | si_printf(value, out, "H"); | |
207 | break; | |
34eaf4bc UH |
208 | case SR_UNIT_GRAM: |
209 | si_printf(value, out, "g"); | |
210 | break; | |
211 | case SR_UNIT_CARAT: | |
212 | si_printf(value, out, "ct"); | |
213 | break; | |
214 | case SR_UNIT_OUNCE: | |
215 | si_printf(value, out, "oz"); | |
216 | break; | |
217 | case SR_UNIT_TROY_OUNCE: | |
218 | si_printf(value, out, "oz t"); | |
219 | break; | |
220 | case SR_UNIT_POUND: | |
221 | si_printf(value, out, "lb"); | |
222 | break; | |
223 | case SR_UNIT_PENNYWEIGHT: | |
224 | si_printf(value, out, "dwt"); | |
225 | break; | |
226 | case SR_UNIT_GRAIN: | |
227 | si_printf(value, out, "gr"); | |
228 | break; | |
229 | case SR_UNIT_TAEL: | |
230 | si_printf(value, out, "tael"); | |
231 | break; | |
232 | case SR_UNIT_MOMME: | |
233 | si_printf(value, out, "momme"); | |
234 | break; | |
235 | case SR_UNIT_TOLA: | |
236 | si_printf(value, out, "tola"); | |
237 | break; | |
238 | case SR_UNIT_PIECE: | |
239 | si_printf(value, out, "pcs"); | |
240 | break; | |
06c45a66 UH |
241 | default: |
242 | si_printf(value, out, ""); | |
243 | break; | |
161a8a27 | 244 | } |
e6523173 | 245 | |
ca7dbb56 | 246 | /* Please use the same order as in enum sr_mqflag (libsigrok.h). */ |
e6523173 | 247 | if (mqflags & SR_MQFLAG_AC) |
161a8a27 | 248 | g_string_append_printf(out, " AC"); |
e6523173 | 249 | if (mqflags & SR_MQFLAG_DC) |
161a8a27 | 250 | g_string_append_printf(out, " DC"); |
e6523173 UH |
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"); | |
c984f2f9 UH |
265 | /* Note: SR_MQFLAG_SPL_* is handled above. */ |
266 | if (mqflags & SR_MQFLAG_DURATION) | |
267 | g_string_append_printf(out, " DURATION"); | |
f5027ca4 AJ |
268 | if (mqflags & SR_MQFLAG_AVG) |
269 | g_string_append_printf(out, " AVG"); | |
02c7c482 JH |
270 | if (mqflags & SR_MQFLAG_REFERENCE) |
271 | g_string_append_printf(out, " REF"); | |
34eaf4bc UH |
272 | if (mqflags & SR_MQFLAG_UNSTABLE) |
273 | g_string_append_printf(out, " UNSTABLE"); | |
161a8a27 | 274 | g_string_append_c(out, '\n'); |
161a8a27 BV |
275 | } |
276 | ||
a755b0e1 | 277 | static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet, |
dba3e682 | 278 | GString **out) |
161a8a27 | 279 | { |
820c48f8 | 280 | struct context *ctx; |
69e19dd7 | 281 | const struct sr_datafeed_analog *analog; |
e02e9e6a | 282 | const struct sr_datafeed_analog2 *analog2; |
ba7dd8bb | 283 | struct sr_channel *ch; |
69e19dd7 | 284 | GSList *l; |
e02e9e6a BV |
285 | float *fdata; |
286 | unsigned int i; | |
820c48f8 | 287 | int num_channels, c, ret, si, digits; |
a24da9a8 | 288 | char *number, *suffix; |
161a8a27 | 289 | |
17f63de6 | 290 | *out = NULL; |
161a8a27 | 291 | if (!o || !o->sdi) |
17f63de6 | 292 | return SR_ERR_ARG; |
820c48f8 | 293 | ctx = o->priv; |
161a8a27 | 294 | |
161a8a27 | 295 | switch (packet->type) { |
161a8a27 | 296 | case SR_DF_FRAME_BEGIN: |
17f63de6 | 297 | *out = g_string_new("FRAME-BEGIN\n"); |
161a8a27 BV |
298 | break; |
299 | case SR_DF_FRAME_END: | |
17f63de6 | 300 | *out = g_string_new("FRAME-END\n"); |
161a8a27 BV |
301 | break; |
302 | case SR_DF_ANALOG: | |
303 | analog = packet->payload; | |
e02e9e6a | 304 | fdata = (float *)analog->data; |
17f63de6 | 305 | *out = g_string_sized_new(512); |
6e6babb9 | 306 | num_channels = g_slist_length(analog->channels); |
e02e9e6a | 307 | for (si = 0; si < analog->num_samples; si++) { |
6e6babb9 | 308 | for (l = analog->channels, c = 0; l; l = l->next, c++) { |
ba7dd8bb UH |
309 | ch = l->data; |
310 | g_string_append_printf(*out, "%s: ", ch->name); | |
161a8a27 | 311 | fancyprint(analog->unit, analog->mqflags, |
e02e9e6a BV |
312 | fdata[si * num_channels + c], *out); |
313 | } | |
314 | } | |
315 | break; | |
316 | case SR_DF_ANALOG2: | |
317 | analog2 = packet->payload; | |
fa74a26b ML |
318 | num_channels = g_slist_length(analog2->meaning->channels); |
319 | if (!(fdata = g_try_malloc( | |
320 | analog2->num_samples * num_channels * sizeof(float)))) | |
e02e9e6a BV |
321 | return SR_ERR_MALLOC; |
322 | if ((ret = sr_analog_to_float(analog2, fdata)) != SR_OK) | |
323 | return ret; | |
324 | *out = g_string_sized_new(512); | |
820c48f8 BV |
325 | if (analog2->encoding->is_digits_decimal) { |
326 | if (ctx->digits == DIGITS_ALL) | |
327 | digits = analog2->encoding->digits; | |
328 | else | |
329 | digits = analog2->spec->spec_digits; | |
330 | } else { | |
331 | /* TODO we don't know how to print by number of bits yet. */ | |
332 | digits = 6; | |
333 | } | |
a24da9a8 | 334 | sr_analog_unit_to_string(analog2, &suffix); |
e02e9e6a BV |
335 | for (i = 0; i < analog2->num_samples; i++) { |
336 | for (l = analog2->meaning->channels, c = 0; l; l = l->next, c++) { | |
337 | ch = l->data; | |
338 | g_string_append_printf(*out, "%s: ", ch->name); | |
820c48f8 | 339 | sr_analog_float_to_string(fdata[i * num_channels + c], |
a24da9a8 | 340 | digits, &number); |
820c48f8 | 341 | g_string_append(*out, number); |
a24da9a8 | 342 | g_free(number); |
820c48f8 BV |
343 | g_string_append(*out, " "); |
344 | g_string_append(*out, suffix); | |
345 | g_string_append(*out, "\n"); | |
161a8a27 BV |
346 | } |
347 | } | |
a24da9a8 | 348 | g_free(suffix); |
161a8a27 BV |
349 | break; |
350 | } | |
351 | ||
17f63de6 | 352 | return SR_OK; |
161a8a27 BV |
353 | } |
354 | ||
355 | static int cleanup(struct sr_output *o) | |
356 | { | |
357 | struct context *ctx; | |
358 | ||
359 | if (!o || !o->sdi) | |
360 | return SR_ERR_ARG; | |
d686c5ec | 361 | ctx = o->priv; |
161a8a27 | 362 | |
ba7dd8bb | 363 | g_ptr_array_free(ctx->channellist, 1); |
161a8a27 | 364 | g_free(ctx); |
d686c5ec | 365 | o->priv = NULL; |
161a8a27 BV |
366 | |
367 | return SR_OK; | |
368 | } | |
369 | ||
820c48f8 BV |
370 | static struct sr_option options[] = { |
371 | { "digits", "Digits", "Digits to show", NULL, NULL }, | |
372 | ALL_ZERO | |
373 | }; | |
374 | ||
375 | static const struct sr_option *get_options(void) | |
376 | { | |
377 | if (!options[0].def) { | |
378 | options[0].def = g_variant_ref_sink(g_variant_new_string("all")); | |
379 | options[0].values = g_slist_append(options[0].values, | |
380 | g_variant_ref_sink(g_variant_new_string("all"))); | |
381 | options[0].values = g_slist_append(options[0].values, | |
382 | g_variant_ref_sink(g_variant_new_string("spec"))); | |
383 | } | |
384 | ||
385 | return options; | |
386 | } | |
387 | ||
a755b0e1 | 388 | SR_PRIV struct sr_output_module output_analog = { |
161a8a27 | 389 | .id = "analog", |
a755b0e1 BV |
390 | .name = "Analog", |
391 | .desc = "Analog data and types", | |
8a174d23 | 392 | .exts = NULL, |
3cd4b381 | 393 | .flags = 0, |
820c48f8 | 394 | .options = get_options, |
161a8a27 | 395 | .init = init, |
17f63de6 | 396 | .receive = receive, |
161a8a27 BV |
397 | .cleanup = cleanup |
398 | }; |