]> sigrok.org Git - libsigrok.git/blob - output/output_text.c
Start of code base layout restructuring.
[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         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 void 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_SAMPLE_RATE));
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 }
113
114
115 static int event(struct output *o, int event_type, char **data_out, uint64_t *length_out)
116 {
117         struct context *ctx;
118         int outsize;
119         char *outbuf;
120
121         ctx = o->internal;
122         switch(event_type) {
123         case DF_TRIGGER:
124                 break;
125         case DF_END:
126                 outsize = ctx->num_enabled_probes * (ctx->samples_per_line + 20) + 512;
127                 outbuf = calloc(1, outsize);
128                 flush_linebufs(ctx, o->device->probes, outbuf);
129                 *data_out = outbuf;
130                 *length_out = strlen(outbuf);
131                 free(o->internal);
132                 o->internal = NULL;
133                 break;
134         }
135
136         return SIGROK_OK;
137 }
138
139
140 static void init_binary(struct output *o)
141 {
142
143         init(o, DEFAULT_BPL_BIN);
144
145 }
146
147
148 static int data_binary(struct output *o, char *data_in, uint64_t length_in, char **data_out, uint64_t *length_out)
149 {
150         struct context *ctx;
151         int outsize, offset, p;
152         uint64_t sample;
153         char *outbuf;
154
155         ctx = o->internal;
156         outsize = length_in / ctx->unitsize * ctx->num_enabled_probes * 3 + 512;
157         outbuf = calloc(1, outsize+1);
158         if(ctx->header) {
159                 /* the header is still in here, we must be on the first data packet */
160                 strncpy(outbuf, ctx->header, outsize);
161                 free(ctx->header);
162                 ctx->header = NULL;
163         }
164         else
165                 outbuf[0] = 0;
166
167         if(length_in > ctx->unitsize) {
168                 for(offset = 0; offset <= length_in - ctx->unitsize; offset += ctx->unitsize) {
169                         memcpy(&sample, data_in + offset, ctx->unitsize);
170                         for(p = 0; p < ctx->num_enabled_probes; p++) {
171                                 if(sample & ((uint64_t) 1 << p))
172                                         ctx->linebuf[p * ctx->linebuf_len + ctx->line_offset] = '1';
173                                 else
174                                         ctx->linebuf[p * ctx->linebuf_len + ctx->line_offset] = '0';
175                         }
176                         ctx->line_offset++;
177                         ctx->spl_cnt++;
178
179                         /* space every 8th bit */
180                         if((ctx->spl_cnt & 7) == 0) {
181                                 for(p = 0; p < ctx->num_enabled_probes; p++)
182                                         ctx->linebuf[p * ctx->linebuf_len + ctx->line_offset] = ' ';
183                                 ctx->line_offset++;
184                         }
185
186                         /* end of line */
187                         if(ctx->spl_cnt >= ctx->samples_per_line) {
188                                 flush_linebufs(ctx, o->device->probes, outbuf);
189                                 ctx->line_offset = ctx->spl_cnt = 0;
190                         }
191                 }
192         } else
193                 g_message("short buffer (length_in=%"PRIu64")", length_in);
194
195         *data_out = outbuf;
196         *length_out = strlen(outbuf);
197
198         return SIGROK_OK;
199 }
200
201
202 static void init_hex(struct output *o)
203 {
204
205         init(o, DEFAULT_BPL_BIN);
206
207 }
208
209
210 static int data_hex(struct output *o, char *data_in, uint64_t length_in, char **data_out, uint64_t *length_out)
211 {
212         struct context *ctx;
213         int outsize, offset, p;
214         uint64_t sample;
215         char *outbuf;
216
217         ctx = o->internal;
218         outsize = length_in / ctx->unitsize * ctx->num_enabled_probes * 3 + 512;
219         outbuf = calloc(1, outsize+1);
220         if(ctx->header) {
221                 /* the header is still in here, we must be on the first data packet */
222                 strncpy(outbuf, ctx->header, outsize);
223                 free(ctx->header);
224                 ctx->header = NULL;
225         }
226         else
227                 outbuf[0] = 0;
228
229         ctx->line_offset = 0;
230         for(offset = 0; offset <= length_in - ctx->unitsize; offset += ctx->unitsize) {
231                 memcpy(&sample, data_in + offset, ctx->unitsize);
232                 for(p = 0; p < ctx->num_enabled_probes; p++) {
233                         ctx->linevalues[p] <<= 1;
234             if(sample & ((uint64_t) 1 << p))
235                 ctx->linevalues[p] |= 1;
236             sprintf(ctx->linebuf + (p * ctx->linebuf_len) + ctx->line_offset, "%.2x", ctx->linevalues[p]);
237                 }
238                 ctx->spl_cnt++;
239
240                 /* space after every complete hex byte */
241                 if((ctx->spl_cnt & 7) == 0) {
242                         for(p = 0; p < ctx->num_enabled_probes; p++)
243                                 ctx->linebuf[p * ctx->linebuf_len + ctx->line_offset + 2] = ' ';
244                         ctx->line_offset += 3;
245                 }
246
247                 /* end of line */
248                 if(ctx->spl_cnt >= ctx->samples_per_line) {
249                         flush_linebufs(ctx, o->device->probes, outbuf);
250                         ctx->line_offset = ctx->spl_cnt = 0;
251                 }
252         }
253
254         *data_out = outbuf;
255         *length_out = strlen(outbuf);
256
257         return SIGROK_OK;
258 }
259
260
261
262 struct output_format output_text_binary = {
263         "bin",
264         "Text (binary)",
265         init_binary,
266         data_binary,
267         event
268 };
269
270
271 struct output_format output_text_hex = {
272         "hex",
273         "Text (hexadecimal)",
274         init_hex,
275         data_hex,
276         event
277 };
278