]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2011 HÃ¥vard Espeland <gus@ping.uio.no> | |
5 | * Copyright (C) 2014 Bert Vermeulen <bert@biot.com> | |
6 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <config.h> | |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | #include <glib.h> | |
25 | #include <libsigrok/libsigrok.h> | |
26 | #include "libsigrok-internal.h" | |
27 | ||
28 | #define LOG_PREFIX "output/hex" | |
29 | ||
30 | #define DEFAULT_SAMPLES_PER_LINE 74 | |
31 | ||
32 | /* | |
33 | * The string looks ugly with escape characters, here is the readable | |
34 | * version: Use . and " for low and high bits, use \ and / to draw | |
35 | * falling and rising edges respectively. | |
36 | */ | |
37 | #define DEFAULT_ASCII_CHARS ".\"\\/" | |
38 | ||
39 | struct context { | |
40 | unsigned int num_enabled_channels; | |
41 | int spl; | |
42 | int bit_cnt; | |
43 | int spl_cnt; | |
44 | int trigger; | |
45 | uint64_t samplerate; | |
46 | int *channel_index; | |
47 | char **channel_names; | |
48 | char **line_values; | |
49 | uint8_t *prev_sample; | |
50 | gboolean header_done; | |
51 | GString **lines; | |
52 | GString *header; | |
53 | const char *charset; | |
54 | gboolean edges; | |
55 | }; | |
56 | ||
57 | static int init(struct sr_output *o, GHashTable *options) | |
58 | { | |
59 | struct context *ctx; | |
60 | struct sr_channel *ch; | |
61 | GSList *l; | |
62 | unsigned int i, j; | |
63 | ||
64 | if (!o || !o->sdi) | |
65 | return SR_ERR_ARG; | |
66 | ||
67 | ctx = g_malloc0(sizeof(struct context)); | |
68 | o->priv = ctx; | |
69 | ctx->trigger = -1; | |
70 | ctx->spl = g_variant_get_uint32(g_hash_table_lookup(options, "width")); | |
71 | ctx->charset = g_strdup(g_variant_get_string( | |
72 | g_hash_table_lookup(options, "charset"), NULL)); | |
73 | if (!ctx->charset || strlen(ctx->charset) < 2) { | |
74 | g_free((gpointer)ctx->charset); | |
75 | ctx->charset = g_strdup(DEFAULT_ASCII_CHARS); | |
76 | } | |
77 | ctx->edges = (strlen(ctx->charset) >= 4) ? TRUE : FALSE; | |
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 | ctx->prev_sample = g_malloc(g_slist_length(o->sdi->channels)); | |
91 | ||
92 | j = 0; | |
93 | for (i = 0, l = o->sdi->channels; l; l = l->next, i++) { | |
94 | ch = l->data; | |
95 | if (ch->type != SR_CHANNEL_LOGIC) | |
96 | continue; | |
97 | if (!ch->enabled) | |
98 | continue; | |
99 | ctx->channel_index[j] = ch->index; | |
100 | ctx->channel_names[j] = ch->name; | |
101 | ctx->lines[j] = g_string_sized_new(80); | |
102 | g_string_printf(ctx->lines[j], "%s:", ch->name); | |
103 | j++; | |
104 | } | |
105 | ||
106 | return SR_OK; | |
107 | } | |
108 | ||
109 | static GString *gen_header(const struct sr_output *o) | |
110 | { | |
111 | struct context *ctx; | |
112 | GVariant *gvar; | |
113 | GString *header; | |
114 | int num_channels; | |
115 | char *samplerate_s; | |
116 | ||
117 | ctx = o->priv; | |
118 | if (ctx->samplerate == 0) { | |
119 | if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE, | |
120 | &gvar) == SR_OK) { | |
121 | ctx->samplerate = g_variant_get_uint64(gvar); | |
122 | g_variant_unref(gvar); | |
123 | } | |
124 | } | |
125 | ||
126 | header = g_string_sized_new(512); | |
127 | g_string_printf(header, "%s %s\n", PACKAGE_NAME, sr_package_version_string_get()); | |
128 | num_channels = g_slist_length(o->sdi->channels); | |
129 | g_string_append_printf(header, "Acquisition with %d/%d channels", | |
130 | ctx->num_enabled_channels, num_channels); | |
131 | if (ctx->samplerate != 0) { | |
132 | samplerate_s = sr_samplerate_string(ctx->samplerate); | |
133 | g_string_append_printf(header, " at %s", samplerate_s); | |
134 | g_free(samplerate_s); | |
135 | } | |
136 | g_string_append_printf(header, "\n"); | |
137 | ||
138 | return header; | |
139 | } | |
140 | ||
141 | static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet, | |
142 | GString **out) | |
143 | { | |
144 | const struct sr_datafeed_meta *meta; | |
145 | const struct sr_datafeed_logic *logic; | |
146 | const struct sr_config *src; | |
147 | GSList *l; | |
148 | struct context *ctx; | |
149 | int idx, offset, curbit, prevbit; | |
150 | uint64_t i, j; | |
151 | gchar *p, c; | |
152 | size_t charidx; | |
153 | ||
154 | *out = NULL; | |
155 | if (!o || !o->sdi) | |
156 | return SR_ERR_ARG; | |
157 | if (!(ctx = o->priv)) | |
158 | return SR_ERR_ARG; | |
159 | ||
160 | switch (packet->type) { | |
161 | case SR_DF_META: | |
162 | meta = packet->payload; | |
163 | for (l = meta->config; l; l = l->next) { | |
164 | src = l->data; | |
165 | if (src->key != SR_CONF_SAMPLERATE) | |
166 | continue; | |
167 | ctx->samplerate = g_variant_get_uint64(src->data); | |
168 | } | |
169 | break; | |
170 | case SR_DF_TRIGGER: | |
171 | ctx->trigger = ctx->spl_cnt; | |
172 | break; | |
173 | case SR_DF_LOGIC: | |
174 | if (!ctx->header_done) { | |
175 | *out = gen_header(o); | |
176 | ctx->header_done = TRUE; | |
177 | } else | |
178 | *out = g_string_sized_new(512); | |
179 | ||
180 | logic = packet->payload; | |
181 | for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) { | |
182 | ctx->spl_cnt++; | |
183 | for (j = 0; j < ctx->num_enabled_channels; j++) { | |
184 | idx = ctx->channel_index[j]; | |
185 | p = logic->data + i + idx / 8; | |
186 | curbit = *p & (1 << (idx % 8)); | |
187 | prevbit = (ctx->prev_sample[idx / 8] & ((uint8_t) 1 << (idx % 8))); | |
188 | ||
189 | charidx = curbit ? 1 : 0; | |
190 | if (ctx->edges && ctx->spl_cnt > 1) { | |
191 | if (curbit != prevbit) | |
192 | charidx += 2; | |
193 | } | |
194 | c = ctx->charset[charidx]; | |
195 | g_string_append_c(ctx->lines[j], c); | |
196 | ||
197 | if (ctx->spl_cnt == ctx->spl) { | |
198 | /* Flush line buffers. */ | |
199 | g_string_append_len(*out, ctx->lines[j]->str, ctx->lines[j]->len); | |
200 | g_string_append_c(*out, '\n'); | |
201 | if (j == ctx->num_enabled_channels - 1 && ctx->trigger > -1) { | |
202 | /* | |
203 | * Sample data lines have one character per bit and | |
204 | * no separator between bytes. Align trigger marker | |
205 | * to this layout. | |
206 | */ | |
207 | offset = ctx->trigger; | |
208 | g_string_append_printf(*out, "T:%*s^ %d\n", offset, "", ctx->trigger); | |
209 | ctx->trigger = -1; | |
210 | } | |
211 | g_string_printf(ctx->lines[j], "%s:", ctx->channel_names[j]); | |
212 | } | |
213 | } | |
214 | if (ctx->spl_cnt == ctx->spl) | |
215 | /* Line buffers were already flushed. */ | |
216 | ctx->spl_cnt = 0; | |
217 | memcpy(ctx->prev_sample, logic->data + i, logic->unitsize); | |
218 | } | |
219 | break; | |
220 | case SR_DF_END: | |
221 | if (ctx->spl_cnt) { | |
222 | /* Line buffers need flushing. */ | |
223 | *out = g_string_sized_new(512); | |
224 | for (i = 0; i < ctx->num_enabled_channels; i++) { | |
225 | g_string_append_len(*out, ctx->lines[i]->str, ctx->lines[i]->len); | |
226 | g_string_append_c(*out, '\n'); | |
227 | } | |
228 | } | |
229 | break; | |
230 | } | |
231 | ||
232 | return SR_OK; | |
233 | } | |
234 | ||
235 | static int cleanup(struct sr_output *o) | |
236 | { | |
237 | struct context *ctx; | |
238 | unsigned int i; | |
239 | ||
240 | if (!o) | |
241 | return SR_ERR_ARG; | |
242 | ||
243 | if (!(ctx = o->priv)) | |
244 | return SR_OK; | |
245 | ||
246 | g_free(ctx->channel_index); | |
247 | g_free(ctx->prev_sample); | |
248 | g_free(ctx->channel_names); | |
249 | for (i = 0; i < ctx->num_enabled_channels; i++) | |
250 | g_string_free(ctx->lines[i], TRUE); | |
251 | g_free(ctx->lines); | |
252 | g_free((gpointer)ctx->charset); | |
253 | g_free(ctx); | |
254 | o->priv = NULL; | |
255 | ||
256 | return SR_OK; | |
257 | } | |
258 | ||
259 | static struct sr_option options[] = { | |
260 | { "width", "Width", "Number of samples per line", NULL, NULL }, | |
261 | { "charset", "Charset", "Characters for 0/1 bits (and fall/rise edges)", NULL, NULL }, | |
262 | ALL_ZERO | |
263 | }; | |
264 | ||
265 | static const struct sr_option *get_options(void) | |
266 | { | |
267 | if (!options[0].def) { | |
268 | options[0].def = g_variant_new_uint32(DEFAULT_SAMPLES_PER_LINE); | |
269 | g_variant_ref_sink(options[0].def); | |
270 | options[1].def = g_variant_new_string(DEFAULT_ASCII_CHARS); | |
271 | g_variant_ref_sink(options[1].def); | |
272 | } | |
273 | ||
274 | return options; | |
275 | } | |
276 | ||
277 | SR_PRIV struct sr_output_module output_ascii = { | |
278 | .id = "ascii", | |
279 | .name = "ASCII", | |
280 | .desc = "ASCII art logic data", | |
281 | .exts = (const char*[]){"txt", NULL}, | |
282 | .flags = 0, | |
283 | .options = get_options, | |
284 | .init = init, | |
285 | .receive = receive, | |
286 | .cleanup = cleanup, | |
287 | }; |