]> sigrok.org Git - libsigrok.git/blame - output/text/text.c
Input modules: Use message logging helpers.
[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>
545f9786 25#include "config.h" /* Needed for PACKAGE_STRING and others. */
45c59c8b
BV
26#include "libsigrok.h"
27#include "libsigrok-internal.h"
97554432
BV
28#include "text.h"
29
054e6709 30SR_PRIV void flush_linebufs(struct context *ctx, uint8_t *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++) {
054e6709
UH
48 sprintf((char *)outbuf + strlen((const char *)outbuf),
49 "%*s:%s\n", max_probename_len,
97554432
BV
50 ctx->probelist[i], ctx->linebuf + i * ctx->linebuf_len);
51 }
52
53 /* Mark trigger with a ^ character. */
54 if (ctx->mark_trigger != -1)
55 {
56 int space_offset = ctx->mark_trigger / 8;
57
58 if (ctx->mode == MODE_ASCII)
59 space_offset = 0;
60
054e6709
UH
61 sprintf((char *)outbuf + strlen((const char *)outbuf),
62 "T:%*s^\n", ctx->mark_trigger + space_offset, "");
97554432
BV
63 }
64
65 memset(ctx->linebuf, 0, i * ctx->linebuf_len);
66}
67
7c1d391c 68SR_PRIV int init(struct sr_output *o, int default_spl, enum outputmode mode)
97554432
BV
69{
70 struct context *ctx;
1afe8989 71 struct sr_probe *probe;
97554432 72 GSList *l;
5c3c1241
BV
73 uint64_t *samplerate;
74 int num_probes, ret;
97554432
BV
75 char *samplerate_s;
76
133a37bf
UH
77 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
78 sr_err("text out: %s: ctx malloc failed", __func__);
e46b8fb1 79 return SR_ERR_MALLOC;
133a37bf 80 }
97554432
BV
81
82 o->internal = ctx;
83 ctx->num_enabled_probes = 0;
84
5c3c1241 85 for (l = o->sdi->probes; l; l = l->next) {
97554432
BV
86 probe = l->data;
87 if (!probe->enabled)
88 continue;
89 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
90 }
91
92 ctx->probelist[ctx->num_enabled_probes] = 0;
93 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
94 ctx->line_offset = 0;
95 ctx->spl_cnt = 0;
96 ctx->mark_trigger = -1;
97 ctx->mode = mode;
98
aee878fa 99 ret = SR_OK;
97554432
BV
100 if (o->param && o->param[0]) {
101 ctx->samples_per_line = strtoul(o->param, NULL, 10);
5c3c1241
BV
102 if (ctx->samples_per_line < 1) {
103 ret = SR_ERR;
104 goto err;
105 }
97554432
BV
106 } else
107 ctx->samples_per_line = default_spl;
108
133a37bf 109 if (!(ctx->header = g_try_malloc0(512))) {
133a37bf 110 sr_err("text out: %s: ctx->header malloc failed", __func__);
5c3c1241
BV
111 ret = SR_ERR_MALLOC;
112 goto err;
97554432
BV
113 }
114
115 snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
5c3c1241
BV
116 num_probes = g_slist_length(o->sdi->probes);
117 if (o->sdi->driver || sr_dev_has_hwcap(o->sdi, SR_HWCAP_SAMPLERATE)) {
118 ret = o->sdi->driver->info_get(SR_DI_CUR_SAMPLERATE,
119 (const void **)&samplerate, o->sdi);
120 if (ret != SR_OK)
121 goto err;
122 if (!(samplerate_s = sr_samplerate_string(*samplerate))) {
123 ret = SR_ERR;
124 goto err;
97554432
BV
125 }
126 snprintf(ctx->header + strlen(ctx->header),
127 511 - strlen(ctx->header),
128 "Acquisition with %d/%d probes at %s\n",
129 ctx->num_enabled_probes, num_probes, samplerate_s);
133a37bf 130 g_free(samplerate_s);
97554432
BV
131 }
132
133 ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
133a37bf 134 if (!(ctx->linebuf = g_try_malloc0(num_probes * ctx->linebuf_len))) {
133a37bf 135 sr_err("text out: %s: ctx->linebuf malloc failed", __func__);
5c3c1241
BV
136 ret = SR_ERR_MALLOC;
137 goto err;
97554432 138 }
5c3c1241 139
133a37bf 140 if (!(ctx->linevalues = g_try_malloc0(num_probes))) {
5c3c1241
BV
141 sr_err("text out: %s: ctx->linevalues malloc failed", __func__);
142 ret = SR_ERR_MALLOC;
143 }
144
145err:
146 if (ret != SR_OK) {
133a37bf
UH
147 g_free(ctx->header);
148 g_free(ctx);
97554432
BV
149 }
150
5c3c1241 151 return ret;
97554432
BV
152}
153
054e6709 154SR_PRIV int event(struct sr_output *o, int event_type, uint8_t **data_out,
7c1d391c 155 uint64_t *length_out)
97554432
BV
156{
157 struct context *ctx;
158 int outsize;
054e6709 159 uint8_t *outbuf;
97554432
BV
160
161 ctx = o->internal;
162 switch (event_type) {
5a2326a7 163 case SR_DF_TRIGGER:
97554432
BV
164 ctx->mark_trigger = ctx->spl_cnt;
165 *data_out = NULL;
166 *length_out = 0;
167 break;
5a2326a7 168 case SR_DF_END:
97554432
BV
169 outsize = ctx->num_enabled_probes
170 * (ctx->samples_per_line + 20) + 512;
133a37bf
UH
171 if (!(outbuf = g_try_malloc0(outsize))) {
172 sr_err("text out: %s: outbuf malloc failed", __func__);
e46b8fb1 173 return SR_ERR_MALLOC;
133a37bf 174 }
97554432
BV
175 flush_linebufs(ctx, outbuf);
176 *data_out = outbuf;
054e6709 177 *length_out = strlen((const char *)outbuf);
133a37bf 178 g_free(o->internal);
97554432
BV
179 o->internal = NULL;
180 break;
181 default:
182 *data_out = NULL;
183 *length_out = 0;
184 break;
185 }
186
e46b8fb1 187 return SR_OK;
97554432 188}