]> sigrok.org Git - libsigrok.git/blob - src/output/wavedrom.c
output/wavedrom: address style nits
[libsigrok.git] / src / output / wavedrom.c
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
27 struct context {
28         uint32_t channel_count;
29         struct sr_channel **channels;
30         GString **channel_outputs; /* output strings */
31 };
32
33 /* Converts accumulated output data to a JSON string. */
34 static GString *wavedrom_render(const struct context *ctx)
35 {
36         GString *output;
37         size_t ch, i;
38         char last_char, curr_char;
39
40         output = g_string_new("{ \"signal\": [");
41         for (ch = 0; ch < ctx->channel_count; ch++) {
42                 if (!ctx->channel_outputs[ch])
43                         continue;
44
45                 /* Channel strip. */
46                 g_string_append_printf(output,
47                         "{ \"name\": \"%s\", \"wave\": \"", ctx->channels[ch]->name);
48
49                 last_char = 0;
50                 for (i = 0; i < ctx->channel_outputs[ch]->len; i++) {
51                         curr_char = ctx->channel_outputs[ch]->str[i];
52                         /* Data point. */
53                         if (curr_char == last_char) {
54                                 g_string_append_c(output, '.');
55                         } else {
56                                 g_string_append_c(output, curr_char);
57                                 last_char = curr_char;
58                         }
59                 }
60                 if (ch < ctx->channel_count - 1) {
61                         g_string_append(output, "\" },");
62                 } else {
63                         /* Last channel, no comma. */
64                         g_string_append(output, "\" }");
65                 }
66         }
67         g_string_append(output, "], \"config\": { \"skin\": \"narrow\" }}");
68
69         return output;
70 }
71
72 static int init(struct sr_output *o, GHashTable *options)
73 {
74         struct context *ctx;
75         struct sr_channel *channel;
76         GSList *l;
77         size_t i;
78
79         (void)options;
80
81         if (!o || !o->sdi)
82                 return SR_ERR_ARG;
83
84         o->priv = ctx = g_malloc0(sizeof(*ctx));
85
86         ctx->channel_count = g_slist_length(o->sdi->channels);
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);
91
92         for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
93                 channel = l->data;
94                 if (channel->enabled && channel->type == SR_CHANNEL_LOGIC) {
95                         ctx->channels[i] = channel;
96                         ctx->channel_outputs[i] = g_string_new(NULL);
97                 }
98         }
99
100         return SR_OK;
101 }
102
103 static int cleanup(struct sr_output *o)
104 {
105         struct context *ctx;
106         GString *s;
107
108         if (!o)
109                 return SR_ERR_ARG;
110
111         ctx = o->priv;
112         o->priv = NULL;
113
114         if (ctx) {
115                 while (--ctx->channel_count) {
116                         s = ctx->channel_outputs[ctx->channel_count];
117                         if (s)
118                                 g_string_free(s, TRUE);
119                 }
120                 g_free(ctx->channel_outputs);
121                 g_free(ctx->channels);
122                 g_free(ctx);
123         }
124
125         return SR_OK;
126 }
127
128 static void process_logic(const struct context *ctx,
129         const struct sr_datafeed_logic *logic)
130 {
131         size_t sample_count, ch, i;
132         uint8_t *sample;
133
134         sample_count = logic->length / logic->unitsize;
135
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          */
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],
147                                                 sample[ch / 8] & (1 << (ch % 8)) ? '1' : '0');
148                                 }
149                         }
150                 }
151         }
152 }
153
154 static int receive(const struct sr_output *o,
155         const struct sr_datafeed_packet *packet, GString **out)
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
178 SR_PRIV struct sr_output_module output_wavedrom = {
179         .id = "wavedrom",
180         .name = "WaveDrom",
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 };