]> sigrok.org Git - libsigrok.git/blame - src/output/bits.c
output: Modules can keep track of option resources without wrapper help.
[libsigrok.git] / src / output / bits.c
CommitLineData
dbd0aeff
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.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 <stdlib.h>
21#include <string.h>
22#include <glib.h>
23#include "libsigrok.h"
24#include "libsigrok-internal.h"
25
26#define LOG_PREFIX "output/bits"
27
28#define DEFAULT_SAMPLES_PER_LINE 64
29
30struct context {
31 unsigned int num_enabled_channels;
32 int samples_per_line;
33 int spl_cnt;
34 int trigger;
35159a6b 35 uint64_t samplerate;
dbd0aeff
BV
36 int *channel_index;
37 char **channel_names;
35159a6b 38 gboolean header_done;
dbd0aeff 39 GString **lines;
dbd0aeff
BV
40};
41
a755b0e1 42static int init(struct sr_output *o, GHashTable *options)
dbd0aeff
BV
43{
44 struct context *ctx;
45 struct sr_channel *ch;
46 GSList *l;
dba3e682
BV
47 GHashTableIter iter;
48 gpointer key, value;
dbd0aeff 49 unsigned int i, j;
a755b0e1 50 uint32_t spl;
dbd0aeff
BV
51
52 if (!o || !o->sdi)
53 return SR_ERR_ARG;
dba3e682
BV
54
55 spl = DEFAULT_SAMPLES_PER_LINE;
a755b0e1
BV
56 if (options) {
57 g_hash_table_iter_init(&iter, options);
58 while (g_hash_table_iter_next(&iter, &key, &value)) {
59 if (!strcmp(key, "width")) {
60 if (!g_variant_is_of_type(value, G_VARIANT_TYPE_UINT32)) {
61 sr_err("Invalid type for 'width' option.");
62 return SR_ERR_ARG;
63 }
64 if (!(spl = g_variant_get_uint32(value))) {
65 sr_err("Invalid width.");
66 return SR_ERR_ARG;
67 }
68 } else {
69 sr_err("Unknown option '%s'.", key);
dba3e682 70 }
dba3e682
BV
71 }
72 }
73
dbd0aeff 74 ctx = g_malloc0(sizeof(struct context));
d686c5ec 75 o->priv = ctx;
dbd0aeff 76 ctx->trigger = -1;
dbd0aeff
BV
77 ctx->samples_per_line = spl;
78
79 for (l = o->sdi->channels; l; l = l->next) {
80 ch = l->data;
81 if (ch->type != SR_CHANNEL_LOGIC)
82 continue;
83 if (!ch->enabled)
84 continue;
85 ctx->num_enabled_channels++;
86 }
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
91 j = 0;
92 for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
93 ch = l->data;
94 if (ch->type != SR_CHANNEL_LOGIC)
95 continue;
96 if (!ch->enabled)
97 continue;
98 ctx->channel_index[j] = ch->index;
99 ctx->channel_names[j] = ch->name;
100 ctx->lines[j] = g_string_sized_new(80);
101 g_string_printf(ctx->lines[j], "%s:", ch->name);
102 j++;
103 }
104
35159a6b
BV
105 return SR_OK;
106}
107
a755b0e1 108static GString *gen_header(const struct sr_output *o)
35159a6b
BV
109{
110 struct context *ctx;
111 GVariant *gvar;
112 GString *header;
113 int num_channels;
114 char *samplerate_s;
115
d686c5ec 116 ctx = o->priv;
35159a6b
BV
117 if (ctx->samplerate == 0) {
118 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
119 &gvar) == SR_OK) {
120 ctx->samplerate = g_variant_get_uint64(gvar);
121 g_variant_unref(gvar);
122 }
123 }
124
125 header = g_string_sized_new(512);
126 g_string_printf(header, "%s\n", PACKAGE_STRING);
dbd0aeff 127 num_channels = g_slist_length(o->sdi->channels);
35159a6b
BV
128 g_string_append_printf(header, "Acquisition with %d/%d channels",
129 ctx->num_enabled_channels, num_channels);
130 if (ctx->samplerate != 0) {
131 samplerate_s = sr_samplerate_string(ctx->samplerate);
132 g_string_append_printf(header, " at %s", samplerate_s);
dbd0aeff 133 g_free(samplerate_s);
dbd0aeff 134 }
35159a6b 135 g_string_append_printf(header, "\n");
dbd0aeff 136
35159a6b 137 return header;
dbd0aeff
BV
138}
139
a755b0e1 140static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
dba3e682 141 GString **out)
dbd0aeff 142{
35159a6b 143 const struct sr_datafeed_meta *meta;
dbd0aeff 144 const struct sr_datafeed_logic *logic;
35159a6b 145 const struct sr_config *src;
dbd0aeff 146 struct context *ctx;
35159a6b 147 GSList *l;
dbd0aeff
BV
148 int idx, offset;
149 uint64_t i, j;
150 gchar *p, c;
151
dbd0aeff
BV
152 *out = NULL;
153 if (!o || !o->sdi)
154 return SR_ERR_ARG;
d686c5ec 155 if (!(ctx = o->priv))
dbd0aeff
BV
156 return SR_ERR_ARG;
157
158 switch (packet->type) {
35159a6b
BV
159 case SR_DF_META:
160 meta = packet->payload;
161 for (l = meta->config; l; l = l->next) {
162 src = l->data;
163 if (src->key != SR_CONF_SAMPLERATE)
164 continue;
165 ctx->samplerate = g_variant_get_uint64(src->data);
166 }
167 break;
dbd0aeff
BV
168 case SR_DF_TRIGGER:
169 ctx->trigger = ctx->spl_cnt;
170 break;
171 case SR_DF_LOGIC:
35159a6b
BV
172 if (!ctx->header_done) {
173 *out = gen_header(o);
174 ctx->header_done = TRUE;
dbd0aeff
BV
175 } else
176 *out = g_string_sized_new(512);
177
178 logic = packet->payload;
179 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
180 ctx->spl_cnt++;
181 for (j = 0; j < ctx->num_enabled_channels; j++) {
182 idx = ctx->channel_index[j];
183 p = logic->data + i + idx / 8;
184 c = (*p & (1 << (idx % 8))) ? '1' : '0';
185 g_string_append_c(ctx->lines[j], c);
186
187 if (ctx->spl_cnt == ctx->samples_per_line) {
188 /* Flush line buffers. */
189 g_string_append_len(*out, ctx->lines[j]->str, ctx->lines[j]->len);
190 g_string_append_c(*out, '\n');
191 if (j == ctx->num_enabled_channels - 1 && ctx->trigger > -1) {
192 offset = ctx->trigger + ctx->trigger / 8;
193 g_string_append_printf(*out, "T:%*s^ %d\n", offset, "", ctx->trigger);
194 ctx->trigger = -1;
195 }
196 g_string_printf(ctx->lines[j], "%s:", ctx->channel_names[j]);
197 } else if ((ctx->spl_cnt & 7) == 0) {
198 /* Add a space every 8th bit. */
199 g_string_append_c(ctx->lines[j], ' ');
200 }
201 }
202 if (ctx->spl_cnt == ctx->samples_per_line)
203 /* Line buffers were already flushed. */
204 ctx->spl_cnt = 0;
205 }
206 break;
207 case SR_DF_END:
208 if (ctx->spl_cnt) {
209 /* Line buffers need flushing. */
210 *out = g_string_sized_new(512);
211 for (i = 0; i < ctx->num_enabled_channels; i++) {
212 g_string_append_len(*out, ctx->lines[i]->str, ctx->lines[i]->len);
213 g_string_append_c(*out, '\n');
214 }
215 }
216 break;
217 }
218
219 return SR_OK;
220}
221
222static int cleanup(struct sr_output *o)
223{
224 struct context *ctx;
225 unsigned int i;
226
227 if (!o)
228 return SR_ERR_ARG;
229
d686c5ec 230 if (!(ctx = o->priv))
dbd0aeff
BV
231 return SR_OK;
232
dbd0aeff
BV
233 g_free(ctx->channel_index);
234 g_free(ctx->channel_names);
235 for (i = 0; i < ctx->num_enabled_channels; i++)
236 g_string_free(ctx->lines[i], TRUE);
237 g_free(ctx->lines);
dbd0aeff 238 g_free(ctx);
d686c5ec 239 o->priv = NULL;
dbd0aeff
BV
240
241 return SR_OK;
242}
243
a755b0e1
BV
244static struct sr_option options[] = {
245 { "width", "Width", "Number of samples per line", NULL, NULL },
246 { 0 }
247};
248
950043c3 249static struct sr_option *get_options(void)
a755b0e1 250{
950043c3
BV
251 if (!options[0].def) {
252 options[0].def = g_variant_new_uint32(DEFAULT_SAMPLES_PER_LINE);
253 g_variant_ref_sink(options[0].def);
254 }
a755b0e1
BV
255
256 return options;
257}
258
259SR_PRIV struct sr_output_module output_bits = {
dbd0aeff 260 .id = "bits",
a755b0e1
BV
261 .name = "Bits",
262 .desc = "0/1 digits",
263 .options = get_options,
dbd0aeff
BV
264 .init = init,
265 .receive = receive,
266 .cleanup = cleanup,
267};