]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> | |
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> | |
25 | #include "config.h" /* Needed for PACKAGE_STRING and others. */ | |
26 | #include "libsigrok.h" | |
27 | #include "libsigrok-internal.h" | |
28 | #include "text.h" | |
29 | ||
30 | #define LOG_PREFIX "output/text" | |
31 | ||
32 | SR_PRIV void flush_linebufs(struct context *ctx, uint8_t *outbuf) | |
33 | { | |
34 | static int max_channelname_len = 0; | |
35 | int len, i; | |
36 | GSList *l; | |
37 | char *channel_name; | |
38 | ||
39 | if (ctx->linebuf[0] == 0) | |
40 | return; | |
41 | ||
42 | if (max_channelname_len == 0) { | |
43 | /* First time through... */ | |
44 | for (l = ctx->channelnames; l; l = l->next) { | |
45 | channel_name = l->data; | |
46 | len = strlen(channel_name); | |
47 | if (len > max_channelname_len) | |
48 | max_channelname_len = len; | |
49 | } | |
50 | } | |
51 | ||
52 | for (i = 0, l = ctx->channelnames; l; l = l->next, i++) { | |
53 | channel_name = l->data; | |
54 | sprintf((char *)outbuf + strlen((const char *)outbuf), | |
55 | "%*s:%s\n", max_channelname_len, | |
56 | channel_name, ctx->linebuf + i * ctx->linebuf_len); | |
57 | } | |
58 | ||
59 | /* Mark trigger with a ^ character. */ | |
60 | if (ctx->mark_trigger != -1) | |
61 | { | |
62 | int space_offset = ctx->mark_trigger / 8; | |
63 | ||
64 | if (ctx->mode == MODE_ASCII) | |
65 | space_offset = 0; | |
66 | ||
67 | sprintf((char *)outbuf + strlen((const char *)outbuf), | |
68 | "T:%*s^\n", ctx->mark_trigger + space_offset, ""); | |
69 | } | |
70 | ||
71 | memset(ctx->linebuf, 0, i * ctx->linebuf_len); | |
72 | } | |
73 | ||
74 | SR_PRIV int init(struct sr_output *o, int default_spl, enum outputmode mode) | |
75 | { | |
76 | struct context *ctx; | |
77 | struct sr_channel *ch; | |
78 | GSList *l; | |
79 | GVariant *gvar; | |
80 | uint64_t samplerate; | |
81 | int num_channels, ret; | |
82 | char *samplerate_s; | |
83 | ||
84 | if (!(ctx = g_try_malloc0(sizeof(struct context)))) { | |
85 | sr_err("%s: ctx malloc failed", __func__); | |
86 | return SR_ERR_MALLOC; | |
87 | } | |
88 | ||
89 | o->internal = ctx; | |
90 | ctx->num_enabled_channels = 0; | |
91 | ctx->channelnames = NULL; | |
92 | ||
93 | for (l = o->sdi->channels; l; l = l->next) { | |
94 | ch = l->data; | |
95 | if (ch->type != SR_CHANNEL_LOGIC) | |
96 | continue; | |
97 | if (!ch->enabled) | |
98 | continue; | |
99 | ctx->channelnames = g_slist_append(ctx->channelnames, ch->name); | |
100 | ctx->num_enabled_channels++; | |
101 | } | |
102 | ||
103 | ctx->unitsize = (ctx->num_enabled_channels + 7) / 8; | |
104 | ctx->line_offset = 0; | |
105 | ctx->spl_cnt = 0; | |
106 | ctx->mark_trigger = -1; | |
107 | ctx->mode = mode; | |
108 | ||
109 | ret = SR_OK; | |
110 | if (o->param && o->param[0]) { | |
111 | ctx->samples_per_line = strtoul(o->param, NULL, 10); | |
112 | if (ctx->samples_per_line < 1) { | |
113 | ret = SR_ERR; | |
114 | goto err; | |
115 | } | |
116 | } else | |
117 | ctx->samples_per_line = default_spl; | |
118 | ||
119 | if (!(ctx->header = g_try_malloc0(512))) { | |
120 | sr_err("%s: ctx->header malloc failed", __func__); | |
121 | ret = SR_ERR_MALLOC; | |
122 | goto err; | |
123 | } | |
124 | ||
125 | snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING); | |
126 | num_channels = g_slist_length(o->sdi->channels); | |
127 | if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE, | |
128 | &gvar) == SR_OK) { | |
129 | samplerate = g_variant_get_uint64(gvar); | |
130 | g_variant_unref(gvar); | |
131 | if (!(samplerate_s = sr_samplerate_string(samplerate))) { | |
132 | ret = SR_ERR; | |
133 | goto err; | |
134 | } | |
135 | snprintf(ctx->header + strlen(ctx->header), | |
136 | 511 - strlen(ctx->header), | |
137 | "Acquisition with %d/%d channels at %s\n", | |
138 | ctx->num_enabled_channels, num_channels, samplerate_s); | |
139 | g_free(samplerate_s); | |
140 | } | |
141 | ||
142 | ctx->linebuf_len = ctx->samples_per_line * 2 + 4; | |
143 | if (!(ctx->linebuf = g_try_malloc0(num_channels * ctx->linebuf_len))) { | |
144 | sr_err("%s: ctx->linebuf malloc failed", __func__); | |
145 | ret = SR_ERR_MALLOC; | |
146 | goto err; | |
147 | } | |
148 | ||
149 | if (!(ctx->linevalues = g_try_malloc0(num_channels))) { | |
150 | sr_err("%s: ctx->linevalues malloc failed", __func__); | |
151 | ret = SR_ERR_MALLOC; | |
152 | } | |
153 | ||
154 | if (mode == MODE_ASCII && | |
155 | !(ctx->prevsample = g_try_malloc0(num_channels / 8))) { | |
156 | sr_err("%s: ctx->prevsample malloc failed", __func__); | |
157 | ret = SR_ERR_MALLOC; | |
158 | } | |
159 | ||
160 | err: | |
161 | if (ret != SR_OK) { | |
162 | g_free(ctx->header); | |
163 | g_free(ctx); | |
164 | } | |
165 | ||
166 | return ret; | |
167 | } | |
168 | ||
169 | SR_PRIV int text_cleanup(struct sr_output *o) | |
170 | { | |
171 | struct context *ctx; | |
172 | ||
173 | if (!o) | |
174 | return SR_ERR_ARG; | |
175 | ||
176 | ctx = o->internal; | |
177 | ||
178 | g_free(ctx->header); | |
179 | g_free(ctx->linebuf); | |
180 | g_free(ctx->linevalues); | |
181 | ||
182 | if (ctx->prevsample) | |
183 | g_free(ctx->prevsample); | |
184 | ||
185 | g_slist_free(ctx->channelnames); | |
186 | ||
187 | g_free(ctx); | |
188 | ||
189 | o->internal = NULL; | |
190 | ||
191 | return SR_OK; | |
192 | } | |
193 | ||
194 | SR_PRIV int event(struct sr_output *o, int event_type, uint8_t **data_out, | |
195 | uint64_t *length_out) | |
196 | { | |
197 | struct context *ctx; | |
198 | int outsize; | |
199 | uint8_t *outbuf; | |
200 | ||
201 | ctx = o->internal; | |
202 | switch (event_type) { | |
203 | case SR_DF_TRIGGER: | |
204 | ctx->mark_trigger = ctx->spl_cnt; | |
205 | *data_out = NULL; | |
206 | *length_out = 0; | |
207 | break; | |
208 | case SR_DF_END: | |
209 | outsize = ctx->num_enabled_channels | |
210 | * (ctx->samples_per_line + 20) + 512; | |
211 | if (!(outbuf = g_try_malloc0(outsize))) { | |
212 | sr_err("%s: outbuf malloc failed", __func__); | |
213 | return SR_ERR_MALLOC; | |
214 | } | |
215 | flush_linebufs(ctx, outbuf); | |
216 | *data_out = outbuf; | |
217 | *length_out = strlen((const char *)outbuf); | |
218 | break; | |
219 | default: | |
220 | *data_out = NULL; | |
221 | *length_out = 0; | |
222 | break; | |
223 | } | |
224 | ||
225 | return SR_OK; | |
226 | } |