]> sigrok.org Git - libsigrok.git/blame_incremental - output/text/ascii.c
Centralise duplicated logging helper defines.
[libsigrok.git] / output / text / ascii.c
... / ...
CommitLineData
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#define LOG_PREFIX "output/ascii"
29
30SR_PRIV int init_ascii(struct sr_output *o)
31{
32 return init(o, DEFAULT_BPL_ASCII, MODE_ASCII);
33}
34
35SR_PRIV int data_ascii(struct sr_output *o, const uint8_t *data_in,
36 uint64_t length_in, uint8_t **data_out,
37 uint64_t *length_out)
38{
39 struct context *ctx;
40 unsigned int outsize, offset, p;
41 int max_linelen;
42 const uint8_t *sample;
43 uint8_t *outbuf;
44
45 ctx = o->internal;
46 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
47 + ctx->samples_per_line / 8;
48 /*
49 * Calculate space needed for probes. Set aside 512 bytes for
50 * extra output, e.g. trigger.
51 */
52 outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
53 * (ctx->num_enabled_probes * max_linelen);
54
55 if (!(outbuf = g_try_malloc0(outsize + 1))) {
56 sr_err("%s: outbuf malloc failed", __func__);
57 return SR_ERR_MALLOC;
58 }
59
60 outbuf[0] = '\0';
61 if (ctx->header) {
62 /* The header is still here, this must be the first packet. */
63 strncpy((char *)outbuf, ctx->header, outsize);
64 g_free(ctx->header);
65 ctx->header = NULL;
66 }
67
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
73 char tmpval[ctx->num_enabled_probes];
74
75 for (p = 0; p < ctx->num_enabled_probes; p++) {
76 uint8_t curbit = (sample[p / 8] & ((uint8_t) 1 << (p % 8)));
77 uint8_t prevbit = (ctx->prevsample[p / 8] &
78 ((uint8_t) 1 << (p % 8)));
79
80 if (curbit < prevbit && ctx->line_offset > 0) {
81 ctx->linebuf[p * ctx->linebuf_len +
82 ctx->line_offset-1] = '\\';
83 }
84
85 if (curbit > prevbit) {
86 tmpval[p] = '/';
87 } else {
88 if (curbit)
89 tmpval[p] = '"';
90 else
91 tmpval[p] = '.';
92 }
93 }
94
95 /* End of line. */
96 if (ctx->spl_cnt >= ctx->samples_per_line) {
97 flush_linebufs(ctx, outbuf);
98 ctx->line_offset = ctx->spl_cnt = 0;
99 ctx->mark_trigger = -1;
100 }
101
102 for (p = 0; p < ctx->num_enabled_probes; p++) {
103 ctx->linebuf[p * ctx->linebuf_len +
104 ctx->line_offset] = tmpval[p];
105 }
106
107 ctx->line_offset++;
108 ctx->spl_cnt++;
109
110 memcpy(ctx->prevsample, sample, ctx->unitsize);
111 }
112 } else {
113 sr_info("Short buffer (length_in=%" PRIu64 ").", length_in);
114 }
115
116 *data_out = outbuf;
117 *length_out = strlen((const char *)outbuf);
118
119 return SR_OK;
120}
121
122SR_PRIV struct sr_output_format output_text_ascii = {
123 .id = "ascii",
124 .description = "ASCII",
125 .df_type = SR_DF_LOGIC,
126 .init = init_ascii,
127 .data = data_ascii,
128 .event = event,
129 .cleanup = text_cleanup,
130};