2 * This file is part of the sigrok project.
4 * Copyright (C) 2010-2012 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/>.
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
30 SR_PRIV void flush_linebufs(struct context *ctx, uint8_t *outbuf)
32 static int max_probename_len = 0;
35 if (ctx->linebuf[0] == 0)
38 if (max_probename_len == 0) {
39 /* First time through... */
40 for (i = 0; ctx->probelist[i]; i++) {
41 len = strlen(ctx->probelist[i]);
42 if (len > max_probename_len)
43 max_probename_len = len;
47 for (i = 0; ctx->probelist[i]; i++) {
48 sprintf((char *)outbuf + strlen((const char *)outbuf),
49 "%*s:%s\n", max_probename_len,
50 ctx->probelist[i], ctx->linebuf + i * ctx->linebuf_len);
53 /* Mark trigger with a ^ character. */
54 if (ctx->mark_trigger != -1)
56 int space_offset = ctx->mark_trigger / 8;
58 if (ctx->mode == MODE_ASCII)
61 sprintf((char *)outbuf + strlen((const char *)outbuf),
62 "T:%*s^\n", ctx->mark_trigger + space_offset, "");
65 memset(ctx->linebuf, 0, i * ctx->linebuf_len);
68 SR_PRIV int init(struct sr_output *o, int default_spl, enum outputmode mode)
71 struct sr_probe *probe;
77 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
78 sr_err("text out: %s: ctx malloc failed", __func__);
83 ctx->num_enabled_probes = 0;
85 for (l = o->dev->probes; l; l = l->next) {
89 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
92 ctx->probelist[ctx->num_enabled_probes] = 0;
93 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
96 ctx->mark_trigger = -1;
99 if (o->param && o->param[0]) {
100 ctx->samples_per_line = strtoul(o->param, NULL, 10);
101 if (ctx->samples_per_line < 1)
104 ctx->samples_per_line = default_spl;
106 if (!(ctx->header = g_try_malloc0(512))) {
108 sr_err("text out: %s: ctx->header malloc failed", __func__);
109 return SR_ERR_MALLOC;
112 snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
113 num_probes = g_slist_length(o->dev->probes);
114 if (o->dev->driver || sr_dev_has_hwcap(o->dev, SR_HWCAP_SAMPLERATE)) {
115 samplerate = *((uint64_t *) o->dev->driver->dev_info_get(
116 o->dev->driver_index, SR_DI_CUR_SAMPLERATE));
117 if (!(samplerate_s = sr_samplerate_string(samplerate))) {
122 snprintf(ctx->header + strlen(ctx->header),
123 511 - strlen(ctx->header),
124 "Acquisition with %d/%d probes at %s\n",
125 ctx->num_enabled_probes, num_probes, samplerate_s);
126 g_free(samplerate_s);
129 ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
130 if (!(ctx->linebuf = g_try_malloc0(num_probes * ctx->linebuf_len))) {
133 sr_err("text out: %s: ctx->linebuf malloc failed", __func__);
134 return SR_ERR_MALLOC;
136 if (!(ctx->linevalues = g_try_malloc0(num_probes))) {
139 sr_err("text out: %s: ctx->linevalues malloc failed", __func__);
140 return SR_ERR_MALLOC;
146 SR_PRIV int event(struct sr_output *o, int event_type, uint8_t **data_out,
147 uint64_t *length_out)
154 switch (event_type) {
156 ctx->mark_trigger = ctx->spl_cnt;
161 outsize = ctx->num_enabled_probes
162 * (ctx->samples_per_line + 20) + 512;
163 if (!(outbuf = g_try_malloc0(outsize))) {
164 sr_err("text out: %s: outbuf malloc failed", __func__);
165 return SR_ERR_MALLOC;
167 flush_linebufs(ctx, outbuf);
169 *length_out = strlen((const char *)outbuf);