]>
Commit | Line | Data |
---|---|---|
6f64ebb2 BV |
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 | ||
6ec6c43b | 21 | #include <config.h> |
6f64ebb2 BV |
22 | #include <stdlib.h> |
23 | #include <string.h> | |
24 | #include <glib.h> | |
c1aae900 | 25 | #include <libsigrok/libsigrok.h> |
6f64ebb2 BV |
26 | #include "libsigrok-internal.h" |
27 | ||
28 | #define LOG_PREFIX "output/hex" | |
29 | ||
30 | #define DEFAULT_SAMPLES_PER_LINE 74 | |
31 | ||
0150fdca GS |
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 | ||
6f64ebb2 BV |
39 | struct context { |
40 | unsigned int num_enabled_channels; | |
dcc55fe9 | 41 | int spl; |
6f64ebb2 BV |
42 | int spl_cnt; |
43 | int trigger; | |
787e9bc9 | 44 | uint64_t samplerate; |
6f64ebb2 BV |
45 | int *channel_index; |
46 | char **channel_names; | |
47 | char **line_values; | |
48 | uint8_t *prev_sample; | |
787e9bc9 | 49 | gboolean header_done; |
6f64ebb2 BV |
50 | GString **lines; |
51 | GString *header; | |
0150fdca GS |
52 | const char *charset; |
53 | gboolean edges; | |
6f64ebb2 BV |
54 | }; |
55 | ||
a755b0e1 | 56 | static int init(struct sr_output *o, GHashTable *options) |
6f64ebb2 BV |
57 | { |
58 | struct context *ctx; | |
59 | struct sr_channel *ch; | |
60 | GSList *l; | |
6f64ebb2 | 61 | unsigned int i, j; |
6f64ebb2 BV |
62 | |
63 | if (!o || !o->sdi) | |
64 | return SR_ERR_ARG; | |
dba3e682 | 65 | |
6f64ebb2 | 66 | ctx = g_malloc0(sizeof(struct context)); |
d686c5ec | 67 | o->priv = ctx; |
6f64ebb2 | 68 | ctx->trigger = -1; |
dcc55fe9 | 69 | ctx->spl = g_variant_get_uint32(g_hash_table_lookup(options, "width")); |
0150fdca GS |
70 | ctx->charset = g_strdup(g_variant_get_string( |
71 | g_hash_table_lookup(options, "charset"), NULL)); | |
72 | if (!ctx->charset || strlen(ctx->charset) < 2) { | |
73 | g_free((gpointer)ctx->charset); | |
74 | ctx->charset = g_strdup(DEFAULT_ASCII_CHARS); | |
75 | } | |
76 | ctx->edges = (strlen(ctx->charset) >= 4) ? TRUE : FALSE; | |
6f64ebb2 BV |
77 | |
78 | for (l = o->sdi->channels; l; l = l->next) { | |
79 | ch = l->data; | |
80 | if (ch->type != SR_CHANNEL_LOGIC) | |
81 | continue; | |
82 | if (!ch->enabled) | |
83 | continue; | |
84 | ctx->num_enabled_channels++; | |
85 | } | |
86 | ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels); | |
87 | ctx->channel_names = g_malloc(sizeof(char *) * ctx->num_enabled_channels); | |
88 | ctx->lines = g_malloc(sizeof(GString *) * ctx->num_enabled_channels); | |
89 | ctx->prev_sample = g_malloc(g_slist_length(o->sdi->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 | ||
787e9bc9 BV |
105 | return SR_OK; |
106 | } | |
107 | ||
a755b0e1 | 108 | static GString *gen_header(const struct sr_output *o) |
787e9bc9 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; |
787e9bc9 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); | |
2868bca3 | 126 | g_string_printf(header, "%s %s\n", PACKAGE_NAME, sr_package_version_string_get()); |
6f64ebb2 | 127 | num_channels = g_slist_length(o->sdi->channels); |
787e9bc9 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); | |
6f64ebb2 | 133 | g_free(samplerate_s); |
6f64ebb2 | 134 | } |
787e9bc9 | 135 | g_string_append_printf(header, "\n"); |
6f64ebb2 | 136 | |
787e9bc9 | 137 | return header; |
6f64ebb2 BV |
138 | } |
139 | ||
a755b0e1 | 140 | static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet, |
dba3e682 | 141 | GString **out) |
6f64ebb2 | 142 | { |
787e9bc9 | 143 | const struct sr_datafeed_meta *meta; |
6f64ebb2 | 144 | const struct sr_datafeed_logic *logic; |
787e9bc9 BV |
145 | const struct sr_config *src; |
146 | GSList *l; | |
6f64ebb2 BV |
147 | struct context *ctx; |
148 | int idx, offset, curbit, prevbit; | |
149 | uint64_t i, j; | |
150 | gchar *p, c; | |
0150fdca | 151 | size_t charidx; |
6f64ebb2 | 152 | |
6f64ebb2 BV |
153 | *out = NULL; |
154 | if (!o || !o->sdi) | |
155 | return SR_ERR_ARG; | |
d686c5ec | 156 | if (!(ctx = o->priv)) |
6f64ebb2 BV |
157 | return SR_ERR_ARG; |
158 | ||
159 | switch (packet->type) { | |
787e9bc9 BV |
160 | case SR_DF_META: |
161 | meta = packet->payload; | |
162 | for (l = meta->config; l; l = l->next) { | |
163 | src = l->data; | |
164 | if (src->key != SR_CONF_SAMPLERATE) | |
165 | continue; | |
166 | ctx->samplerate = g_variant_get_uint64(src->data); | |
167 | } | |
168 | break; | |
6f64ebb2 BV |
169 | case SR_DF_TRIGGER: |
170 | ctx->trigger = ctx->spl_cnt; | |
171 | break; | |
172 | case SR_DF_LOGIC: | |
787e9bc9 BV |
173 | if (!ctx->header_done) { |
174 | *out = gen_header(o); | |
175 | ctx->header_done = TRUE; | |
6f64ebb2 BV |
176 | } else |
177 | *out = g_string_sized_new(512); | |
178 | ||
179 | logic = packet->payload; | |
180 | for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) { | |
181 | ctx->spl_cnt++; | |
182 | for (j = 0; j < ctx->num_enabled_channels; j++) { | |
183 | idx = ctx->channel_index[j]; | |
184 | p = logic->data + i + idx / 8; | |
185 | curbit = *p & (1 << (idx % 8)); | |
186 | prevbit = (ctx->prev_sample[idx / 8] & ((uint8_t) 1 << (idx % 8))); | |
187 | ||
0150fdca GS |
188 | charidx = curbit ? 1 : 0; |
189 | if (ctx->edges && ctx->spl_cnt > 1) { | |
190 | if (curbit != prevbit) | |
191 | charidx += 2; | |
6f64ebb2 | 192 | } |
0150fdca | 193 | c = ctx->charset[charidx]; |
6f64ebb2 BV |
194 | g_string_append_c(ctx->lines[j], c); |
195 | ||
dcc55fe9 | 196 | if (ctx->spl_cnt == ctx->spl) { |
6f64ebb2 BV |
197 | /* Flush line buffers. */ |
198 | g_string_append_len(*out, ctx->lines[j]->str, ctx->lines[j]->len); | |
199 | g_string_append_c(*out, '\n'); | |
d9251a2c | 200 | if (j == ctx->num_enabled_channels - 1 && ctx->trigger > -1) { |
67b345b9 | 201 | /* |
3387a5d8 GS |
202 | * Sample data lines have one character per bit and |
203 | * no separator between bytes. Align trigger marker | |
204 | * to this layout. | |
67b345b9 GS |
205 | */ |
206 | offset = ctx->trigger; | |
6f64ebb2 BV |
207 | g_string_append_printf(*out, "T:%*s^ %d\n", offset, "", ctx->trigger); |
208 | ctx->trigger = -1; | |
209 | } | |
210 | g_string_printf(ctx->lines[j], "%s:", ctx->channel_names[j]); | |
211 | } | |
212 | } | |
dcc55fe9 | 213 | if (ctx->spl_cnt == ctx->spl) |
6f64ebb2 BV |
214 | /* Line buffers were already flushed. */ |
215 | ctx->spl_cnt = 0; | |
216 | memcpy(ctx->prev_sample, logic->data + i, logic->unitsize); | |
217 | } | |
218 | break; | |
219 | case SR_DF_END: | |
220 | if (ctx->spl_cnt) { | |
221 | /* Line buffers need flushing. */ | |
222 | *out = g_string_sized_new(512); | |
223 | for (i = 0; i < ctx->num_enabled_channels; i++) { | |
224 | g_string_append_len(*out, ctx->lines[i]->str, ctx->lines[i]->len); | |
225 | g_string_append_c(*out, '\n'); | |
226 | } | |
227 | } | |
228 | break; | |
229 | } | |
230 | ||
231 | return SR_OK; | |
232 | } | |
233 | ||
234 | static int cleanup(struct sr_output *o) | |
235 | { | |
236 | struct context *ctx; | |
237 | unsigned int i; | |
238 | ||
239 | if (!o) | |
240 | return SR_ERR_ARG; | |
241 | ||
d686c5ec | 242 | if (!(ctx = o->priv)) |
6f64ebb2 BV |
243 | return SR_OK; |
244 | ||
6f64ebb2 BV |
245 | g_free(ctx->channel_index); |
246 | g_free(ctx->prev_sample); | |
247 | g_free(ctx->channel_names); | |
248 | for (i = 0; i < ctx->num_enabled_channels; i++) | |
249 | g_string_free(ctx->lines[i], TRUE); | |
250 | g_free(ctx->lines); | |
0150fdca | 251 | g_free((gpointer)ctx->charset); |
6f64ebb2 | 252 | g_free(ctx); |
d686c5ec | 253 | o->priv = NULL; |
6f64ebb2 BV |
254 | |
255 | return SR_OK; | |
256 | } | |
257 | ||
a755b0e1 BV |
258 | static struct sr_option options[] = { |
259 | { "width", "Width", "Number of samples per line", NULL, NULL }, | |
0150fdca | 260 | { "charset", "Charset", "Characters for 0/1 bits (and fall/rise edges)", NULL, NULL }, |
1685c276 | 261 | ALL_ZERO |
a755b0e1 BV |
262 | }; |
263 | ||
af7d656d | 264 | static const struct sr_option *get_options(void) |
a755b0e1 | 265 | { |
950043c3 BV |
266 | if (!options[0].def) { |
267 | options[0].def = g_variant_new_uint32(DEFAULT_SAMPLES_PER_LINE); | |
268 | g_variant_ref_sink(options[0].def); | |
0150fdca GS |
269 | options[1].def = g_variant_new_string(DEFAULT_ASCII_CHARS); |
270 | g_variant_ref_sink(options[1].def); | |
950043c3 | 271 | } |
a755b0e1 BV |
272 | |
273 | return options; | |
274 | } | |
275 | ||
276 | SR_PRIV struct sr_output_module output_ascii = { | |
6f64ebb2 | 277 | .id = "ascii", |
a755b0e1 | 278 | .name = "ASCII", |
b20eb520 | 279 | .desc = "ASCII art logic data", |
8a174d23 | 280 | .exts = (const char*[]){"txt", NULL}, |
3cd4b381 | 281 | .flags = 0, |
a755b0e1 | 282 | .options = get_options, |
6f64ebb2 BV |
283 | .init = init, |
284 | .receive = receive, | |
285 | .cleanup = cleanup, | |
286 | }; |