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