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