]> sigrok.org Git - libsigrok.git/blame - src/output/wavedrom.c
output/wavedrom: address style nits
[libsigrok.git] / src / output / wavedrom.c
CommitLineData
40f812f5
MJ
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2019 Marc Jacobi <obiwanjacobi@hotmail.com>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include <config.h>
21#include <string.h>
22#include <libsigrok/libsigrok.h>
23#include "libsigrok-internal.h"
24
25#define LOG_PREFIX "output/wavedrom"
26
dd5735c9 27struct context {
40f812f5 28 uint32_t channel_count;
40f812f5 29 struct sr_channel **channels;
dd5735c9 30 GString **channel_outputs; /* output strings */
40f812f5
MJ
31};
32
dd5735c9 33/* Converts accumulated output data to a JSON string. */
40f812f5
MJ
34static GString *wavedrom_render(const struct context *ctx)
35{
dd5735c9
GS
36 GString *output;
37 size_t ch, i;
38 char last_char, curr_char;
40f812f5 39
dd5735c9 40 output = g_string_new("{ \"signal\": [");
40f812f5
MJ
41 for (ch = 0; ch < ctx->channel_count; ch++) {
42 if (!ctx->channel_outputs[ch])
43 continue;
44
dd5735c9 45 /* Channel strip. */
40f812f5
MJ
46 g_string_append_printf(output,
47 "{ \"name\": \"%s\", \"wave\": \"", ctx->channels[ch]->name);
48
dd5735c9 49 last_char = 0;
40f812f5 50 for (i = 0; i < ctx->channel_outputs[ch]->len; i++) {
dd5735c9
GS
51 curr_char = ctx->channel_outputs[ch]->str[i];
52 /* Data point. */
53 if (curr_char == last_char) {
40f812f5
MJ
54 g_string_append_c(output, '.');
55 } else {
dd5735c9
GS
56 g_string_append_c(output, curr_char);
57 last_char = curr_char;
40f812f5
MJ
58 }
59 }
60 if (ch < ctx->channel_count - 1) {
61 g_string_append(output, "\" },");
62 } else {
dd5735c9 63 /* Last channel, no comma. */
40f812f5
MJ
64 g_string_append(output, "\" }");
65 }
66 }
40f812f5 67 g_string_append(output, "], \"config\": { \"skin\": \"narrow\" }}");
dd5735c9 68
40f812f5
MJ
69 return output;
70}
71
72static int init(struct sr_output *o, GHashTable *options)
73{
74 struct context *ctx;
75 struct sr_channel *channel;
76 GSList *l;
dd5735c9 77 size_t i;
40f812f5
MJ
78
79 (void)options;
80
81 if (!o || !o->sdi)
82 return SR_ERR_ARG;
83
dd5735c9 84 o->priv = ctx = g_malloc0(sizeof(*ctx));
40f812f5
MJ
85
86 ctx->channel_count = g_slist_length(o->sdi->channels);
dd5735c9
GS
87 ctx->channels = g_malloc0(
88 sizeof(ctx->channels[0]) * ctx->channel_count);
89 ctx->channel_outputs = g_malloc0(
90 sizeof(ctx->channel_outputs[0]) * ctx->channel_count);
40f812f5
MJ
91
92 for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
93 channel = l->data;
dd5735c9 94 if (channel->enabled && channel->type == SR_CHANNEL_LOGIC) {
40f812f5
MJ
95 ctx->channels[i] = channel;
96 ctx->channel_outputs[i] = g_string_new(NULL);
40f812f5
MJ
97 }
98 }
99
100 return SR_OK;
101}
102
103static int cleanup(struct sr_output *o)
104{
105 struct context *ctx;
dd5735c9 106 GString *s;
40f812f5
MJ
107
108 if (!o)
109 return SR_ERR_ARG;
110
111 ctx = o->priv;
112 o->priv = NULL;
113
114 if (ctx) {
dd5735c9
GS
115 while (--ctx->channel_count) {
116 s = ctx->channel_outputs[ctx->channel_count];
117 if (s)
118 g_string_free(s, TRUE);
40f812f5
MJ
119 }
120 g_free(ctx->channel_outputs);
121 g_free(ctx->channels);
122 g_free(ctx);
123 }
124
125 return SR_OK;
126}
127
128static void process_logic(const struct context *ctx,
dd5735c9 129 const struct sr_datafeed_logic *logic)
40f812f5 130{
dd5735c9 131 size_t sample_count, ch, i;
40f812f5
MJ
132 uint8_t *sample;
133
134 sample_count = logic->length / logic->unitsize;
135
dd5735c9
GS
136 /*
137 * Extract the logic bits for each channel and store them
138 * as wavedrom letters (1/0) in each channel's text string.
139 */
40f812f5
MJ
140 for (ch = 0; ch < ctx->channel_count; ch++) {
141 if (ctx->channels[ch]) {
142 for (i = 0; i < sample_count; i++) {
143 sample = logic->data + i * logic->unitsize;
144
145 if (ctx->channel_outputs[ch]) {
146 g_string_append_c(ctx->channel_outputs[ch],
dd5735c9 147 sample[ch / 8] & (1 << (ch % 8)) ? '1' : '0');
40f812f5
MJ
148 }
149 }
150 }
151 }
152}
153
154static int receive(const struct sr_output *o,
dd5735c9 155 const struct sr_datafeed_packet *packet, GString **out)
40f812f5
MJ
156{
157 struct context *ctx;
158
159 *out = NULL;
160
161 if (!o || !o->sdi || !o->priv)
162 return SR_ERR_ARG;
163
164 ctx = o->priv;
165
166 switch (packet->type) {
167 case SR_DF_LOGIC:
168 process_logic(ctx, packet->payload);
169 break;
170 case SR_DF_END:
171 *out = wavedrom_render(ctx);
172 break;
173 }
174
175 return SR_OK;
176}
177
178SR_PRIV struct sr_output_module output_wavedrom = {
179 .id = "wavedrom",
dd5735c9 180 .name = "WaveDrom",
40f812f5
MJ
181 .desc = "WaveDrom.com file format",
182 .exts = (const char *[]){"wavedrom", "json", NULL},
183 .flags = 0,
184 .options = NULL,
185 .init = init,
186 .receive = receive,
187 .cleanup = cleanup,
188};