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