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;
47 static int init(struct sr_output *o)
50 struct sr_channel *ch;
60 spl = DEFAULT_SAMPLES_PER_LINE;
61 g_hash_table_iter_init(&iter, o->params);
62 while (g_hash_table_iter_next(&iter, &key, &value)) {
63 if (!strcmp(key, "width")) {
64 if ((spl = strtoul(value, NULL, 10)) < 1) {
65 sr_err("Invalid width.");
69 sr_err("Unknown parameter '%s'.", key);
74 ctx = g_malloc0(sizeof(struct context));
77 ctx->samples_per_line = spl;
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)
99 ctx->channel_index[j] = ch->index;
100 ctx->channel_names[j] = ch->name;
101 ctx->lines[j] = g_string_sized_new(80);
102 g_string_printf(ctx->lines[j], "%s:", ch->name);
109 static GString *gen_header(struct sr_output *o)
118 if (ctx->samplerate == 0) {
119 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
121 ctx->samplerate = g_variant_get_uint64(gvar);
122 g_variant_unref(gvar);
126 header = g_string_sized_new(512);
127 g_string_printf(header, "%s\n", PACKAGE_STRING);
128 num_channels = g_slist_length(o->sdi->channels);
129 g_string_append_printf(header, "Acquisition with %d/%d channels",
130 ctx->num_enabled_channels, num_channels);
131 if (ctx->samplerate != 0) {
132 samplerate_s = sr_samplerate_string(ctx->samplerate);
133 g_string_append_printf(header, " at %s", samplerate_s);
134 g_free(samplerate_s);
136 g_string_append_printf(header, "\n");
141 static int receive(struct sr_output *o, const struct sr_datafeed_packet *packet,
144 const struct sr_datafeed_meta *meta;
145 const struct sr_datafeed_logic *logic;
146 const struct sr_config *src;
149 int idx, offset, curbit, prevbit;
156 if (!(ctx = o->internal))
159 switch (packet->type) {
161 meta = packet->payload;
162 for (l = meta->config; l; l = l->next) {
164 if (src->key != SR_CONF_SAMPLERATE)
166 ctx->samplerate = g_variant_get_uint64(src->data);
170 ctx->trigger = ctx->spl_cnt;
173 if (!ctx->header_done) {
174 *out = gen_header(o);
175 ctx->header_done = TRUE;
177 *out = g_string_sized_new(512);
179 logic = packet->payload;
180 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
182 for (j = 0; j < ctx->num_enabled_channels; j++) {
183 idx = ctx->channel_index[j];
184 p = logic->data + i + idx / 8;
185 curbit = *p & (1 << (idx % 8));
186 prevbit = (ctx->prev_sample[idx / 8] & ((uint8_t) 1 << (idx % 8)));
188 c = curbit ? '"' : '.';
189 if (ctx->spl_cnt > 1) {
190 if (curbit < prevbit)
192 else if (curbit > prevbit)
195 g_string_append_c(ctx->lines[j], c);
197 if (ctx->spl_cnt == ctx->samples_per_line) {
198 /* Flush line buffers. */
199 g_string_append_len(*out, ctx->lines[j]->str, ctx->lines[j]->len);
200 g_string_append_c(*out, '\n');
201 if (j == ctx->num_enabled_channels - 1 && ctx->trigger > -1) {
202 offset = ctx->trigger + ctx->trigger / 8;
203 g_string_append_printf(*out, "T:%*s^ %d\n", offset, "", ctx->trigger);
206 g_string_printf(ctx->lines[j], "%s:", ctx->channel_names[j]);
209 if (ctx->spl_cnt == ctx->samples_per_line)
210 /* Line buffers were already flushed. */
212 memcpy(ctx->prev_sample, logic->data + i, logic->unitsize);
217 /* Line buffers need flushing. */
218 *out = g_string_sized_new(512);
219 for (i = 0; i < ctx->num_enabled_channels; i++) {
220 g_string_append_len(*out, ctx->lines[i]->str, ctx->lines[i]->len);
221 g_string_append_c(*out, '\n');
230 static int cleanup(struct sr_output *o)
238 if (!(ctx = o->internal))
241 g_free(ctx->channel_index);
242 g_free(ctx->prev_sample);
243 g_free(ctx->channel_names);
244 for (i = 0; i < ctx->num_enabled_channels; i++)
245 g_string_free(ctx->lines[i], TRUE);
253 SR_PRIV struct sr_output_format output_ascii = {
255 .description = "ASCII",