]> sigrok.org Git - libsigrok.git/blame - output/bits.c
session: Show unitsize of the received data.
[libsigrok.git] / 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;
35 int *channel_index;
36 char **channel_names;
37 GString **lines;
38 GString *header;
39};
40
41static int init(struct sr_output *o)
42{
43 struct context *ctx;
44 struct sr_channel *ch;
45 GSList *l;
46 GVariant *gvar;
dba3e682
BV
47 GHashTableIter iter;
48 gpointer key, value;
dbd0aeff
BV
49 uint64_t samplerate;
50 unsigned int i, j;
51 int spl, num_channels;
52 char *samplerate_s;
53
54 if (!o || !o->sdi)
55 return SR_ERR_ARG;
dba3e682
BV
56
57 spl = DEFAULT_SAMPLES_PER_LINE;
58 g_hash_table_iter_init(&iter, o->params);
59 while (g_hash_table_iter_next(&iter, &key, &value)) {
60 if (!strcmp(key, "width")) {
61 if ((spl = strtoul(value, NULL, 10)) < 1) {
62 sr_err("Invalid width.");
63 return SR_ERR_ARG;
64 }
65 } else {
66 sr_err("Unknown parameter '%s'.", key);
67 return SR_ERR_ARG;
68 }
69 }
70
dbd0aeff
BV
71 ctx = g_malloc0(sizeof(struct context));
72 o->internal = ctx;
73 ctx->trigger = -1;
dbd0aeff
BV
74 ctx->samples_per_line = spl;
75
76 for (l = o->sdi->channels; l; l = l->next) {
77 ch = l->data;
78 if (ch->type != SR_CHANNEL_LOGIC)
79 continue;
80 if (!ch->enabled)
81 continue;
82 ctx->num_enabled_channels++;
83 }
84 ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
85 ctx->channel_names = g_malloc(sizeof(char *) * ctx->num_enabled_channels);
86 ctx->lines = g_malloc(sizeof(GString *) * ctx->num_enabled_channels);
87
88 j = 0;
89 for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
90 ch = l->data;
91 if (ch->type != SR_CHANNEL_LOGIC)
92 continue;
93 if (!ch->enabled)
94 continue;
95 ctx->channel_index[j] = ch->index;
96 ctx->channel_names[j] = ch->name;
97 ctx->lines[j] = g_string_sized_new(80);
98 g_string_printf(ctx->lines[j], "%s:", ch->name);
99 j++;
100 }
101
102 ctx->header = g_string_sized_new(512);
103 g_string_printf(ctx->header, "%s\n", PACKAGE_STRING);
104 num_channels = g_slist_length(o->sdi->channels);
105 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
106 &gvar) == SR_OK) {
107 samplerate = g_variant_get_uint64(gvar);
108 samplerate_s = sr_samplerate_string(samplerate);
109 g_string_append_printf(ctx->header, "Acquisition with %d/%d channels at %s\n",
110 ctx->num_enabled_channels, num_channels, samplerate_s);
111 g_free(samplerate_s);
112 g_variant_unref(gvar);
113 }
114
115 return SR_OK;
116}
117
dba3e682
BV
118static int receive(struct sr_output *o, const struct sr_datafeed_packet *packet,
119 GString **out)
dbd0aeff
BV
120{
121 const struct sr_datafeed_logic *logic;
122 struct context *ctx;
123 int idx, offset;
124 uint64_t i, j;
125 gchar *p, c;
126
dbd0aeff
BV
127 *out = NULL;
128 if (!o || !o->sdi)
129 return SR_ERR_ARG;
130 if (!(ctx = o->internal))
131 return SR_ERR_ARG;
132
133 switch (packet->type) {
134 case SR_DF_TRIGGER:
135 ctx->trigger = ctx->spl_cnt;
136 break;
137 case SR_DF_LOGIC:
138 if (ctx->header) {
139 /* The header is still here, this must be the first packet. */
140 *out = ctx->header;
141 ctx->header = NULL;
142 } else
143 *out = g_string_sized_new(512);
144
145 logic = packet->payload;
146 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
147 ctx->spl_cnt++;
148 for (j = 0; j < ctx->num_enabled_channels; j++) {
149 idx = ctx->channel_index[j];
150 p = logic->data + i + idx / 8;
151 c = (*p & (1 << (idx % 8))) ? '1' : '0';
152 g_string_append_c(ctx->lines[j], c);
153
154 if (ctx->spl_cnt == ctx->samples_per_line) {
155 /* Flush line buffers. */
156 g_string_append_len(*out, ctx->lines[j]->str, ctx->lines[j]->len);
157 g_string_append_c(*out, '\n');
158 if (j == ctx->num_enabled_channels - 1 && ctx->trigger > -1) {
159 offset = ctx->trigger + ctx->trigger / 8;
160 g_string_append_printf(*out, "T:%*s^ %d\n", offset, "", ctx->trigger);
161 ctx->trigger = -1;
162 }
163 g_string_printf(ctx->lines[j], "%s:", ctx->channel_names[j]);
164 } else if ((ctx->spl_cnt & 7) == 0) {
165 /* Add a space every 8th bit. */
166 g_string_append_c(ctx->lines[j], ' ');
167 }
168 }
169 if (ctx->spl_cnt == ctx->samples_per_line)
170 /* Line buffers were already flushed. */
171 ctx->spl_cnt = 0;
172 }
173 break;
174 case SR_DF_END:
175 if (ctx->spl_cnt) {
176 /* Line buffers need flushing. */
177 *out = g_string_sized_new(512);
178 for (i = 0; i < ctx->num_enabled_channels; i++) {
179 g_string_append_len(*out, ctx->lines[i]->str, ctx->lines[i]->len);
180 g_string_append_c(*out, '\n');
181 }
182 }
183 break;
184 }
185
186 return SR_OK;
187}
188
189static int cleanup(struct sr_output *o)
190{
191 struct context *ctx;
192 unsigned int i;
193
194 if (!o)
195 return SR_ERR_ARG;
196
197 if (!(ctx = o->internal))
198 return SR_OK;
199
dbd0aeff
BV
200 g_free(ctx->channel_index);
201 g_free(ctx->channel_names);
202 for (i = 0; i < ctx->num_enabled_channels; i++)
203 g_string_free(ctx->lines[i], TRUE);
204 g_free(ctx->lines);
205 if (ctx->header)
206 g_string_free(ctx->header, TRUE);
207 g_free(ctx);
208 o->internal = NULL;
209
210 return SR_OK;
211}
212
213SR_PRIV struct sr_output_format output_bits = {
214 .id = "bits",
215 .description = "Bits",
216 .init = init,
217 .receive = receive,
218 .cleanup = cleanup,
219};