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