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/>.
23 #include "libsigrok.h"
24 #include "libsigrok-internal.h"
26 #define LOG_PREFIX "output/bits"
28 #define DEFAULT_SAMPLES_PER_LINE 64
31 unsigned int num_enabled_channels;
41 static int init(struct sr_output *o)
44 struct sr_channel *ch;
49 int spl, num_channels;
54 ctx = g_malloc0(sizeof(struct context));
58 if (o->param && o->param[0]) {
59 if ((spl = strtoul(o->param, NULL, 10)) < 1)
62 spl = DEFAULT_SAMPLES_PER_LINE;
63 ctx->samples_per_line = spl;
65 for (l = o->sdi->channels; l; l = l->next) {
67 if (ch->type != SR_CHANNEL_LOGIC)
71 ctx->num_enabled_channels++;
73 ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
74 ctx->channel_names = g_malloc(sizeof(char *) * ctx->num_enabled_channels);
75 ctx->lines = g_malloc(sizeof(GString *) * ctx->num_enabled_channels);
78 for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
80 if (ch->type != SR_CHANNEL_LOGIC)
84 ctx->channel_index[j] = ch->index;
85 ctx->channel_names[j] = ch->name;
86 ctx->lines[j] = g_string_sized_new(80);
87 g_string_printf(ctx->lines[j], "%s:", ch->name);
91 ctx->header = g_string_sized_new(512);
92 g_string_printf(ctx->header, "%s\n", PACKAGE_STRING);
93 num_channels = g_slist_length(o->sdi->channels);
94 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
96 samplerate = g_variant_get_uint64(gvar);
97 samplerate_s = sr_samplerate_string(samplerate);
98 g_string_append_printf(ctx->header, "Acquisition with %d/%d channels at %s\n",
99 ctx->num_enabled_channels, num_channels, samplerate_s);
100 g_free(samplerate_s);
101 g_variant_unref(gvar);
107 static int receive(struct sr_output *o, const struct sr_dev_inst *sdi,
108 const struct sr_datafeed_packet *packet, GString **out)
110 const struct sr_datafeed_logic *logic;
121 if (!(ctx = o->internal))
124 switch (packet->type) {
126 ctx->trigger = ctx->spl_cnt;
130 /* The header is still here, this must be the first packet. */
134 *out = g_string_sized_new(512);
136 logic = packet->payload;
137 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
139 for (j = 0; j < ctx->num_enabled_channels; j++) {
140 idx = ctx->channel_index[j];
141 p = logic->data + i + idx / 8;
142 c = (*p & (1 << (idx % 8))) ? '1' : '0';
143 g_string_append_c(ctx->lines[j], c);
145 if (ctx->spl_cnt == ctx->samples_per_line) {
146 /* Flush line buffers. */
147 g_string_append_len(*out, ctx->lines[j]->str, ctx->lines[j]->len);
148 g_string_append_c(*out, '\n');
149 if (j == ctx->num_enabled_channels - 1 && ctx->trigger > -1) {
150 offset = ctx->trigger + ctx->trigger / 8;
151 g_string_append_printf(*out, "T:%*s^ %d\n", offset, "", ctx->trigger);
154 g_string_printf(ctx->lines[j], "%s:", ctx->channel_names[j]);
155 } else if ((ctx->spl_cnt & 7) == 0) {
156 /* Add a space every 8th bit. */
157 g_string_append_c(ctx->lines[j], ' ');
160 if (ctx->spl_cnt == ctx->samples_per_line)
161 /* Line buffers were already flushed. */
167 /* Line buffers need flushing. */
168 *out = g_string_sized_new(512);
169 for (i = 0; i < ctx->num_enabled_channels; i++) {
170 g_string_append_len(*out, ctx->lines[i]->str, ctx->lines[i]->len);
171 g_string_append_c(*out, '\n');
180 static int cleanup(struct sr_output *o)
188 if (!(ctx = o->internal))
192 g_free(ctx->channel_index);
193 g_free(ctx->channel_names);
194 for (i = 0; i < ctx->num_enabled_channels; i++)
195 g_string_free(ctx->lines[i], TRUE);
198 g_string_free(ctx->header, TRUE);
205 SR_PRIV struct sr_output_format output_bits = {
207 .description = "Bits",