2 * This file is part of the sigrok project.
4 * Copyright (C) 2011 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/>.
30 void flush_linebufs(struct context *ctx, char *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(outbuf + strlen(outbuf), "%*s:%s\n", max_probename_len,
49 ctx->probelist[i], ctx->linebuf + i * ctx->linebuf_len);
52 /* Mark trigger with a ^ character. */
53 if (ctx->mark_trigger != -1)
55 int space_offset = ctx->mark_trigger / 8;
57 if (ctx->mode == MODE_ASCII)
60 sprintf(outbuf + strlen(outbuf), "T:%*s^\n",
61 ctx->mark_trigger + space_offset, "");
64 memset(ctx->linebuf, 0, i * ctx->linebuf_len);
67 int init(struct sr_output *o, int default_spl, enum outputmode mode)
70 struct sr_probe *probe;
76 if (!(ctx = calloc(1, sizeof(struct context))))
80 ctx->num_enabled_probes = 0;
82 for (l = o->device->probes; l; l = l->next) {
86 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
89 ctx->probelist[ctx->num_enabled_probes] = 0;
90 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
93 ctx->mark_trigger = -1;
96 if (o->param && o->param[0]) {
97 ctx->samples_per_line = strtoul(o->param, NULL, 10);
98 if (ctx->samples_per_line < 1)
101 ctx->samples_per_line = default_spl;
103 if (!(ctx->header = malloc(512))) {
105 return SR_ERR_MALLOC;
108 snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
109 num_probes = g_slist_length(o->device->probes);
110 if (o->device->plugin || sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) {
111 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
112 o->device->plugin_index, SR_DI_CUR_SAMPLERATE));
113 if (!(samplerate_s = sr_samplerate_string(samplerate))) {
118 snprintf(ctx->header + strlen(ctx->header),
119 511 - strlen(ctx->header),
120 "Acquisition with %d/%d probes at %s\n",
121 ctx->num_enabled_probes, num_probes, samplerate_s);
125 ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
126 if (!(ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len))) {
129 return SR_ERR_MALLOC;
131 if (!(ctx->linevalues = calloc(1, num_probes))) {
134 return SR_ERR_MALLOC;
140 int event(struct sr_output *o, int event_type, char **data_out,
141 uint64_t *length_out)
148 switch (event_type) {
150 ctx->mark_trigger = ctx->spl_cnt;
155 outsize = ctx->num_enabled_probes
156 * (ctx->samples_per_line + 20) + 512;
157 if (!(outbuf = calloc(1, outsize)))
158 return SR_ERR_MALLOC;
159 flush_linebufs(ctx, outbuf);
161 *length_out = strlen(outbuf);