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/>.
29 #define DEFAULT_BPL_BITS 64
30 #define DEFAULT_BPL_HEX 192
31 #define DEFAULT_BPL_ASCII 74
40 unsigned int num_enabled_probes;
42 unsigned int unitsize;
51 // struct sr_analog_sample *prevsample;
55 static void flush_linebufs(struct context *ctx, char *outbuf)
57 static int max_probename_len = 0;
60 if (ctx->linebuf[0] == 0)
63 if (max_probename_len == 0) {
64 /* First time through... */
65 for (i = 0; ctx->probelist[i]; i++) {
66 len = strlen(ctx->probelist[i]);
67 if (len > max_probename_len)
68 max_probename_len = len;
72 for (i = 0; ctx->probelist[i]; i++) {
73 sprintf(outbuf + strlen(outbuf), "%*s:%s\n", max_probename_len,
74 ctx->probelist[i], ctx->linebuf + i * ctx->linebuf_len);
77 /* Mark trigger with a ^ character. */
78 if (ctx->mark_trigger != -1)
80 int space_offset = ctx->mark_trigger / 8;
82 if (ctx->mode == MODE_ASCII)
85 sprintf(outbuf + strlen(outbuf), "T:%*s^\n",
86 ctx->mark_trigger + space_offset, "");
89 memset(ctx->linebuf, 0, i * ctx->linebuf_len);
92 static int init(struct sr_output *o, int default_spl, enum outputmode mode)
95 struct sr_probe *probe;
101 if (!(ctx = calloc(1, sizeof(struct context))))
102 return SR_ERR_MALLOC;
105 ctx->num_enabled_probes = 0;
107 for (l = o->device->probes; l; l = l->next) {
111 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
114 ctx->probelist[ctx->num_enabled_probes] = 0;
115 ctx->unitsize = sizeof(struct sr_analog_sample) +
116 (ctx->num_enabled_probes * sizeof(struct sr_analog_probe));
117 ctx->line_offset = 0;
119 ctx->mark_trigger = -1;
122 if (o->param && o->param[0]) {
123 ctx->samples_per_line = strtoul(o->param, NULL, 10);
124 if (ctx->samples_per_line < 1)
127 ctx->samples_per_line = default_spl;
129 if (!(ctx->header = malloc(512))) {
131 return SR_ERR_MALLOC;
134 snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
135 num_probes = g_slist_length(o->device->probes);
136 if (o->device->plugin && sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) {
137 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
138 o->device->plugin_index, SR_DI_CUR_SAMPLERATE));
139 if (!(samplerate_s = sr_samplerate_string(samplerate))) {
144 snprintf(ctx->header + strlen(ctx->header),
145 511 - strlen(ctx->header),
146 "Acquisition with %d/%d probes at %s\n",
147 ctx->num_enabled_probes, num_probes, samplerate_s);
151 ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
152 if (!(ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len))) {
155 return SR_ERR_MALLOC;
157 if (!(ctx->linevalues = calloc(1, num_probes))) {
160 return SR_ERR_MALLOC;
166 static int event(struct sr_output *o, int event_type, char **data_out,
167 uint64_t *length_out)
174 switch (event_type) {
176 ctx->mark_trigger = ctx->spl_cnt;
181 outsize = ctx->num_enabled_probes
182 * (ctx->samples_per_line + 20) + 512;
183 if (!(outbuf = calloc(1, outsize)))
184 return SR_ERR_MALLOC;
185 flush_linebufs(ctx, outbuf);
187 *length_out = strlen(outbuf);
200 static int init_bits(struct sr_output *o)
202 return init(o, DEFAULT_BPL_BITS, MODE_BITS);
205 static int data_bits(struct sr_output *o, const char *data_in,
206 uint64_t length_in, char **data_out, uint64_t *length_out)
209 unsigned int outsize, offset, p;
211 struct sr_analog_sample *sample;
215 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
216 + ctx->samples_per_line / 8;
218 * Calculate space needed for probes. Set aside 512 bytes for
219 * extra output, e.g. trigger.
221 outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
222 * (ctx->num_enabled_probes * max_linelen);
224 if (!(outbuf = calloc(1, outsize + 1)))
225 return SR_ERR_MALLOC;
229 /* The header is still here, this must be the first packet. */
230 strncpy(outbuf, ctx->header, outsize);
234 /* Ensure first transition. */
235 // memcpy(&ctx->prevsample, data_in, ctx->unitsize);
236 // ctx->prevsample = ~ctx->prevsample;
239 if (length_in >= ctx->unitsize) {
240 for (offset = 0; offset <= length_in - ctx->unitsize;
241 offset += ctx->unitsize) {
242 sample = (struct sr_analog_sample *) (data_in + offset);
243 for (p = 0; p < ctx->num_enabled_probes; p++) {
244 int val = sample->probes[p].val;
245 int res = sample->probes[p].res;
247 c = '0' + (val & ((1 << res) - 1));
250 * Scale analog resolution down so it
253 c = 'A' + (((val & ((1 << res) - 1)) /
255 ctx->linebuf[p * ctx->linebuf_len +
256 ctx->line_offset] = c;
261 /* Add a space every 8th bit. */
262 if ((ctx->spl_cnt & 7) == 0) {
263 for (p = 0; p < ctx->num_enabled_probes; p++)
264 ctx->linebuf[p * ctx->linebuf_len +
265 ctx->line_offset] = ' ';
270 if (ctx->spl_cnt >= ctx->samples_per_line) {
271 flush_linebufs(ctx, outbuf);
272 ctx->line_offset = ctx->spl_cnt = 0;
273 ctx->mark_trigger = -1;
277 g_message("short buffer (length_in=%" PRIu64 ")", length_in);
281 *length_out = strlen(outbuf);
286 static int init_hex(struct sr_output *o)
288 return init(o, DEFAULT_BPL_HEX, MODE_HEX);
291 static int data_hex(struct sr_output *o, const char *data_in,
292 uint64_t length_in, char **data_out, uint64_t *length_out)
295 unsigned int outsize, offset, p;
301 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
302 + ctx->samples_per_line / 2;
303 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes
304 / ctx->samples_per_line * max_linelen + 512;
306 if (!(outbuf = calloc(1, outsize + 1)))
307 return SR_ERR_MALLOC;
311 /* The header is still here, this must be the first packet. */
312 strncpy(outbuf, ctx->header, outsize);
317 ctx->line_offset = 0;
318 for (offset = 0; offset <= length_in - ctx->unitsize;
319 offset += ctx->unitsize) {
320 memcpy(&sample, data_in + offset, ctx->unitsize);
321 for (p = 0; p < ctx->num_enabled_probes; p++) {
322 ctx->linevalues[p] <<= 1;
323 if (sample & ((uint64_t) 1 << p))
324 ctx->linevalues[p] |= 1;
325 sprintf(ctx->linebuf + (p * ctx->linebuf_len) +
326 ctx->line_offset, "%.2x", ctx->linevalues[p]);
330 /* Add a space after every complete hex byte. */
331 if ((ctx->spl_cnt & 7) == 0) {
332 for (p = 0; p < ctx->num_enabled_probes; p++)
333 ctx->linebuf[p * ctx->linebuf_len +
334 ctx->line_offset + 2] = ' ';
335 ctx->line_offset += 3;
339 if (ctx->spl_cnt >= ctx->samples_per_line) {
340 flush_linebufs(ctx, outbuf);
341 ctx->line_offset = ctx->spl_cnt = 0;
346 *length_out = strlen(outbuf);
351 static int init_ascii(struct sr_output *o)
353 return init(o, DEFAULT_BPL_ASCII, MODE_ASCII);
356 static int data_ascii(struct sr_output *o, const char *data_in,
357 uint64_t length_in, char **data_out, uint64_t *length_out)
360 unsigned int outsize, offset, p;
366 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
367 + ctx->samples_per_line / 8;
369 * Calculate space needed for probes. Set aside 512 bytes for
370 * extra output, e.g. trigger.
372 outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
373 * (ctx->num_enabled_probes * max_linelen);
375 if (!(outbuf = calloc(1, outsize + 1)))
376 return SR_ERR_MALLOC;
380 /* The header is still here, this must be the first packet. */
381 strncpy(outbuf, ctx->header, outsize);
386 if (length_in >= ctx->unitsize) {
387 for (offset = 0; offset <= length_in - ctx->unitsize;
388 offset += ctx->unitsize) {
389 memcpy(&sample, data_in + offset, ctx->unitsize);
391 char tmpval[ctx->num_enabled_probes];
393 for (p = 0; p < ctx->num_enabled_probes; p++) {
394 uint64_t curbit = (sample & ((uint64_t) 1 << p));
395 uint64_t prevbit = (ctx->prevsample &
396 ((uint64_t) 1 << p));
398 if (curbit < prevbit && ctx->line_offset > 0) {
399 ctx->linebuf[p * ctx->linebuf_len +
400 ctx->line_offset-1] = '\\';
403 if (curbit > prevbit) {
414 if (ctx->spl_cnt >= ctx->samples_per_line) {
415 flush_linebufs(ctx, outbuf);
416 ctx->line_offset = ctx->spl_cnt = 0;
417 ctx->mark_trigger = -1;
420 for (p = 0; p < ctx->num_enabled_probes; p++) {
421 ctx->linebuf[p * ctx->linebuf_len +
422 ctx->line_offset] = tmpval[p];
428 ctx->prevsample = sample;
431 g_message("short buffer (length_in=%" PRIu64 ")", length_in);
435 *length_out = strlen(outbuf);
441 struct sr_output_format output_analog_bits = {
443 "Bits (takes argument, default 64)",
450 struct sr_output_format output_analog_hex = {
452 "Hexadecimal (takes argument, default 192)",
459 struct sr_output_format output_analog_ascii = {
461 "ASCII (takes argument, default 74)",