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