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