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/>.
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
27 #define LOG_PREFIX "output/hex"
29 #define DEFAULT_SAMPLES_PER_LINE 74
32 unsigned int num_enabled_channels;
45 static int init(struct sr_output *o)
48 struct sr_channel *ch;
53 int spl, num_channels;
58 ctx = g_malloc0(sizeof(struct context));
62 if (o->param && o->param[0]) {
63 if ((spl = strtoul(o->param, NULL, 10)) < 1)
66 spl = DEFAULT_SAMPLES_PER_LINE;
67 ctx->samples_per_line = spl;
69 for (l = o->sdi->channels; l; l = l->next) {
71 if (ch->type != SR_CHANNEL_LOGIC)
75 ctx->num_enabled_channels++;
77 ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
78 ctx->channel_names = g_malloc(sizeof(char *) * ctx->num_enabled_channels);
79 ctx->lines = g_malloc(sizeof(GString *) * ctx->num_enabled_channels);
80 ctx->prev_sample = g_malloc(g_slist_length(o->sdi->channels));
83 for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
85 if (ch->type != SR_CHANNEL_LOGIC)
89 ctx->channel_index[j] = ch->index;
90 ctx->channel_names[j] = ch->name;
91 ctx->lines[j] = g_string_sized_new(80);
92 g_string_printf(ctx->lines[j], "%s:", ch->name);
96 ctx->header = g_string_sized_new(512);
97 g_string_printf(ctx->header, "%s\n", PACKAGE_STRING);
98 num_channels = g_slist_length(o->sdi->channels);
99 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
101 samplerate = g_variant_get_uint64(gvar);
102 samplerate_s = sr_samplerate_string(samplerate);
103 g_string_append_printf(ctx->header, "Acquisition with %d/%d channels at %s\n",
104 ctx->num_enabled_channels, num_channels, samplerate_s);
105 g_free(samplerate_s);
106 g_variant_unref(gvar);
112 static int receive(struct sr_output *o, const struct sr_dev_inst *sdi,
113 const struct sr_datafeed_packet *packet, GString **out)
115 const struct sr_datafeed_logic *logic;
117 int idx, offset, curbit, prevbit;
126 if (!(ctx = o->internal))
129 switch (packet->type) {
131 ctx->trigger = ctx->spl_cnt;
135 /* The header is still here, this must be the first packet. */
139 *out = g_string_sized_new(512);
141 logic = packet->payload;
142 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
144 for (j = 0; j < ctx->num_enabled_channels; j++) {
145 idx = ctx->channel_index[j];
146 p = logic->data + i + idx / 8;
147 curbit = *p & (1 << (idx % 8));
148 prevbit = (ctx->prev_sample[idx / 8] & ((uint8_t) 1 << (idx % 8)));
150 c = curbit ? '"' : '.';
151 if (ctx->spl_cnt > 1) {
152 if (curbit < prevbit)
154 else if (curbit > prevbit)
157 g_string_append_c(ctx->lines[j], c);
159 if (ctx->spl_cnt == ctx->samples_per_line) {
160 /* Flush line buffers. */
161 g_string_append_len(*out, ctx->lines[j]->str, ctx->lines[j]->len);
162 g_string_append_c(*out, '\n');
163 if (j == ctx->num_enabled_channels - 1 && ctx->trigger > -1) {
164 offset = ctx->trigger + ctx->trigger / 8;
165 g_string_append_printf(*out, "T:%*s^ %d\n", offset, "", ctx->trigger);
168 g_string_printf(ctx->lines[j], "%s:", ctx->channel_names[j]);
171 if (ctx->spl_cnt == ctx->samples_per_line)
172 /* Line buffers were already flushed. */
174 memcpy(ctx->prev_sample, logic->data + i, logic->unitsize);
179 /* Line buffers need flushing. */
180 *out = g_string_sized_new(512);
181 for (i = 0; i < ctx->num_enabled_channels; i++) {
182 g_string_append_len(*out, ctx->lines[i]->str, ctx->lines[i]->len);
183 g_string_append_c(*out, '\n');
192 static int cleanup(struct sr_output *o)
200 if (!(ctx = o->internal))
204 g_free(ctx->channel_index);
205 g_free(ctx->prev_sample);
206 g_free(ctx->channel_names);
207 for (i = 0; i < ctx->num_enabled_channels; i++)
208 g_string_free(ctx->lines[i], TRUE);
211 g_string_free(ctx->header, TRUE);
218 SR_PRIV struct sr_output_format output_ascii = {
220 .description = "ASCII",