]> sigrok.org Git - libsigrok.git/blame_incremental - output/analog.c
sr/cli/gtk/qt: s/device/dev/ in many places.
[libsigrok.git] / output / analog.c
... / ...
CommitLineData
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2011 HÃ¥vard Espeland <gus@ping.uio.no>
6 * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25#include <glib.h>
26#include "sigrok.h"
27
28#define DEFAULT_BPL_BITS 64
29#define DEFAULT_BPL_HEX 192
30#define DEFAULT_BPL_ASCII 74
31
32enum outputmode {
33 MODE_BITS = 1,
34 MODE_HEX,
35 MODE_ASCII,
36};
37
38struct context {
39 unsigned int num_enabled_probes;
40 int samples_per_line;
41 unsigned int unitsize;
42 int line_offset;
43 int linebuf_len;
44 char *probelist[65];
45 char *linebuf;
46 int spl_cnt;
47 uint8_t *linevalues;
48 char *header;
49 int mark_trigger;
50// struct sr_analog_sample *prevsample;
51 enum outputmode mode;
52};
53
54static void flush_linebufs(struct context *ctx, char *outbuf)
55{
56 static int max_probename_len = 0;
57 int len, i;
58
59 if (ctx->linebuf[0] == 0)
60 return;
61
62 if (max_probename_len == 0) {
63 /* First time through... */
64 for (i = 0; ctx->probelist[i]; i++) {
65 len = strlen(ctx->probelist[i]);
66 if (len > max_probename_len)
67 max_probename_len = len;
68 }
69 }
70
71 for (i = 0; ctx->probelist[i]; i++) {
72 sprintf(outbuf + strlen(outbuf), "%*s:%s\n", max_probename_len,
73 ctx->probelist[i], ctx->linebuf + i * ctx->linebuf_len);
74 }
75
76 /* Mark trigger with a ^ character. */
77 if (ctx->mark_trigger != -1)
78 {
79 int space_offset = ctx->mark_trigger / 8;
80
81 if (ctx->mode == MODE_ASCII)
82 space_offset = 0;
83
84 sprintf(outbuf + strlen(outbuf), "T:%*s^\n",
85 ctx->mark_trigger + space_offset, "");
86 }
87
88 memset(ctx->linebuf, 0, i * ctx->linebuf_len);
89}
90
91static int init(struct sr_output *o, int default_spl, enum outputmode mode)
92{
93 struct context *ctx;
94 struct sr_probe *probe;
95 GSList *l;
96 uint64_t samplerate;
97 int num_probes;
98 char *samplerate_s;
99
100 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
101 sr_err("analog out: %s: ctx malloc failed", __func__);
102 return SR_ERR_MALLOC;
103 }
104
105 o->internal = ctx;
106 ctx->num_enabled_probes = 0;
107
108 for (l = o->dev->probes; l; l = l->next) {
109 probe = l->data;
110 if (!probe->enabled)
111 continue;
112 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
113 }
114
115 ctx->probelist[ctx->num_enabled_probes] = 0;
116 ctx->unitsize = sizeof(struct sr_analog_sample) +
117 (ctx->num_enabled_probes * sizeof(struct sr_analog_probe));
118 ctx->line_offset = 0;
119 ctx->spl_cnt = 0;
120 ctx->mark_trigger = -1;
121 ctx->mode = mode;
122
123 if (o->param && o->param[0]) {
124 ctx->samples_per_line = strtoul(o->param, NULL, 10);
125 if (ctx->samples_per_line < 1)
126 return SR_ERR;
127 } else
128 ctx->samples_per_line = default_spl;
129
130 if (!(ctx->header = g_try_malloc(512))) {
131 g_free(ctx);
132 sr_err("analog out: %s: ctx->header malloc failed", __func__);
133 return SR_ERR_MALLOC;
134 }
135
136 snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
137 num_probes = g_slist_length(o->dev->probes);
138 if (o->dev->plugin && sr_dev_has_hwcap(o->dev, SR_HWCAP_SAMPLERATE)) {
139 samplerate = *((uint64_t *) o->dev->plugin->get_dev_info(
140 o->dev->plugin_index, SR_DI_CUR_SAMPLERATE));
141 if (!(samplerate_s = sr_samplerate_string(samplerate))) {
142 g_free(ctx->header);
143 g_free(ctx);
144 return SR_ERR;
145 }
146 snprintf(ctx->header + strlen(ctx->header),
147 511 - strlen(ctx->header),
148 "Acquisition with %d/%d probes at %s\n",
149 ctx->num_enabled_probes, num_probes, samplerate_s);
150 g_free(samplerate_s);
151 }
152
153 ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
154 if (!(ctx->linebuf = g_try_malloc0(num_probes * ctx->linebuf_len))) {
155 g_free(ctx->header);
156 g_free(ctx);
157 sr_err("analog out: %s: ctx->linebuf malloc failed", __func__);
158 return SR_ERR_MALLOC;
159 }
160 if (!(ctx->linevalues = g_try_malloc0(num_probes))) {
161 g_free(ctx->header);
162 g_free(ctx);
163 sr_err("analog out: %s: ctx->linevalues malloc failed",
164 __func__);
165 return SR_ERR_MALLOC;
166 }
167
168 return SR_OK;
169}
170
171static int event(struct sr_output *o, int event_type, char **data_out,
172 uint64_t *length_out)
173{
174 struct context *ctx;
175 int outsize;
176 char *outbuf;
177
178 ctx = o->internal;
179 switch (event_type) {
180 case SR_DF_TRIGGER:
181 ctx->mark_trigger = ctx->spl_cnt;
182 *data_out = NULL;
183 *length_out = 0;
184 break;
185 case SR_DF_END:
186 outsize = ctx->num_enabled_probes
187 * (ctx->samples_per_line + 20) + 512;
188 if (!(outbuf = g_try_malloc0(outsize))) {
189 sr_err("analog out: %s: outbuf malloc failed",
190 __func__);
191 return SR_ERR_MALLOC;
192 }
193 flush_linebufs(ctx, outbuf);
194 *data_out = outbuf;
195 *length_out = strlen(outbuf);
196 g_free(o->internal);
197 o->internal = NULL;
198 break;
199 default:
200 *data_out = NULL;
201 *length_out = 0;
202 break;
203 }
204
205 return SR_OK;
206}
207
208static int init_bits(struct sr_output *o)
209{
210 return init(o, DEFAULT_BPL_BITS, MODE_BITS);
211}
212
213static int data_bits(struct sr_output *o, const char *data_in,
214 uint64_t length_in, char **data_out, uint64_t *length_out)
215{
216 struct context *ctx;
217 unsigned int outsize, offset, p;
218 int max_linelen;
219 struct sr_analog_sample *sample;
220 char *outbuf, c;
221
222 ctx = o->internal;
223 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
224 + ctx->samples_per_line / 8;
225 /*
226 * Calculate space needed for probes. Set aside 512 bytes for
227 * extra output, e.g. trigger.
228 */
229 outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
230 * (ctx->num_enabled_probes * max_linelen);
231
232 if (!(outbuf = g_try_malloc0(outsize + 1))) {
233 sr_err("analog out: %s: outbuf malloc failed", __func__);
234 return SR_ERR_MALLOC;
235 }
236
237 outbuf[0] = '\0';
238 if (ctx->header) {
239 /* The header is still here, this must be the first packet. */
240 strncpy(outbuf, ctx->header, outsize);
241 g_free(ctx->header);
242 ctx->header = NULL;
243
244 /* Ensure first transition. */
245// memcpy(&ctx->prevsample, data_in, ctx->unitsize);
246// ctx->prevsample = ~ctx->prevsample;
247 }
248
249 if (length_in >= ctx->unitsize) {
250 for (offset = 0; offset <= length_in - ctx->unitsize;
251 offset += ctx->unitsize) {
252 sample = (struct sr_analog_sample *) (data_in + offset);
253 for (p = 0; p < ctx->num_enabled_probes; p++) {
254 int val = sample->probes[p].val;
255 int res = sample->probes[p].res;
256 if (res == 1)
257 c = '0' + (val & ((1 << res) - 1));
258 else
259 /*
260 * Scale analog resolution down so it
261 * fits 25 letters
262 */
263 c = 'A' + (((val & ((1 << res) - 1)) /
264 (res * res)) / 10);
265 ctx->linebuf[p * ctx->linebuf_len +
266 ctx->line_offset] = c;
267 }
268 ctx->line_offset++;
269 ctx->spl_cnt++;
270
271 /* Add a space every 8th bit. */
272 if ((ctx->spl_cnt & 7) == 0) {
273 for (p = 0; p < ctx->num_enabled_probes; p++)
274 ctx->linebuf[p * ctx->linebuf_len +
275 ctx->line_offset] = ' ';
276 ctx->line_offset++;
277 }
278
279 /* End of line. */
280 if (ctx->spl_cnt >= ctx->samples_per_line) {
281 flush_linebufs(ctx, outbuf);
282 ctx->line_offset = ctx->spl_cnt = 0;
283 ctx->mark_trigger = -1;
284 }
285 }
286 } else {
287 sr_info("analog out: short buffer (length_in=%" PRIu64 ")",
288 length_in);
289 }
290
291 *data_out = outbuf;
292 *length_out = strlen(outbuf);
293
294 return SR_OK;
295}
296#if 0
297static int init_hex(struct sr_output *o)
298{
299 return init(o, DEFAULT_BPL_HEX, MODE_HEX);
300}
301
302static int data_hex(struct sr_output *o, const char *data_in,
303 uint64_t length_in, char **data_out, uint64_t *length_out)
304{
305 struct context *ctx;
306 unsigned int outsize, offset, p;
307 int max_linelen;
308 uint64_t sample;
309 char *outbuf;
310
311 ctx = o->internal;
312 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
313 + ctx->samples_per_line / 2;
314 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes
315 / ctx->samples_per_line * max_linelen + 512;
316
317 if (!(outbuf = g_try_malloc0(outsize + 1))) {
318 sr_err("analog out: %s: outbuf malloc failed", __func__);
319 return SR_ERR_MALLOC;
320 }
321
322 outbuf[0] = '\0';
323 if (ctx->header) {
324 /* The header is still here, this must be the first packet. */
325 strncpy(outbuf, ctx->header, outsize);
326 g_free(ctx->header);
327 ctx->header = NULL;
328 }
329
330 ctx->line_offset = 0;
331 for (offset = 0; offset <= length_in - ctx->unitsize;
332 offset += ctx->unitsize) {
333 memcpy(&sample, data_in + offset, ctx->unitsize);
334 for (p = 0; p < ctx->num_enabled_probes; p++) {
335 ctx->linevalues[p] <<= 1;
336 if (sample & ((uint64_t) 1 << p))
337 ctx->linevalues[p] |= 1;
338 sprintf(ctx->linebuf + (p * ctx->linebuf_len) +
339 ctx->line_offset, "%.2x", ctx->linevalues[p]);
340 }
341 ctx->spl_cnt++;
342
343 /* Add a space after every complete hex byte. */
344 if ((ctx->spl_cnt & 7) == 0) {
345 for (p = 0; p < ctx->num_enabled_probes; p++)
346 ctx->linebuf[p * ctx->linebuf_len +
347 ctx->line_offset + 2] = ' ';
348 ctx->line_offset += 3;
349 }
350
351 /* End of line. */
352 if (ctx->spl_cnt >= ctx->samples_per_line) {
353 flush_linebufs(ctx, outbuf);
354 ctx->line_offset = ctx->spl_cnt = 0;
355 }
356 }
357
358 *data_out = outbuf;
359 *length_out = strlen(outbuf);
360
361 return SR_OK;
362}
363
364static int init_ascii(struct sr_output *o)
365{
366 return init(o, DEFAULT_BPL_ASCII, MODE_ASCII);
367}
368
369static int data_ascii(struct sr_output *o, const char *data_in,
370 uint64_t length_in, char **data_out, uint64_t *length_out)
371{
372 struct context *ctx;
373 unsigned int outsize, offset, p;
374 int max_linelen;
375 uint64_t sample;
376 char *outbuf;
377
378 ctx = o->internal;
379 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
380 + ctx->samples_per_line / 8;
381 /*
382 * Calculate space needed for probes. Set aside 512 bytes for
383 * extra output, e.g. trigger.
384 */
385 outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
386 * (ctx->num_enabled_probes * max_linelen);
387
388 if (!(outbuf = g_try_malloc0(outsize + 1))) {
389 sr_err("analog out: %s: outbuf malloc failed", __func__);
390 return SR_ERR_MALLOC;
391 }
392
393 outbuf[0] = '\0';
394 if (ctx->header) {
395 /* The header is still here, this must be the first packet. */
396 strncpy(outbuf, ctx->header, outsize);
397 g_free(ctx->header);
398 ctx->header = NULL;
399 }
400
401 if (length_in >= ctx->unitsize) {
402 for (offset = 0; offset <= length_in - ctx->unitsize;
403 offset += ctx->unitsize) {
404 memcpy(&sample, data_in + offset, ctx->unitsize);
405
406 char tmpval[ctx->num_enabled_probes];
407
408 for (p = 0; p < ctx->num_enabled_probes; p++) {
409 uint64_t curbit = (sample & ((uint64_t) 1 << p));
410 uint64_t prevbit = (ctx->prevsample &
411 ((uint64_t) 1 << p));
412
413 if (curbit < prevbit && ctx->line_offset > 0) {
414 ctx->linebuf[p * ctx->linebuf_len +
415 ctx->line_offset-1] = '\\';
416 }
417
418 if (curbit > prevbit) {
419 tmpval[p] = '/';
420 } else {
421 if (curbit)
422 tmpval[p] = '"';
423 else
424 tmpval[p] = '.';
425 }
426 }
427
428 /* End of line. */
429 if (ctx->spl_cnt >= ctx->samples_per_line) {
430 flush_linebufs(ctx, outbuf);
431 ctx->line_offset = ctx->spl_cnt = 0;
432 ctx->mark_trigger = -1;
433 }
434
435 for (p = 0; p < ctx->num_enabled_probes; p++) {
436 ctx->linebuf[p * ctx->linebuf_len +
437 ctx->line_offset] = tmpval[p];
438 }
439
440 ctx->line_offset++;
441 ctx->spl_cnt++;
442
443 ctx->prevsample = sample;
444 }
445 } else {
446 sr_info("analog out: short buffer (length_in=%" PRIu64 ")",
447 length_in);
448 }
449
450 *data_out = outbuf;
451 *length_out = strlen(outbuf);
452
453 return SR_OK;
454}
455#endif
456
457SR_PRIV struct sr_output_format output_analog_bits = {
458 .id = "analog_bits",
459 .description = "Bits (takes argument, default 64)",
460 .df_type = SR_DF_ANALOG,
461 .init = init_bits,
462 .data = data_bits,
463 .event = event,
464};
465
466#if 0
467struct sr_output_format output_analog_hex = {
468 .id = "analog_hex",
469 .description = "Hexadecimal (takes argument, default 192)",
470 .df_type = SR_DF_ANALOG,
471 .init = init_hex,
472 .data = data_hex,
473 .event = event,
474};
475
476struct sr_output_format output_analog_ascii = {
477 .id = "analog_ascii",
478 .description = "ASCII (takes argument, default 74)",
479 .df_type = SR_DF_ANALOG,
480 .init = init_ascii,
481 .data = data_ascii,
482 .event = event,
483};
484#endif