]>
Commit | Line | Data |
---|---|---|
1437e893 BV |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
f3163a6c DR |
4 | * Copyright (C) 2010 Bert Vermeulen <bert@biot.com> |
5 | * Copyright (C) 2011 HÃ¥vard Espeland <gus@ping.uio.no> | |
6eb0e3ea | 6 | * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com> |
1437e893 BV |
7 | * |
8 | * This program is free software: you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation, either version 3 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
1437e893 | 22 | #include <stdlib.h> |
f3163a6c | 23 | #include <stdio.h> |
1437e893 | 24 | #include <string.h> |
f3163a6c | 25 | #include <glib.h> |
1437e893 | 26 | #include <sigrok.h> |
f3163a6c | 27 | #include "config.h" |
1437e893 | 28 | |
f3163a6c DR |
29 | #define DEFAULT_BPL_BITS 64 |
30 | #define DEFAULT_BPL_HEX 192 | |
31 | #define DEFAULT_BPL_ASCII 74 | |
1437e893 | 32 | |
f3163a6c DR |
33 | enum outputmode { |
34 | MODE_BITS = 1, | |
35 | MODE_HEX, | |
36 | MODE_ASCII, | |
37 | }; | |
1437e893 BV |
38 | |
39 | struct context { | |
1437e893 | 40 | unsigned int num_enabled_probes; |
1437e893 BV |
41 | int samples_per_line; |
42 | unsigned int unitsize; | |
43 | int line_offset; | |
44 | int linebuf_len; | |
f3163a6c | 45 | char *probelist[65]; |
1437e893 BV |
46 | char *linebuf; |
47 | int spl_cnt; | |
f3163a6c DR |
48 | uint8_t *linevalues; |
49 | char *header; | |
1437e893 | 50 | int mark_trigger; |
6eb0e3ea | 51 | // struct analog_sample *prevsample; |
f3163a6c | 52 | enum outputmode mode; |
1437e893 BV |
53 | }; |
54 | ||
1437e893 BV |
55 | static void flush_linebufs(struct context *ctx, char *outbuf) |
56 | { | |
57 | static int max_probename_len = 0; | |
58 | int len, i; | |
59 | ||
60 | if (ctx->linebuf[0] == 0) | |
61 | return; | |
62 | ||
63 | if (max_probename_len == 0) { | |
64 | /* First time through... */ | |
65 | for (i = 0; ctx->probelist[i]; i++) { | |
66 | len = strlen(ctx->probelist[i]); | |
67 | if (len > max_probename_len) | |
68 | max_probename_len = len; | |
69 | } | |
70 | } | |
71 | ||
72 | for (i = 0; ctx->probelist[i]; i++) { | |
73 | sprintf(outbuf + strlen(outbuf), "%*s:%s\n", max_probename_len, | |
74 | ctx->probelist[i], ctx->linebuf + i * ctx->linebuf_len); | |
75 | } | |
76 | ||
77 | /* Mark trigger with a ^ character. */ | |
78 | if (ctx->mark_trigger != -1) | |
f3163a6c DR |
79 | { |
80 | int space_offset = ctx->mark_trigger / 8; | |
81 | ||
82 | if (ctx->mode == MODE_ASCII) | |
83 | space_offset = 0; | |
84 | ||
1437e893 | 85 | sprintf(outbuf + strlen(outbuf), "T:%*s^\n", |
f3163a6c DR |
86 | ctx->mark_trigger + space_offset, ""); |
87 | } | |
1437e893 BV |
88 | |
89 | memset(ctx->linebuf, 0, i * ctx->linebuf_len); | |
90 | } | |
91 | ||
f50f3f40 | 92 | static int init(struct sr_output *o, int default_spl, enum outputmode mode) |
1437e893 BV |
93 | { |
94 | struct context *ctx; | |
1afe8989 | 95 | struct sr_probe *probe; |
1437e893 BV |
96 | GSList *l; |
97 | uint64_t samplerate; | |
98 | int num_probes; | |
99 | char *samplerate_s; | |
100 | ||
101 | if (!(ctx = calloc(1, sizeof(struct context)))) | |
e46b8fb1 | 102 | return SR_ERR_MALLOC; |
1437e893 | 103 | |
1437e893 | 104 | o->internal = ctx; |
1437e893 | 105 | ctx->num_enabled_probes = 0; |
f3163a6c DR |
106 | |
107 | for (l = o->device->probes; l; l = l->next) { | |
108 | probe = l->data; | |
109 | if (!probe->enabled) | |
110 | continue; | |
111 | ctx->probelist[ctx->num_enabled_probes++] = probe->name; | |
112 | } | |
113 | ||
114 | ctx->probelist[ctx->num_enabled_probes] = 0; | |
6eb0e3ea DR |
115 | ctx->unitsize = sizeof(struct analog_sample) + |
116 | (ctx->num_enabled_probes * sizeof(struct analog_probe)); | |
f3163a6c DR |
117 | ctx->line_offset = 0; |
118 | ctx->spl_cnt = 0; | |
1437e893 | 119 | ctx->mark_trigger = -1; |
f3163a6c DR |
120 | ctx->mode = mode; |
121 | ||
9b36e360 BV |
122 | if (o->param && o->param[0]) { |
123 | ctx->samples_per_line = strtoul(o->param, NULL, 10); | |
124 | if (ctx->samples_per_line < 1) | |
e46b8fb1 | 125 | return SR_ERR; |
9b36e360 | 126 | } else |
f3163a6c | 127 | ctx->samples_per_line = default_spl; |
9b36e360 | 128 | |
f3163a6c DR |
129 | if (!(ctx->header = malloc(512))) { |
130 | free(ctx); | |
e46b8fb1 | 131 | return SR_ERR_MALLOC; |
1437e893 | 132 | } |
1437e893 BV |
133 | |
134 | snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING); | |
f3163a6c | 135 | num_probes = g_slist_length(o->device->probes); |
2bf4aca6 | 136 | if (o->device->plugin && sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) { |
1437e893 | 137 | samplerate = *((uint64_t *) o->device->plugin->get_device_info( |
5a2326a7 | 138 | o->device->plugin_index, SR_DI_CUR_SAMPLERATE)); |
a00ba012 | 139 | if (!(samplerate_s = sr_samplerate_string(samplerate))) { |
1437e893 BV |
140 | free(ctx->header); |
141 | free(ctx); | |
e46b8fb1 | 142 | return SR_ERR; |
1437e893 BV |
143 | } |
144 | snprintf(ctx->header + strlen(ctx->header), | |
145 | 511 - strlen(ctx->header), | |
146 | "Acquisition with %d/%d probes at %s\n", | |
147 | ctx->num_enabled_probes, num_probes, samplerate_s); | |
148 | free(samplerate_s); | |
149 | } | |
150 | ||
f3163a6c DR |
151 | ctx->linebuf_len = ctx->samples_per_line * 2 + 4; |
152 | if (!(ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len))) { | |
153 | free(ctx->header); | |
154 | free(ctx); | |
e46b8fb1 | 155 | return SR_ERR_MALLOC; |
f3163a6c DR |
156 | } |
157 | if (!(ctx->linevalues = calloc(1, num_probes))) { | |
1437e893 BV |
158 | free(ctx->header); |
159 | free(ctx); | |
e46b8fb1 | 160 | return SR_ERR_MALLOC; |
1437e893 BV |
161 | } |
162 | ||
e46b8fb1 | 163 | return SR_OK; |
1437e893 BV |
164 | } |
165 | ||
f50f3f40 | 166 | static int event(struct sr_output *o, int event_type, char **data_out, |
f3163a6c | 167 | uint64_t *length_out) |
1437e893 BV |
168 | { |
169 | struct context *ctx; | |
f3163a6c DR |
170 | int outsize; |
171 | char *outbuf; | |
1437e893 BV |
172 | |
173 | ctx = o->internal; | |
f3163a6c | 174 | switch (event_type) { |
5a2326a7 | 175 | case SR_DF_TRIGGER: |
f3163a6c DR |
176 | ctx->mark_trigger = ctx->spl_cnt; |
177 | *data_out = NULL; | |
178 | *length_out = 0; | |
179 | break; | |
5a2326a7 | 180 | case SR_DF_END: |
f3163a6c DR |
181 | outsize = ctx->num_enabled_probes |
182 | * (ctx->samples_per_line + 20) + 512; | |
183 | if (!(outbuf = calloc(1, outsize))) | |
e46b8fb1 | 184 | return SR_ERR_MALLOC; |
f3163a6c DR |
185 | flush_linebufs(ctx, outbuf); |
186 | *data_out = outbuf; | |
187 | *length_out = strlen(outbuf); | |
188 | free(o->internal); | |
189 | o->internal = NULL; | |
190 | break; | |
191 | default: | |
192 | *data_out = NULL; | |
193 | *length_out = 0; | |
194 | break; | |
195 | } | |
196 | ||
e46b8fb1 | 197 | return SR_OK; |
f3163a6c DR |
198 | } |
199 | ||
f50f3f40 | 200 | static int init_bits(struct sr_output *o) |
f3163a6c DR |
201 | { |
202 | return init(o, DEFAULT_BPL_BITS, MODE_BITS); | |
203 | } | |
204 | ||
f50f3f40 | 205 | static int data_bits(struct sr_output *o, char *data_in, uint64_t length_in, |
f3163a6c DR |
206 | char **data_out, uint64_t *length_out) |
207 | { | |
208 | struct context *ctx; | |
209 | unsigned int outsize, offset, p; | |
210 | int max_linelen; | |
6eb0e3ea | 211 | struct analog_sample *sample; |
f3163a6c DR |
212 | char *outbuf, c; |
213 | ||
214 | ctx = o->internal; | |
215 | max_linelen = MAX_PROBENAME_LEN + 3 + ctx->samples_per_line | |
1437e893 | 216 | + ctx->samples_per_line / 8; |
f3163a6c DR |
217 | /* |
218 | * Calculate space needed for probes. Set aside 512 bytes for | |
219 | * extra output, e.g. trigger. | |
220 | */ | |
221 | outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line) | |
222 | * (ctx->num_enabled_probes * max_linelen); | |
223 | ||
1437e893 | 224 | if (!(outbuf = calloc(1, outsize + 1))) |
e46b8fb1 | 225 | return SR_ERR_MALLOC; |
1437e893 BV |
226 | |
227 | outbuf[0] = '\0'; | |
228 | if (ctx->header) { | |
229 | /* The header is still here, this must be the first packet. */ | |
230 | strncpy(outbuf, ctx->header, outsize); | |
231 | free(ctx->header); | |
232 | ctx->header = NULL; | |
f3163a6c DR |
233 | |
234 | /* Ensure first transition. */ | |
6eb0e3ea DR |
235 | // memcpy(&ctx->prevsample, data_in, ctx->unitsize); |
236 | // ctx->prevsample = ~ctx->prevsample; | |
1437e893 BV |
237 | } |
238 | ||
239 | if (length_in >= ctx->unitsize) { | |
240 | for (offset = 0; offset <= length_in - ctx->unitsize; | |
241 | offset += ctx->unitsize) { | |
6eb0e3ea | 242 | sample = (struct analog_sample *) (data_in + offset); |
1437e893 | 243 | for (p = 0; p < ctx->num_enabled_probes; p++) { |
6eb0e3ea DR |
244 | int val = sample->probes[p].val; |
245 | int res = sample->probes[p].res; | |
246 | if (res == 1) | |
247 | c = '0' + (val & ((1 << res) - 1)); | |
248 | else | |
249 | /* | |
250 | * Scale analog resolution down so it | |
251 | * fits 25 letters | |
252 | */ | |
253 | c = 'A' + (((val & ((1 << res) - 1)) / | |
254 | (res * res)) / 10); | |
f3163a6c DR |
255 | ctx->linebuf[p * ctx->linebuf_len + |
256 | ctx->line_offset] = c; | |
257 | } | |
258 | ctx->line_offset++; | |
259 | ctx->spl_cnt++; | |
260 | ||
261 | /* Add a space every 8th bit. */ | |
262 | if ((ctx->spl_cnt & 7) == 0) { | |
263 | for (p = 0; p < ctx->num_enabled_probes; p++) | |
264 | ctx->linebuf[p * ctx->linebuf_len + | |
265 | ctx->line_offset] = ' '; | |
266 | ctx->line_offset++; | |
1437e893 BV |
267 | } |
268 | ||
269 | /* End of line. */ | |
270 | if (ctx->spl_cnt >= ctx->samples_per_line) { | |
271 | flush_linebufs(ctx, outbuf); | |
272 | ctx->line_offset = ctx->spl_cnt = 0; | |
273 | ctx->mark_trigger = -1; | |
274 | } | |
1437e893 BV |
275 | } |
276 | } else { | |
277 | g_message("short buffer (length_in=%" PRIu64 ")", length_in); | |
278 | } | |
279 | ||
280 | *data_out = outbuf; | |
281 | *length_out = strlen(outbuf); | |
282 | ||
e46b8fb1 | 283 | return SR_OK; |
1437e893 | 284 | } |
6eb0e3ea | 285 | #if 0 |
f50f3f40 | 286 | static int init_hex(struct sr_output *o) |
f3163a6c DR |
287 | { |
288 | return init(o, DEFAULT_BPL_HEX, MODE_HEX); | |
289 | } | |
290 | ||
f50f3f40 | 291 | static int data_hex(struct sr_output *o, char *data_in, uint64_t length_in, |
f3163a6c | 292 | char **data_out, uint64_t *length_out) |
1437e893 BV |
293 | { |
294 | struct context *ctx; | |
f3163a6c DR |
295 | unsigned int outsize, offset, p; |
296 | int max_linelen; | |
297 | uint64_t sample; | |
298 | char *outbuf; | |
1437e893 BV |
299 | |
300 | ctx = o->internal; | |
f3163a6c DR |
301 | max_linelen = MAX_PROBENAME_LEN + 3 + ctx->samples_per_line |
302 | + ctx->samples_per_line / 2; | |
303 | outsize = length_in / ctx->unitsize * ctx->num_enabled_probes | |
304 | / ctx->samples_per_line * max_linelen + 512; | |
305 | ||
306 | if (!(outbuf = calloc(1, outsize + 1))) | |
e46b8fb1 | 307 | return SR_ERR_MALLOC; |
f3163a6c DR |
308 | |
309 | outbuf[0] = '\0'; | |
310 | if (ctx->header) { | |
311 | /* The header is still here, this must be the first packet. */ | |
312 | strncpy(outbuf, ctx->header, outsize); | |
313 | free(ctx->header); | |
314 | ctx->header = NULL; | |
1437e893 BV |
315 | } |
316 | ||
f3163a6c DR |
317 | ctx->line_offset = 0; |
318 | for (offset = 0; offset <= length_in - ctx->unitsize; | |
319 | offset += ctx->unitsize) { | |
320 | memcpy(&sample, data_in + offset, ctx->unitsize); | |
321 | for (p = 0; p < ctx->num_enabled_probes; p++) { | |
322 | ctx->linevalues[p] <<= 1; | |
323 | if (sample & ((uint64_t) 1 << p)) | |
324 | ctx->linevalues[p] |= 1; | |
325 | sprintf(ctx->linebuf + (p * ctx->linebuf_len) + | |
326 | ctx->line_offset, "%.2x", ctx->linevalues[p]); | |
327 | } | |
328 | ctx->spl_cnt++; | |
329 | ||
330 | /* Add a space after every complete hex byte. */ | |
331 | if ((ctx->spl_cnt & 7) == 0) { | |
332 | for (p = 0; p < ctx->num_enabled_probes; p++) | |
333 | ctx->linebuf[p * ctx->linebuf_len + | |
334 | ctx->line_offset + 2] = ' '; | |
335 | ctx->line_offset += 3; | |
336 | } | |
337 | ||
338 | /* End of line. */ | |
339 | if (ctx->spl_cnt >= ctx->samples_per_line) { | |
340 | flush_linebufs(ctx, outbuf); | |
341 | ctx->line_offset = ctx->spl_cnt = 0; | |
342 | } | |
343 | } | |
344 | ||
345 | *data_out = outbuf; | |
346 | *length_out = strlen(outbuf); | |
1437e893 | 347 | |
e46b8fb1 | 348 | return SR_OK; |
1437e893 BV |
349 | } |
350 | ||
f50f3f40 | 351 | static int init_ascii(struct sr_output *o) |
f3163a6c DR |
352 | { |
353 | return init(o, DEFAULT_BPL_ASCII, MODE_ASCII); | |
354 | } | |
355 | ||
f50f3f40 | 356 | static int data_ascii(struct sr_output *o, char *data_in, uint64_t length_in, |
f3163a6c DR |
357 | char **data_out, uint64_t *length_out) |
358 | { | |
359 | struct context *ctx; | |
360 | unsigned int outsize, offset, p; | |
361 | int max_linelen; | |
362 | uint64_t sample; | |
363 | char *outbuf; | |
364 | ||
365 | ctx = o->internal; | |
366 | max_linelen = MAX_PROBENAME_LEN + 3 + ctx->samples_per_line | |
367 | + ctx->samples_per_line / 8; | |
368 | /* | |
369 | * Calculate space needed for probes. Set aside 512 bytes for | |
370 | * extra output, e.g. trigger. | |
371 | */ | |
372 | outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line) | |
373 | * (ctx->num_enabled_probes * max_linelen); | |
374 | ||
375 | if (!(outbuf = calloc(1, outsize + 1))) | |
e46b8fb1 | 376 | return SR_ERR_MALLOC; |
f3163a6c DR |
377 | |
378 | outbuf[0] = '\0'; | |
379 | if (ctx->header) { | |
380 | /* The header is still here, this must be the first packet. */ | |
381 | strncpy(outbuf, ctx->header, outsize); | |
382 | free(ctx->header); | |
383 | ctx->header = NULL; | |
384 | } | |
385 | ||
386 | if (length_in >= ctx->unitsize) { | |
387 | for (offset = 0; offset <= length_in - ctx->unitsize; | |
388 | offset += ctx->unitsize) { | |
389 | memcpy(&sample, data_in + offset, ctx->unitsize); | |
390 | ||
391 | char tmpval[ctx->num_enabled_probes]; | |
392 | ||
393 | for (p = 0; p < ctx->num_enabled_probes; p++) { | |
394 | uint64_t curbit = (sample & ((uint64_t) 1 << p)); | |
395 | uint64_t prevbit = (ctx->prevsample & | |
396 | ((uint64_t) 1 << p)); | |
397 | ||
398 | if (curbit < prevbit && ctx->line_offset > 0) { | |
399 | ctx->linebuf[p * ctx->linebuf_len + | |
400 | ctx->line_offset-1] = '\\'; | |
401 | } | |
402 | ||
403 | if (curbit > prevbit) { | |
404 | tmpval[p] = '/'; | |
405 | } else { | |
406 | if (curbit) | |
407 | tmpval[p] = '"'; | |
408 | else | |
409 | tmpval[p] = '.'; | |
410 | } | |
411 | } | |
412 | ||
413 | /* End of line. */ | |
414 | if (ctx->spl_cnt >= ctx->samples_per_line) { | |
415 | flush_linebufs(ctx, outbuf); | |
416 | ctx->line_offset = ctx->spl_cnt = 0; | |
417 | ctx->mark_trigger = -1; | |
418 | } | |
419 | ||
420 | for (p = 0; p < ctx->num_enabled_probes; p++) { | |
421 | ctx->linebuf[p * ctx->linebuf_len + | |
422 | ctx->line_offset] = tmpval[p]; | |
423 | } | |
424 | ||
425 | ctx->line_offset++; | |
426 | ctx->spl_cnt++; | |
427 | ||
428 | ctx->prevsample = sample; | |
429 | } | |
430 | } else { | |
431 | g_message("short buffer (length_in=%" PRIu64 ")", length_in); | |
432 | } | |
433 | ||
434 | *data_out = outbuf; | |
435 | *length_out = strlen(outbuf); | |
436 | ||
e46b8fb1 | 437 | return SR_OK; |
f3163a6c | 438 | } |
6eb0e3ea | 439 | #endif |
f3163a6c | 440 | |
f50f3f40 | 441 | struct sr_output_format output_analog_bits = { |
6eb0e3ea | 442 | "analog_bits", |
f3163a6c | 443 | "Bits (takes argument, default 64)", |
5a2326a7 | 444 | SR_DF_ANALOG, |
f3163a6c DR |
445 | init_bits, |
446 | data_bits, | |
1437e893 BV |
447 | event, |
448 | }; | |
6eb0e3ea | 449 | #if 0 |
f50f3f40 | 450 | struct sr_output_format output_analog_hex = { |
6eb0e3ea | 451 | "analog_hex", |
f3163a6c | 452 | "Hexadecimal (takes argument, default 192)", |
5a2326a7 | 453 | SR_DF_ANALOG, |
f3163a6c DR |
454 | init_hex, |
455 | data_hex, | |
456 | event, | |
457 | }; | |
458 | ||
f50f3f40 | 459 | struct sr_output_format output_analog_ascii = { |
6eb0e3ea | 460 | "analog_ascii", |
f3163a6c | 461 | "ASCII (takes argument, default 74)", |
5a2326a7 | 462 | SR_DF_ANALOG, |
f3163a6c DR |
463 | init_ascii, |
464 | data_ascii, | |
465 | event, | |
466 | }; | |
6eb0e3ea | 467 | #endif |