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;
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 = 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->plugin && sr_dev_has_hwcap(o->dev, SR_HWCAP_SAMPLERATE)) {
139 samplerate = *((uint64_t *) o->dev->plugin->dev_info_get(
140 o->dev->plugin_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, char **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 char *data_in,
214 uint64_t length_in, char **data_out, uint64_t *length_out)
217 unsigned int outsize, offset, p;
219 struct sr_analog_sample *sample;
223 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
224 + ctx->samples_per_line / 8;
226 * Calculate space needed for probes. Set aside 512 bytes for
227 * extra output, e.g. trigger.
229 outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
230 * (ctx->num_enabled_probes * max_linelen);
232 if (!(outbuf = g_try_malloc0(outsize + 1))) {
233 sr_err("analog out: %s: outbuf malloc failed", __func__);
234 return SR_ERR_MALLOC;
239 /* The header is still here, this must be the first packet. */
240 strncpy(outbuf, ctx->header, outsize);
244 /* Ensure first transition. */
245 // memcpy(&ctx->prevsample, data_in, ctx->unitsize);
246 // ctx->prevsample = ~ctx->prevsample;
249 if (length_in >= ctx->unitsize) {
250 for (offset = 0; offset <= length_in - ctx->unitsize;
251 offset += ctx->unitsize) {
252 sample = (struct sr_analog_sample *) (data_in + offset);
253 for (p = 0; p < ctx->num_enabled_probes; p++) {
254 int val = sample->probes[p].val;
255 int res = sample->probes[p].res;
257 c = '0' + (val & ((1 << res) - 1));
260 * Scale analog resolution down so it
263 c = 'A' + (((val & ((1 << res) - 1)) /
265 ctx->linebuf[p * ctx->linebuf_len +
266 ctx->line_offset] = c;
271 /* Add a space every 8th bit. */
272 if ((ctx->spl_cnt & 7) == 0) {
273 for (p = 0; p < ctx->num_enabled_probes; p++)
274 ctx->linebuf[p * ctx->linebuf_len +
275 ctx->line_offset] = ' ';
280 if (ctx->spl_cnt >= ctx->samples_per_line) {
281 flush_linebufs(ctx, outbuf);
282 ctx->line_offset = ctx->spl_cnt = 0;
283 ctx->mark_trigger = -1;
287 sr_info("analog out: short buffer (length_in=%" PRIu64 ")",
292 *length_out = strlen(outbuf);
297 static int init_hex(struct sr_output *o)
299 return init(o, DEFAULT_BPL_HEX, MODE_HEX);
302 static int data_hex(struct sr_output *o, const char *data_in,
303 uint64_t length_in, char **data_out, uint64_t *length_out)
306 unsigned int outsize, offset, p;
312 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
313 + ctx->samples_per_line / 2;
314 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes
315 / ctx->samples_per_line * max_linelen + 512;
317 if (!(outbuf = g_try_malloc0(outsize + 1))) {
318 sr_err("analog out: %s: outbuf malloc failed", __func__);
319 return SR_ERR_MALLOC;
324 /* The header is still here, this must be the first packet. */
325 strncpy(outbuf, ctx->header, outsize);
330 ctx->line_offset = 0;
331 for (offset = 0; offset <= length_in - ctx->unitsize;
332 offset += ctx->unitsize) {
333 memcpy(&sample, data_in + offset, ctx->unitsize);
334 for (p = 0; p < ctx->num_enabled_probes; p++) {
335 ctx->linevalues[p] <<= 1;
336 if (sample & ((uint64_t) 1 << p))
337 ctx->linevalues[p] |= 1;
338 sprintf(ctx->linebuf + (p * ctx->linebuf_len) +
339 ctx->line_offset, "%.2x", ctx->linevalues[p]);
343 /* Add a space after every complete hex byte. */
344 if ((ctx->spl_cnt & 7) == 0) {
345 for (p = 0; p < ctx->num_enabled_probes; p++)
346 ctx->linebuf[p * ctx->linebuf_len +
347 ctx->line_offset + 2] = ' ';
348 ctx->line_offset += 3;
352 if (ctx->spl_cnt >= ctx->samples_per_line) {
353 flush_linebufs(ctx, outbuf);
354 ctx->line_offset = ctx->spl_cnt = 0;
359 *length_out = strlen(outbuf);
364 static int init_ascii(struct sr_output *o)
366 return init(o, DEFAULT_BPL_ASCII, MODE_ASCII);
369 static int data_ascii(struct sr_output *o, const char *data_in,
370 uint64_t length_in, char **data_out, uint64_t *length_out)
373 unsigned int outsize, offset, p;
379 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
380 + ctx->samples_per_line / 8;
382 * Calculate space needed for probes. Set aside 512 bytes for
383 * extra output, e.g. trigger.
385 outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
386 * (ctx->num_enabled_probes * max_linelen);
388 if (!(outbuf = g_try_malloc0(outsize + 1))) {
389 sr_err("analog out: %s: outbuf malloc failed", __func__);
390 return SR_ERR_MALLOC;
395 /* The header is still here, this must be the first packet. */
396 strncpy(outbuf, ctx->header, outsize);
401 if (length_in >= ctx->unitsize) {
402 for (offset = 0; offset <= length_in - ctx->unitsize;
403 offset += ctx->unitsize) {
404 memcpy(&sample, data_in + offset, ctx->unitsize);
406 char tmpval[ctx->num_enabled_probes];
408 for (p = 0; p < ctx->num_enabled_probes; p++) {
409 uint64_t curbit = (sample & ((uint64_t) 1 << p));
410 uint64_t prevbit = (ctx->prevsample &
411 ((uint64_t) 1 << p));
413 if (curbit < prevbit && ctx->line_offset > 0) {
414 ctx->linebuf[p * ctx->linebuf_len +
415 ctx->line_offset-1] = '\\';
418 if (curbit > prevbit) {
429 if (ctx->spl_cnt >= ctx->samples_per_line) {
430 flush_linebufs(ctx, outbuf);
431 ctx->line_offset = ctx->spl_cnt = 0;
432 ctx->mark_trigger = -1;
435 for (p = 0; p < ctx->num_enabled_probes; p++) {
436 ctx->linebuf[p * ctx->linebuf_len +
437 ctx->line_offset] = tmpval[p];
443 ctx->prevsample = sample;
446 sr_info("analog out: short buffer (length_in=%" PRIu64 ")",
451 *length_out = strlen(outbuf);
457 SR_PRIV struct sr_output_format output_analog_bits = {
459 .description = "Bits (takes argument, default 64)",
460 .df_type = SR_DF_ANALOG,
467 struct sr_output_format output_analog_hex = {
469 .description = "Hexadecimal (takes argument, default 192)",
470 .df_type = SR_DF_ANALOG,
476 struct sr_output_format output_analog_ascii = {
477 .id = "analog_ascii",
478 .description = "ASCII (takes argument, default 74)",
479 .df_type = SR_DF_ANALOG,