2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2011 HÃ¥vard Espeland <gus@ping.uio.no>
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.
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.
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/>.
25 #include "config.h" /* Needed for PACKAGE_STRING and others. */
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
30 #define LOG_PREFIX "output/text"
32 SR_PRIV void flush_linebufs(struct context *ctx, uint8_t *outbuf)
34 static int max_channelname_len = 0;
39 if (ctx->linebuf[0] == 0)
42 if (max_channelname_len == 0) {
43 /* First time through... */
44 for (l = ctx->channelnames; l; l = l->next) {
45 channel_name = l->data;
46 len = strlen(channel_name);
47 if (len > max_channelname_len)
48 max_channelname_len = len;
52 for (i = 0, l = ctx->channelnames; l; l = l->next, i++) {
53 channel_name = l->data;
54 sprintf((char *)outbuf + strlen((const char *)outbuf),
55 "%*s:%s\n", max_channelname_len,
56 channel_name, ctx->linebuf + i * ctx->linebuf_len);
59 /* Mark trigger with a ^ character. */
60 if (ctx->mark_trigger != -1)
62 int space_offset = ctx->mark_trigger / 8;
64 if (ctx->mode == MODE_ASCII)
67 sprintf((char *)outbuf + strlen((const char *)outbuf),
68 "T:%*s^\n", ctx->mark_trigger + space_offset, "");
71 memset(ctx->linebuf, 0, i * ctx->linebuf_len);
74 SR_PRIV int init(struct sr_output *o, int default_spl, enum outputmode mode)
77 struct sr_channel *ch;
81 int num_channels, ret;
84 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
85 sr_err("%s: ctx malloc failed", __func__);
90 ctx->num_enabled_channels = 0;
91 ctx->channelnames = NULL;
93 for (l = o->sdi->channels; l; l = l->next) {
95 if (ch->type != SR_PROBE_LOGIC)
99 ctx->channelnames = g_slist_append(ctx->channelnames, ch->name);
100 ctx->num_enabled_channels++;
103 ctx->unitsize = (ctx->num_enabled_channels + 7) / 8;
104 ctx->line_offset = 0;
106 ctx->mark_trigger = -1;
110 if (o->param && o->param[0]) {
111 ctx->samples_per_line = strtoul(o->param, NULL, 10);
112 if (ctx->samples_per_line < 1) {
117 ctx->samples_per_line = default_spl;
119 if (!(ctx->header = g_try_malloc0(512))) {
120 sr_err("%s: ctx->header malloc failed", __func__);
125 snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
126 num_channels = g_slist_length(o->sdi->channels);
127 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
129 samplerate = g_variant_get_uint64(gvar);
130 g_variant_unref(gvar);
131 if (!(samplerate_s = sr_samplerate_string(samplerate))) {
135 snprintf(ctx->header + strlen(ctx->header),
136 511 - strlen(ctx->header),
137 "Acquisition with %d/%d channels at %s\n",
138 ctx->num_enabled_channels, num_channels, samplerate_s);
139 g_free(samplerate_s);
142 ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
143 if (!(ctx->linebuf = g_try_malloc0(num_channels * ctx->linebuf_len))) {
144 sr_err("%s: ctx->linebuf malloc failed", __func__);
149 if (!(ctx->linevalues = g_try_malloc0(num_channels))) {
150 sr_err("%s: ctx->linevalues malloc failed", __func__);
154 if (mode == MODE_ASCII &&
155 !(ctx->prevsample = g_try_malloc0(num_channels / 8))) {
156 sr_err("%s: ctx->prevsample malloc failed", __func__);
169 SR_PRIV int text_cleanup(struct sr_output *o)
179 g_free(ctx->linebuf);
180 g_free(ctx->linevalues);
183 g_free(ctx->prevsample);
185 g_slist_free(ctx->channelnames);
194 SR_PRIV int event(struct sr_output *o, int event_type, uint8_t **data_out,
195 uint64_t *length_out)
202 switch (event_type) {
204 ctx->mark_trigger = ctx->spl_cnt;
209 outsize = ctx->num_enabled_channels
210 * (ctx->samples_per_line + 20) + 512;
211 if (!(outbuf = g_try_malloc0(outsize))) {
212 sr_err("%s: outbuf malloc failed", __func__);
213 return SR_ERR_MALLOC;
215 flush_linebufs(ctx, outbuf);
217 *length_out = strlen((const char *)outbuf);