]> sigrok.org Git - libsigrok.git/blame - output/output_text.c
Initial, unfinished demo/simulation hardware driver.
[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>
d2b36a10 25#include "config.h"
a1bb33af 26
02076d69
UH
27#define DEFAULT_BPL_BITS 64
28#define DEFAULT_BPL_HEX 256
a1bb33af
UH
29
30struct context {
afc8e4de 31 unsigned int num_enabled_probes;
a1bb33af 32 int samples_per_line;
afc8e4de 33 unsigned int unitsize;
a1bb33af
UH
34 int line_offset;
35 int linebuf_len;
36 char *probelist[65];
37 char *linebuf;
38 int spl_cnt;
39 uint8_t *linevalues;
40 char *header;
5045c217 41 int mark_trigger;
a1bb33af
UH
42};
43
afc8e4de 44static void flush_linebufs(struct context *ctx, char *outbuf)
a1bb33af
UH
45{
46 static int max_probename_len = 0;
47 int len, i;
48
99c1fc59 49 if (ctx->linebuf[0] == 0)
a1bb33af
UH
50 return;
51
99c1fc59
UH
52 if (max_probename_len == 0) {
53 /* First time through... */
54 for (i = 0; ctx->probelist[i]; i++) {
a1bb33af 55 len = strlen(ctx->probelist[i]);
99c1fc59 56 if (len > max_probename_len)
a1bb33af
UH
57 max_probename_len = len;
58 }
59 }
60
99c1fc59
UH
61 for (i = 0; ctx->probelist[i]; i++) {
62 sprintf(outbuf + strlen(outbuf), "%*s:%s\n", max_probename_len,
63 ctx->probelist[i], ctx->linebuf + i * ctx->linebuf_len);
a1bb33af 64 }
5045c217 65
e734b81a 66 /* Mark trigger with a ^ character. */
5045c217
HE
67 if (ctx->mark_trigger != -1)
68 sprintf(outbuf + strlen(outbuf), "T:%*s^\n",
69 ctx->mark_trigger + (ctx->mark_trigger / 8), "");
70
a1bb33af 71 memset(ctx->linebuf, 0, i * ctx->linebuf_len);
33972913 72
a1bb33af
UH
73}
74
5a8fda15 75static int init(struct output *o, int default_spl)
a1bb33af
UH
76{
77 struct context *ctx;
78 struct probe *probe;
79 GSList *l;
80 uint64_t samplerate;
81 int num_probes;
25e7d9b1 82 char *samplerate_s;
a1bb33af 83
e734b81a
UH
84 if (!(ctx = calloc(1, sizeof(struct context))))
85 return SIGROK_ERR_MALLOC;
86
a1bb33af
UH
87 o->internal = ctx;
88 ctx->num_enabled_probes = 0;
99c1fc59
UH
89
90 for (l = o->device->probes; l; l = l->next) {
a1bb33af 91 probe = l->data;
99c1fc59 92 if (probe->enabled)
a1bb33af
UH
93 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
94 }
99c1fc59 95
a1bb33af
UH
96 ctx->probelist[ctx->num_enabled_probes] = 0;
97 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
98 ctx->line_offset = 0;
99 ctx->spl_cnt = 0;
5045c217 100 ctx->mark_trigger = -1;
99c1fc59 101
ee5f5e81 102 if (o->param && o->param[0]) {
a1bb33af 103 ctx->samples_per_line = strtoul(o->param, NULL, 10);
ee5f5e81
BV
104 if (ctx->samples_per_line < 1)
105 return SIGROK_ERR;
106 } else
a1bb33af
UH
107 ctx->samples_per_line = default_spl;
108
e734b81a
UH
109 if (!(ctx->header = malloc(512))) {
110 free(ctx);
111 return SIGROK_ERR_MALLOC;
112 }
113
d2b36a10 114 snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
7aae7462 115 if (o->device->plugin) {
7aae7462
BV
116 num_probes = g_slist_length(o->device->probes);
117 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
118 o->device->plugin_index, DI_CUR_SAMPLERATE));
e734b81a
UH
119 if (!(samplerate_s = sigrok_samplerate_string(samplerate))) {
120 free(ctx->header);
121 free(ctx);
d2b36a10 122 return SIGROK_ERR;
e734b81a
UH
123 }
124 snprintf(ctx->header + strlen(ctx->header),
125 511 - strlen(ctx->header),
126 "Acquisition with %d/%d probes at %s\n",
127 ctx->num_enabled_probes, num_probes, samplerate_s);
7aae7462 128 free(samplerate_s);
7aae7462 129 }
a1bb33af 130
ee5f5e81 131 ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
e734b81a
UH
132 if (!(ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len))) {
133 free(ctx->header);
134 free(ctx);
135 return SIGROK_ERR_MALLOC;
136 }
137 if (!(ctx->linevalues = calloc(1, num_probes))) {
138 free(ctx->header);
139 free(ctx);
140 return SIGROK_ERR_MALLOC;
141 }
a1bb33af 142
e734b81a 143 return SIGROK_OK;
a1bb33af
UH
144}
145
99c1fc59
UH
146static int event(struct output *o, int event_type, char **data_out,
147 uint64_t *length_out)
a1bb33af
UH
148{
149 struct context *ctx;
150 int outsize;
151 char *outbuf;
152
153 ctx = o->internal;
99c1fc59 154 switch (event_type) {
a1bb33af 155 case DF_TRIGGER:
5045c217 156 ctx->mark_trigger = ctx->spl_cnt;
a1bb33af
UH
157 break;
158 case DF_END:
99c1fc59
UH
159 outsize = ctx->num_enabled_probes
160 * (ctx->samples_per_line + 20) + 512;
e734b81a
UH
161 if (!(outbuf = calloc(1, outsize)))
162 return SIGROK_ERR_MALLOC;
afc8e4de 163 flush_linebufs(ctx, outbuf);
a1bb33af
UH
164 *data_out = outbuf;
165 *length_out = strlen(outbuf);
166 free(o->internal);
167 o->internal = NULL;
168 break;
169 }
170
171 return SIGROK_OK;
172}
173
02076d69 174static int init_bits(struct output *o)
a1bb33af 175{
02076d69 176 return init(o, DEFAULT_BPL_BITS);
a1bb33af
UH
177}
178
99c1fc59
UH
179static int data_bits(struct output *o, char *data_in, uint64_t length_in,
180 char **data_out, uint64_t *length_out)
a1bb33af
UH
181{
182 struct context *ctx;
afc8e4de 183 unsigned int outsize, offset, p;
33972913 184 int max_linelen;
a1bb33af
UH
185 uint64_t sample;
186 char *outbuf;
187
188 ctx = o->internal;
33972913
BV
189 max_linelen = MAX_PROBENAME_LEN + 3 + ctx->samples_per_line + ctx->samples_per_line / 8;
190 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes / ctx->samples_per_line * max_linelen + 512;
e734b81a
UH
191
192 if (!(outbuf = calloc(1, outsize + 1)))
193 return SIGROK_ERR_MALLOC;
194
195 outbuf[0] = '\0';
99c1fc59
UH
196 if (ctx->header) {
197 /* The header is still here, this must be the first packet. */
a1bb33af
UH
198 strncpy(outbuf, ctx->header, outsize);
199 free(ctx->header);
200 ctx->header = NULL;
e734b81a 201 }
a1bb33af 202
99c1fc59
UH
203 if (length_in >= ctx->unitsize) {
204 for (offset = 0; offset <= length_in - ctx->unitsize;
205 offset += ctx->unitsize) {
a1bb33af 206 memcpy(&sample, data_in + offset, ctx->unitsize);
99c1fc59
UH
207 for (p = 0; p < ctx->num_enabled_probes; p++) {
208 if (sample & ((uint64_t) 1 << p))
209 ctx->linebuf[p * ctx->linebuf_len +
210 ctx->line_offset] = '1';
a1bb33af 211 else
99c1fc59
UH
212 ctx->linebuf[p * ctx->linebuf_len +
213 ctx->line_offset] = '0';
a1bb33af
UH
214 }
215 ctx->line_offset++;
216 ctx->spl_cnt++;
217
99c1fc59
UH
218 /* Add a space every 8th bit. */
219 if ((ctx->spl_cnt & 7) == 0) {
220 for (p = 0; p < ctx->num_enabled_probes; p++)
221 ctx->linebuf[p * ctx->linebuf_len +
222 ctx->line_offset] = ' ';
a1bb33af
UH
223 ctx->line_offset++;
224 }
225
99c1fc59
UH
226 /* End of line. */
227 if (ctx->spl_cnt >= ctx->samples_per_line) {
afc8e4de 228 flush_linebufs(ctx, outbuf);
a1bb33af 229 ctx->line_offset = ctx->spl_cnt = 0;
5045c217 230 ctx->mark_trigger = -1;
a1bb33af
UH
231 }
232 }
e734b81a 233 } else {
99c1fc59 234 g_message("short buffer (length_in=%" PRIu64 ")", length_in);
e734b81a 235 }
a1bb33af
UH
236
237 *data_out = outbuf;
238 *length_out = strlen(outbuf);
239
240 return SIGROK_OK;
241}
242
5a8fda15 243static int init_hex(struct output *o)
a1bb33af 244{
02076d69 245 return init(o, DEFAULT_BPL_BITS);
a1bb33af
UH
246}
247
99c1fc59
UH
248static int data_hex(struct output *o, char *data_in, uint64_t length_in,
249 char **data_out, uint64_t *length_out)
a1bb33af
UH
250{
251 struct context *ctx;
afc8e4de 252 unsigned int outsize, offset, p;
33972913 253 int max_linelen;
a1bb33af
UH
254 uint64_t sample;
255 char *outbuf;
256
257 ctx = o->internal;
33972913
BV
258 max_linelen = MAX_PROBENAME_LEN + 3 + ctx->samples_per_line + ctx->samples_per_line / 2;
259 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes / ctx->samples_per_line * max_linelen + 512;
e734b81a
UH
260
261 if (!(outbuf = calloc(1, outsize + 1)))
262 return SIGROK_ERR_MALLOC;
263
264 outbuf[0] = '\0';
99c1fc59
UH
265 if (ctx->header) {
266 /* The header is still here, this must be the first packet. */
a1bb33af
UH
267 strncpy(outbuf, ctx->header, outsize);
268 free(ctx->header);
269 ctx->header = NULL;
e734b81a 270 }
a1bb33af
UH
271
272 ctx->line_offset = 0;
99c1fc59
UH
273 for (offset = 0; offset <= length_in - ctx->unitsize;
274 offset += ctx->unitsize) {
a1bb33af 275 memcpy(&sample, data_in + offset, ctx->unitsize);
99c1fc59 276 for (p = 0; p < ctx->num_enabled_probes; p++) {
a1bb33af 277 ctx->linevalues[p] <<= 1;
99c1fc59
UH
278 if (sample & ((uint64_t) 1 << p))
279 ctx->linevalues[p] |= 1;
280 sprintf(ctx->linebuf + (p * ctx->linebuf_len) +
281 ctx->line_offset, "%.2x", ctx->linevalues[p]);
a1bb33af
UH
282 }
283 ctx->spl_cnt++;
284
99c1fc59
UH
285 /* Add a space after every complete hex byte. */
286 if ((ctx->spl_cnt & 7) == 0) {
287 for (p = 0; p < ctx->num_enabled_probes; p++)
288 ctx->linebuf[p * ctx->linebuf_len +
289 ctx->line_offset + 2] = ' ';
a1bb33af
UH
290 ctx->line_offset += 3;
291 }
292
99c1fc59
UH
293 /* End of line. */
294 if (ctx->spl_cnt >= ctx->samples_per_line) {
afc8e4de 295 flush_linebufs(ctx, outbuf);
a1bb33af
UH
296 ctx->line_offset = ctx->spl_cnt = 0;
297 }
298 }
299
300 *data_out = outbuf;
301 *length_out = strlen(outbuf);
302
303 return SIGROK_OK;
304}
305
02076d69 306struct output_format output_text_bits = {
1c5b9d30
UH
307 "bits",
308 "Text (bits)",
02076d69
UH
309 init_bits,
310 data_bits,
99c1fc59 311 event,
a1bb33af
UH
312};
313
a1bb33af
UH
314struct output_format output_text_hex = {
315 "hex",
316 "Text (hexadecimal)",
317 init_hex,
318 data_hex,
99c1fc59 319 event,
a1bb33af 320};