2 * This file is part of the sigrok project.
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>
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.
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.
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/>.
28 #define DEFAULT_BPL_BITS 64
29 #define DEFAULT_BPL_HEX 192
30 #define DEFAULT_BPL_ASCII 74
39 unsigned int num_enabled_probes;
41 unsigned int unitsize;
50 // struct sr_analog_sample *prevsample;
54 static void flush_linebufs(struct context *ctx, char *outbuf)
56 static int max_probename_len = 0;
59 if (ctx->linebuf[0] == 0)
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;
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);
76 /* Mark trigger with a ^ character. */
77 if (ctx->mark_trigger != -1)
79 int space_offset = ctx->mark_trigger / 8;
81 if (ctx->mode == MODE_ASCII)
84 sprintf(outbuf + strlen(outbuf), "T:%*s^\n",
85 ctx->mark_trigger + space_offset, "");
88 memset(ctx->linebuf, 0, i * ctx->linebuf_len);
91 static int init(struct sr_output *o, int default_spl, enum outputmode mode)
94 struct sr_probe *probe;
100 if (!(ctx = calloc(1, sizeof(struct context))))
101 return SR_ERR_MALLOC;
104 ctx->num_enabled_probes = 0;
106 for (l = o->device->probes; l; l = l->next) {
110 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
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;
118 ctx->mark_trigger = -1;
121 if (o->param && o->param[0]) {
122 ctx->samples_per_line = strtoul(o->param, NULL, 10);
123 if (ctx->samples_per_line < 1)
126 ctx->samples_per_line = default_spl;
128 if (!(ctx->header = malloc(512))) {
130 return SR_ERR_MALLOC;
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))) {
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);
150 ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
151 if (!(ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len))) {
154 return SR_ERR_MALLOC;
156 if (!(ctx->linevalues = calloc(1, num_probes))) {
159 return SR_ERR_MALLOC;
165 static int event(struct sr_output *o, int event_type, char **data_out,
166 uint64_t *length_out)
173 switch (event_type) {
175 ctx->mark_trigger = ctx->spl_cnt;
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);
186 *length_out = strlen(outbuf);
199 static int init_bits(struct sr_output *o)
201 return init(o, DEFAULT_BPL_BITS, MODE_BITS);
204 static int data_bits(struct sr_output *o, const char *data_in,
205 uint64_t length_in, char **data_out, uint64_t *length_out)
208 unsigned int outsize, offset, p;
210 struct sr_analog_sample *sample;
214 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
215 + ctx->samples_per_line / 8;
217 * Calculate space needed for probes. Set aside 512 bytes for
218 * extra output, e.g. trigger.
220 outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
221 * (ctx->num_enabled_probes * max_linelen);
223 if (!(outbuf = calloc(1, outsize + 1)))
224 return SR_ERR_MALLOC;
228 /* The header is still here, this must be the first packet. */
229 strncpy(outbuf, ctx->header, outsize);
233 /* Ensure first transition. */
234 // memcpy(&ctx->prevsample, data_in, ctx->unitsize);
235 // ctx->prevsample = ~ctx->prevsample;
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;
246 c = '0' + (val & ((1 << res) - 1));
249 * Scale analog resolution down so it
252 c = 'A' + (((val & ((1 << res) - 1)) /
254 ctx->linebuf[p * ctx->linebuf_len +
255 ctx->line_offset] = c;
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] = ' ';
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;
276 sr_info("short buffer (length_in=%" PRIu64 ")", length_in);
280 *length_out = strlen(outbuf);
285 static int init_hex(struct sr_output *o)
287 return init(o, DEFAULT_BPL_HEX, MODE_HEX);
290 static int data_hex(struct sr_output *o, const char *data_in,
291 uint64_t length_in, char **data_out, uint64_t *length_out)
294 unsigned int outsize, offset, p;
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;
305 if (!(outbuf = calloc(1, outsize + 1)))
306 return SR_ERR_MALLOC;
310 /* The header is still here, this must be the first packet. */
311 strncpy(outbuf, ctx->header, outsize);
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]);
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;
338 if (ctx->spl_cnt >= ctx->samples_per_line) {
339 flush_linebufs(ctx, outbuf);
340 ctx->line_offset = ctx->spl_cnt = 0;
345 *length_out = strlen(outbuf);
350 static int init_ascii(struct sr_output *o)
352 return init(o, DEFAULT_BPL_ASCII, MODE_ASCII);
355 static int data_ascii(struct sr_output *o, const char *data_in,
356 uint64_t length_in, char **data_out, uint64_t *length_out)
359 unsigned int outsize, offset, p;
365 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
366 + ctx->samples_per_line / 8;
368 * Calculate space needed for probes. Set aside 512 bytes for
369 * extra output, e.g. trigger.
371 outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
372 * (ctx->num_enabled_probes * max_linelen);
374 if (!(outbuf = calloc(1, outsize + 1)))
375 return SR_ERR_MALLOC;
379 /* The header is still here, this must be the first packet. */
380 strncpy(outbuf, ctx->header, outsize);
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);
390 char tmpval[ctx->num_enabled_probes];
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));
397 if (curbit < prevbit && ctx->line_offset > 0) {
398 ctx->linebuf[p * ctx->linebuf_len +
399 ctx->line_offset-1] = '\\';
402 if (curbit > prevbit) {
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;
419 for (p = 0; p < ctx->num_enabled_probes; p++) {
420 ctx->linebuf[p * ctx->linebuf_len +
421 ctx->line_offset] = tmpval[p];
427 ctx->prevsample = sample;
430 sr_info("short buffer (length_in=%" PRIu64 ")", length_in);
434 *length_out = strlen(outbuf);
440 struct sr_output_format output_analog_bits = {
442 .description = "Bits (takes argument, default 64)",
443 .df_type = SR_DF_ANALOG,
449 struct sr_output_format output_analog_hex = {
451 .description = "Hexadecimal (takes argument, default 192)",
452 .df_type = SR_DF_ANALOG,
458 struct sr_output_format output_analog_ascii = {
459 .id = "analog_ascii",
460 .description = "ASCII (takes argument, default 74)",
461 .df_type = SR_DF_ANALOG,