]> sigrok.org Git - libsigrok.git/blame - output/analog.c
sr: adjust copyright year
[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;
f3163a6c 44 char *probelist[65];
1437e893
BV
45 char *linebuf;
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
1437e893
BV
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)
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
DR
107
108 for (l = o->device->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;
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);
f3163a6c 137 num_probes = g_slist_length(o->device->probes);
03168500 138 if (o->device->plugin && sr_dev_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) {
1437e893 139 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
5a2326a7 140 o->device->plugin_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
f50f3f40 171static int event(struct sr_output *o, int event_type, char **data_out,
f3163a6c 172 uint64_t *length_out)
1437e893
BV
173{
174 struct context *ctx;
f3163a6c
DR
175 int outsize;
176 char *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
54ac5277
UH
213static int data_bits(struct sr_output *o, const char *data_in,
214 uint64_t length_in, char **data_out, uint64_t *length_out)
f3163a6c
DR
215{
216 struct context *ctx;
217 unsigned int outsize, offset, p;
218 int max_linelen;
809c5f20 219 struct sr_analog_sample *sample;
f3163a6c
DR
220 char *outbuf, c;
221
222 ctx = o->internal;
9688b443 223 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
1437e893 224 + ctx->samples_per_line / 8;
f3163a6c
DR
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
133a37bf
UH
232 if (!(outbuf = g_try_malloc0(outsize + 1))) {
233 sr_err("analog out: %s: outbuf malloc failed", __func__);
e46b8fb1 234 return SR_ERR_MALLOC;
133a37bf 235 }
1437e893
BV
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);
133a37bf 241 g_free(ctx->header);
1437e893 242 ctx->header = NULL;
f3163a6c
DR
243
244 /* Ensure first transition. */
6eb0e3ea
DR
245// memcpy(&ctx->prevsample, data_in, ctx->unitsize);
246// ctx->prevsample = ~ctx->prevsample;
1437e893
BV
247 }
248
249 if (length_in >= ctx->unitsize) {
250 for (offset = 0; offset <= length_in - ctx->unitsize;
251 offset += ctx->unitsize) {
809c5f20 252 sample = (struct sr_analog_sample *) (data_in + offset);
1437e893 253 for (p = 0; p < ctx->num_enabled_probes; p++) {
6eb0e3ea
DR
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);
f3163a6c
DR
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++;
1437e893
BV
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 }
1437e893
BV
285 }
286 } else {
b08024a8 287 sr_info("short buffer (length_in=%" PRIu64 ")", length_in);
1437e893
BV
288 }
289
290 *data_out = outbuf;
291 *length_out = strlen(outbuf);
292
e46b8fb1 293 return SR_OK;
1437e893 294}
6eb0e3ea 295#if 0
f50f3f40 296static int init_hex(struct sr_output *o)
f3163a6c
DR
297{
298 return init(o, DEFAULT_BPL_HEX, MODE_HEX);
299}
300
54ac5277
UH
301static int data_hex(struct sr_output *o, const char *data_in,
302 uint64_t length_in, char **data_out, uint64_t *length_out)
1437e893
BV
303{
304 struct context *ctx;
f3163a6c
DR
305 unsigned int outsize, offset, p;
306 int max_linelen;
307 uint64_t sample;
308 char *outbuf;
1437e893
BV
309
310 ctx = o->internal;
9688b443 311 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
f3163a6c
DR
312 + ctx->samples_per_line / 2;
313 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes
314 / ctx->samples_per_line * max_linelen + 512;
315
133a37bf
UH
316 if (!(outbuf = g_try_malloc0(outsize + 1))) {
317 sr_err("analog out: %s: outbuf malloc failed", __func__);
e46b8fb1 318 return SR_ERR_MALLOC;
133a37bf 319 }
f3163a6c
DR
320
321 outbuf[0] = '\0';
322 if (ctx->header) {
323 /* The header is still here, this must be the first packet. */
324 strncpy(outbuf, ctx->header, outsize);
133a37bf 325 g_free(ctx->header);
f3163a6c 326 ctx->header = NULL;
1437e893
BV
327 }
328
f3163a6c
DR
329 ctx->line_offset = 0;
330 for (offset = 0; offset <= length_in - ctx->unitsize;
331 offset += ctx->unitsize) {
332 memcpy(&sample, data_in + offset, ctx->unitsize);
333 for (p = 0; p < ctx->num_enabled_probes; p++) {
334 ctx->linevalues[p] <<= 1;
335 if (sample & ((uint64_t) 1 << p))
336 ctx->linevalues[p] |= 1;
337 sprintf(ctx->linebuf + (p * ctx->linebuf_len) +
338 ctx->line_offset, "%.2x", ctx->linevalues[p]);
339 }
340 ctx->spl_cnt++;
341
342 /* Add a space after every complete hex byte. */
343 if ((ctx->spl_cnt & 7) == 0) {
344 for (p = 0; p < ctx->num_enabled_probes; p++)
345 ctx->linebuf[p * ctx->linebuf_len +
346 ctx->line_offset + 2] = ' ';
347 ctx->line_offset += 3;
348 }
349
350 /* End of line. */
351 if (ctx->spl_cnt >= ctx->samples_per_line) {
352 flush_linebufs(ctx, outbuf);
353 ctx->line_offset = ctx->spl_cnt = 0;
354 }
355 }
356
357 *data_out = outbuf;
358 *length_out = strlen(outbuf);
1437e893 359
e46b8fb1 360 return SR_OK;
1437e893
BV
361}
362
f50f3f40 363static int init_ascii(struct sr_output *o)
f3163a6c
DR
364{
365 return init(o, DEFAULT_BPL_ASCII, MODE_ASCII);
366}
367
54ac5277
UH
368static int data_ascii(struct sr_output *o, const char *data_in,
369 uint64_t length_in, char **data_out, uint64_t *length_out)
f3163a6c
DR
370{
371 struct context *ctx;
372 unsigned int outsize, offset, p;
373 int max_linelen;
374 uint64_t sample;
375 char *outbuf;
376
377 ctx = o->internal;
9688b443 378 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
f3163a6c
DR
379 + ctx->samples_per_line / 8;
380 /*
381 * Calculate space needed for probes. Set aside 512 bytes for
382 * extra output, e.g. trigger.
383 */
384 outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
385 * (ctx->num_enabled_probes * max_linelen);
386
133a37bf
UH
387 if (!(outbuf = g_try_malloc0(outsize + 1))) {
388 sr_err("analog out: %s: outbuf malloc failed", __func__);
e46b8fb1 389 return SR_ERR_MALLOC;
133a37bf 390 }
f3163a6c
DR
391
392 outbuf[0] = '\0';
393 if (ctx->header) {
394 /* The header is still here, this must be the first packet. */
395 strncpy(outbuf, ctx->header, outsize);
133a37bf 396 g_free(ctx->header);
f3163a6c
DR
397 ctx->header = NULL;
398 }
399
400 if (length_in >= ctx->unitsize) {
401 for (offset = 0; offset <= length_in - ctx->unitsize;
402 offset += ctx->unitsize) {
403 memcpy(&sample, data_in + offset, ctx->unitsize);
404
405 char tmpval[ctx->num_enabled_probes];
406
407 for (p = 0; p < ctx->num_enabled_probes; p++) {
408 uint64_t curbit = (sample & ((uint64_t) 1 << p));
409 uint64_t prevbit = (ctx->prevsample &
410 ((uint64_t) 1 << p));
411
412 if (curbit < prevbit && ctx->line_offset > 0) {
413 ctx->linebuf[p * ctx->linebuf_len +
414 ctx->line_offset-1] = '\\';
415 }
416
417 if (curbit > prevbit) {
418 tmpval[p] = '/';
419 } else {
420 if (curbit)
421 tmpval[p] = '"';
422 else
423 tmpval[p] = '.';
424 }
425 }
426
427 /* End of line. */
428 if (ctx->spl_cnt >= ctx->samples_per_line) {
429 flush_linebufs(ctx, outbuf);
430 ctx->line_offset = ctx->spl_cnt = 0;
431 ctx->mark_trigger = -1;
432 }
433
434 for (p = 0; p < ctx->num_enabled_probes; p++) {
435 ctx->linebuf[p * ctx->linebuf_len +
436 ctx->line_offset] = tmpval[p];
437 }
438
439 ctx->line_offset++;
440 ctx->spl_cnt++;
441
442 ctx->prevsample = sample;
443 }
444 } else {
b08024a8 445 sr_info("short buffer (length_in=%" PRIu64 ")", length_in);
f3163a6c
DR
446 }
447
448 *data_out = outbuf;
449 *length_out = strlen(outbuf);
450
e46b8fb1 451 return SR_OK;
f3163a6c 452}
6eb0e3ea 453#endif
f3163a6c 454
7c1d391c 455SR_PRIV struct sr_output_format output_analog_bits = {
cdb3573c 456 .id = "analog_bits",
d494a4aa
UH
457 .description = "Bits (takes argument, default 64)",
458 .df_type = SR_DF_ANALOG,
459 .init = init_bits,
460 .data = data_bits,
461 .event = event,
1437e893 462};
7c1d391c 463
6eb0e3ea 464#if 0
f50f3f40 465struct sr_output_format output_analog_hex = {
cdb3573c 466 .id = "analog_hex",
d494a4aa
UH
467 .description = "Hexadecimal (takes argument, default 192)",
468 .df_type = SR_DF_ANALOG,
469 .init = init_hex,
470 .data = data_hex,
471 .event = event,
f3163a6c
DR
472};
473
f50f3f40 474struct sr_output_format output_analog_ascii = {
cdb3573c 475 .id = "analog_ascii",
d494a4aa
UH
476 .description = "ASCII (takes argument, default 74)",
477 .df_type = SR_DF_ANALOG,
478 .init = init_ascii,
479 .data = data_ascii,
480 .event = event,
f3163a6c 481};
6eb0e3ea 482#endif