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