]> sigrok.org Git - libsigrok.git/blame_incremental - output/analog.c
sr: Mark API functions with SR_API/SR_PRIV.
[libsigrok.git] / output / analog.c
... / ...
CommitLineData
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 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 = calloc(1, sizeof(struct context))))
101 return SR_ERR_MALLOC;
102
103 o->internal = ctx;
104 ctx->num_enabled_probes = 0;
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;
114 ctx->unitsize = sizeof(struct sr_analog_sample) +
115 (ctx->num_enabled_probes * sizeof(struct sr_analog_probe));
116 ctx->line_offset = 0;
117 ctx->spl_cnt = 0;
118 ctx->mark_trigger = -1;
119 ctx->mode = mode;
120
121 if (o->param && o->param[0]) {
122 ctx->samples_per_line = strtoul(o->param, NULL, 10);
123 if (ctx->samples_per_line < 1)
124 return SR_ERR;
125 } else
126 ctx->samples_per_line = default_spl;
127
128 if (!(ctx->header = malloc(512))) {
129 free(ctx);
130 return SR_ERR_MALLOC;
131 }
132
133 snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
134 num_probes = g_slist_length(o->device->probes);
135 if (o->device->plugin && sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) {
136 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
137 o->device->plugin_index, SR_DI_CUR_SAMPLERATE));
138 if (!(samplerate_s = sr_samplerate_string(samplerate))) {
139 free(ctx->header);
140 free(ctx);
141 return SR_ERR;
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
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);
154 return SR_ERR_MALLOC;
155 }
156 if (!(ctx->linevalues = calloc(1, num_probes))) {
157 free(ctx->header);
158 free(ctx);
159 return SR_ERR_MALLOC;
160 }
161
162 return SR_OK;
163}
164
165static int event(struct sr_output *o, int event_type, char **data_out,
166 uint64_t *length_out)
167{
168 struct context *ctx;
169 int outsize;
170 char *outbuf;
171
172 ctx = o->internal;
173 switch (event_type) {
174 case SR_DF_TRIGGER:
175 ctx->mark_trigger = ctx->spl_cnt;
176 *data_out = NULL;
177 *length_out = 0;
178 break;
179 case SR_DF_END:
180 outsize = ctx->num_enabled_probes
181 * (ctx->samples_per_line + 20) + 512;
182 if (!(outbuf = calloc(1, outsize)))
183 return SR_ERR_MALLOC;
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
196 return SR_OK;
197}
198
199static int init_bits(struct sr_output *o)
200{
201 return init(o, DEFAULT_BPL_BITS, MODE_BITS);
202}
203
204static int data_bits(struct sr_output *o, const char *data_in,
205 uint64_t length_in, char **data_out, uint64_t *length_out)
206{
207 struct context *ctx;
208 unsigned int outsize, offset, p;
209 int max_linelen;
210 struct sr_analog_sample *sample;
211 char *outbuf, c;
212
213 ctx = o->internal;
214 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
215 + ctx->samples_per_line / 8;
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
223 if (!(outbuf = calloc(1, outsize + 1)))
224 return SR_ERR_MALLOC;
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;
232
233 /* Ensure first transition. */
234// memcpy(&ctx->prevsample, data_in, ctx->unitsize);
235// ctx->prevsample = ~ctx->prevsample;
236 }
237
238 if (length_in >= ctx->unitsize) {
239 for (offset = 0; offset <= length_in - ctx->unitsize;
240 offset += ctx->unitsize) {
241 sample = (struct sr_analog_sample *) (data_in + offset);
242 for (p = 0; p < ctx->num_enabled_probes; p++) {
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);
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++;
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 }
274 }
275 } else {
276 sr_info("short buffer (length_in=%" PRIu64 ")", length_in);
277 }
278
279 *data_out = outbuf;
280 *length_out = strlen(outbuf);
281
282 return SR_OK;
283}
284#if 0
285static int init_hex(struct sr_output *o)
286{
287 return init(o, DEFAULT_BPL_HEX, MODE_HEX);
288}
289
290static int data_hex(struct sr_output *o, const char *data_in,
291 uint64_t length_in, char **data_out, uint64_t *length_out)
292{
293 struct context *ctx;
294 unsigned int outsize, offset, p;
295 int max_linelen;
296 uint64_t sample;
297 char *outbuf;
298
299 ctx = o->internal;
300 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
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)))
306 return SR_ERR_MALLOC;
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;
314 }
315
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);
346
347 return SR_OK;
348}
349
350static int init_ascii(struct sr_output *o)
351{
352 return init(o, DEFAULT_BPL_ASCII, MODE_ASCII);
353}
354
355static int data_ascii(struct sr_output *o, const char *data_in,
356 uint64_t length_in, char **data_out, uint64_t *length_out)
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;
365 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
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)))
375 return SR_ERR_MALLOC;
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 {
430 sr_info("short buffer (length_in=%" PRIu64 ")", length_in);
431 }
432
433 *data_out = outbuf;
434 *length_out = strlen(outbuf);
435
436 return SR_OK;
437}
438#endif
439
440struct sr_output_format output_analog_bits = {
441 .id = "analog_bits",
442 .description = "Bits (takes argument, default 64)",
443 .df_type = SR_DF_ANALOG,
444 .init = init_bits,
445 .data = data_bits,
446 .event = event,
447};
448#if 0
449struct sr_output_format output_analog_hex = {
450 .id = "analog_hex",
451 .description = "Hexadecimal (takes argument, default 192)",
452 .df_type = SR_DF_ANALOG,
453 .init = init_hex,
454 .data = data_hex,
455 .event = event,
456};
457
458struct sr_output_format output_analog_ascii = {
459 .id = "analog_ascii",
460 .description = "ASCII (takes argument, default 74)",
461 .df_type = SR_DF_ANALOG,
462 .init = init_ascii,
463 .data = data_ascii,
464 .event = event,
465};
466#endif