]>
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 | ||
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 samples_per_line; | |
34 | int bit_cnt; | |
35 | int spl_cnt; | |
36 | int trigger; | |
787e9bc9 | 37 | uint64_t samplerate; |
6f64ebb2 BV |
38 | int *channel_index; |
39 | char **channel_names; | |
40 | char **line_values; | |
41 | uint8_t *prev_sample; | |
787e9bc9 | 42 | gboolean header_done; |
6f64ebb2 BV |
43 | GString **lines; |
44 | GString *header; | |
45 | }; | |
46 | ||
47 | static int init(struct sr_output *o) | |
48 | { | |
49 | struct context *ctx; | |
50 | struct sr_channel *ch; | |
51 | GSList *l; | |
dba3e682 BV |
52 | GHashTableIter iter; |
53 | gpointer key, value; | |
6f64ebb2 | 54 | unsigned int i, j; |
787e9bc9 | 55 | int spl; |
6f64ebb2 BV |
56 | |
57 | if (!o || !o->sdi) | |
58 | return SR_ERR_ARG; | |
dba3e682 BV |
59 | |
60 | spl = DEFAULT_SAMPLES_PER_LINE; | |
61 | g_hash_table_iter_init(&iter, o->params); | |
62 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
63 | if (!strcmp(key, "width")) { | |
64 | if ((spl = strtoul(value, NULL, 10)) < 1) { | |
65 | sr_err("Invalid width."); | |
66 | return SR_ERR_ARG; | |
67 | } | |
68 | } else { | |
69 | sr_err("Unknown parameter '%s'.", key); | |
70 | return SR_ERR_ARG; | |
71 | } | |
72 | } | |
73 | ||
6f64ebb2 BV |
74 | ctx = g_malloc0(sizeof(struct context)); |
75 | o->internal = ctx; | |
76 | ctx->trigger = -1; | |
6f64ebb2 BV |
77 | ctx->samples_per_line = spl; |
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 | ||
787e9bc9 BV |
106 | return SR_OK; |
107 | } | |
108 | ||
109 | static GString *gen_header(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->internal; | |
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\n", PACKAGE_STRING); | |
6f64ebb2 | 128 | num_channels = g_slist_length(o->sdi->channels); |
787e9bc9 BV |
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); | |
6f64ebb2 | 134 | g_free(samplerate_s); |
6f64ebb2 | 135 | } |
787e9bc9 | 136 | g_string_append_printf(header, "\n"); |
6f64ebb2 | 137 | |
787e9bc9 | 138 | return header; |
6f64ebb2 BV |
139 | } |
140 | ||
dba3e682 BV |
141 | static int receive(struct sr_output *o, const struct sr_datafeed_packet *packet, |
142 | GString **out) | |
6f64ebb2 | 143 | { |
787e9bc9 | 144 | const struct sr_datafeed_meta *meta; |
6f64ebb2 | 145 | const struct sr_datafeed_logic *logic; |
787e9bc9 BV |
146 | const struct sr_config *src; |
147 | GSList *l; | |
6f64ebb2 BV |
148 | struct context *ctx; |
149 | int idx, offset, curbit, prevbit; | |
150 | uint64_t i, j; | |
151 | gchar *p, c; | |
152 | ||
6f64ebb2 BV |
153 | *out = NULL; |
154 | if (!o || !o->sdi) | |
155 | return SR_ERR_ARG; | |
156 | if (!(ctx = o->internal)) | |
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 | ||
188 | c = curbit ? '"' : '.'; | |
189 | if (ctx->spl_cnt > 1) { | |
190 | if (curbit < prevbit) | |
191 | c = '\\'; | |
192 | else if (curbit > prevbit) | |
193 | c = '/'; | |
194 | } | |
195 | g_string_append_c(ctx->lines[j], c); | |
196 | ||
197 | if (ctx->spl_cnt == ctx->samples_per_line) { | |
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 | offset = ctx->trigger + ctx->trigger / 8; | |
203 | g_string_append_printf(*out, "T:%*s^ %d\n", offset, "", ctx->trigger); | |
204 | ctx->trigger = -1; | |
205 | } | |
206 | g_string_printf(ctx->lines[j], "%s:", ctx->channel_names[j]); | |
207 | } | |
208 | } | |
209 | if (ctx->spl_cnt == ctx->samples_per_line) | |
210 | /* Line buffers were already flushed. */ | |
211 | ctx->spl_cnt = 0; | |
212 | memcpy(ctx->prev_sample, logic->data + i, logic->unitsize); | |
213 | } | |
214 | break; | |
215 | case SR_DF_END: | |
216 | if (ctx->spl_cnt) { | |
217 | /* Line buffers need flushing. */ | |
218 | *out = g_string_sized_new(512); | |
219 | for (i = 0; i < ctx->num_enabled_channels; i++) { | |
220 | g_string_append_len(*out, ctx->lines[i]->str, ctx->lines[i]->len); | |
221 | g_string_append_c(*out, '\n'); | |
222 | } | |
223 | } | |
224 | break; | |
225 | } | |
226 | ||
227 | return SR_OK; | |
228 | } | |
229 | ||
230 | static int cleanup(struct sr_output *o) | |
231 | { | |
232 | struct context *ctx; | |
233 | unsigned int i; | |
234 | ||
235 | if (!o) | |
236 | return SR_ERR_ARG; | |
237 | ||
238 | if (!(ctx = o->internal)) | |
239 | return SR_OK; | |
240 | ||
6f64ebb2 BV |
241 | g_free(ctx->channel_index); |
242 | g_free(ctx->prev_sample); | |
243 | g_free(ctx->channel_names); | |
244 | for (i = 0; i < ctx->num_enabled_channels; i++) | |
245 | g_string_free(ctx->lines[i], TRUE); | |
246 | g_free(ctx->lines); | |
6f64ebb2 BV |
247 | g_free(ctx); |
248 | o->internal = NULL; | |
249 | ||
250 | return SR_OK; | |
251 | } | |
252 | ||
253 | SR_PRIV struct sr_output_format output_ascii = { | |
254 | .id = "ascii", | |
255 | .description = "ASCII", | |
256 | .init = init, | |
257 | .receive = receive, | |
258 | .cleanup = cleanup, | |
259 | }; | |
260 | ||
261 |