]>
Commit | Line | Data |
---|---|---|
6f64ebb2 BV |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2011 HÃ¥vard Espeland <gus@ping.uio.no> | |
5 | * Copyright (C) 2014 Bert Vermeulen <bert@biot.com> | |
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 | ||
6ec6c43b | 21 | #include <config.h> |
6f64ebb2 BV |
22 | #include <stdlib.h> |
23 | #include <string.h> | |
24 | #include <glib.h> | |
c1aae900 | 25 | #include <libsigrok/libsigrok.h> |
6f64ebb2 BV |
26 | #include "libsigrok-internal.h" |
27 | ||
28 | #define LOG_PREFIX "output/hex" | |
29 | ||
30 | #define DEFAULT_SAMPLES_PER_LINE 74 | |
31 | ||
0150fdca GS |
32 | /* |
33 | * The string looks ugly with escape characters, here is the readable | |
34 | * version: Use . and " for low and high bits, use \ and / to draw | |
35 | * falling and rising edges respectively. | |
36 | */ | |
37 | #define DEFAULT_ASCII_CHARS ".\"\\/" | |
38 | ||
6f64ebb2 BV |
39 | struct context { |
40 | unsigned int num_enabled_channels; | |
dcc55fe9 | 41 | int spl; |
6f64ebb2 BV |
42 | int spl_cnt; |
43 | int trigger; | |
787e9bc9 | 44 | uint64_t samplerate; |
6f64ebb2 | 45 | int *channel_index; |
3b93d3c2 | 46 | GString **channel_names; |
47 | int max_namelen; | |
6f64ebb2 BV |
48 | char **line_values; |
49 | uint8_t *prev_sample; | |
787e9bc9 | 50 | gboolean header_done; |
6f64ebb2 BV |
51 | GString **lines; |
52 | GString *header; | |
0150fdca GS |
53 | const char *charset; |
54 | gboolean edges; | |
6f64ebb2 BV |
55 | }; |
56 | ||
a755b0e1 | 57 | static int init(struct sr_output *o, GHashTable *options) |
6f64ebb2 BV |
58 | { |
59 | struct context *ctx; | |
60 | struct sr_channel *ch; | |
61 | GSList *l; | |
3b93d3c2 | 62 | unsigned int i, j, max_namelen; |
6f64ebb2 BV |
63 | |
64 | if (!o || !o->sdi) | |
65 | return SR_ERR_ARG; | |
dba3e682 | 66 | |
6f64ebb2 | 67 | ctx = g_malloc0(sizeof(struct context)); |
d686c5ec | 68 | o->priv = ctx; |
6f64ebb2 | 69 | ctx->trigger = -1; |
dcc55fe9 | 70 | ctx->spl = g_variant_get_uint32(g_hash_table_lookup(options, "width")); |
0150fdca GS |
71 | ctx->charset = g_strdup(g_variant_get_string( |
72 | g_hash_table_lookup(options, "charset"), NULL)); | |
73 | if (!ctx->charset || strlen(ctx->charset) < 2) { | |
74 | g_free((gpointer)ctx->charset); | |
75 | ctx->charset = g_strdup(DEFAULT_ASCII_CHARS); | |
76 | } | |
77 | ctx->edges = (strlen(ctx->charset) >= 4) ? TRUE : FALSE; | |
6f64ebb2 BV |
78 | |
79 | for (l = o->sdi->channels; l; l = l->next) { | |
80 | ch = l->data; | |
81 | if (ch->type != SR_CHANNEL_LOGIC) | |
82 | continue; | |
83 | if (!ch->enabled) | |
84 | continue; | |
85 | ctx->num_enabled_channels++; | |
86 | } | |
87 | ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels); | |
88 | ctx->channel_names = g_malloc(sizeof(char *) * ctx->num_enabled_channels); | |
89 | ctx->lines = g_malloc(sizeof(GString *) * ctx->num_enabled_channels); | |
90 | ctx->prev_sample = g_malloc(g_slist_length(o->sdi->channels)); | |
91 | ||
3b93d3c2 | 92 | max_namelen = 0; |
93 | for (i = 0, l = o->sdi->channels; l; l = l->next, i++) { | |
94 | ch = l->data; | |
95 | if (ch->type != SR_CHANNEL_LOGIC) | |
96 | continue; | |
97 | if (!ch->enabled) | |
98 | continue; | |
99 | ||
100 | max_namelen = MAX(max_namelen, strlen(ch->name)); | |
101 | } | |
102 | ctx->max_namelen = max_namelen; | |
103 | ||
6f64ebb2 BV |
104 | j = 0; |
105 | for (i = 0, l = o->sdi->channels; l; l = l->next, i++) { | |
106 | ch = l->data; | |
107 | if (ch->type != SR_CHANNEL_LOGIC) | |
108 | continue; | |
109 | if (!ch->enabled) | |
110 | continue; | |
3b93d3c2 | 111 | |
6f64ebb2 | 112 | ctx->channel_index[j] = ch->index; |
3b93d3c2 | 113 | ctx->channel_names[j] = g_string_sized_new(16); |
114 | g_string_printf(ctx->channel_names[j], "%*s%s", (int)(max_namelen - strlen(ch->name)), "", ch->name); | |
115 | ||
6f64ebb2 | 116 | ctx->lines[j] = g_string_sized_new(80); |
3b93d3c2 | 117 | g_string_printf(ctx->lines[j], "%s:", ctx->channel_names[j]->str); |
118 | ||
6f64ebb2 BV |
119 | j++; |
120 | } | |
121 | ||
787e9bc9 BV |
122 | return SR_OK; |
123 | } | |
124 | ||
a755b0e1 | 125 | static GString *gen_header(const struct sr_output *o) |
787e9bc9 BV |
126 | { |
127 | struct context *ctx; | |
128 | GVariant *gvar; | |
129 | GString *header; | |
130 | int num_channels; | |
131 | char *samplerate_s; | |
132 | ||
d686c5ec | 133 | ctx = o->priv; |
787e9bc9 BV |
134 | if (ctx->samplerate == 0) { |
135 | if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE, | |
136 | &gvar) == SR_OK) { | |
137 | ctx->samplerate = g_variant_get_uint64(gvar); | |
138 | g_variant_unref(gvar); | |
139 | } | |
140 | } | |
141 | ||
142 | header = g_string_sized_new(512); | |
2868bca3 | 143 | g_string_printf(header, "%s %s\n", PACKAGE_NAME, sr_package_version_string_get()); |
6f64ebb2 | 144 | num_channels = g_slist_length(o->sdi->channels); |
787e9bc9 BV |
145 | g_string_append_printf(header, "Acquisition with %d/%d channels", |
146 | ctx->num_enabled_channels, num_channels); | |
147 | if (ctx->samplerate != 0) { | |
148 | samplerate_s = sr_samplerate_string(ctx->samplerate); | |
149 | g_string_append_printf(header, " at %s", samplerate_s); | |
6f64ebb2 | 150 | g_free(samplerate_s); |
6f64ebb2 | 151 | } |
787e9bc9 | 152 | g_string_append_printf(header, "\n"); |
6f64ebb2 | 153 | |
787e9bc9 | 154 | return header; |
6f64ebb2 BV |
155 | } |
156 | ||
cc835205 | 157 | static void maybe_add_trigger(struct context *ctx, GString *out) { |
158 | if (ctx->trigger <= -1) | |
159 | return; | |
160 | ||
161 | int offset = ctx->trigger; | |
162 | ||
163 | /* | |
164 | * Sample data lines have one character per bit and | |
165 | * no separator between bytes. Align trigger marker | |
166 | * to this layout. | |
167 | */ | |
168 | g_string_append_printf(out, "%*sT:%*s^ %d\n", ctx->max_namelen - 1, "", offset, "", offset); | |
169 | ||
170 | ctx->trigger = -1; | |
171 | } | |
172 | ||
173 | ||
a755b0e1 | 174 | static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet, |
dba3e682 | 175 | GString **out) |
6f64ebb2 | 176 | { |
787e9bc9 | 177 | const struct sr_datafeed_meta *meta; |
6f64ebb2 | 178 | const struct sr_datafeed_logic *logic; |
787e9bc9 BV |
179 | const struct sr_config *src; |
180 | GSList *l; | |
6f64ebb2 | 181 | struct context *ctx; |
cc835205 | 182 | int idx, curbit, prevbit; |
6f64ebb2 BV |
183 | uint64_t i, j; |
184 | gchar *p, c; | |
0150fdca | 185 | size_t charidx; |
6f64ebb2 | 186 | |
6f64ebb2 BV |
187 | *out = NULL; |
188 | if (!o || !o->sdi) | |
189 | return SR_ERR_ARG; | |
d686c5ec | 190 | if (!(ctx = o->priv)) |
6f64ebb2 BV |
191 | return SR_ERR_ARG; |
192 | ||
193 | switch (packet->type) { | |
787e9bc9 BV |
194 | case SR_DF_META: |
195 | meta = packet->payload; | |
196 | for (l = meta->config; l; l = l->next) { | |
197 | src = l->data; | |
198 | if (src->key != SR_CONF_SAMPLERATE) | |
199 | continue; | |
200 | ctx->samplerate = g_variant_get_uint64(src->data); | |
201 | } | |
202 | break; | |
6f64ebb2 BV |
203 | case SR_DF_TRIGGER: |
204 | ctx->trigger = ctx->spl_cnt; | |
205 | break; | |
206 | case SR_DF_LOGIC: | |
787e9bc9 BV |
207 | if (!ctx->header_done) { |
208 | *out = gen_header(o); | |
209 | ctx->header_done = TRUE; | |
6f64ebb2 BV |
210 | } else |
211 | *out = g_string_sized_new(512); | |
212 | ||
213 | logic = packet->payload; | |
214 | for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) { | |
215 | ctx->spl_cnt++; | |
216 | for (j = 0; j < ctx->num_enabled_channels; j++) { | |
217 | idx = ctx->channel_index[j]; | |
218 | p = logic->data + i + idx / 8; | |
219 | curbit = *p & (1 << (idx % 8)); | |
220 | prevbit = (ctx->prev_sample[idx / 8] & ((uint8_t) 1 << (idx % 8))); | |
221 | ||
0150fdca GS |
222 | charidx = curbit ? 1 : 0; |
223 | if (ctx->edges && ctx->spl_cnt > 1) { | |
224 | if (curbit != prevbit) | |
225 | charidx += 2; | |
6f64ebb2 | 226 | } |
0150fdca | 227 | c = ctx->charset[charidx]; |
6f64ebb2 BV |
228 | g_string_append_c(ctx->lines[j], c); |
229 | ||
dcc55fe9 | 230 | if (ctx->spl_cnt == ctx->spl) { |
6f64ebb2 BV |
231 | /* Flush line buffers. */ |
232 | g_string_append_len(*out, ctx->lines[j]->str, ctx->lines[j]->len); | |
233 | g_string_append_c(*out, '\n'); | |
cc835205 | 234 | if (j == ctx->num_enabled_channels - 1) |
235 | maybe_add_trigger(ctx, *out); | |
3b93d3c2 | 236 | g_string_printf(ctx->lines[j], "%s:", ctx->channel_names[j]->str); |
6f64ebb2 BV |
237 | } |
238 | } | |
dcc55fe9 | 239 | if (ctx->spl_cnt == ctx->spl) |
6f64ebb2 BV |
240 | /* Line buffers were already flushed. */ |
241 | ctx->spl_cnt = 0; | |
242 | memcpy(ctx->prev_sample, logic->data + i, logic->unitsize); | |
243 | } | |
244 | break; | |
245 | case SR_DF_END: | |
246 | if (ctx->spl_cnt) { | |
247 | /* Line buffers need flushing. */ | |
248 | *out = g_string_sized_new(512); | |
249 | for (i = 0; i < ctx->num_enabled_channels; i++) { | |
250 | g_string_append_len(*out, ctx->lines[i]->str, ctx->lines[i]->len); | |
251 | g_string_append_c(*out, '\n'); | |
252 | } | |
cc835205 | 253 | maybe_add_trigger(ctx, *out); |
6f64ebb2 BV |
254 | } |
255 | break; | |
256 | } | |
257 | ||
258 | return SR_OK; | |
259 | } | |
260 | ||
261 | static int cleanup(struct sr_output *o) | |
262 | { | |
263 | struct context *ctx; | |
264 | unsigned int i; | |
265 | ||
266 | if (!o) | |
267 | return SR_ERR_ARG; | |
268 | ||
d686c5ec | 269 | if (!(ctx = o->priv)) |
6f64ebb2 BV |
270 | return SR_OK; |
271 | ||
6f64ebb2 BV |
272 | g_free(ctx->channel_index); |
273 | g_free(ctx->prev_sample); | |
3b93d3c2 | 274 | for (i = 0; i < ctx->num_enabled_channels; i++) { |
275 | g_string_free(ctx->channel_names[i], TRUE); | |
6f64ebb2 | 276 | g_string_free(ctx->lines[i], TRUE); |
3b93d3c2 | 277 | } |
278 | g_free(ctx->channel_names); | |
6f64ebb2 | 279 | g_free(ctx->lines); |
0150fdca | 280 | g_free((gpointer)ctx->charset); |
6f64ebb2 | 281 | g_free(ctx); |
d686c5ec | 282 | o->priv = NULL; |
6f64ebb2 BV |
283 | |
284 | return SR_OK; | |
285 | } | |
286 | ||
a755b0e1 BV |
287 | static struct sr_option options[] = { |
288 | { "width", "Width", "Number of samples per line", NULL, NULL }, | |
0150fdca | 289 | { "charset", "Charset", "Characters for 0/1 bits (and fall/rise edges)", NULL, NULL }, |
1685c276 | 290 | ALL_ZERO |
a755b0e1 BV |
291 | }; |
292 | ||
af7d656d | 293 | static const struct sr_option *get_options(void) |
a755b0e1 | 294 | { |
950043c3 BV |
295 | if (!options[0].def) { |
296 | options[0].def = g_variant_new_uint32(DEFAULT_SAMPLES_PER_LINE); | |
297 | g_variant_ref_sink(options[0].def); | |
0150fdca GS |
298 | options[1].def = g_variant_new_string(DEFAULT_ASCII_CHARS); |
299 | g_variant_ref_sink(options[1].def); | |
950043c3 | 300 | } |
a755b0e1 BV |
301 | |
302 | return options; | |
303 | } | |
304 | ||
305 | SR_PRIV struct sr_output_module output_ascii = { | |
6f64ebb2 | 306 | .id = "ascii", |
a755b0e1 | 307 | .name = "ASCII", |
b20eb520 | 308 | .desc = "ASCII art logic data", |
8a174d23 | 309 | .exts = (const char*[]){"txt", NULL}, |
3cd4b381 | 310 | .flags = 0, |
a755b0e1 | 311 | .options = get_options, |
6f64ebb2 BV |
312 | .init = init, |
313 | .receive = receive, | |
314 | .cleanup = cleanup, | |
315 | }; |