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