]> sigrok.org Git - libsigrok.git/blob - output/text/bits.c
split output_text into more manageable pieces
[libsigrok.git] / output / text / bits.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2011 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 <sigrok.h>
25 #include "text.h"
26
27
28 int init_bits(struct output *o)
29 {
30         return init(o, DEFAULT_BPL_BITS, MODE_BITS);
31 }
32
33 int data_bits(struct output *o, char *data_in, uint64_t length_in,
34                      char **data_out, uint64_t *length_out)
35 {
36         struct context *ctx;
37         unsigned int outsize, offset, p;
38         int max_linelen;
39         uint64_t sample;
40         char *outbuf, c;
41
42         ctx = o->internal;
43         max_linelen = MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
44                         + ctx->samples_per_line / 8;
45         /*
46          * Calculate space needed for probes. Set aside 512 bytes for
47          * extra output, e.g. trigger.
48          */
49         outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
50             * (ctx->num_enabled_probes * max_linelen);
51
52         if (!(outbuf = calloc(1, outsize + 1)))
53                 return SIGROK_ERR_MALLOC;
54
55         outbuf[0] = '\0';
56         if (ctx->header) {
57                 /* The header is still here, this must be the first packet. */
58                 strncpy(outbuf, ctx->header, outsize);
59                 free(ctx->header);
60                 ctx->header = NULL;
61
62                 /* Ensure first transition. */
63                 memcpy(&ctx->prevsample, data_in, ctx->unitsize);
64                 ctx->prevsample = ~ctx->prevsample;
65         }
66
67         if (length_in >= ctx->unitsize) {
68                 for (offset = 0; offset <= length_in - ctx->unitsize;
69                      offset += ctx->unitsize) {
70                         memcpy(&sample, data_in + offset, ctx->unitsize);
71                         for (p = 0; p < ctx->num_enabled_probes; p++) {
72                                 c = (sample & ((uint64_t) 1 << p)) ? '1' : '0';
73                                 ctx->linebuf[p * ctx->linebuf_len +
74                                              ctx->line_offset] = c;
75                         }
76                         ctx->line_offset++;
77                         ctx->spl_cnt++;
78
79                         /* Add a space every 8th bit. */
80                         if ((ctx->spl_cnt & 7) == 0) {
81                                 for (p = 0; p < ctx->num_enabled_probes; p++)
82                                         ctx->linebuf[p * ctx->linebuf_len +
83                                                      ctx->line_offset] = ' ';
84                                 ctx->line_offset++;
85                         }
86
87                         /* End of line. */
88                         if (ctx->spl_cnt >= ctx->samples_per_line) {
89                                 flush_linebufs(ctx, outbuf);
90                                 ctx->line_offset = ctx->spl_cnt = 0;
91                                 ctx->mark_trigger = -1;
92                         }
93                 }
94         } else {
95                 g_message("short buffer (length_in=%" PRIu64 ")", length_in);
96         }
97
98         *data_out = outbuf;
99         *length_out = strlen(outbuf);
100
101         return SIGROK_OK;
102 }
103
104
105 struct output_format output_text_bits = {
106         "bits",
107         "Bits (takes argument, default 64)",
108         DF_LOGIC,
109         init_bits,
110         data_bits,
111         event,
112 };
113