]> sigrok.org Git - libsigrok.git/blame - output/output_text.c
Rename "bin" output module to "bits" everywhere.
[libsigrok.git] / output / output_text.c
CommitLineData
a1bb33af
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23#include <glib.h>
25e7d9b1 24#include <sigrok.h>
a1bb33af 25
02076d69
UH
26#define DEFAULT_BPL_BITS 64
27#define DEFAULT_BPL_HEX 256
a1bb33af
UH
28
29struct context {
afc8e4de 30 unsigned int num_enabled_probes;
a1bb33af 31 int samples_per_line;
afc8e4de 32 unsigned int unitsize;
a1bb33af
UH
33 int line_offset;
34 int linebuf_len;
35 char *probelist[65];
36 char *linebuf;
37 int spl_cnt;
38 uint8_t *linevalues;
39 char *header;
40};
41
42
afc8e4de 43static void flush_linebufs(struct context *ctx, char *outbuf)
a1bb33af
UH
44{
45 static int max_probename_len = 0;
46 int len, i;
47
48 if(ctx->linebuf[0] == 0)
49 return;
50
51 if(max_probename_len == 0) {
52 /* first time through */
53 for(i = 0; ctx->probelist[i]; i++) {
54 len = strlen(ctx->probelist[i]);
55 if(len > max_probename_len)
56 max_probename_len = len;
57 }
58 }
59
60 for(i = 0; ctx->probelist[i]; i++) {
61 sprintf(outbuf + strlen(outbuf), "%*s:%s\n", max_probename_len, ctx->probelist[i],
62 ctx->linebuf + i * ctx->linebuf_len);
63 }
64 memset(ctx->linebuf, 0, i * ctx->linebuf_len);
65
66}
67
68
5a8fda15 69static int init(struct output *o, int default_spl)
a1bb33af
UH
70{
71 struct context *ctx;
72 struct probe *probe;
73 GSList *l;
74 uint64_t samplerate;
75 int num_probes;
25e7d9b1 76 char *samplerate_s;
a1bb33af
UH
77
78 ctx = malloc(sizeof(struct context));
79 o->internal = ctx;
80 ctx->num_enabled_probes = 0;
81 for(l = o->device->probes; l; l = l->next) {
82 probe = l->data;
83 if(probe->enabled)
84 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
85 }
86 ctx->probelist[ctx->num_enabled_probes] = 0;
87 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
88 ctx->line_offset = 0;
89 ctx->spl_cnt = 0;
90 if(o->param && o->param[0])
91 ctx->samples_per_line = strtoul(o->param, NULL, 10);
92 else
93 ctx->samples_per_line = default_spl;
94
95 ctx->header = malloc(512);
96 num_probes = g_slist_length(o->device->probes);
4c100f32 97 samplerate = *((uint64_t *) o->device->plugin->get_device_info(o->device->plugin_index, DI_CUR_SAMPLERATE));
a1bb33af 98 snprintf(ctx->header, 512, "Acquisition with %d/%d probes at ", ctx->num_enabled_probes, num_probes);
25e7d9b1
UH
99
100 if ((samplerate_s = sigrok_samplerate_string(samplerate)) == NULL)
101 return -1; // FIXME
102 snprintf(ctx->header + strlen(ctx->header), 512, "%s\n", samplerate_s);
103 free(samplerate_s);
a1bb33af
UH
104
105 ctx->linebuf_len = ctx->samples_per_line * 2;
106 ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len);
107 ctx->linevalues = calloc(1, num_probes);
108
5a8fda15 109 return 0;
a1bb33af
UH
110}
111
112
113static int event(struct output *o, int event_type, char **data_out, uint64_t *length_out)
114{
115 struct context *ctx;
116 int outsize;
117 char *outbuf;
118
119 ctx = o->internal;
120 switch(event_type) {
121 case DF_TRIGGER:
122 break;
123 case DF_END:
124 outsize = ctx->num_enabled_probes * (ctx->samples_per_line + 20) + 512;
125 outbuf = calloc(1, outsize);
afc8e4de 126 flush_linebufs(ctx, outbuf);
a1bb33af
UH
127 *data_out = outbuf;
128 *length_out = strlen(outbuf);
129 free(o->internal);
130 o->internal = NULL;
131 break;
132 }
133
134 return SIGROK_OK;
135}
136
137
02076d69 138static int init_bits(struct output *o)
a1bb33af
UH
139{
140
02076d69 141 return init(o, DEFAULT_BPL_BITS);
a1bb33af
UH
142
143}
144
145
02076d69 146static int data_bits(struct output *o, char *data_in, uint64_t length_in, char **data_out, uint64_t *length_out)
a1bb33af
UH
147{
148 struct context *ctx;
afc8e4de 149 unsigned int outsize, offset, p;
a1bb33af
UH
150 uint64_t sample;
151 char *outbuf;
152
153 ctx = o->internal;
37aea2d3 154 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes * ctx->samples_per_line + 512;
a1bb33af
UH
155 outbuf = calloc(1, outsize+1);
156 if(ctx->header) {
157 /* the header is still in here, we must be on the first data packet */
158 strncpy(outbuf, ctx->header, outsize);
159 free(ctx->header);
160 ctx->header = NULL;
161 }
162 else
163 outbuf[0] = 0;
164
d2cd86ac 165 if(length_in >= ctx->unitsize) {
a1bb33af
UH
166 for(offset = 0; offset <= length_in - ctx->unitsize; offset += ctx->unitsize) {
167 memcpy(&sample, data_in + offset, ctx->unitsize);
168 for(p = 0; p < ctx->num_enabled_probes; p++) {
169 if(sample & ((uint64_t) 1 << p))
170 ctx->linebuf[p * ctx->linebuf_len + ctx->line_offset] = '1';
171 else
172 ctx->linebuf[p * ctx->linebuf_len + ctx->line_offset] = '0';
173 }
174 ctx->line_offset++;
175 ctx->spl_cnt++;
176
177 /* space every 8th bit */
178 if((ctx->spl_cnt & 7) == 0) {
179 for(p = 0; p < ctx->num_enabled_probes; p++)
180 ctx->linebuf[p * ctx->linebuf_len + ctx->line_offset] = ' ';
181 ctx->line_offset++;
182 }
183
184 /* end of line */
185 if(ctx->spl_cnt >= ctx->samples_per_line) {
afc8e4de 186 flush_linebufs(ctx, outbuf);
a1bb33af
UH
187 ctx->line_offset = ctx->spl_cnt = 0;
188 }
189 }
190 } else
191 g_message("short buffer (length_in=%"PRIu64")", length_in);
192
193 *data_out = outbuf;
194 *length_out = strlen(outbuf);
195
196 return SIGROK_OK;
197}
198
199
5a8fda15 200static int init_hex(struct output *o)
a1bb33af
UH
201{
202
02076d69 203 return init(o, DEFAULT_BPL_BITS);
a1bb33af
UH
204
205}
206
207
208static int data_hex(struct output *o, char *data_in, uint64_t length_in, char **data_out, uint64_t *length_out)
209{
210 struct context *ctx;
afc8e4de 211 unsigned int outsize, offset, p;
a1bb33af
UH
212 uint64_t sample;
213 char *outbuf;
214
215 ctx = o->internal;
37aea2d3 216 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes * ctx->samples_per_line + 512;
a1bb33af
UH
217 outbuf = calloc(1, outsize+1);
218 if(ctx->header) {
219 /* the header is still in here, we must be on the first data packet */
220 strncpy(outbuf, ctx->header, outsize);
221 free(ctx->header);
222 ctx->header = NULL;
223 }
224 else
225 outbuf[0] = 0;
226
227 ctx->line_offset = 0;
228 for(offset = 0; offset <= length_in - ctx->unitsize; offset += ctx->unitsize) {
229 memcpy(&sample, data_in + offset, ctx->unitsize);
230 for(p = 0; p < ctx->num_enabled_probes; p++) {
231 ctx->linevalues[p] <<= 1;
232 if(sample & ((uint64_t) 1 << p))
233 ctx->linevalues[p] |= 1;
234 sprintf(ctx->linebuf + (p * ctx->linebuf_len) + ctx->line_offset, "%.2x", ctx->linevalues[p]);
235 }
236 ctx->spl_cnt++;
237
238 /* space after every complete hex byte */
239 if((ctx->spl_cnt & 7) == 0) {
240 for(p = 0; p < ctx->num_enabled_probes; p++)
241 ctx->linebuf[p * ctx->linebuf_len + ctx->line_offset + 2] = ' ';
242 ctx->line_offset += 3;
243 }
244
245 /* end of line */
246 if(ctx->spl_cnt >= ctx->samples_per_line) {
afc8e4de 247 flush_linebufs(ctx, outbuf);
a1bb33af
UH
248 ctx->line_offset = ctx->spl_cnt = 0;
249 }
250 }
251
252 *data_out = outbuf;
253 *length_out = strlen(outbuf);
254
255 return SIGROK_OK;
256}
257
258
259
02076d69 260struct output_format output_text_bits = {
1c5b9d30
UH
261 "bits",
262 "Text (bits)",
02076d69
UH
263 init_bits,
264 data_bits,
a1bb33af
UH
265 event
266};
267
268
269struct output_format output_text_hex = {
270 "hex",
271 "Text (hexadecimal)",
272 init_hex,
273 data_hex,
274 event
275};
276