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