ascii.c 7.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * This file is part of the libsigrok project.
 *
 * Copyright (C) 2011 Håvard Espeland <gus@ping.uio.no>
 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

21
#include <config.h>
22 23 24
#include <stdlib.h>
#include <string.h>
#include <glib.h>
25
#include <libsigrok/libsigrok.h>
26 27 28 29 30 31
#include "libsigrok-internal.h"

#define LOG_PREFIX "output/hex"

#define DEFAULT_SAMPLES_PER_LINE 74

32 33 34 35 36 37 38
/*
 * The string looks ugly with escape characters, here is the readable
 * version: Use . and " for low and high bits, use \ and / to draw
 * falling and rising edges respectively.
 */
#define DEFAULT_ASCII_CHARS ".\"\\/"

39 40
struct context {
	unsigned int num_enabled_channels;
41
	int spl;
42 43 44
	int bit_cnt;
	int spl_cnt;
	int trigger;
45
	uint64_t samplerate;
46 47 48 49
	int *channel_index;
	char **channel_names;
	char **line_values;
	uint8_t *prev_sample;
50
	gboolean header_done;
51 52
	GString **lines;
	GString *header;
53 54
	const char *charset;
	gboolean edges;
55 56
};

57
static int init(struct sr_output *o, GHashTable *options)
58 59 60 61 62 63 64 65
{
	struct context *ctx;
	struct sr_channel *ch;
	GSList *l;
	unsigned int i, j;

	if (!o || !o->sdi)
		return SR_ERR_ARG;
66

67
	ctx = g_malloc0(sizeof(struct context));
68
	o->priv = ctx;
69
	ctx->trigger = -1;
70
	ctx->spl = g_variant_get_uint32(g_hash_table_lookup(options, "width"));
71 72 73 74 75 76 77
	ctx->charset = g_strdup(g_variant_get_string(
		g_hash_table_lookup(options, "charset"), NULL));
	if (!ctx->charset || strlen(ctx->charset) < 2) {
		g_free((gpointer)ctx->charset);
		ctx->charset = g_strdup(DEFAULT_ASCII_CHARS);
	}
	ctx->edges = (strlen(ctx->charset) >= 4) ? TRUE : FALSE;
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

	for (l = o->sdi->channels; l; l = l->next) {
		ch = l->data;
		if (ch->type != SR_CHANNEL_LOGIC)
			continue;
		if (!ch->enabled)
			continue;
		ctx->num_enabled_channels++;
	}
	ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
	ctx->channel_names = g_malloc(sizeof(char *) * ctx->num_enabled_channels);
	ctx->lines = g_malloc(sizeof(GString *) * ctx->num_enabled_channels);
	ctx->prev_sample = g_malloc(g_slist_length(o->sdi->channels));

	j = 0;
	for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
		ch = l->data;
		if (ch->type != SR_CHANNEL_LOGIC)
			continue;
		if (!ch->enabled)
			continue;
		ctx->channel_index[j] = ch->index;
		ctx->channel_names[j] = ch->name;
		ctx->lines[j] = g_string_sized_new(80);
		g_string_printf(ctx->lines[j], "%s:", ch->name);
		j++;
	}

106 107 108
	return SR_OK;
}

109
static GString *gen_header(const struct sr_output *o)
110 111 112 113 114 115 116
{
	struct context *ctx;
	GVariant *gvar;
	GString *header;
	int num_channels;
	char *samplerate_s;

117
	ctx = o->priv;
118 119 120 121 122 123 124 125 126
	if (ctx->samplerate == 0) {
		if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
				&gvar) == SR_OK) {
			ctx->samplerate = g_variant_get_uint64(gvar);
			g_variant_unref(gvar);
		}
	}

	header = g_string_sized_new(512);
127
	g_string_printf(header, "%s %s\n", PACKAGE_NAME, SR_PACKAGE_VERSION_STRING);
128
	num_channels = g_slist_length(o->sdi->channels);
129 130 131 132 133
	g_string_append_printf(header, "Acquisition with %d/%d channels",
			ctx->num_enabled_channels, num_channels);
	if (ctx->samplerate != 0) {
		samplerate_s = sr_samplerate_string(ctx->samplerate);
		g_string_append_printf(header, " at %s", samplerate_s);
134 135
		g_free(samplerate_s);
	}
136
	g_string_append_printf(header, "\n");
137

138
	return header;
139 140
}

141
static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
142
		GString **out)
143
{
144
	const struct sr_datafeed_meta *meta;
145
	const struct sr_datafeed_logic *logic;
146 147
	const struct sr_config *src;
	GSList *l;
148 149 150 151
	struct context *ctx;
	int idx, offset, curbit, prevbit;
	uint64_t i, j;
	gchar *p, c;
152
	size_t charidx;
153 154 155 156

	*out = NULL;
	if (!o || !o->sdi)
		return SR_ERR_ARG;
157
	if (!(ctx = o->priv))
158 159 160
		return SR_ERR_ARG;

	switch (packet->type) {
161 162 163 164 165 166 167 168 169
	case SR_DF_META:
		meta = packet->payload;
		for (l = meta->config; l; l = l->next) {
			src = l->data;
			if (src->key != SR_CONF_SAMPLERATE)
				continue;
			ctx->samplerate = g_variant_get_uint64(src->data);
		}
		break;
170 171 172 173
	case SR_DF_TRIGGER:
		ctx->trigger = ctx->spl_cnt;
		break;
	case SR_DF_LOGIC:
174 175 176
		if (!ctx->header_done) {
			*out = gen_header(o);
			ctx->header_done = TRUE;
177 178 179 180 181 182 183 184 185 186 187 188
		} else
			*out = g_string_sized_new(512);

		logic = packet->payload;
		for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
			ctx->spl_cnt++;
			for (j = 0; j < ctx->num_enabled_channels; j++) {
				idx = ctx->channel_index[j];
				p = logic->data + i + idx / 8;
				curbit = *p & (1 << (idx % 8));
				prevbit = (ctx->prev_sample[idx / 8] & ((uint8_t) 1 << (idx % 8)));

189 190 191 192
				charidx = curbit ? 1 : 0;
				if (ctx->edges && ctx->spl_cnt > 1) {
					if (curbit != prevbit)
						charidx += 2;
193
				}
194
				c = ctx->charset[charidx];
195 196
				g_string_append_c(ctx->lines[j], c);

197
				if (ctx->spl_cnt == ctx->spl) {
198 199 200
					/* Flush line buffers. */
					g_string_append_len(*out, ctx->lines[j]->str, ctx->lines[j]->len);
					g_string_append_c(*out, '\n');
201
					if (j == ctx->num_enabled_channels - 1 && ctx->trigger > -1) {
202 203 204 205 206 207 208
						offset = ctx->trigger + ctx->trigger / 8;
						g_string_append_printf(*out, "T:%*s^ %d\n", offset, "", ctx->trigger);
						ctx->trigger = -1;
					}
					g_string_printf(ctx->lines[j], "%s:", ctx->channel_names[j]);
				}
			}
209
			if (ctx->spl_cnt == ctx->spl)
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
				/* Line buffers were already flushed. */
				ctx->spl_cnt = 0;
			memcpy(ctx->prev_sample, logic->data + i, logic->unitsize);
		}
		break;
	case SR_DF_END:
		if (ctx->spl_cnt) {
			/* Line buffers need flushing. */
			*out = g_string_sized_new(512);
			for (i = 0; i < ctx->num_enabled_channels; i++) {
				g_string_append_len(*out, ctx->lines[i]->str, ctx->lines[i]->len);
				g_string_append_c(*out, '\n');
			}
		}
		break;
	}

	return SR_OK;
}

static int cleanup(struct sr_output *o)
{
	struct context *ctx;
	unsigned int i;

	if (!o)
		return SR_ERR_ARG;

238
	if (!(ctx = o->priv))
239 240 241 242 243 244 245 246
		return SR_OK;

	g_free(ctx->channel_index);
	g_free(ctx->prev_sample);
	g_free(ctx->channel_names);
	for (i = 0; i < ctx->num_enabled_channels; i++)
		g_string_free(ctx->lines[i], TRUE);
	g_free(ctx->lines);
247
	g_free((gpointer)ctx->charset);
248
	g_free(ctx);
249
	o->priv = NULL;
250 251 252 253

	return SR_OK;
}

254 255
static struct sr_option options[] = {
	{ "width", "Width", "Number of samples per line", NULL, NULL },
256
	{ "charset", "Charset", "Characters for 0/1 bits (and fall/rise edges)", NULL, NULL },
257
	ALL_ZERO
258 259
};

260
static const struct sr_option *get_options(void)
261
{
262 263 264
	if (!options[0].def) {
		options[0].def = g_variant_new_uint32(DEFAULT_SAMPLES_PER_LINE);
		g_variant_ref_sink(options[0].def);
265 266
		options[1].def = g_variant_new_string(DEFAULT_ASCII_CHARS);
		g_variant_ref_sink(options[1].def);
267
	}
268 269 270 271 272

	return options;
}

SR_PRIV struct sr_output_module output_ascii = {
273
	.id = "ascii",
274 275
	.name = "ASCII",
	.desc = "ASCII art",
276
	.exts = (const char*[]){"txt", NULL},
Soeren Apel's avatar
Soeren Apel committed
277
	.flags = 0,
278
	.options = get_options,
279 280 281 282
	.init = init,
	.receive = receive,
	.cleanup = cleanup,
};