]> sigrok.org Git - libsigrok.git/blame - output/text/ascii.c
input/output formats: Explicit struct member names.
[libsigrok.git] / output / text / ascii.c
CommitLineData
97554432
BV
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
f50f3f40 27int init_ascii(struct sr_output *o)
97554432
BV
28{
29 return init(o, DEFAULT_BPL_ASCII, MODE_ASCII);
30}
31
54ac5277
UH
32int data_ascii(struct sr_output *o, const char *data_in, uint64_t length_in,
33 char **data_out, uint64_t *length_out)
97554432
BV
34{
35 struct context *ctx;
36 unsigned int outsize, offset, p;
37 int max_linelen;
38 uint64_t sample;
39 char *outbuf;
40
41 ctx = o->internal;
9688b443 42 max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
97554432
BV
43 + ctx->samples_per_line / 8;
44 /*
45 * Calculate space needed for probes. Set aside 512 bytes for
46 * extra output, e.g. trigger.
47 */
48 outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
49 * (ctx->num_enabled_probes * max_linelen);
50
51 if (!(outbuf = calloc(1, outsize + 1)))
e46b8fb1 52 return SR_ERR_MALLOC;
97554432
BV
53
54 outbuf[0] = '\0';
55 if (ctx->header) {
56 /* The header is still here, this must be the first packet. */
57 strncpy(outbuf, ctx->header, outsize);
58 free(ctx->header);
59 ctx->header = NULL;
60 }
61
62 if (length_in >= ctx->unitsize) {
63 for (offset = 0; offset <= length_in - ctx->unitsize;
64 offset += ctx->unitsize) {
65 memcpy(&sample, data_in + offset, ctx->unitsize);
66
67 char tmpval[ctx->num_enabled_probes];
68
69 for (p = 0; p < ctx->num_enabled_probes; p++) {
70 uint64_t curbit = (sample & ((uint64_t) 1 << p));
71 uint64_t prevbit = (ctx->prevsample &
72 ((uint64_t) 1 << p));
73
74 if (curbit < prevbit && ctx->line_offset > 0) {
75 ctx->linebuf[p * ctx->linebuf_len +
76 ctx->line_offset-1] = '\\';
77 }
78
79 if (curbit > prevbit) {
80 tmpval[p] = '/';
81 } else {
82 if (curbit)
83 tmpval[p] = '"';
84 else
85 tmpval[p] = '.';
86 }
87 }
88
89 /* End of line. */
90 if (ctx->spl_cnt >= ctx->samples_per_line) {
91 flush_linebufs(ctx, outbuf);
92 ctx->line_offset = ctx->spl_cnt = 0;
93 ctx->mark_trigger = -1;
94 }
95
96 for (p = 0; p < ctx->num_enabled_probes; p++) {
97 ctx->linebuf[p * ctx->linebuf_len +
98 ctx->line_offset] = tmpval[p];
99 }
100
101 ctx->line_offset++;
102 ctx->spl_cnt++;
103
104 ctx->prevsample = sample;
105 }
106 } else {
107 g_message("short buffer (length_in=%" PRIu64 ")", length_in);
108 }
109
110 *data_out = outbuf;
111 *length_out = strlen(outbuf);
112
e46b8fb1 113 return SR_OK;
97554432
BV
114}
115
f50f3f40 116struct sr_output_format output_text_ascii = {
d494a4aa
UH
117 .extension = "ascii",
118 .description = "ASCII (takes argument, default 74)",
119 .df_type = SR_DF_LOGIC,
120 .init = init_ascii,
121 .data = data_ascii,
122 .event = event,
97554432 123};