]> sigrok.org Git - libsigrok.git/blame - output/analog.c
sr: fx2lafw: Forgot to add (C) line to fx2lafw.h in recent commit.
[libsigrok.git] / output / analog.c
CommitLineData
1437e893
BV
1/*
2 * This file is part of the sigrok project.
3 *
c73d2ea4 4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
f3163a6c 5 * Copyright (C) 2011 HÃ¥vard Espeland <gus@ping.uio.no>
6eb0e3ea 6 * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com>
1437e893
BV
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
1437e893 22#include <stdlib.h>
f3163a6c 23#include <stdio.h>
1437e893 24#include <string.h>
f3163a6c 25#include <glib.h>
cd315a80 26#include "sigrok.h"
1437e893 27
f3163a6c
DR
28#define DEFAULT_BPL_BITS 64
29#define DEFAULT_BPL_HEX 192
30#define DEFAULT_BPL_ASCII 74
1437e893 31
f3163a6c
DR
32enum outputmode {
33 MODE_BITS = 1,
34 MODE_HEX,
35 MODE_ASCII,
36};
1437e893
BV
37
38struct context {
1437e893 39 unsigned int num_enabled_probes;
1437e893
BV
40 int samples_per_line;
41 unsigned int unitsize;
42 int line_offset;
43 int linebuf_len;
8ec95d22 44 char *probelist[SR_MAX_NUM_PROBES + 1];
054e6709 45 uint8_t *linebuf;
1437e893 46 int spl_cnt;
f3163a6c
DR
47 uint8_t *linevalues;
48 char *header;
1437e893 49 int mark_trigger;
809c5f20 50// struct sr_analog_sample *prevsample;
f3163a6c 51 enum outputmode mode;
1437e893
BV
52};
53
054e6709 54static void flush_linebufs(struct context *ctx, uint8_t *outbuf)
1437e893
BV
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)
f3163a6c
DR
78 {
79 int space_offset = ctx->mark_trigger / 8;
80
81 if (ctx->mode == MODE_ASCII)
82 space_offset = 0;
83
1437e893 84 sprintf(outbuf + strlen(outbuf), "T:%*s^\n",
f3163a6c
DR
85 ctx->mark_trigger + space_offset, "");
86 }
1437e893
BV
87
88 memset(ctx->linebuf, 0, i * ctx->linebuf_len);
89}
90
f50f3f40 91static int init(struct sr_output *o, int default_spl, enum outputmode mode)
1437e893
BV
92{
93 struct context *ctx;
1afe8989 94 struct sr_probe *probe;
1437e893
BV
95 GSList *l;
96 uint64_t samplerate;
97 int num_probes;
98 char *samplerate_s;
99
133a37bf
UH
100 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
101 sr_err("analog out: %s: ctx malloc failed", __func__);
e46b8fb1 102 return SR_ERR_MALLOC;
133a37bf 103 }
1437e893 104
1437e893 105 o->internal = ctx;
1437e893 106 ctx->num_enabled_probes = 0;
f3163a6c 107
bb7ef793 108 for (l = o->dev->probes; l; l = l->next) {
f3163a6c
DR
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;
809c5f20
UH
116 ctx->unitsize = sizeof(struct sr_analog_sample) +
117 (ctx->num_enabled_probes * sizeof(struct sr_analog_probe));
f3163a6c
DR
118 ctx->line_offset = 0;
119 ctx->spl_cnt = 0;
1437e893 120 ctx->mark_trigger = -1;
f3163a6c
DR
121 ctx->mode = mode;
122
9b36e360
BV
123 if (o->param && o->param[0]) {
124 ctx->samples_per_line = strtoul(o->param, NULL, 10);
125 if (ctx->samples_per_line < 1)
e46b8fb1 126 return SR_ERR;
9b36e360 127 } else
f3163a6c 128 ctx->samples_per_line = default_spl;
9b36e360 129
133a37bf
UH
130 if (!(ctx->header = g_try_malloc(512))) {
131 g_free(ctx);
132 sr_err("analog out: %s: ctx->header malloc failed", __func__);
e46b8fb1 133 return SR_ERR_MALLOC;
1437e893 134 }
1437e893
BV
135
136 snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
bb7ef793 137 num_probes = g_slist_length(o->dev->probes);
c09f0b57
UH
138 if (o->dev->driver && sr_dev_has_hwcap(o->dev, SR_HWCAP_SAMPLERATE)) {
139 samplerate = *((uint64_t *) o->dev->driver->dev_info_get(
140 o->dev->driver_index, SR_DI_CUR_SAMPLERATE));
a00ba012 141 if (!(samplerate_s = sr_samplerate_string(samplerate))) {
133a37bf
UH
142 g_free(ctx->header);
143 g_free(ctx);
e46b8fb1 144 return SR_ERR;
1437e893
BV
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);
133a37bf 150 g_free(samplerate_s);
1437e893
BV
151 }
152
f3163a6c 153 ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
133a37bf
UH
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__);
e46b8fb1 158 return SR_ERR_MALLOC;
f3163a6c 159 }
133a37bf
UH
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__);
e46b8fb1 165 return SR_ERR_MALLOC;
1437e893
BV
166 }
167
e46b8fb1 168 return SR_OK;
1437e893
BV
169}
170
054e6709 171static int event(struct sr_output *o, int event_type, uint8_t **data_out,
f3163a6c 172 uint64_t *length_out)
1437e893
BV
173{
174 struct context *ctx;
f3163a6c 175 int outsize;
054e6709 176 uint8_t *outbuf;
1437e893
BV
177
178 ctx = o->internal;
f3163a6c 179 switch (event_type) {
5a2326a7 180 case SR_DF_TRIGGER:
f3163a6c
DR
181 ctx->mark_trigger = ctx->spl_cnt;
182 *data_out = NULL;
183 *length_out = 0;
184 break;
5a2326a7 185 case SR_DF_END:
f3163a6c
DR
186 outsize = ctx->num_enabled_probes
187 * (ctx->samples_per_line + 20) + 512;
133a37bf
UH
188 if (!(outbuf = g_try_malloc0(outsize))) {
189 sr_err("analog out: %s: outbuf malloc failed",
190 __func__);
e46b8fb1 191 return SR_ERR_MALLOC;
133a37bf 192 }
f3163a6c
DR
193 flush_linebufs(ctx, outbuf);
194 *data_out = outbuf;
195 *length_out = strlen(outbuf);
133a37bf 196 g_free(o->internal);
f3163a6c
DR
197 o->internal = NULL;
198 break;
199 default:
200 *data_out = NULL;
201 *length_out = 0;
202 break;
203 }
204
e46b8fb1 205 return SR_OK;
f3163a6c
DR
206}
207
f50f3f40 208static int init_bits(struct sr_output *o)
f3163a6c
DR
209{
210 return init(o, DEFAULT_BPL_BITS, MODE_BITS);
211}
212
054e6709
UH
213static int data_bits(struct sr_output *o, const uint8_t *data_in,
214 uint64_t length_in, uint8_t **data_out,
215 uint64_t *length_out)
f3163a6c
DR
216{
217 struct context *ctx;
218 unsigned int outsize, offset, p;
219 int max_linelen;
809c5f20 220 struct sr_analog_sample *sample;
054e6709 221 uint8_t *outbuf, c;
f3163a6c
DR
222
223 ctx = o->internal;
9688b443 224 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
1437e893 225 + ctx->samples_per_line / 8;
f3163a6c
DR
226 /*
227 * Calculate space needed for probes. Set aside 512 bytes for
228 * extra output, e.g. trigger.
229 */
230 outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
231 * (ctx->num_enabled_probes * max_linelen);
232
133a37bf
UH
233 if (!(outbuf = g_try_malloc0(outsize + 1))) {
234 sr_err("analog out: %s: outbuf malloc failed", __func__);
e46b8fb1 235 return SR_ERR_MALLOC;
133a37bf 236 }
1437e893
BV
237
238 outbuf[0] = '\0';
239 if (ctx->header) {
240 /* The header is still here, this must be the first packet. */
241 strncpy(outbuf, ctx->header, outsize);
133a37bf 242 g_free(ctx->header);
1437e893 243 ctx->header = NULL;
f3163a6c
DR
244
245 /* Ensure first transition. */
6eb0e3ea
DR
246// memcpy(&ctx->prevsample, data_in, ctx->unitsize);
247// ctx->prevsample = ~ctx->prevsample;
1437e893
BV
248 }
249
250 if (length_in >= ctx->unitsize) {
251 for (offset = 0; offset <= length_in - ctx->unitsize;
252 offset += ctx->unitsize) {
809c5f20 253 sample = (struct sr_analog_sample *) (data_in + offset);
1437e893 254 for (p = 0; p < ctx->num_enabled_probes; p++) {
6eb0e3ea
DR
255 int val = sample->probes[p].val;
256 int res = sample->probes[p].res;
257 if (res == 1)
258 c = '0' + (val & ((1 << res) - 1));
259 else
260 /*
261 * Scale analog resolution down so it
262 * fits 25 letters
263 */
264 c = 'A' + (((val & ((1 << res) - 1)) /
265 (res * res)) / 10);
f3163a6c
DR
266 ctx->linebuf[p * ctx->linebuf_len +
267 ctx->line_offset] = c;
268 }
269 ctx->line_offset++;
270 ctx->spl_cnt++;
271
272 /* Add a space every 8th bit. */
273 if ((ctx->spl_cnt & 7) == 0) {
274 for (p = 0; p < ctx->num_enabled_probes; p++)
275 ctx->linebuf[p * ctx->linebuf_len +
276 ctx->line_offset] = ' ';
277 ctx->line_offset++;
1437e893
BV
278 }
279
280 /* End of line. */
281 if (ctx->spl_cnt >= ctx->samples_per_line) {
282 flush_linebufs(ctx, outbuf);
283 ctx->line_offset = ctx->spl_cnt = 0;
284 ctx->mark_trigger = -1;
285 }
1437e893
BV
286 }
287 } else {
7b48d6e1
UH
288 sr_info("analog out: short buffer (length_in=%" PRIu64 ")",
289 length_in);
1437e893
BV
290 }
291
292 *data_out = outbuf;
293 *length_out = strlen(outbuf);
294
e46b8fb1 295 return SR_OK;
1437e893 296}
6eb0e3ea 297#if 0
f50f3f40 298static int init_hex(struct sr_output *o)
f3163a6c
DR
299{
300 return init(o, DEFAULT_BPL_HEX, MODE_HEX);
301}
302
054e6709
UH
303static int data_hex(struct sr_output *o, const uint8_t *data_in,
304 uint64_t length_in, uint8_t **data_out,
305 uint64_t *length_out)
1437e893
BV
306{
307 struct context *ctx;
f3163a6c
DR
308 unsigned int outsize, offset, p;
309 int max_linelen;
310 uint64_t sample;
054e6709 311 uint8_t *outbuf;
1437e893
BV
312
313 ctx = o->internal;
9688b443 314 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
f3163a6c
DR
315 + ctx->samples_per_line / 2;
316 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes
317 / ctx->samples_per_line * max_linelen + 512;
318
133a37bf
UH
319 if (!(outbuf = g_try_malloc0(outsize + 1))) {
320 sr_err("analog out: %s: outbuf malloc failed", __func__);
e46b8fb1 321 return SR_ERR_MALLOC;
133a37bf 322 }
f3163a6c
DR
323
324 outbuf[0] = '\0';
325 if (ctx->header) {
326 /* The header is still here, this must be the first packet. */
327 strncpy(outbuf, ctx->header, outsize);
133a37bf 328 g_free(ctx->header);
f3163a6c 329 ctx->header = NULL;
1437e893
BV
330 }
331
f3163a6c
DR
332 ctx->line_offset = 0;
333 for (offset = 0; offset <= length_in - ctx->unitsize;
334 offset += ctx->unitsize) {
335 memcpy(&sample, data_in + offset, ctx->unitsize);
336 for (p = 0; p < ctx->num_enabled_probes; p++) {
337 ctx->linevalues[p] <<= 1;
338 if (sample & ((uint64_t) 1 << p))
339 ctx->linevalues[p] |= 1;
340 sprintf(ctx->linebuf + (p * ctx->linebuf_len) +
341 ctx->line_offset, "%.2x", ctx->linevalues[p]);
342 }
343 ctx->spl_cnt++;
344
345 /* Add a space after every complete hex byte. */
346 if ((ctx->spl_cnt & 7) == 0) {
347 for (p = 0; p < ctx->num_enabled_probes; p++)
348 ctx->linebuf[p * ctx->linebuf_len +
349 ctx->line_offset + 2] = ' ';
350 ctx->line_offset += 3;
351 }
352
353 /* End of line. */
354 if (ctx->spl_cnt >= ctx->samples_per_line) {
355 flush_linebufs(ctx, outbuf);
356 ctx->line_offset = ctx->spl_cnt = 0;
357 }
358 }
359
360 *data_out = outbuf;
361 *length_out = strlen(outbuf);
1437e893 362
e46b8fb1 363 return SR_OK;
1437e893
BV
364}
365
f50f3f40 366static int init_ascii(struct sr_output *o)
f3163a6c
DR
367{
368 return init(o, DEFAULT_BPL_ASCII, MODE_ASCII);
369}
370
054e6709
UH
371static int data_ascii(struct sr_output *o, const uint8_t *data_in,
372 uint64_t length_in, uint8_t **data_out,
373 uint64_t *length_out)
f3163a6c
DR
374{
375 struct context *ctx;
376 unsigned int outsize, offset, p;
377 int max_linelen;
378 uint64_t sample;
054e6709 379 uint8_t *outbuf;
f3163a6c
DR
380
381 ctx = o->internal;
9688b443 382 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
f3163a6c
DR
383 + ctx->samples_per_line / 8;
384 /*
385 * Calculate space needed for probes. Set aside 512 bytes for
386 * extra output, e.g. trigger.
387 */
388 outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
389 * (ctx->num_enabled_probes * max_linelen);
390
133a37bf
UH
391 if (!(outbuf = g_try_malloc0(outsize + 1))) {
392 sr_err("analog out: %s: outbuf malloc failed", __func__);
e46b8fb1 393 return SR_ERR_MALLOC;
133a37bf 394 }
f3163a6c
DR
395
396 outbuf[0] = '\0';
397 if (ctx->header) {
398 /* The header is still here, this must be the first packet. */
399 strncpy(outbuf, ctx->header, outsize);
133a37bf 400 g_free(ctx->header);
f3163a6c
DR
401 ctx->header = NULL;
402 }
403
404 if (length_in >= ctx->unitsize) {
405 for (offset = 0; offset <= length_in - ctx->unitsize;
406 offset += ctx->unitsize) {
407 memcpy(&sample, data_in + offset, ctx->unitsize);
408
409 char tmpval[ctx->num_enabled_probes];
410
411 for (p = 0; p < ctx->num_enabled_probes; p++) {
412 uint64_t curbit = (sample & ((uint64_t) 1 << p));
413 uint64_t prevbit = (ctx->prevsample &
414 ((uint64_t) 1 << p));
415
416 if (curbit < prevbit && ctx->line_offset > 0) {
417 ctx->linebuf[p * ctx->linebuf_len +
418 ctx->line_offset-1] = '\\';
419 }
420
421 if (curbit > prevbit) {
422 tmpval[p] = '/';
423 } else {
424 if (curbit)
425 tmpval[p] = '"';
426 else
427 tmpval[p] = '.';
428 }
429 }
430
431 /* End of line. */
432 if (ctx->spl_cnt >= ctx->samples_per_line) {
433 flush_linebufs(ctx, outbuf);
434 ctx->line_offset = ctx->spl_cnt = 0;
435 ctx->mark_trigger = -1;
436 }
437
438 for (p = 0; p < ctx->num_enabled_probes; p++) {
439 ctx->linebuf[p * ctx->linebuf_len +
440 ctx->line_offset] = tmpval[p];
441 }
442
443 ctx->line_offset++;
444 ctx->spl_cnt++;
445
446 ctx->prevsample = sample;
447 }
448 } else {
7b48d6e1
UH
449 sr_info("analog out: short buffer (length_in=%" PRIu64 ")",
450 length_in);
f3163a6c
DR
451 }
452
453 *data_out = outbuf;
454 *length_out = strlen(outbuf);
455
e46b8fb1 456 return SR_OK;
f3163a6c 457}
6eb0e3ea 458#endif
f3163a6c 459
7c1d391c 460SR_PRIV struct sr_output_format output_analog_bits = {
cdb3573c 461 .id = "analog_bits",
2e7cb004 462 .description = "Bits",
d494a4aa
UH
463 .df_type = SR_DF_ANALOG,
464 .init = init_bits,
465 .data = data_bits,
466 .event = event,
1437e893 467};
7c1d391c 468
6eb0e3ea 469#if 0
f50f3f40 470struct sr_output_format output_analog_hex = {
cdb3573c 471 .id = "analog_hex",
2e7cb004 472 .description = "Hexadecimal",
d494a4aa
UH
473 .df_type = SR_DF_ANALOG,
474 .init = init_hex,
475 .data = data_hex,
476 .event = event,
f3163a6c
DR
477};
478
f50f3f40 479struct sr_output_format output_analog_ascii = {
cdb3573c 480 .id = "analog_ascii",
2e7cb004 481 .description = "ASCII",
d494a4aa
UH
482 .df_type = SR_DF_ANALOG,
483 .init = init_ascii,
484 .data = data_ascii,
485 .event = event,
f3163a6c 486};
6eb0e3ea 487#endif