]> sigrok.org Git - libsigrok.git/blob - output/text/ascii.c
sr: input/output: Mark more symbols with SR_PRIV.
[libsigrok.git] / output / text / ascii.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 "sigrok-internal.h"
26 #include "text.h"
27
28 SR_PRIV int init_ascii(struct sr_output *o)
29 {
30         return init(o, DEFAULT_BPL_ASCII, MODE_ASCII);
31 }
32
33 SR_PRIV int data_ascii(struct sr_output *o, const char *data_in,
34                        uint64_t length_in, char **data_out,
35                        uint64_t *length_out)
36 {
37         struct context *ctx;
38         unsigned int outsize, offset, p;
39         int max_linelen;
40         uint64_t sample;
41         char *outbuf;
42
43         ctx = o->internal;
44         max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
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
53         if (!(outbuf = calloc(1, outsize + 1)))
54                 return SR_ERR_MALLOC;
55
56         outbuf[0] = '\0';
57         if (ctx->header) {
58                 /* The header is still here, this must be the first packet. */
59                 strncpy(outbuf, ctx->header, outsize);
60                 free(ctx->header);
61                 ctx->header = NULL;
62         }
63
64         if (length_in >= ctx->unitsize) {
65                 for (offset = 0; offset <= length_in - ctx->unitsize;
66                      offset += ctx->unitsize) {
67                         memcpy(&sample, data_in + offset, ctx->unitsize);
68
69                         char tmpval[ctx->num_enabled_probes];
70
71                         for (p = 0; p < ctx->num_enabled_probes; p++) {
72                                 uint64_t curbit = (sample & ((uint64_t) 1 << p));
73                                 uint64_t prevbit = (ctx->prevsample &
74                                                 ((uint64_t) 1 << p));
75
76                                 if (curbit < prevbit && ctx->line_offset > 0) {
77                                         ctx->linebuf[p * ctx->linebuf_len +
78                                                 ctx->line_offset-1] = '\\';
79                                 }
80
81                                 if (curbit > prevbit) {
82                                         tmpval[p] = '/';
83                                 } else {
84                                         if (curbit)
85                                                 tmpval[p] = '"';
86                                         else
87                                                 tmpval[p] = '.';
88                                 }
89                         }
90
91                         /* End of line. */
92                         if (ctx->spl_cnt >= ctx->samples_per_line) {
93                                 flush_linebufs(ctx, outbuf);
94                                 ctx->line_offset = ctx->spl_cnt = 0;
95                                 ctx->mark_trigger = -1;
96                         }
97
98                         for (p = 0; p < ctx->num_enabled_probes; p++) {
99                                 ctx->linebuf[p * ctx->linebuf_len +
100                                              ctx->line_offset] = tmpval[p];
101                         }
102
103                         ctx->line_offset++;
104                         ctx->spl_cnt++;
105
106                         ctx->prevsample = sample;
107                 }
108         } else {
109                 sr_info("short buffer (length_in=%" PRIu64 ")", length_in);
110         }
111
112         *data_out = outbuf;
113         *length_out = strlen(outbuf);
114
115         return SR_OK;
116 }
117
118 SR_PRIV struct sr_output_format output_text_ascii = {
119         .id = "ascii",
120         .description = "ASCII (takes argument, default 74)",
121         .df_type = SR_DF_LOGIC,
122         .init = init_ascii,
123         .data = data_ascii,
124         .event = event,
125 };