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_probename_len = 0;
39 if (ctx->linebuf[0] == 0)
42 if (max_probename_len == 0) {
43 /* First time through... */
44 for (l = ctx->probenames; l; l = l->next) {
46 len = strlen(probe_name);
47 if (len > max_probename_len)
48 max_probename_len = len;
52 for (i = 0, l = ctx->probenames; l; l = l->next, i++) {
54 sprintf((char *)outbuf + strlen((const char *)outbuf),
55 "%*s:%s\n", max_probename_len,
56 probe_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_probe *probe;
84 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
85 sr_err("%s: ctx malloc failed", __func__);
90 ctx->num_enabled_probes = 0;
91 ctx->probenames = NULL;
93 for (l = o->sdi->probes; l; l = l->next) {
97 ctx->probenames = g_slist_append(ctx->probenames, probe->name);
98 ctx->num_enabled_probes++;
101 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
102 ctx->line_offset = 0;
104 ctx->mark_trigger = -1;
108 if (o->param && o->param[0]) {
109 ctx->samples_per_line = strtoul(o->param, NULL, 10);
110 if (ctx->samples_per_line < 1) {
115 ctx->samples_per_line = default_spl;
117 if (!(ctx->header = g_try_malloc0(512))) {
118 sr_err("%s: ctx->header malloc failed", __func__);
123 snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
124 num_probes = g_slist_length(o->sdi->probes);
125 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
127 samplerate = g_variant_get_uint64(gvar);
128 g_variant_unref(gvar);
129 if (!(samplerate_s = sr_samplerate_string(samplerate))) {
133 snprintf(ctx->header + strlen(ctx->header),
134 511 - strlen(ctx->header),
135 "Acquisition with %d/%d probes at %s\n",
136 ctx->num_enabled_probes, num_probes, samplerate_s);
137 g_free(samplerate_s);
140 ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
141 if (!(ctx->linebuf = g_try_malloc0(num_probes * ctx->linebuf_len))) {
142 sr_err("%s: ctx->linebuf malloc failed", __func__);
147 if (!(ctx->linevalues = g_try_malloc0(num_probes))) {
148 sr_err("%s: ctx->linevalues malloc failed", __func__);
152 if (mode == MODE_ASCII &&
153 !(ctx->prevsample = g_try_malloc0(num_probes / 8))) {
154 sr_err("%s: ctx->prevsample malloc failed", __func__);
167 SR_PRIV int text_cleanup(struct sr_output *o)
177 g_free(ctx->linebuf);
178 g_free(ctx->linevalues);
181 g_free(ctx->prevsample);
183 g_slist_free(ctx->probenames);
192 SR_PRIV int event(struct sr_output *o, int event_type, uint8_t **data_out,
193 uint64_t *length_out)
200 switch (event_type) {
202 ctx->mark_trigger = ctx->spl_cnt;
207 outsize = ctx->num_enabled_probes
208 * (ctx->samples_per_line + 20) + 512;
209 if (!(outbuf = g_try_malloc0(outsize))) {
210 sr_err("%s: outbuf malloc failed", __func__);
211 return SR_ERR_MALLOC;
213 flush_linebufs(ctx, outbuf);
215 *length_out = strlen((const char *)outbuf);