2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
26 #define DEFAULT_BPL_BIN 64
27 #define DEFAULT_BPL_HEX 256
30 int num_enabled_probes;
43 static void flush_linebufs(struct context *ctx, GSList *probes, char *outbuf)
45 static int max_probename_len = 0;
48 if(ctx->linebuf[0] == 0)
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;
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);
64 memset(ctx->linebuf, 0, i * ctx->linebuf_len);
69 static int init(struct output *o, int default_spl)
77 ctx = malloc(sizeof(struct context));
79 ctx->num_enabled_probes = 0;
80 for(l = o->device->probes; l; l = l->next) {
83 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
85 ctx->probelist[ctx->num_enabled_probes] = 0;
86 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
89 if(o->param && o->param[0])
90 ctx->samples_per_line = strtoul(o->param, NULL, 10);
92 ctx->samples_per_line = default_spl;
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);
105 snprintf(ctx->header + strlen(ctx->header), 512, "%"PRIu64" Hz", samplerate);
106 snprintf(ctx->header + strlen(ctx->header), 512, "\n");
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);
116 static int event(struct output *o, int event_type, char **data_out, uint64_t *length_out)
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);
131 *length_out = strlen(outbuf);
141 static int init_binary(struct output *o)
144 return init(o, DEFAULT_BPL_BIN);
149 static int data_binary(struct output *o, char *data_in, uint64_t length_in, char **data_out, uint64_t *length_out)
152 int outsize, offset, p;
157 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes * 3 + 512;
158 outbuf = calloc(1, outsize+1);
160 /* the header is still in here, we must be on the first data packet */
161 strncpy(outbuf, ctx->header, outsize);
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';
175 ctx->linebuf[p * ctx->linebuf_len + ctx->line_offset] = '0';
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] = ' ';
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;
194 g_message("short buffer (length_in=%"PRIu64")", length_in);
197 *length_out = strlen(outbuf);
203 static int init_hex(struct output *o)
206 return init(o, DEFAULT_BPL_BIN);
211 static int data_hex(struct output *o, char *data_in, uint64_t length_in, char **data_out, uint64_t *length_out)
214 int outsize, offset, p;
219 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes * 3 + 512;
220 outbuf = calloc(1, outsize+1);
222 /* the header is still in here, we must be on the first data packet */
223 strncpy(outbuf, ctx->header, outsize);
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]);
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;
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;
256 *length_out = strlen(outbuf);
263 struct output_format output_text_binary = {
272 struct output_format output_text_hex = {
274 "Text (hexadecimal)",