]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2010 Bert Vermeulen <bert@biot.com> | |
5 | * Copyright (C) 2011 HÃ¥vard Espeland <gus@ping.uio.no> | |
6 | * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com> | |
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 | ||
22 | #include <stdlib.h> | |
23 | #include <stdio.h> | |
24 | #include <string.h> | |
25 | #include <glib.h> | |
26 | #include <sigrok.h> | |
27 | #include "config.h" | |
28 | ||
29 | #define DEFAULT_BPL_BITS 64 | |
30 | #define DEFAULT_BPL_HEX 192 | |
31 | #define DEFAULT_BPL_ASCII 74 | |
32 | ||
33 | enum outputmode { | |
34 | MODE_BITS = 1, | |
35 | MODE_HEX, | |
36 | MODE_ASCII, | |
37 | }; | |
38 | ||
39 | struct context { | |
40 | unsigned int num_enabled_probes; | |
41 | int samples_per_line; | |
42 | unsigned int unitsize; | |
43 | int line_offset; | |
44 | int linebuf_len; | |
45 | char *probelist[65]; | |
46 | char *linebuf; | |
47 | int spl_cnt; | |
48 | uint8_t *linevalues; | |
49 | char *header; | |
50 | int mark_trigger; | |
51 | // struct sr_analog_sample *prevsample; | |
52 | enum outputmode mode; | |
53 | }; | |
54 | ||
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) | |
79 | { | |
80 | int space_offset = ctx->mark_trigger / 8; | |
81 | ||
82 | if (ctx->mode == MODE_ASCII) | |
83 | space_offset = 0; | |
84 | ||
85 | sprintf(outbuf + strlen(outbuf), "T:%*s^\n", | |
86 | ctx->mark_trigger + space_offset, ""); | |
87 | } | |
88 | ||
89 | memset(ctx->linebuf, 0, i * ctx->linebuf_len); | |
90 | } | |
91 | ||
92 | static int init(struct sr_output *o, int default_spl, enum outputmode mode) | |
93 | { | |
94 | struct context *ctx; | |
95 | struct sr_probe *probe; | |
96 | GSList *l; | |
97 | uint64_t samplerate; | |
98 | int num_probes; | |
99 | char *samplerate_s; | |
100 | ||
101 | if (!(ctx = calloc(1, sizeof(struct context)))) | |
102 | return SR_ERR_MALLOC; | |
103 | ||
104 | o->internal = ctx; | |
105 | ctx->num_enabled_probes = 0; | |
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; | |
115 | ctx->unitsize = sizeof(struct sr_analog_sample) + | |
116 | (ctx->num_enabled_probes * sizeof(struct sr_analog_probe)); | |
117 | ctx->line_offset = 0; | |
118 | ctx->spl_cnt = 0; | |
119 | ctx->mark_trigger = -1; | |
120 | ctx->mode = mode; | |
121 | ||
122 | if (o->param && o->param[0]) { | |
123 | ctx->samples_per_line = strtoul(o->param, NULL, 10); | |
124 | if (ctx->samples_per_line < 1) | |
125 | return SR_ERR; | |
126 | } else | |
127 | ctx->samples_per_line = default_spl; | |
128 | ||
129 | if (!(ctx->header = malloc(512))) { | |
130 | free(ctx); | |
131 | return SR_ERR_MALLOC; | |
132 | } | |
133 | ||
134 | snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING); | |
135 | num_probes = g_slist_length(o->device->probes); | |
136 | if (o->device->plugin && sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) { | |
137 | samplerate = *((uint64_t *) o->device->plugin->get_device_info( | |
138 | o->device->plugin_index, SR_DI_CUR_SAMPLERATE)); | |
139 | if (!(samplerate_s = sr_samplerate_string(samplerate))) { | |
140 | free(ctx->header); | |
141 | free(ctx); | |
142 | return SR_ERR; | |
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 | ||
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); | |
155 | return SR_ERR_MALLOC; | |
156 | } | |
157 | if (!(ctx->linevalues = calloc(1, num_probes))) { | |
158 | free(ctx->header); | |
159 | free(ctx); | |
160 | return SR_ERR_MALLOC; | |
161 | } | |
162 | ||
163 | return SR_OK; | |
164 | } | |
165 | ||
166 | static int event(struct sr_output *o, int event_type, char **data_out, | |
167 | uint64_t *length_out) | |
168 | { | |
169 | struct context *ctx; | |
170 | int outsize; | |
171 | char *outbuf; | |
172 | ||
173 | ctx = o->internal; | |
174 | switch (event_type) { | |
175 | case SR_DF_TRIGGER: | |
176 | ctx->mark_trigger = ctx->spl_cnt; | |
177 | *data_out = NULL; | |
178 | *length_out = 0; | |
179 | break; | |
180 | case SR_DF_END: | |
181 | outsize = ctx->num_enabled_probes | |
182 | * (ctx->samples_per_line + 20) + 512; | |
183 | if (!(outbuf = calloc(1, outsize))) | |
184 | return SR_ERR_MALLOC; | |
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 | ||
197 | return SR_OK; | |
198 | } | |
199 | ||
200 | static int init_bits(struct sr_output *o) | |
201 | { | |
202 | return init(o, DEFAULT_BPL_BITS, MODE_BITS); | |
203 | } | |
204 | ||
205 | static int data_bits(struct sr_output *o, const char *data_in, | |
206 | uint64_t length_in, char **data_out, uint64_t *length_out) | |
207 | { | |
208 | struct context *ctx; | |
209 | unsigned int outsize, offset, p; | |
210 | int max_linelen; | |
211 | struct sr_analog_sample *sample; | |
212 | char *outbuf, c; | |
213 | ||
214 | ctx = o->internal; | |
215 | max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line | |
216 | + ctx->samples_per_line / 8; | |
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 | ||
224 | if (!(outbuf = calloc(1, outsize + 1))) | |
225 | return SR_ERR_MALLOC; | |
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; | |
233 | ||
234 | /* Ensure first transition. */ | |
235 | // memcpy(&ctx->prevsample, data_in, ctx->unitsize); | |
236 | // ctx->prevsample = ~ctx->prevsample; | |
237 | } | |
238 | ||
239 | if (length_in >= ctx->unitsize) { | |
240 | for (offset = 0; offset <= length_in - ctx->unitsize; | |
241 | offset += ctx->unitsize) { | |
242 | sample = (struct sr_analog_sample *) (data_in + offset); | |
243 | for (p = 0; p < ctx->num_enabled_probes; p++) { | |
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); | |
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++; | |
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 | } | |
275 | } | |
276 | } else { | |
277 | sr_info("short buffer (length_in=%" PRIu64 ")", length_in); | |
278 | } | |
279 | ||
280 | *data_out = outbuf; | |
281 | *length_out = strlen(outbuf); | |
282 | ||
283 | return SR_OK; | |
284 | } | |
285 | #if 0 | |
286 | static int init_hex(struct sr_output *o) | |
287 | { | |
288 | return init(o, DEFAULT_BPL_HEX, MODE_HEX); | |
289 | } | |
290 | ||
291 | static int data_hex(struct sr_output *o, const char *data_in, | |
292 | uint64_t length_in, char **data_out, uint64_t *length_out) | |
293 | { | |
294 | struct context *ctx; | |
295 | unsigned int outsize, offset, p; | |
296 | int max_linelen; | |
297 | uint64_t sample; | |
298 | char *outbuf; | |
299 | ||
300 | ctx = o->internal; | |
301 | max_linelen = SR_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))) | |
307 | return SR_ERR_MALLOC; | |
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; | |
315 | } | |
316 | ||
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); | |
347 | ||
348 | return SR_OK; | |
349 | } | |
350 | ||
351 | static int init_ascii(struct sr_output *o) | |
352 | { | |
353 | return init(o, DEFAULT_BPL_ASCII, MODE_ASCII); | |
354 | } | |
355 | ||
356 | static int data_ascii(struct sr_output *o, const char *data_in, | |
357 | uint64_t length_in, 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 = SR_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))) | |
376 | return SR_ERR_MALLOC; | |
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 | sr_info("short buffer (length_in=%" PRIu64 ")", length_in); | |
432 | } | |
433 | ||
434 | *data_out = outbuf; | |
435 | *length_out = strlen(outbuf); | |
436 | ||
437 | return SR_OK; | |
438 | } | |
439 | #endif | |
440 | ||
441 | struct sr_output_format output_analog_bits = { | |
442 | .id = "analog_bits", | |
443 | .description = "Bits (takes argument, default 64)", | |
444 | .df_type = SR_DF_ANALOG, | |
445 | .init = init_bits, | |
446 | .data = data_bits, | |
447 | .event = event, | |
448 | }; | |
449 | #if 0 | |
450 | struct sr_output_format output_analog_hex = { | |
451 | .id = "analog_hex", | |
452 | .description = "Hexadecimal (takes argument, default 192)", | |
453 | .df_type = SR_DF_ANALOG, | |
454 | .init = init_hex, | |
455 | .data = data_hex, | |
456 | .event = event, | |
457 | }; | |
458 | ||
459 | struct sr_output_format output_analog_ascii = { | |
460 | .id = "analog_ascii", | |
461 | .description = "ASCII (takes argument, default 74)", | |
462 | .df_type = SR_DF_ANALOG, | |
463 | .init = init_ascii, | |
464 | .data = data_ascii, | |
465 | .event = event, | |
466 | }; | |
467 | #endif |