2 * This file is part of the libsigrok project.
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2011 HÃ¥vard Espeland <gus@ping.uio.no>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
28 #define LOG_PREFIX "output/bits"
30 SR_PRIV int init_bits(struct sr_output *o)
32 return init(o, DEFAULT_BPL_BITS, MODE_BITS);
35 SR_PRIV int data_bits(struct sr_output *o, const uint8_t *data_in,
36 uint64_t length_in, uint8_t **data_out,
40 unsigned int outsize, offset, p;
42 const uint8_t *sample;
46 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
47 + ctx->samples_per_line / 8;
49 * Calculate space needed for probes. Set aside 512 bytes for
50 * extra output, e.g. trigger.
52 outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
53 * (ctx->num_enabled_channels * max_linelen);
55 if (!(outbuf = g_try_malloc0(outsize + 1))) {
56 sr_err("%s: outbuf malloc failed", __func__);
62 /* The header is still here, this must be the first packet. */
63 strncpy((char *)outbuf, ctx->header, outsize);
68 if (length_in >= ctx->unitsize) {
69 for (offset = 0; offset <= length_in - ctx->unitsize;
70 offset += ctx->unitsize) {
71 sample = data_in + offset;
72 for (p = 0; p < ctx->num_enabled_channels; p++) {
73 c = (sample[p / 8] & ((uint8_t) 1 << (p % 8))) ? '1' : '0';
74 ctx->linebuf[p * ctx->linebuf_len +
75 ctx->line_offset] = c;
80 /* Add a space every 8th bit. */
81 if ((ctx->spl_cnt & 7) == 0) {
82 for (p = 0; p < ctx->num_enabled_channels; p++)
83 ctx->linebuf[p * ctx->linebuf_len +
84 ctx->line_offset] = ' ';
89 if (ctx->spl_cnt >= ctx->samples_per_line) {
90 flush_linebufs(ctx, outbuf);
91 ctx->line_offset = ctx->spl_cnt = 0;
92 ctx->mark_trigger = -1;
96 sr_info("Short buffer (length_in=%" PRIu64 ").", length_in);
100 *length_out = strlen((const char *)outbuf);
105 SR_PRIV struct sr_output_format output_text_bits = {
107 .description = "Bits",
108 .df_type = SR_DF_LOGIC,
112 .cleanup = text_cleanup,