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 /* Message logging helpers with subsystem-specific prefix string. */
31 #define LOG_PREFIX "output/text: "
32 #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
33 #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
34 #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
35 #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
36 #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
37 #define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args)
39 SR_PRIV void flush_linebufs(struct context *ctx, uint8_t *outbuf)
41 static int max_probename_len = 0;
46 if (ctx->linebuf[0] == 0)
49 if (max_probename_len == 0) {
50 /* First time through... */
51 for (l = ctx->probenames; l; l = l->next) {
53 len = strlen(probe_name);
54 if (len > max_probename_len)
55 max_probename_len = len;
59 for (i = 0, l = ctx->probenames; l; l = l->next, i++) {
61 sprintf((char *)outbuf + strlen((const char *)outbuf),
62 "%*s:%s\n", max_probename_len,
63 probe_name, ctx->linebuf + i * ctx->linebuf_len);
66 /* Mark trigger with a ^ character. */
67 if (ctx->mark_trigger != -1)
69 int space_offset = ctx->mark_trigger / 8;
71 if (ctx->mode == MODE_ASCII)
74 sprintf((char *)outbuf + strlen((const char *)outbuf),
75 "T:%*s^\n", ctx->mark_trigger + space_offset, "");
78 memset(ctx->linebuf, 0, i * ctx->linebuf_len);
81 SR_PRIV int init(struct sr_output *o, int default_spl, enum outputmode mode)
84 struct sr_probe *probe;
91 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
92 sr_err("%s: ctx malloc failed", __func__);
97 ctx->num_enabled_probes = 0;
98 ctx->probenames = NULL;
100 for (l = o->sdi->probes; l; l = l->next) {
104 ctx->probenames = g_slist_append(ctx->probenames, probe->name);
105 ctx->num_enabled_probes++;
108 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
109 ctx->line_offset = 0;
111 ctx->mark_trigger = -1;
115 if (o->param && o->param[0]) {
116 ctx->samples_per_line = strtoul(o->param, NULL, 10);
117 if (ctx->samples_per_line < 1) {
122 ctx->samples_per_line = default_spl;
124 if (!(ctx->header = g_try_malloc0(512))) {
125 sr_err("%s: ctx->header malloc failed", __func__);
130 snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
131 num_probes = g_slist_length(o->sdi->probes);
132 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
134 samplerate = g_variant_get_uint64(gvar);
135 g_variant_unref(gvar);
136 if (!(samplerate_s = sr_samplerate_string(samplerate))) {
140 snprintf(ctx->header + strlen(ctx->header),
141 511 - strlen(ctx->header),
142 "Acquisition with %d/%d probes at %s\n",
143 ctx->num_enabled_probes, num_probes, samplerate_s);
144 g_free(samplerate_s);
147 ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
148 if (!(ctx->linebuf = g_try_malloc0(num_probes * ctx->linebuf_len))) {
149 sr_err("%s: ctx->linebuf malloc failed", __func__);
154 if (!(ctx->linevalues = g_try_malloc0(num_probes))) {
155 sr_err("%s: ctx->linevalues malloc failed", __func__);
159 if (mode == MODE_ASCII &&
160 !(ctx->prevsample = g_try_malloc0(num_probes / 8))) {
161 sr_err("%s: ctx->prevsample malloc failed", __func__);
174 SR_PRIV int event(struct sr_output *o, int event_type, uint8_t **data_out,
175 uint64_t *length_out)
182 switch (event_type) {
184 ctx->mark_trigger = ctx->spl_cnt;
189 outsize = ctx->num_enabled_probes
190 * (ctx->samples_per_line + 20) + 512;
191 if (!(outbuf = g_try_malloc0(outsize))) {
192 sr_err("%s: outbuf malloc failed", __func__);
193 return SR_ERR_MALLOC;
195 flush_linebufs(ctx, outbuf);
197 *length_out = strlen((const char *)outbuf);