2 * This file is part of the sigrok project.
4 * Copyright (C) 2010-2012 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;
44 char *probelist[SR_MAX_NUM_PROBES + 1];
50 // struct sr_analog_sample *prevsample;
54 static void flush_linebufs(struct context *ctx, uint8_t *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 = g_try_malloc0(sizeof(struct context)))) {
101 sr_err("analog out: %s: ctx malloc failed", __func__);
102 return SR_ERR_MALLOC;
106 ctx->num_enabled_probes = 0;
108 for (l = o->dev->probes; l; l = l->next) {
112 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
115 ctx->probelist[ctx->num_enabled_probes] = 0;
116 ctx->unitsize = sizeof(struct sr_analog_sample) +
117 (ctx->num_enabled_probes * sizeof(struct sr_analog_probe));
118 ctx->line_offset = 0;
120 ctx->mark_trigger = -1;
123 if (o->param && o->param[0]) {
124 ctx->samples_per_line = strtoul(o->param, NULL, 10);
125 if (ctx->samples_per_line < 1)
128 ctx->samples_per_line = default_spl;
130 if (!(ctx->header = g_try_malloc(512))) {
132 sr_err("analog out: %s: ctx->header malloc failed", __func__);
133 return SR_ERR_MALLOC;
136 snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
137 num_probes = g_slist_length(o->dev->probes);
138 if (o->dev->driver && sr_dev_has_hwcap(o->dev, SR_HWCAP_SAMPLERATE)) {
139 samplerate = *((uint64_t *) o->dev->driver->dev_info_get(
140 o->dev->driver_index, SR_DI_CUR_SAMPLERATE));
141 if (!(samplerate_s = sr_samplerate_string(samplerate))) {
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);
150 g_free(samplerate_s);
153 ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
154 if (!(ctx->linebuf = g_try_malloc0(num_probes * ctx->linebuf_len))) {
157 sr_err("analog out: %s: ctx->linebuf malloc failed", __func__);
158 return SR_ERR_MALLOC;
160 if (!(ctx->linevalues = g_try_malloc0(num_probes))) {
163 sr_err("analog out: %s: ctx->linevalues malloc failed",
165 return SR_ERR_MALLOC;
171 static int event(struct sr_output *o, int event_type, uint8_t **data_out,
172 uint64_t *length_out)
179 switch (event_type) {
181 ctx->mark_trigger = ctx->spl_cnt;
186 outsize = ctx->num_enabled_probes
187 * (ctx->samples_per_line + 20) + 512;
188 if (!(outbuf = g_try_malloc0(outsize))) {
189 sr_err("analog out: %s: outbuf malloc failed",
191 return SR_ERR_MALLOC;
193 flush_linebufs(ctx, outbuf);
195 *length_out = strlen(outbuf);
208 static int init_bits(struct sr_output *o)
210 return init(o, DEFAULT_BPL_BITS, MODE_BITS);
213 static int data_bits(struct sr_output *o, const uint8_t *data_in,
214 uint64_t length_in, uint8_t **data_out,
215 uint64_t *length_out)
218 unsigned int outsize, offset, p;
220 struct sr_analog_sample *sample;
224 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
225 + ctx->samples_per_line / 8;
227 * Calculate space needed for probes. Set aside 512 bytes for
228 * extra output, e.g. trigger.
230 outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
231 * (ctx->num_enabled_probes * max_linelen);
233 if (!(outbuf = g_try_malloc0(outsize + 1))) {
234 sr_err("analog out: %s: outbuf malloc failed", __func__);
235 return SR_ERR_MALLOC;
240 /* The header is still here, this must be the first packet. */
241 strncpy(outbuf, ctx->header, outsize);
245 /* Ensure first transition. */
246 // memcpy(&ctx->prevsample, data_in, ctx->unitsize);
247 // ctx->prevsample = ~ctx->prevsample;
250 if (length_in >= ctx->unitsize) {
251 for (offset = 0; offset <= length_in - ctx->unitsize;
252 offset += ctx->unitsize) {
253 sample = (struct sr_analog_sample *) (data_in + offset);
254 for (p = 0; p < ctx->num_enabled_probes; p++) {
255 int val = sample->probes[p].val;
256 int res = sample->probes[p].res;
258 c = '0' + (val & ((1 << res) - 1));
261 * Scale analog resolution down so it
264 c = 'A' + (((val & ((1 << res) - 1)) /
266 ctx->linebuf[p * ctx->linebuf_len +
267 ctx->line_offset] = c;
272 /* Add a space every 8th bit. */
273 if ((ctx->spl_cnt & 7) == 0) {
274 for (p = 0; p < ctx->num_enabled_probes; p++)
275 ctx->linebuf[p * ctx->linebuf_len +
276 ctx->line_offset] = ' ';
281 if (ctx->spl_cnt >= ctx->samples_per_line) {
282 flush_linebufs(ctx, outbuf);
283 ctx->line_offset = ctx->spl_cnt = 0;
284 ctx->mark_trigger = -1;
288 sr_info("analog out: short buffer (length_in=%" PRIu64 ")",
293 *length_out = strlen(outbuf);
298 static int init_hex(struct sr_output *o)
300 return init(o, DEFAULT_BPL_HEX, MODE_HEX);
303 static int data_hex(struct sr_output *o, const uint8_t *data_in,
304 uint64_t length_in, uint8_t **data_out,
305 uint64_t *length_out)
308 unsigned int outsize, offset, p;
314 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
315 + ctx->samples_per_line / 2;
316 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes
317 / ctx->samples_per_line * max_linelen + 512;
319 if (!(outbuf = g_try_malloc0(outsize + 1))) {
320 sr_err("analog out: %s: outbuf malloc failed", __func__);
321 return SR_ERR_MALLOC;
326 /* The header is still here, this must be the first packet. */
327 strncpy(outbuf, ctx->header, outsize);
332 ctx->line_offset = 0;
333 for (offset = 0; offset <= length_in - ctx->unitsize;
334 offset += ctx->unitsize) {
335 memcpy(&sample, data_in + offset, ctx->unitsize);
336 for (p = 0; p < ctx->num_enabled_probes; p++) {
337 ctx->linevalues[p] <<= 1;
338 if (sample & ((uint64_t) 1 << p))
339 ctx->linevalues[p] |= 1;
340 sprintf(ctx->linebuf + (p * ctx->linebuf_len) +
341 ctx->line_offset, "%.2x", ctx->linevalues[p]);
345 /* Add a space after every complete hex byte. */
346 if ((ctx->spl_cnt & 7) == 0) {
347 for (p = 0; p < ctx->num_enabled_probes; p++)
348 ctx->linebuf[p * ctx->linebuf_len +
349 ctx->line_offset + 2] = ' ';
350 ctx->line_offset += 3;
354 if (ctx->spl_cnt >= ctx->samples_per_line) {
355 flush_linebufs(ctx, outbuf);
356 ctx->line_offset = ctx->spl_cnt = 0;
361 *length_out = strlen(outbuf);
366 static int init_ascii(struct sr_output *o)
368 return init(o, DEFAULT_BPL_ASCII, MODE_ASCII);
371 static int data_ascii(struct sr_output *o, const uint8_t *data_in,
372 uint64_t length_in, uint8_t **data_out,
373 uint64_t *length_out)
376 unsigned int outsize, offset, p;
382 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
383 + ctx->samples_per_line / 8;
385 * Calculate space needed for probes. Set aside 512 bytes for
386 * extra output, e.g. trigger.
388 outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
389 * (ctx->num_enabled_probes * max_linelen);
391 if (!(outbuf = g_try_malloc0(outsize + 1))) {
392 sr_err("analog out: %s: outbuf malloc failed", __func__);
393 return SR_ERR_MALLOC;
398 /* The header is still here, this must be the first packet. */
399 strncpy(outbuf, ctx->header, outsize);
404 if (length_in >= ctx->unitsize) {
405 for (offset = 0; offset <= length_in - ctx->unitsize;
406 offset += ctx->unitsize) {
407 memcpy(&sample, data_in + offset, ctx->unitsize);
409 char tmpval[ctx->num_enabled_probes];
411 for (p = 0; p < ctx->num_enabled_probes; p++) {
412 uint64_t curbit = (sample & ((uint64_t) 1 << p));
413 uint64_t prevbit = (ctx->prevsample &
414 ((uint64_t) 1 << p));
416 if (curbit < prevbit && ctx->line_offset > 0) {
417 ctx->linebuf[p * ctx->linebuf_len +
418 ctx->line_offset-1] = '\\';
421 if (curbit > prevbit) {
432 if (ctx->spl_cnt >= ctx->samples_per_line) {
433 flush_linebufs(ctx, outbuf);
434 ctx->line_offset = ctx->spl_cnt = 0;
435 ctx->mark_trigger = -1;
438 for (p = 0; p < ctx->num_enabled_probes; p++) {
439 ctx->linebuf[p * ctx->linebuf_len +
440 ctx->line_offset] = tmpval[p];
446 ctx->prevsample = sample;
449 sr_info("analog out: short buffer (length_in=%" PRIu64 ")",
454 *length_out = strlen(outbuf);
460 SR_PRIV struct sr_output_format output_analog_bits = {
462 .description = "Bits",
463 .df_type = SR_DF_ANALOG,
470 struct sr_output_format output_analog_hex = {
472 .description = "Hexadecimal",
473 .df_type = SR_DF_ANALOG,
479 struct sr_output_format output_analog_ascii = {
480 .id = "analog_ascii",
481 .description = "ASCII",
482 .df_type = SR_DF_ANALOG,