]> sigrok.org Git - libsigrok.git/blame - output/text/bits.c
Input modules: Use message logging helpers.
[libsigrok.git] / output / text / bits.c
CommitLineData
97554432
BV
1/*
2 * This file is part of the sigrok project.
3 *
c73d2ea4 4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
97554432
BV
5 * Copyright (C) 2011 HÃ¥vard Espeland <gus@ping.uio.no>
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
21#include <stdlib.h>
22#include <string.h>
23#include <glib.h>
45c59c8b
BV
24#include "libsigrok.h"
25#include "libsigrok-internal.h"
97554432
BV
26#include "text.h"
27
7c1d391c 28SR_PRIV int init_bits(struct sr_output *o)
97554432
BV
29{
30 return init(o, DEFAULT_BPL_BITS, MODE_BITS);
31}
32
054e6709
UH
33SR_PRIV int data_bits(struct sr_output *o, const uint8_t *data_in,
34 uint64_t length_in, uint8_t **data_out,
35 uint64_t *length_out)
97554432
BV
36{
37 struct context *ctx;
38 unsigned int outsize, offset, p;
39 int max_linelen;
40 uint64_t sample;
054e6709 41 uint8_t *outbuf, c;
97554432
BV
42
43 ctx = o->internal;
9688b443 44 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
97554432
BV
45 + ctx->samples_per_line / 8;
46 /*
47 * Calculate space needed for probes. Set aside 512 bytes for
48 * extra output, e.g. trigger.
49 */
50 outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
51 * (ctx->num_enabled_probes * max_linelen);
52
133a37bf
UH
53 if (!(outbuf = g_try_malloc0(outsize + 1))) {
54 sr_err("bits out: %s: outbuf malloc failed", __func__);
e46b8fb1 55 return SR_ERR_MALLOC;
133a37bf 56 }
97554432
BV
57
58 outbuf[0] = '\0';
59 if (ctx->header) {
60 /* The header is still here, this must be the first packet. */
054e6709 61 strncpy((char *)outbuf, ctx->header, outsize);
133a37bf 62 g_free(ctx->header);
97554432
BV
63 ctx->header = NULL;
64
65 /* Ensure first transition. */
66 memcpy(&ctx->prevsample, data_in, ctx->unitsize);
67 ctx->prevsample = ~ctx->prevsample;
68 }
69
70 if (length_in >= ctx->unitsize) {
71 for (offset = 0; offset <= length_in - ctx->unitsize;
72 offset += ctx->unitsize) {
73 memcpy(&sample, data_in + offset, ctx->unitsize);
74 for (p = 0; p < ctx->num_enabled_probes; p++) {
75 c = (sample & ((uint64_t) 1 << p)) ? '1' : '0';
76 ctx->linebuf[p * ctx->linebuf_len +
77 ctx->line_offset] = c;
78 }
79 ctx->line_offset++;
80 ctx->spl_cnt++;
81
82 /* Add a space every 8th bit. */
83 if ((ctx->spl_cnt & 7) == 0) {
84 for (p = 0; p < ctx->num_enabled_probes; p++)
85 ctx->linebuf[p * ctx->linebuf_len +
86 ctx->line_offset] = ' ';
87 ctx->line_offset++;
88 }
89
90 /* End of line. */
91 if (ctx->spl_cnt >= ctx->samples_per_line) {
92 flush_linebufs(ctx, outbuf);
93 ctx->line_offset = ctx->spl_cnt = 0;
94 ctx->mark_trigger = -1;
95 }
96 }
97 } else {
7b48d6e1
UH
98 sr_info("bits out: short buffer (length_in=%" PRIu64 ")",
99 length_in);
97554432
BV
100 }
101
102 *data_out = outbuf;
054e6709 103 *length_out = strlen((const char *)outbuf);
97554432 104
e46b8fb1 105 return SR_OK;
97554432
BV
106}
107
7c1d391c 108SR_PRIV struct sr_output_format output_text_bits = {
cdb3573c 109 .id = "bits",
2e7cb004 110 .description = "Bits",
d494a4aa
UH
111 .df_type = SR_DF_LOGIC,
112 .init = init_bits,
113 .data = data_bits,
114 .event = event,
97554432 115};