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