2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <libsigrok/libsigrok.h>
25 #include "libsigrok-internal.h"
27 #define LOG_PREFIX "output/bits"
29 #define DEFAULT_SAMPLES_PER_LINE 64
32 unsigned int num_enabled_channels;
43 static int init(struct sr_output *o, GHashTable *options)
46 struct sr_channel *ch;
53 ctx = g_malloc0(sizeof(struct context));
56 ctx->spl = g_variant_get_uint32(g_hash_table_lookup(options, "width"));
58 for (l = o->sdi->channels; l; l = l->next) {
60 if (ch->type != SR_CHANNEL_LOGIC)
64 ctx->num_enabled_channels++;
66 ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
67 ctx->channel_names = g_malloc(sizeof(char *) * ctx->num_enabled_channels);
68 ctx->lines = g_malloc(sizeof(GString *) * ctx->num_enabled_channels);
71 for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
73 if (ch->type != SR_CHANNEL_LOGIC)
77 ctx->channel_index[j] = ch->index;
78 ctx->channel_names[j] = ch->name;
79 ctx->lines[j] = g_string_sized_new(80);
80 g_string_printf(ctx->lines[j], "%s:", ch->name);
87 static GString *gen_header(const struct sr_output *o)
96 if (ctx->samplerate == 0) {
97 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
99 ctx->samplerate = g_variant_get_uint64(gvar);
100 g_variant_unref(gvar);
104 header = g_string_sized_new(512);
105 g_string_printf(header, "%s %s\n", PACKAGE_NAME, SR_PACKAGE_VERSION_STRING);
106 num_channels = g_slist_length(o->sdi->channels);
107 g_string_append_printf(header, "Acquisition with %d/%d channels",
108 ctx->num_enabled_channels, num_channels);
109 if (ctx->samplerate != 0) {
110 samplerate_s = sr_samplerate_string(ctx->samplerate);
111 g_string_append_printf(header, " at %s", samplerate_s);
112 g_free(samplerate_s);
114 g_string_append_printf(header, "\n");
119 static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
122 const struct sr_datafeed_meta *meta;
123 const struct sr_datafeed_logic *logic;
124 const struct sr_config *src;
134 if (!(ctx = o->priv))
137 switch (packet->type) {
139 meta = packet->payload;
140 for (l = meta->config; l; l = l->next) {
142 if (src->key != SR_CONF_SAMPLERATE)
144 ctx->samplerate = g_variant_get_uint64(src->data);
148 ctx->trigger = ctx->spl_cnt;
151 if (!ctx->header_done) {
152 *out = gen_header(o);
153 ctx->header_done = TRUE;
155 *out = g_string_sized_new(512);
157 logic = packet->payload;
158 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
160 for (j = 0; j < ctx->num_enabled_channels; j++) {
161 idx = ctx->channel_index[j];
162 p = logic->data + i + idx / 8;
163 c = (*p & (1 << (idx % 8))) ? '1' : '0';
164 g_string_append_c(ctx->lines[j], c);
166 if (ctx->spl_cnt == ctx->spl) {
167 /* Flush line buffers. */
168 g_string_append_len(*out, ctx->lines[j]->str, ctx->lines[j]->len);
169 g_string_append_c(*out, '\n');
170 if (j == ctx->num_enabled_channels - 1 && ctx->trigger > -1) {
172 * Sample data lines have one character per bit,
173 * plus one separator per byte. Align trigger marker
176 offset = ctx->trigger + ctx->trigger / 8;
177 g_string_append_printf(*out, "T:%*s^ %d\n", offset, "", ctx->trigger);
180 g_string_printf(ctx->lines[j], "%s:", ctx->channel_names[j]);
181 } else if ((ctx->spl_cnt & 7) == 0) {
182 /* Add a space every 8th bit. */
183 g_string_append_c(ctx->lines[j], ' ');
186 if (ctx->spl_cnt == ctx->spl)
187 /* Line buffers were already flushed. */
193 /* Line buffers need flushing. */
194 *out = g_string_sized_new(512);
195 for (i = 0; i < ctx->num_enabled_channels; i++) {
196 g_string_append_len(*out, ctx->lines[i]->str, ctx->lines[i]->len);
197 g_string_append_c(*out, '\n');
206 static int cleanup(struct sr_output *o)
214 if (!(ctx = o->priv))
217 g_free(ctx->channel_index);
218 g_free(ctx->channel_names);
219 for (i = 0; i < ctx->num_enabled_channels; i++)
220 g_string_free(ctx->lines[i], TRUE);
228 static struct sr_option options[] = {
229 { "width", "Width", "Number of samples per line", NULL, NULL },
233 static const struct sr_option *get_options(void)
235 if (!options[0].def) {
236 options[0].def = g_variant_new_uint32(DEFAULT_SAMPLES_PER_LINE);
237 g_variant_ref_sink(options[0].def);
243 SR_PRIV struct sr_output_module output_bits = {
246 .desc = "0/1 digits logic data",
247 .exts = (const char*[]){"txt", NULL},
249 .options = get_options,