]> sigrok.org Git - libsigrok.git/blame - src/output/wavedrom.c
output/wavedrom: separate data processing logic from init/cleanup
[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
7a0d1bdc
GS
72static void process_logic(const struct context *ctx,
73 const struct sr_datafeed_logic *logic)
74{
75 size_t sample_count, ch, i;
76 uint8_t *sample;
77
78 sample_count = logic->length / logic->unitsize;
79
80 /*
81 * Extract the logic bits for each channel and store them
82 * as wavedrom letters (1/0) in each channel's text string.
83 */
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;
88
89 if (ctx->channel_outputs[ch]) {
90 g_string_append_c(ctx->channel_outputs[ch],
91 sample[ch / 8] & (1 << (ch % 8)) ? '1' : '0');
92 }
93 }
94 }
95 }
96}
97
98static int receive(const struct sr_output *o,
99 const struct sr_datafeed_packet *packet, GString **out)
100{
101 struct context *ctx;
102
103 *out = NULL;
104
105 if (!o || !o->sdi || !o->priv)
106 return SR_ERR_ARG;
107
108 ctx = o->priv;
109
110 switch (packet->type) {
111 case SR_DF_LOGIC:
112 process_logic(ctx, packet->payload);
113 break;
114 case SR_DF_END:
115 *out = wavedrom_render(ctx);
116 break;
117 }
118
119 return SR_OK;
120}
121
40f812f5
MJ
122static int init(struct sr_output *o, GHashTable *options)
123{
124 struct context *ctx;
125 struct sr_channel *channel;
126 GSList *l;
dd5735c9 127 size_t i;
40f812f5
MJ
128
129 (void)options;
130
131 if (!o || !o->sdi)
132 return SR_ERR_ARG;
133
dd5735c9 134 o->priv = ctx = g_malloc0(sizeof(*ctx));
40f812f5
MJ
135
136 ctx->channel_count = g_slist_length(o->sdi->channels);
dd5735c9
GS
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);
40f812f5
MJ
141
142 for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
143 channel = l->data;
dd5735c9 144 if (channel->enabled && channel->type == SR_CHANNEL_LOGIC) {
40f812f5
MJ
145 ctx->channels[i] = channel;
146 ctx->channel_outputs[i] = g_string_new(NULL);
40f812f5
MJ
147 }
148 }
149
150 return SR_OK;
151}
152
153static int cleanup(struct sr_output *o)
154{
155 struct context *ctx;
dd5735c9 156 GString *s;
40f812f5
MJ
157
158 if (!o)
159 return SR_ERR_ARG;
160
161 ctx = o->priv;
162 o->priv = NULL;
163
164 if (ctx) {
dd5735c9
GS
165 while (--ctx->channel_count) {
166 s = ctx->channel_outputs[ctx->channel_count];
167 if (s)
168 g_string_free(s, TRUE);
40f812f5
MJ
169 }
170 g_free(ctx->channel_outputs);
171 g_free(ctx->channels);
172 g_free(ctx);
173 }
174
175 return SR_OK;
176}
177
40f812f5
MJ
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};