]>
Commit | Line | Data |
---|---|---|
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> | |
24 | #include "sigrok.h" | |
25 | ||
26 | #define DEFAULT_BPL_BIN 64 | |
27 | #define DEFAULT_BPL_HEX 256 | |
28 | ||
29 | struct context { | |
30 | int num_enabled_probes; | |
31 | int samples_per_line; | |
32 | int unitsize; | |
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 | ||
43 | static void flush_linebufs(struct context *ctx, GSList *probes, char *outbuf) | |
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 | ||
69 | static int init(struct output *o, int default_spl) | |
70 | { | |
71 | struct context *ctx; | |
72 | struct probe *probe; | |
73 | GSList *l; | |
74 | uint64_t samplerate; | |
75 | int num_probes; | |
76 | ||
77 | ctx = malloc(sizeof(struct context)); | |
78 | o->internal = ctx; | |
79 | ctx->num_enabled_probes = 0; | |
80 | for(l = o->device->probes; l; l = l->next) { | |
81 | probe = l->data; | |
82 | if(probe->enabled) | |
83 | ctx->probelist[ctx->num_enabled_probes++] = probe->name; | |
84 | } | |
85 | ctx->probelist[ctx->num_enabled_probes] = 0; | |
86 | ctx->unitsize = (ctx->num_enabled_probes + 7) / 8; | |
87 | ctx->line_offset = 0; | |
88 | ctx->spl_cnt = 0; | |
89 | if(o->param && o->param[0]) | |
90 | ctx->samples_per_line = strtoul(o->param, NULL, 10); | |
91 | else | |
92 | ctx->samples_per_line = default_spl; | |
93 | ||
94 | ctx->header = malloc(512); | |
95 | num_probes = g_slist_length(o->device->probes); | |
96 | samplerate = *((uint64_t *) o->device->plugin->get_device_info(o->device->plugin_index, DI_CUR_SAMPLERATE)); | |
97 | snprintf(ctx->header, 512, "Acquisition with %d/%d probes at ", ctx->num_enabled_probes, num_probes); | |
98 | if(samplerate >= GHZ(1)) | |
99 | snprintf(ctx->header + strlen(ctx->header), 512, "%"PRIu64" GHz", samplerate / 1000000000); | |
100 | else if(samplerate >= MHZ(1)) | |
101 | snprintf(ctx->header + strlen(ctx->header), 512, "%"PRIu64" MHz", samplerate / 1000000); | |
102 | else if(samplerate >= KHZ(1)) | |
103 | snprintf(ctx->header + strlen(ctx->header), 512, "%"PRIu64" KHz", samplerate / 1000); | |
104 | else | |
105 | snprintf(ctx->header + strlen(ctx->header), 512, "%"PRIu64" Hz", samplerate); | |
106 | snprintf(ctx->header + strlen(ctx->header), 512, "\n"); | |
107 | ||
108 | ctx->linebuf_len = ctx->samples_per_line * 2; | |
109 | ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len); | |
110 | ctx->linevalues = calloc(1, num_probes); | |
111 | ||
112 | return 0; | |
113 | } | |
114 | ||
115 | ||
116 | static int event(struct output *o, int event_type, char **data_out, uint64_t *length_out) | |
117 | { | |
118 | struct context *ctx; | |
119 | int outsize; | |
120 | char *outbuf; | |
121 | ||
122 | ctx = o->internal; | |
123 | switch(event_type) { | |
124 | case DF_TRIGGER: | |
125 | break; | |
126 | case DF_END: | |
127 | outsize = ctx->num_enabled_probes * (ctx->samples_per_line + 20) + 512; | |
128 | outbuf = calloc(1, outsize); | |
129 | flush_linebufs(ctx, o->device->probes, outbuf); | |
130 | *data_out = outbuf; | |
131 | *length_out = strlen(outbuf); | |
132 | free(o->internal); | |
133 | o->internal = NULL; | |
134 | break; | |
135 | } | |
136 | ||
137 | return SIGROK_OK; | |
138 | } | |
139 | ||
140 | ||
141 | static int init_binary(struct output *o) | |
142 | { | |
143 | ||
144 | return init(o, DEFAULT_BPL_BIN); | |
145 | ||
146 | } | |
147 | ||
148 | ||
149 | static int data_binary(struct output *o, char *data_in, uint64_t length_in, char **data_out, uint64_t *length_out) | |
150 | { | |
151 | struct context *ctx; | |
152 | int outsize, offset, p; | |
153 | uint64_t sample; | |
154 | char *outbuf; | |
155 | ||
156 | ctx = o->internal; | |
157 | outsize = length_in / ctx->unitsize * ctx->num_enabled_probes * 3 + 512; | |
158 | outbuf = calloc(1, outsize+1); | |
159 | if(ctx->header) { | |
160 | /* the header is still in here, we must be on the first data packet */ | |
161 | strncpy(outbuf, ctx->header, outsize); | |
162 | free(ctx->header); | |
163 | ctx->header = NULL; | |
164 | } | |
165 | else | |
166 | outbuf[0] = 0; | |
167 | ||
168 | if(length_in > ctx->unitsize) { | |
169 | for(offset = 0; offset <= length_in - ctx->unitsize; offset += ctx->unitsize) { | |
170 | memcpy(&sample, data_in + offset, ctx->unitsize); | |
171 | for(p = 0; p < ctx->num_enabled_probes; p++) { | |
172 | if(sample & ((uint64_t) 1 << p)) | |
173 | ctx->linebuf[p * ctx->linebuf_len + ctx->line_offset] = '1'; | |
174 | else | |
175 | ctx->linebuf[p * ctx->linebuf_len + ctx->line_offset] = '0'; | |
176 | } | |
177 | ctx->line_offset++; | |
178 | ctx->spl_cnt++; | |
179 | ||
180 | /* space every 8th bit */ | |
181 | if((ctx->spl_cnt & 7) == 0) { | |
182 | for(p = 0; p < ctx->num_enabled_probes; p++) | |
183 | ctx->linebuf[p * ctx->linebuf_len + ctx->line_offset] = ' '; | |
184 | ctx->line_offset++; | |
185 | } | |
186 | ||
187 | /* end of line */ | |
188 | if(ctx->spl_cnt >= ctx->samples_per_line) { | |
189 | flush_linebufs(ctx, o->device->probes, outbuf); | |
190 | ctx->line_offset = ctx->spl_cnt = 0; | |
191 | } | |
192 | } | |
193 | } else | |
194 | g_message("short buffer (length_in=%"PRIu64")", length_in); | |
195 | ||
196 | *data_out = outbuf; | |
197 | *length_out = strlen(outbuf); | |
198 | ||
199 | return SIGROK_OK; | |
200 | } | |
201 | ||
202 | ||
203 | static int init_hex(struct output *o) | |
204 | { | |
205 | ||
206 | return init(o, DEFAULT_BPL_BIN); | |
207 | ||
208 | } | |
209 | ||
210 | ||
211 | static int data_hex(struct output *o, char *data_in, uint64_t length_in, char **data_out, uint64_t *length_out) | |
212 | { | |
213 | struct context *ctx; | |
214 | int outsize, offset, p; | |
215 | uint64_t sample; | |
216 | char *outbuf; | |
217 | ||
218 | ctx = o->internal; | |
219 | outsize = length_in / ctx->unitsize * ctx->num_enabled_probes * 3 + 512; | |
220 | outbuf = calloc(1, outsize+1); | |
221 | if(ctx->header) { | |
222 | /* the header is still in here, we must be on the first data packet */ | |
223 | strncpy(outbuf, ctx->header, outsize); | |
224 | free(ctx->header); | |
225 | ctx->header = NULL; | |
226 | } | |
227 | else | |
228 | outbuf[0] = 0; | |
229 | ||
230 | ctx->line_offset = 0; | |
231 | for(offset = 0; offset <= length_in - ctx->unitsize; offset += ctx->unitsize) { | |
232 | memcpy(&sample, data_in + offset, ctx->unitsize); | |
233 | for(p = 0; p < ctx->num_enabled_probes; p++) { | |
234 | ctx->linevalues[p] <<= 1; | |
235 | if(sample & ((uint64_t) 1 << p)) | |
236 | ctx->linevalues[p] |= 1; | |
237 | sprintf(ctx->linebuf + (p * ctx->linebuf_len) + ctx->line_offset, "%.2x", ctx->linevalues[p]); | |
238 | } | |
239 | ctx->spl_cnt++; | |
240 | ||
241 | /* space after every complete hex byte */ | |
242 | if((ctx->spl_cnt & 7) == 0) { | |
243 | for(p = 0; p < ctx->num_enabled_probes; p++) | |
244 | ctx->linebuf[p * ctx->linebuf_len + ctx->line_offset + 2] = ' '; | |
245 | ctx->line_offset += 3; | |
246 | } | |
247 | ||
248 | /* end of line */ | |
249 | if(ctx->spl_cnt >= ctx->samples_per_line) { | |
250 | flush_linebufs(ctx, o->device->probes, outbuf); | |
251 | ctx->line_offset = ctx->spl_cnt = 0; | |
252 | } | |
253 | } | |
254 | ||
255 | *data_out = outbuf; | |
256 | *length_out = strlen(outbuf); | |
257 | ||
258 | return SIGROK_OK; | |
259 | } | |
260 | ||
261 | ||
262 | ||
263 | struct output_format output_text_binary = { | |
264 | "bin", | |
265 | "Text (binary)", | |
266 | init_binary, | |
267 | data_binary, | |
268 | event | |
269 | }; | |
270 | ||
271 | ||
272 | struct output_format output_text_hex = { | |
273 | "hex", | |
274 | "Text (hexadecimal)", | |
275 | init_hex, | |
276 | data_hex, | |
277 | event | |
278 | }; | |
279 |