]> sigrok.org Git - libsigrok.git/blame - output/analog.c
sr: Mark API functions with SR_API/SR_PRIV.
[libsigrok.git] / output / analog.c
CommitLineData
1437e893
BV
1/*
2 * This file is part of the sigrok project.
3 *
f3163a6c
DR
4 * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
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
100 if (!(ctx = calloc(1, sizeof(struct context))))
e46b8fb1 101 return SR_ERR_MALLOC;
1437e893 102
1437e893 103 o->internal = ctx;
1437e893 104 ctx->num_enabled_probes = 0;
f3163a6c
DR
105
106 for (l = o->device->probes; l; l = l->next) {
107 probe = l->data;
108 if (!probe->enabled)
109 continue;
110 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
111 }
112
113 ctx->probelist[ctx->num_enabled_probes] = 0;
809c5f20
UH
114 ctx->unitsize = sizeof(struct sr_analog_sample) +
115 (ctx->num_enabled_probes * sizeof(struct sr_analog_probe));
f3163a6c
DR
116 ctx->line_offset = 0;
117 ctx->spl_cnt = 0;
1437e893 118 ctx->mark_trigger = -1;
f3163a6c
DR
119 ctx->mode = mode;
120
9b36e360
BV
121 if (o->param && o->param[0]) {
122 ctx->samples_per_line = strtoul(o->param, NULL, 10);
123 if (ctx->samples_per_line < 1)
e46b8fb1 124 return SR_ERR;
9b36e360 125 } else
f3163a6c 126 ctx->samples_per_line = default_spl;
9b36e360 127
f3163a6c
DR
128 if (!(ctx->header = malloc(512))) {
129 free(ctx);
e46b8fb1 130 return SR_ERR_MALLOC;
1437e893 131 }
1437e893
BV
132
133 snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
f3163a6c 134 num_probes = g_slist_length(o->device->probes);
2bf4aca6 135 if (o->device->plugin && sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) {
1437e893 136 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
5a2326a7 137 o->device->plugin_index, SR_DI_CUR_SAMPLERATE));
a00ba012 138 if (!(samplerate_s = sr_samplerate_string(samplerate))) {
1437e893
BV
139 free(ctx->header);
140 free(ctx);
e46b8fb1 141 return SR_ERR;
1437e893
BV
142 }
143 snprintf(ctx->header + strlen(ctx->header),
144 511 - strlen(ctx->header),
145 "Acquisition with %d/%d probes at %s\n",
146 ctx->num_enabled_probes, num_probes, samplerate_s);
147 free(samplerate_s);
148 }
149
f3163a6c
DR
150 ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
151 if (!(ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len))) {
152 free(ctx->header);
153 free(ctx);
e46b8fb1 154 return SR_ERR_MALLOC;
f3163a6c
DR
155 }
156 if (!(ctx->linevalues = calloc(1, num_probes))) {
1437e893
BV
157 free(ctx->header);
158 free(ctx);
e46b8fb1 159 return SR_ERR_MALLOC;
1437e893
BV
160 }
161
e46b8fb1 162 return SR_OK;
1437e893
BV
163}
164
f50f3f40 165static int event(struct sr_output *o, int event_type, char **data_out,
f3163a6c 166 uint64_t *length_out)
1437e893
BV
167{
168 struct context *ctx;
f3163a6c
DR
169 int outsize;
170 char *outbuf;
1437e893
BV
171
172 ctx = o->internal;
f3163a6c 173 switch (event_type) {
5a2326a7 174 case SR_DF_TRIGGER:
f3163a6c
DR
175 ctx->mark_trigger = ctx->spl_cnt;
176 *data_out = NULL;
177 *length_out = 0;
178 break;
5a2326a7 179 case SR_DF_END:
f3163a6c
DR
180 outsize = ctx->num_enabled_probes
181 * (ctx->samples_per_line + 20) + 512;
182 if (!(outbuf = calloc(1, outsize)))
e46b8fb1 183 return SR_ERR_MALLOC;
f3163a6c
DR
184 flush_linebufs(ctx, outbuf);
185 *data_out = outbuf;
186 *length_out = strlen(outbuf);
187 free(o->internal);
188 o->internal = NULL;
189 break;
190 default:
191 *data_out = NULL;
192 *length_out = 0;
193 break;
194 }
195
e46b8fb1 196 return SR_OK;
f3163a6c
DR
197}
198
f50f3f40 199static int init_bits(struct sr_output *o)
f3163a6c
DR
200{
201 return init(o, DEFAULT_BPL_BITS, MODE_BITS);
202}
203
54ac5277
UH
204static int data_bits(struct sr_output *o, const char *data_in,
205 uint64_t length_in, char **data_out, uint64_t *length_out)
f3163a6c
DR
206{
207 struct context *ctx;
208 unsigned int outsize, offset, p;
209 int max_linelen;
809c5f20 210 struct sr_analog_sample *sample;
f3163a6c
DR
211 char *outbuf, c;
212
213 ctx = o->internal;
9688b443 214 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
1437e893 215 + ctx->samples_per_line / 8;
f3163a6c
DR
216 /*
217 * Calculate space needed for probes. Set aside 512 bytes for
218 * extra output, e.g. trigger.
219 */
220 outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
221 * (ctx->num_enabled_probes * max_linelen);
222
1437e893 223 if (!(outbuf = calloc(1, outsize + 1)))
e46b8fb1 224 return SR_ERR_MALLOC;
1437e893
BV
225
226 outbuf[0] = '\0';
227 if (ctx->header) {
228 /* The header is still here, this must be the first packet. */
229 strncpy(outbuf, ctx->header, outsize);
230 free(ctx->header);
231 ctx->header = NULL;
f3163a6c
DR
232
233 /* Ensure first transition. */
6eb0e3ea
DR
234// memcpy(&ctx->prevsample, data_in, ctx->unitsize);
235// ctx->prevsample = ~ctx->prevsample;
1437e893
BV
236 }
237
238 if (length_in >= ctx->unitsize) {
239 for (offset = 0; offset <= length_in - ctx->unitsize;
240 offset += ctx->unitsize) {
809c5f20 241 sample = (struct sr_analog_sample *) (data_in + offset);
1437e893 242 for (p = 0; p < ctx->num_enabled_probes; p++) {
6eb0e3ea
DR
243 int val = sample->probes[p].val;
244 int res = sample->probes[p].res;
245 if (res == 1)
246 c = '0' + (val & ((1 << res) - 1));
247 else
248 /*
249 * Scale analog resolution down so it
250 * fits 25 letters
251 */
252 c = 'A' + (((val & ((1 << res) - 1)) /
253 (res * res)) / 10);
f3163a6c
DR
254 ctx->linebuf[p * ctx->linebuf_len +
255 ctx->line_offset] = c;
256 }
257 ctx->line_offset++;
258 ctx->spl_cnt++;
259
260 /* Add a space every 8th bit. */
261 if ((ctx->spl_cnt & 7) == 0) {
262 for (p = 0; p < ctx->num_enabled_probes; p++)
263 ctx->linebuf[p * ctx->linebuf_len +
264 ctx->line_offset] = ' ';
265 ctx->line_offset++;
1437e893
BV
266 }
267
268 /* End of line. */
269 if (ctx->spl_cnt >= ctx->samples_per_line) {
270 flush_linebufs(ctx, outbuf);
271 ctx->line_offset = ctx->spl_cnt = 0;
272 ctx->mark_trigger = -1;
273 }
1437e893
BV
274 }
275 } else {
b08024a8 276 sr_info("short buffer (length_in=%" PRIu64 ")", length_in);
1437e893
BV
277 }
278
279 *data_out = outbuf;
280 *length_out = strlen(outbuf);
281
e46b8fb1 282 return SR_OK;
1437e893 283}
6eb0e3ea 284#if 0
f50f3f40 285static int init_hex(struct sr_output *o)
f3163a6c
DR
286{
287 return init(o, DEFAULT_BPL_HEX, MODE_HEX);
288}
289
54ac5277
UH
290static int data_hex(struct sr_output *o, const char *data_in,
291 uint64_t length_in, char **data_out, uint64_t *length_out)
1437e893
BV
292{
293 struct context *ctx;
f3163a6c
DR
294 unsigned int outsize, offset, p;
295 int max_linelen;
296 uint64_t sample;
297 char *outbuf;
1437e893
BV
298
299 ctx = o->internal;
9688b443 300 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
f3163a6c
DR
301 + ctx->samples_per_line / 2;
302 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes
303 / ctx->samples_per_line * max_linelen + 512;
304
305 if (!(outbuf = calloc(1, outsize + 1)))
e46b8fb1 306 return SR_ERR_MALLOC;
f3163a6c
DR
307
308 outbuf[0] = '\0';
309 if (ctx->header) {
310 /* The header is still here, this must be the first packet. */
311 strncpy(outbuf, ctx->header, outsize);
312 free(ctx->header);
313 ctx->header = NULL;
1437e893
BV
314 }
315
f3163a6c
DR
316 ctx->line_offset = 0;
317 for (offset = 0; offset <= length_in - ctx->unitsize;
318 offset += ctx->unitsize) {
319 memcpy(&sample, data_in + offset, ctx->unitsize);
320 for (p = 0; p < ctx->num_enabled_probes; p++) {
321 ctx->linevalues[p] <<= 1;
322 if (sample & ((uint64_t) 1 << p))
323 ctx->linevalues[p] |= 1;
324 sprintf(ctx->linebuf + (p * ctx->linebuf_len) +
325 ctx->line_offset, "%.2x", ctx->linevalues[p]);
326 }
327 ctx->spl_cnt++;
328
329 /* Add a space after every complete hex byte. */
330 if ((ctx->spl_cnt & 7) == 0) {
331 for (p = 0; p < ctx->num_enabled_probes; p++)
332 ctx->linebuf[p * ctx->linebuf_len +
333 ctx->line_offset + 2] = ' ';
334 ctx->line_offset += 3;
335 }
336
337 /* End of line. */
338 if (ctx->spl_cnt >= ctx->samples_per_line) {
339 flush_linebufs(ctx, outbuf);
340 ctx->line_offset = ctx->spl_cnt = 0;
341 }
342 }
343
344 *data_out = outbuf;
345 *length_out = strlen(outbuf);
1437e893 346
e46b8fb1 347 return SR_OK;
1437e893
BV
348}
349
f50f3f40 350static int init_ascii(struct sr_output *o)
f3163a6c
DR
351{
352 return init(o, DEFAULT_BPL_ASCII, MODE_ASCII);
353}
354
54ac5277
UH
355static int data_ascii(struct sr_output *o, const char *data_in,
356 uint64_t length_in, char **data_out, uint64_t *length_out)
f3163a6c
DR
357{
358 struct context *ctx;
359 unsigned int outsize, offset, p;
360 int max_linelen;
361 uint64_t sample;
362 char *outbuf;
363
364 ctx = o->internal;
9688b443 365 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
f3163a6c
DR
366 + ctx->samples_per_line / 8;
367 /*
368 * Calculate space needed for probes. Set aside 512 bytes for
369 * extra output, e.g. trigger.
370 */
371 outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
372 * (ctx->num_enabled_probes * max_linelen);
373
374 if (!(outbuf = calloc(1, outsize + 1)))
e46b8fb1 375 return SR_ERR_MALLOC;
f3163a6c
DR
376
377 outbuf[0] = '\0';
378 if (ctx->header) {
379 /* The header is still here, this must be the first packet. */
380 strncpy(outbuf, ctx->header, outsize);
381 free(ctx->header);
382 ctx->header = NULL;
383 }
384
385 if (length_in >= ctx->unitsize) {
386 for (offset = 0; offset <= length_in - ctx->unitsize;
387 offset += ctx->unitsize) {
388 memcpy(&sample, data_in + offset, ctx->unitsize);
389
390 char tmpval[ctx->num_enabled_probes];
391
392 for (p = 0; p < ctx->num_enabled_probes; p++) {
393 uint64_t curbit = (sample & ((uint64_t) 1 << p));
394 uint64_t prevbit = (ctx->prevsample &
395 ((uint64_t) 1 << p));
396
397 if (curbit < prevbit && ctx->line_offset > 0) {
398 ctx->linebuf[p * ctx->linebuf_len +
399 ctx->line_offset-1] = '\\';
400 }
401
402 if (curbit > prevbit) {
403 tmpval[p] = '/';
404 } else {
405 if (curbit)
406 tmpval[p] = '"';
407 else
408 tmpval[p] = '.';
409 }
410 }
411
412 /* End of line. */
413 if (ctx->spl_cnt >= ctx->samples_per_line) {
414 flush_linebufs(ctx, outbuf);
415 ctx->line_offset = ctx->spl_cnt = 0;
416 ctx->mark_trigger = -1;
417 }
418
419 for (p = 0; p < ctx->num_enabled_probes; p++) {
420 ctx->linebuf[p * ctx->linebuf_len +
421 ctx->line_offset] = tmpval[p];
422 }
423
424 ctx->line_offset++;
425 ctx->spl_cnt++;
426
427 ctx->prevsample = sample;
428 }
429 } else {
b08024a8 430 sr_info("short buffer (length_in=%" PRIu64 ")", length_in);
f3163a6c
DR
431 }
432
433 *data_out = outbuf;
434 *length_out = strlen(outbuf);
435
e46b8fb1 436 return SR_OK;
f3163a6c 437}
6eb0e3ea 438#endif
f3163a6c 439
f50f3f40 440struct sr_output_format output_analog_bits = {
cdb3573c 441 .id = "analog_bits",
d494a4aa
UH
442 .description = "Bits (takes argument, default 64)",
443 .df_type = SR_DF_ANALOG,
444 .init = init_bits,
445 .data = data_bits,
446 .event = event,
1437e893 447};
6eb0e3ea 448#if 0
f50f3f40 449struct sr_output_format output_analog_hex = {
cdb3573c 450 .id = "analog_hex",
d494a4aa
UH
451 .description = "Hexadecimal (takes argument, default 192)",
452 .df_type = SR_DF_ANALOG,
453 .init = init_hex,
454 .data = data_hex,
455 .event = event,
f3163a6c
DR
456};
457
f50f3f40 458struct sr_output_format output_analog_ascii = {
cdb3573c 459 .id = "analog_ascii",
d494a4aa
UH
460 .description = "ASCII (takes argument, default 74)",
461 .df_type = SR_DF_ANALOG,
462 .init = init_ascii,
463 .data = data_ascii,
464 .event = event,
f3163a6c 465};
6eb0e3ea 466#endif