]> sigrok.org Git - libsigrok.git/blob - output/text/bits.c
GPL headers: Use correct project name.
[libsigrok.git] / output / text / bits.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
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>
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
26 #include "text.h"
27
28 /* Message logging helpers with driver-specific prefix string. */
29 #define DRIVER_LOG_DOMAIN "output/bits: "
30 #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
31 #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
32 #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
33 #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
34 #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
35 #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
36
37 SR_PRIV int init_bits(struct sr_output *o)
38 {
39         return init(o, DEFAULT_BPL_BITS, MODE_BITS);
40 }
41
42 SR_PRIV int data_bits(struct sr_output *o, const uint8_t *data_in,
43                       uint64_t length_in, uint8_t **data_out,
44                       uint64_t *length_out)
45 {
46         struct context *ctx;
47         unsigned int outsize, offset, p;
48         int max_linelen;
49         uint64_t sample;
50         uint8_t *outbuf, c;
51
52         ctx = o->internal;
53         max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
54                         + ctx->samples_per_line / 8;
55         /*
56          * Calculate space needed for probes. Set aside 512 bytes for
57          * extra output, e.g. trigger.
58          */
59         outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
60             * (ctx->num_enabled_probes * max_linelen);
61
62         if (!(outbuf = g_try_malloc0(outsize + 1))) {
63                 sr_err("%s: outbuf malloc failed", __func__);
64                 return SR_ERR_MALLOC;
65         }
66
67         outbuf[0] = '\0';
68         if (ctx->header) {
69                 /* The header is still here, this must be the first packet. */
70                 strncpy((char *)outbuf, ctx->header, outsize);
71                 g_free(ctx->header);
72                 ctx->header = NULL;
73
74                 /* Ensure first transition. */
75                 memcpy(&ctx->prevsample, data_in, ctx->unitsize);
76                 ctx->prevsample = ~ctx->prevsample;
77         }
78
79         if (length_in >= ctx->unitsize) {
80                 for (offset = 0; offset <= length_in - ctx->unitsize;
81                      offset += ctx->unitsize) {
82                         memcpy(&sample, data_in + offset, ctx->unitsize);
83                         for (p = 0; p < ctx->num_enabled_probes; p++) {
84                                 c = (sample & ((uint64_t) 1 << p)) ? '1' : '0';
85                                 ctx->linebuf[p * ctx->linebuf_len +
86                                              ctx->line_offset] = c;
87                         }
88                         ctx->line_offset++;
89                         ctx->spl_cnt++;
90
91                         /* Add a space every 8th bit. */
92                         if ((ctx->spl_cnt & 7) == 0) {
93                                 for (p = 0; p < ctx->num_enabled_probes; p++)
94                                         ctx->linebuf[p * ctx->linebuf_len +
95                                                      ctx->line_offset] = ' ';
96                                 ctx->line_offset++;
97                         }
98
99                         /* End of line. */
100                         if (ctx->spl_cnt >= ctx->samples_per_line) {
101                                 flush_linebufs(ctx, outbuf);
102                                 ctx->line_offset = ctx->spl_cnt = 0;
103                                 ctx->mark_trigger = -1;
104                         }
105                 }
106         } else {
107                 sr_info("Short buffer (length_in=%" PRIu64 ").", length_in);
108         }
109
110         *data_out = outbuf;
111         *length_out = strlen((const char *)outbuf);
112
113         return SR_OK;
114 }
115
116 SR_PRIV struct sr_output_format output_text_bits = {
117         .id = "bits",
118         .description = "Bits",
119         .df_type = SR_DF_LOGIC,
120         .init = init_bits,
121         .data = data_bits,
122         .event = event,
123 };