]> sigrok.org Git - libsigrok.git/blame - output/text/text.c
sr: adjust copyright year
[libsigrok.git] / output / text / text.c
CommitLineData
97554432
BV
1/*
2 * This file is part of the sigrok project.
3 *
c73d2ea4 4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
97554432
BV
5 * Copyright (C) 2011 HÃ¥vard Espeland <gus@ping.uio.no>
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
21#include <stdlib.h>
22#include <stdio.h>
23#include <string.h>
24#include <glib.h>
97554432 25#include "config.h"
1a081ca6 26#include "sigrok.h"
133a37bf 27#include "sigrok-internal.h"
97554432
BV
28#include "text.h"
29
7c1d391c 30SR_PRIV void flush_linebufs(struct context *ctx, char *outbuf)
97554432
BV
31{
32 static int max_probename_len = 0;
33 int len, i;
34
35 if (ctx->linebuf[0] == 0)
36 return;
37
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;
44 }
45 }
46
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);
50 }
51
52 /* Mark trigger with a ^ character. */
53 if (ctx->mark_trigger != -1)
54 {
55 int space_offset = ctx->mark_trigger / 8;
56
57 if (ctx->mode == MODE_ASCII)
58 space_offset = 0;
59
60 sprintf(outbuf + strlen(outbuf), "T:%*s^\n",
61 ctx->mark_trigger + space_offset, "");
62 }
63
64 memset(ctx->linebuf, 0, i * ctx->linebuf_len);
65}
66
7c1d391c 67SR_PRIV int init(struct sr_output *o, int default_spl, enum outputmode mode)
97554432
BV
68{
69 struct context *ctx;
1afe8989 70 struct sr_probe *probe;
97554432
BV
71 GSList *l;
72 uint64_t samplerate;
73 int num_probes;
74 char *samplerate_s;
75
133a37bf
UH
76 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
77 sr_err("text out: %s: ctx malloc failed", __func__);
e46b8fb1 78 return SR_ERR_MALLOC;
133a37bf 79 }
97554432
BV
80
81 o->internal = ctx;
82 ctx->num_enabled_probes = 0;
83
84 for (l = o->device->probes; l; l = l->next) {
85 probe = l->data;
86 if (!probe->enabled)
87 continue;
88 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
89 }
90
91 ctx->probelist[ctx->num_enabled_probes] = 0;
92 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
93 ctx->line_offset = 0;
94 ctx->spl_cnt = 0;
95 ctx->mark_trigger = -1;
96 ctx->mode = mode;
97
98 if (o->param && o->param[0]) {
99 ctx->samples_per_line = strtoul(o->param, NULL, 10);
100 if (ctx->samples_per_line < 1)
e46b8fb1 101 return SR_ERR;
97554432
BV
102 } else
103 ctx->samples_per_line = default_spl;
104
133a37bf
UH
105 if (!(ctx->header = g_try_malloc0(512))) {
106 g_free(ctx);
107 sr_err("text out: %s: ctx->header malloc failed", __func__);
e46b8fb1 108 return SR_ERR_MALLOC;
97554432
BV
109 }
110
111 snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
112 num_probes = g_slist_length(o->device->probes);
03168500 113 if (o->device->plugin || sr_dev_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) {
97554432 114 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
5a2326a7 115 o->device->plugin_index, SR_DI_CUR_SAMPLERATE));
a00ba012 116 if (!(samplerate_s = sr_samplerate_string(samplerate))) {
133a37bf
UH
117 g_free(ctx->header);
118 g_free(ctx);
e46b8fb1 119 return SR_ERR;
97554432
BV
120 }
121 snprintf(ctx->header + strlen(ctx->header),
122 511 - strlen(ctx->header),
123 "Acquisition with %d/%d probes at %s\n",
124 ctx->num_enabled_probes, num_probes, samplerate_s);
133a37bf 125 g_free(samplerate_s);
97554432
BV
126 }
127
128 ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
133a37bf
UH
129 if (!(ctx->linebuf = g_try_malloc0(num_probes * ctx->linebuf_len))) {
130 g_free(ctx->header);
131 g_free(ctx);
132 sr_err("text out: %s: ctx->linebuf malloc failed", __func__);
e46b8fb1 133 return SR_ERR_MALLOC;
97554432 134 }
133a37bf
UH
135 if (!(ctx->linevalues = g_try_malloc0(num_probes))) {
136 g_free(ctx->header);
137 g_free(ctx);
138 sr_err("text out: %s: ctx->linevalues malloc failed", __func__);
e46b8fb1 139 return SR_ERR_MALLOC;
97554432
BV
140 }
141
e46b8fb1 142 return SR_OK;
97554432
BV
143}
144
7c1d391c
UH
145SR_PRIV int event(struct sr_output *o, int event_type, char **data_out,
146 uint64_t *length_out)
97554432
BV
147{
148 struct context *ctx;
149 int outsize;
150 char *outbuf;
151
152 ctx = o->internal;
153 switch (event_type) {
5a2326a7 154 case SR_DF_TRIGGER:
97554432
BV
155 ctx->mark_trigger = ctx->spl_cnt;
156 *data_out = NULL;
157 *length_out = 0;
158 break;
5a2326a7 159 case SR_DF_END:
97554432
BV
160 outsize = ctx->num_enabled_probes
161 * (ctx->samples_per_line + 20) + 512;
133a37bf
UH
162 if (!(outbuf = g_try_malloc0(outsize))) {
163 sr_err("text out: %s: outbuf malloc failed", __func__);
e46b8fb1 164 return SR_ERR_MALLOC;
133a37bf 165 }
97554432
BV
166 flush_linebufs(ctx, outbuf);
167 *data_out = outbuf;
168 *length_out = strlen(outbuf);
133a37bf 169 g_free(o->internal);
97554432
BV
170 o->internal = NULL;
171 break;
172 default:
173 *data_out = NULL;
174 *length_out = 0;
175 break;
176 }
177
e46b8fb1 178 return SR_OK;
97554432 179}