]>
Commit | Line | Data |
---|---|---|
97554432 BV |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2011 Bert Vermeulen <bert@biot.com> | |
5 | * Copyright (C) 2011 HÃ¥vard Espeland <gus@ping.uio.no> | |
6 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | #include <glib.h> | |
24 | #include <sigrok.h> | |
b08024a8 | 25 | #include <sigrok-internal.h> |
97554432 BV |
26 | #include "text.h" |
27 | ||
f50f3f40 | 28 | int init_ascii(struct sr_output *o) |
97554432 BV |
29 | { |
30 | return init(o, DEFAULT_BPL_ASCII, MODE_ASCII); | |
31 | } | |
32 | ||
54ac5277 UH |
33 | int data_ascii(struct sr_output *o, const char *data_in, uint64_t length_in, |
34 | char **data_out, uint64_t *length_out) | |
97554432 BV |
35 | { |
36 | struct context *ctx; | |
37 | unsigned int outsize, offset, p; | |
38 | int max_linelen; | |
39 | uint64_t sample; | |
40 | char *outbuf; | |
41 | ||
42 | ctx = o->internal; | |
9688b443 | 43 | max_linelen = SR_MAX_PROBENAME_LEN + 3 + ctx->samples_per_line |
97554432 BV |
44 | + ctx->samples_per_line / 8; |
45 | /* | |
46 | * Calculate space needed for probes. Set aside 512 bytes for | |
47 | * extra output, e.g. trigger. | |
48 | */ | |
49 | outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line) | |
50 | * (ctx->num_enabled_probes * max_linelen); | |
51 | ||
52 | if (!(outbuf = calloc(1, outsize + 1))) | |
e46b8fb1 | 53 | return SR_ERR_MALLOC; |
97554432 BV |
54 | |
55 | outbuf[0] = '\0'; | |
56 | if (ctx->header) { | |
57 | /* The header is still here, this must be the first packet. */ | |
58 | strncpy(outbuf, ctx->header, outsize); | |
59 | free(ctx->header); | |
60 | ctx->header = NULL; | |
61 | } | |
62 | ||
63 | if (length_in >= ctx->unitsize) { | |
64 | for (offset = 0; offset <= length_in - ctx->unitsize; | |
65 | offset += ctx->unitsize) { | |
66 | memcpy(&sample, data_in + offset, ctx->unitsize); | |
67 | ||
68 | char tmpval[ctx->num_enabled_probes]; | |
69 | ||
70 | for (p = 0; p < ctx->num_enabled_probes; p++) { | |
71 | uint64_t curbit = (sample & ((uint64_t) 1 << p)); | |
72 | uint64_t prevbit = (ctx->prevsample & | |
73 | ((uint64_t) 1 << p)); | |
74 | ||
75 | if (curbit < prevbit && ctx->line_offset > 0) { | |
76 | ctx->linebuf[p * ctx->linebuf_len + | |
77 | ctx->line_offset-1] = '\\'; | |
78 | } | |
79 | ||
80 | if (curbit > prevbit) { | |
81 | tmpval[p] = '/'; | |
82 | } else { | |
83 | if (curbit) | |
84 | tmpval[p] = '"'; | |
85 | else | |
86 | tmpval[p] = '.'; | |
87 | } | |
88 | } | |
89 | ||
90 | /* End of line. */ | |
91 | if (ctx->spl_cnt >= ctx->samples_per_line) { | |
92 | flush_linebufs(ctx, outbuf); | |
93 | ctx->line_offset = ctx->spl_cnt = 0; | |
94 | ctx->mark_trigger = -1; | |
95 | } | |
96 | ||
97 | for (p = 0; p < ctx->num_enabled_probes; p++) { | |
98 | ctx->linebuf[p * ctx->linebuf_len + | |
99 | ctx->line_offset] = tmpval[p]; | |
100 | } | |
101 | ||
102 | ctx->line_offset++; | |
103 | ctx->spl_cnt++; | |
104 | ||
105 | ctx->prevsample = sample; | |
106 | } | |
107 | } else { | |
b08024a8 | 108 | sr_info("short buffer (length_in=%" PRIu64 ")", length_in); |
97554432 BV |
109 | } |
110 | ||
111 | *data_out = outbuf; | |
112 | *length_out = strlen(outbuf); | |
113 | ||
e46b8fb1 | 114 | return SR_OK; |
97554432 BV |
115 | } |
116 | ||
f50f3f40 | 117 | struct sr_output_format output_text_ascii = { |
cdb3573c | 118 | .id = "ascii", |
d494a4aa UH |
119 | .description = "ASCII (takes argument, default 74)", |
120 | .df_type = SR_DF_LOGIC, | |
121 | .init = init_ascii, | |
122 | .data = data_ascii, | |
123 | .event = event, | |
97554432 | 124 | }; |