2 * This file is part of the libsigrok project.
4 * Copyright (C) 2019 Marc Jacobi <obiwanjacobi@hotmail.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/>.
22 #include <libsigrok/libsigrok.h>
23 #include "libsigrok-internal.h"
25 #define LOG_PREFIX "output/wavedrom"
28 uint32_t channel_count;
29 struct sr_channel **channels;
30 GString **channel_outputs; /* output strings */
33 /* Converts accumulated output data to a JSON string. */
34 static GString *wavedrom_render(const struct context *ctx)
38 char last_char, curr_char;
40 output = g_string_new("{ \"signal\": [");
41 for (ch = 0; ch < ctx->channel_count; ch++) {
42 if (!ctx->channel_outputs[ch])
46 g_string_append_printf(output,
47 "{ \"name\": \"%s\", \"wave\": \"", ctx->channels[ch]->name);
50 for (i = 0; i < ctx->channel_outputs[ch]->len; i++) {
51 curr_char = ctx->channel_outputs[ch]->str[i];
53 if (curr_char == last_char) {
54 g_string_append_c(output, '.');
56 g_string_append_c(output, curr_char);
57 last_char = curr_char;
60 if (ch < ctx->channel_count - 1) {
61 g_string_append(output, "\" },");
63 /* Last channel, no comma. */
64 g_string_append(output, "\" }");
67 g_string_append(output, "], \"config\": { \"skin\": \"narrow\" }}");
72 static void process_logic(const struct context *ctx,
73 const struct sr_datafeed_logic *logic)
75 size_t sample_count, ch, i;
78 sample_count = logic->length / logic->unitsize;
81 * Extract the logic bits for each channel and store them
82 * as wavedrom letters (1/0) in each channel's text string.
84 for (ch = 0; ch < ctx->channel_count; ch++) {
85 if (ctx->channels[ch]) {
86 for (i = 0; i < sample_count; i++) {
87 sample = logic->data + i * logic->unitsize;
89 if (ctx->channel_outputs[ch]) {
90 g_string_append_c(ctx->channel_outputs[ch],
91 sample[ch / 8] & (1 << (ch % 8)) ? '1' : '0');
98 static int receive(const struct sr_output *o,
99 const struct sr_datafeed_packet *packet, GString **out)
105 if (!o || !o->sdi || !o->priv)
110 switch (packet->type) {
112 process_logic(ctx, packet->payload);
115 *out = wavedrom_render(ctx);
122 static int init(struct sr_output *o, GHashTable *options)
125 struct sr_channel *channel;
134 o->priv = ctx = g_malloc0(sizeof(*ctx));
136 ctx->channel_count = g_slist_length(o->sdi->channels);
137 ctx->channels = g_malloc0(
138 sizeof(ctx->channels[0]) * ctx->channel_count);
139 ctx->channel_outputs = g_malloc0(
140 sizeof(ctx->channel_outputs[0]) * ctx->channel_count);
142 for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
144 if (channel->enabled && channel->type == SR_CHANNEL_LOGIC) {
145 ctx->channels[i] = channel;
146 ctx->channel_outputs[i] = g_string_new(NULL);
153 static int cleanup(struct sr_output *o)
165 while (--ctx->channel_count) {
166 s = ctx->channel_outputs[ctx->channel_count];
168 g_string_free(s, TRUE);
170 g_free(ctx->channel_outputs);
171 g_free(ctx->channels);
178 SR_PRIV struct sr_output_module output_wavedrom = {
181 .desc = "WaveDrom.com file format",
182 .exts = (const char *[]){"wavedrom", "json", NULL},