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