2 * This file is part of the libsigrok project.
4 * Copyright (C) 2011 HÃ¥vard Espeland <gus@ping.uio.no>
5 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
25 #include <libsigrok/libsigrok.h>
26 #include "libsigrok-internal.h"
28 #define LOG_PREFIX "output/hex"
30 #define DEFAULT_SAMPLES_PER_LINE 74
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.
37 #define DEFAULT_ASCII_CHARS ".\"\\/"
40 unsigned int num_enabled_channels;
46 GString **channel_names;
57 static int init(struct sr_output *o, GHashTable *options)
60 struct sr_channel *ch;
62 unsigned int i, j, max_namelen;
67 ctx = g_malloc0(sizeof(struct context));
70 ctx->spl = g_variant_get_uint32(g_hash_table_lookup(options, "width"));
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);
77 ctx->edges = (strlen(ctx->charset) >= 4) ? TRUE : FALSE;
79 for (l = o->sdi->channels; l; l = l->next) {
81 if (ch->type != SR_CHANNEL_LOGIC)
85 ctx->num_enabled_channels++;
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));
93 for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
95 if (ch->type != SR_CHANNEL_LOGIC)
100 max_namelen = MAX(max_namelen, strlen(ch->name));
102 ctx->max_namelen = max_namelen;
105 for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
107 if (ch->type != SR_CHANNEL_LOGIC)
112 ctx->channel_index[j] = ch->index;
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);
116 ctx->lines[j] = g_string_sized_new(80);
117 g_string_printf(ctx->lines[j], "%s:", ctx->channel_names[j]->str);
125 static GString *gen_header(const struct sr_output *o)
134 if (ctx->samplerate == 0) {
135 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
137 ctx->samplerate = g_variant_get_uint64(gvar);
138 g_variant_unref(gvar);
142 header = g_string_sized_new(512);
143 g_string_printf(header, "%s %s\n", PACKAGE_NAME, sr_package_version_string_get());
144 num_channels = g_slist_length(o->sdi->channels);
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);
150 g_free(samplerate_s);
152 g_string_append_printf(header, "\n");
157 static void maybe_add_trigger(struct context *ctx, GString *out) {
158 if (ctx->trigger <= -1)
161 int offset = ctx->trigger;
164 * Sample data lines have one character per bit and
165 * no separator between bytes. Align trigger marker
168 g_string_append_printf(out, "%*sT:%*s^ %d\n", ctx->max_namelen - 1, "", offset, "", offset);
174 static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
177 const struct sr_datafeed_meta *meta;
178 const struct sr_datafeed_logic *logic;
179 const struct sr_config *src;
182 int idx, curbit, prevbit;
190 if (!(ctx = o->priv))
193 switch (packet->type) {
195 meta = packet->payload;
196 for (l = meta->config; l; l = l->next) {
198 if (src->key != SR_CONF_SAMPLERATE)
200 ctx->samplerate = g_variant_get_uint64(src->data);
204 ctx->trigger = ctx->spl_cnt;
207 if (!ctx->header_done) {
208 *out = gen_header(o);
209 ctx->header_done = TRUE;
211 *out = g_string_sized_new(512);
213 logic = packet->payload;
214 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
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)));
222 charidx = curbit ? 1 : 0;
223 if (ctx->edges && ctx->spl_cnt > 1) {
224 if (curbit != prevbit)
227 c = ctx->charset[charidx];
228 g_string_append_c(ctx->lines[j], c);
230 if (ctx->spl_cnt == ctx->spl) {
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');
234 if (j == ctx->num_enabled_channels - 1)
235 maybe_add_trigger(ctx, *out);
236 g_string_printf(ctx->lines[j], "%s:", ctx->channel_names[j]->str);
239 if (ctx->spl_cnt == ctx->spl)
240 /* Line buffers were already flushed. */
242 memcpy(ctx->prev_sample, logic->data + i, logic->unitsize);
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');
253 maybe_add_trigger(ctx, *out);
261 static int cleanup(struct sr_output *o)
269 if (!(ctx = o->priv))
272 g_free(ctx->channel_index);
273 g_free(ctx->prev_sample);
274 for (i = 0; i < ctx->num_enabled_channels; i++) {
275 g_string_free(ctx->channel_names[i], TRUE);
276 g_string_free(ctx->lines[i], TRUE);
278 g_free(ctx->channel_names);
280 g_free((gpointer)ctx->charset);
287 static struct sr_option options[] = {
288 { "width", "Width", "Number of samples per line", NULL, NULL },
289 { "charset", "Charset", "Characters for 0/1 bits (and fall/rise edges)", NULL, NULL },
293 static const struct sr_option *get_options(void)
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);
298 options[1].def = g_variant_new_string(DEFAULT_ASCII_CHARS);
299 g_variant_ref_sink(options[1].def);
305 SR_PRIV struct sr_output_module output_ascii = {
308 .desc = "ASCII art logic data",
309 .exts = (const char*[]){"txt", NULL},
311 .options = get_options,