]> sigrok.org Git - libsigrok.git/blob - output/output_gnuplot.c
c3417c45173f8d5962878a862f890880838dddca
[libsigrok.git] / output / output_gnuplot.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
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 2 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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <glib.h>
24 #include <sigrok.h>
25 #include <sigrok-internal.h>
26 #include "config.h"
27
28 struct context {
29         unsigned int num_enabled_probes;
30         unsigned int unitsize;
31         char *probelist[SR_MAX_NUM_PROBES + 1];
32         char *header;
33 };
34
35 #define MAX_HEADER_LEN \
36         (1024 + (SR_MAX_NUM_PROBES * (SR_MAX_PROBENAME_LEN + 10)))
37
38 static const char *gnuplot_header = "\
39 # Sample data in space-separated columns format usable by gnuplot\n\
40 #\n\
41 # Generated by: %s on %s%s\
42 # Period: %s\n\
43 #\n\
44 # Column\tProbe\n\
45 # -------------------------------------\
46 ----------------------------------------\n\
47 # 0\t\tSample counter (for internal gnuplot purposes)\n%s\n";
48
49 static const char *gnuplot_header_comment = "\
50 # Comment: Acquisition with %d/%d probes at %s\n";
51
52 static int init(struct sr_output *o)
53 {
54         struct context *ctx;
55         struct sr_probe *probe;
56         GSList *l;
57         uint64_t samplerate;
58         unsigned int i;
59         int b, num_probes;
60         char *c, *frequency_s;
61         char wbuf[1000], comment[128];
62         time_t t;
63
64         if (!o) {
65                 sr_warn("gnuplot out: %s: o was NULL", __func__);
66                 return SR_ERR_ARG;
67         }
68
69         if (!o->device) {
70                 sr_warn("gnuplot out: %s: o->device was NULL", __func__);
71                 return SR_ERR_ARG;
72         }
73
74         if (!o->device->plugin) {
75                 sr_warn("gnuplot out: %s: o->device->plugin was NULL",
76                         __func__);
77                 return SR_ERR_ARG;
78         }
79
80         if (!(ctx = calloc(1, sizeof(struct context)))) {
81                 sr_warn("gnuplot out: %s: ctx calloc failed", __func__);
82                 return SR_ERR_MALLOC;
83         }
84
85         if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) {
86                 sr_warn("gnuplot out: %s: ctx->header calloc failed",
87                         __func__);
88                 free(ctx);
89                 return SR_ERR_MALLOC;
90         }
91
92         o->internal = ctx;
93         ctx->num_enabled_probes = 0;
94         for (l = o->device->probes; l; l = l->next) {
95                 probe = l->data; /* TODO: Error checks. */
96                 if (!probe->enabled)
97                         continue;
98                 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
99         }
100         ctx->probelist[ctx->num_enabled_probes] = 0;
101         ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
102
103         num_probes = g_slist_length(o->device->probes);
104         comment[0] = '\0';
105         if (sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) {
106                 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
107                                 o->device->plugin_index, SR_DI_CUR_SAMPLERATE));
108                 if (!(frequency_s = sr_samplerate_string(samplerate))) {
109                         sr_warn("gnuplot out: %s: sr_samplerate_string failed",
110                                 __func__);
111                         free(ctx->header);
112                         free(ctx);
113                         return SR_ERR;
114                 }
115                 snprintf(comment, 127, gnuplot_header_comment,
116                         ctx->num_enabled_probes, num_probes, frequency_s);
117                 free(frequency_s);
118         }
119
120         /* Columns / channels */
121         wbuf[0] = '\0';
122         for (i = 0; i < ctx->num_enabled_probes; i++) {
123                 c = (char *)&wbuf + strlen((char *)&wbuf);
124                 sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]);
125         }
126
127         if (!(frequency_s = sr_period_string(samplerate))) {
128                 sr_warn("gnuplot out: %s: sr_period_string failed", __func__);
129                 free(ctx->header);
130                 free(ctx);
131                 return SR_ERR;
132         }
133
134         t = time(NULL);
135         b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
136                      PACKAGE_STRING, ctime(&t), comment, frequency_s,
137                      (char *)&wbuf);
138         free(frequency_s);
139
140         if (b < 0) {
141                 sr_warn("gnuplot out: %s: sprintf failed", __func__);
142                 free(ctx->header);
143                 free(ctx);
144                 return SR_ERR;
145         }
146
147         return 0;
148 }
149
150 static int event(struct sr_output *o, int event_type, char **data_out,
151                  uint64_t *length_out)
152 {
153         struct context *ctx;
154
155         if (!o) {
156                 sr_warn("gnuplot out: %s: o was NULL", __func__);
157                 return SR_ERR_ARG;
158         }
159
160         if (!data_out) {
161                 sr_warn("gnuplot out: %s: data_out was NULL", __func__);
162                 return SR_ERR_ARG;
163         }
164
165         if (!length_out) {
166                 sr_warn("gnuplot out: %s: length_out was NULL", __func__);
167                 return SR_ERR_ARG;
168         }
169
170         ctx = o->internal;
171
172         switch (event_type) {
173         case SR_DF_TRIGGER:
174                 /* TODO: Can a trigger mark be in a gnuplot data file? */
175                 break;
176         case SR_DF_END:
177                 free(o->internal);
178                 o->internal = NULL;
179                 break;
180         default:
181                 sr_warn("gnuplot out: %s: unsupported event type: %d",
182                         __func__, event_type);
183                 break;
184         }
185
186         *data_out = NULL;
187         *length_out = 0;
188
189         return SR_OK;
190 }
191
192 static int data(struct sr_output *o, const char *data_in, uint64_t length_in,
193                 char **data_out, uint64_t *length_out)
194 {
195         struct context *ctx;
196         unsigned int max_linelen, outsize, p, curbit, i;
197         uint64_t sample;
198         static uint64_t samplecount = 0;
199         char *outbuf, *c;
200
201         if (!o) {
202                 sr_warn("gnuplot out: %s: o was NULL", __func__);
203                 return SR_ERR_ARG;
204         }
205
206         if (!o->internal) {
207                 sr_warn("gnuplot out: %s: o->internal was NULL", __func__);
208                 return SR_ERR_ARG;
209         }
210
211         if (!data_in) {
212                 sr_warn("gnuplot out: %s: data_in was NULL", __func__);
213                 return SR_ERR_ARG;
214         }
215
216         if (!data_out) {
217                 sr_warn("gnuplot out: %s: data_out was NULL", __func__);
218                 return SR_ERR_ARG;
219         }
220
221         if (!length_out) {
222                 sr_warn("gnuplot out: %s: length_out was NULL", __func__);
223                 return SR_ERR_ARG;
224         }
225
226         ctx = o->internal;
227         max_linelen = 16 + ctx->num_enabled_probes * 2;
228         outsize = length_in / ctx->unitsize * max_linelen;
229         if (ctx->header)
230                 outsize += strlen(ctx->header);
231
232         if (!(outbuf = calloc(1, outsize))) {
233                 sr_warn("gnuplot out: %s: outbuf calloc failed", __func__);
234                 return SR_ERR_MALLOC;
235         }
236
237         outbuf[0] = '\0';
238         if (ctx->header) {
239                 /* The header is still here, this must be the first packet. */
240                 strncpy(outbuf, ctx->header, outsize);
241                 free(ctx->header);
242                 ctx->header = NULL;
243         }
244
245         for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
246                 memcpy(&sample, data_in + i, ctx->unitsize);
247
248                 /* The first column is a counter (needed for gnuplot). */
249                 c = outbuf + strlen(outbuf);
250                 sprintf(c, "%" PRIu64 "\t", samplecount++);
251
252                 /* The next columns are the values of all channels. */
253                 for (p = 0; p < ctx->num_enabled_probes; p++) {
254                         curbit = (sample & ((uint64_t) (1 << p))) >> p;
255                         c = outbuf + strlen(outbuf);
256                         sprintf(c, "%d ", curbit);
257                 }
258
259                 c = outbuf + strlen(outbuf);
260                 sprintf(c, "\n");
261         }
262
263         *data_out = outbuf;
264         *length_out = strlen(outbuf);
265
266         return SR_OK;
267 }
268
269 struct sr_output_format output_gnuplot = {
270         .id = "gnuplot",
271         .description = "Gnuplot",
272         .df_type = SR_DF_LOGIC,
273         .init = init,
274         .data = data,
275         .event = event,
276 };
277
278 /* Temporarily disabled. */
279 #if 0
280 static int analog_init(struct sr_output *o)
281 {
282         struct context *ctx;
283         struct sr_probe *probe;
284         GSList *l;
285         uint64_t samplerate;
286         unsigned int i;
287         int b, num_probes;
288         char *c, *frequency_s;
289         char wbuf[1000], comment[128];
290         time_t t;
291
292         if (!(ctx = calloc(1, sizeof(struct context))))
293                 return SR_ERR_MALLOC;
294
295         if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) {
296                 free(ctx);
297                 return SR_ERR_MALLOC;
298         }
299
300         o->internal = ctx;
301         ctx->num_enabled_probes = 0;
302         for (l = o->device->probes; l; l = l->next) {
303                 probe = l->data;
304                 if (!probe->enabled)
305                         continue;
306                 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
307         }
308         ctx->probelist[ctx->num_enabled_probes] = 0;
309 //      ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
310         ctx->unitsize = sizeof(struct sr_analog_sample) +
311                         (ctx->num_enabled_probes * sizeof(struct sr_analog_probe));
312
313         num_probes = g_slist_length(o->device->probes);
314         comment[0] = '\0';
315         if (o->device->plugin && sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) {
316                 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
317                                 o->device->plugin_index, SR_DI_CUR_SAMPLERATE));
318                 if (!(frequency_s = sr_samplerate_string(samplerate))) {
319                         free(ctx->header);
320                         free(ctx);
321                         return SR_ERR;
322                 }
323                 snprintf(comment, 127, gnuplot_header_comment,
324                         ctx->num_enabled_probes, num_probes, frequency_s);
325                 free(frequency_s);
326         }
327
328         /* Columns / channels */
329         wbuf[0] = '\0';
330         for (i = 0; i < ctx->num_enabled_probes; i++) {
331                 c = (char *)&wbuf + strlen((char *)&wbuf);
332                 sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]);
333         }
334
335         if (!(frequency_s = sr_period_string(samplerate))) {
336                 free(ctx->header);
337                 free(ctx);
338                 return SR_ERR;
339         }
340         t = time(NULL);
341         b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
342                      PACKAGE_STRING, ctime(&t), comment, frequency_s,
343                      (char *)&wbuf);
344         free(frequency_s);
345
346         if (b < 0) {
347                 free(ctx->header);
348                 free(ctx);
349                 return SR_ERR;
350         }
351
352         return 0;
353 }
354
355 static int analog_data(struct sr_output *o, char *data_in, uint64_t length_in,
356                 char **data_out, uint64_t *length_out)
357 {
358         struct context *ctx;
359         unsigned int max_linelen, outsize, p, /* curbit, */ i;
360 //      uint64_t sample;
361         static uint64_t samplecount = 0;
362         char *outbuf, *c;
363         struct sr_analog_sample *sample;
364
365         ctx = o->internal;
366 //      max_linelen = 16 + ctx->num_enabled_probes * 2;
367         max_linelen = 16 + ctx->num_enabled_probes * 30;
368         outsize = length_in / ctx->unitsize * max_linelen;
369         if (ctx->header)
370                 outsize += strlen(ctx->header);
371
372         if (!(outbuf = calloc(1, outsize)))
373                 return SR_ERR_MALLOC;
374
375         outbuf[0] = '\0';
376         if (ctx->header) {
377                 /* The header is still here, this must be the first packet. */
378                 strncpy(outbuf, ctx->header, outsize);
379                 free(ctx->header);
380                 ctx->header = NULL;
381         }
382
383         for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
384 //              memcpy(&sample, data_in + i, ctx->unitsize);
385                 sample = (struct sr_analog_sample *) (data_in + i);
386
387                 /* The first column is a counter (needed for gnuplot). */
388                 c = outbuf + strlen(outbuf);
389                 sprintf(c, "%" PRIu64 "\t", samplecount++);
390
391                 /* The next columns are the values of all channels. */
392                 for (p = 0; p < ctx->num_enabled_probes; p++) {
393 //                      curbit = (sample & ((uint64_t) (1 << p))) >> p;
394                         c = outbuf + strlen(outbuf);
395 //                      sprintf(c, "%d ", curbit);
396                         /*
397                          * FIXME: Should be doing proper raw->voltage conversion
398                          * here, casting to int16_t isn't it. Remember that if
399                          * res = 1 conversion isn't necessary.
400                          */
401                         sprintf(c, "%f ", (double) ((int16_t) (sample->probes[p].val &
402                                         ((1 << sample->probes[p].res) - 1))));
403                 }
404
405                 c = outbuf + strlen(outbuf);
406                 sprintf(c, "\n");
407         }
408
409         *data_out = outbuf;
410         *length_out = strlen(outbuf);
411
412         return SR_OK;
413 }
414
415 struct sr_output_format output_analog_gnuplot = {
416         .id = "analog_gnuplot",
417         .description = "Gnuplot analog",
418         .df_type = SR_DF_ANALOG,
419         .init = analog_init,
420         .data = analog_data,
421         .event = event,
422 };
423 #endif