]> sigrok.org Git - libsigrok.git/blob - output/output_gnuplot.c
4fd81f2da7092f9564185c325a05ff2d563c603e
[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 "config.h"
26
27 struct context {
28         unsigned int num_enabled_probes;
29         unsigned int unitsize;
30         char *probelist[MAX_NUM_PROBES+1];
31         char *header;
32 };
33
34 #define MAX_HEADER_LEN   1024 + (MAX_NUM_PROBES * (MAX_PROBENAME_LEN + 10))
35 const char *gnuplot_header = "\
36 # Sample data in space-separated columns format usable by gnuplot\n\
37 #\n\
38 # Generated by: %s on %s%s\
39 # Period: %s\n\
40 #\n\
41 # Column\tProbe\n\
42 # -------------------------------------\
43 ----------------------------------------\n\
44 # 0\t\tSample counter (for internal gnuplot purposes)\n%s\n";
45
46 const char *gnuplot_header_comment = "\
47 # Comment: Acquisition with %d/%d probes at %s\n";
48
49 static int init(struct output *o)
50 {
51         struct context *ctx;
52         struct probe *probe;
53         GSList *l;
54         uint64_t samplerate;
55         unsigned int i;
56         int b, num_probes;
57         char *c, *frequency_s;
58         char wbuf[1000], comment[128];
59         time_t t;
60
61         if (!(ctx = calloc(1, sizeof(struct context))))
62                 return SR_ERR_MALLOC;
63
64         if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) {
65                 free(ctx);
66                 return SR_ERR_MALLOC;
67         }
68
69         o->internal = ctx;
70         ctx->num_enabled_probes = 0;
71         for (l = o->device->probes; l; l = l->next) {
72                 probe = l->data;
73                 if (!probe->enabled)
74                         continue;
75                 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
76         }
77         ctx->probelist[ctx->num_enabled_probes] = 0;
78         ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
79
80         num_probes = g_slist_length(o->device->probes);
81         comment[0] = '\0';
82         if (o->device->plugin) {
83                 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
84                                 o->device->plugin_index, DI_CUR_SAMPLERATE));
85                 if (!(frequency_s = sigrok_samplerate_string(samplerate))) {
86                         free(ctx->header);
87                         free(ctx);
88                         return SR_ERR;
89                 }
90                 snprintf(comment, 127, gnuplot_header_comment,
91                         ctx->num_enabled_probes, num_probes, frequency_s);
92                 free(frequency_s);
93         }
94
95         /* Columns / channels */
96         wbuf[0] = '\0';
97         for (i = 0; i < ctx->num_enabled_probes; i++) {
98                 c = (char *)&wbuf + strlen((char *)&wbuf);
99                 sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]);
100         }
101
102         if (!(frequency_s = sigrok_period_string(samplerate))) {
103                 free(ctx->header);
104                 free(ctx);
105                 return SR_ERR;
106         }
107         t = time(NULL);
108         b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
109                      PACKAGE_STRING, ctime(&t), comment, frequency_s,
110                      (char *)&wbuf);
111         free(frequency_s);
112
113         if (b < 0) {
114                 free(ctx->header);
115                 free(ctx);
116                 return SR_ERR;
117         }
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
127         ctx = o->internal;
128         switch (event_type) {
129         case DF_TRIGGER:
130                 /* TODO: can a trigger mark be in a gnuplot data file? */
131                 break;
132         case DF_END:
133                 free(o->internal);
134                 o->internal = NULL;
135                 break;
136         }
137
138         *data_out = NULL;
139         *length_out = 0;
140
141         return SR_OK;
142 }
143
144 static int data(struct output *o, char *data_in, uint64_t length_in,
145                 char **data_out, uint64_t *length_out)
146 {
147         struct context *ctx;
148         unsigned int max_linelen, outsize, p, curbit, i;
149         uint64_t sample;
150         static uint64_t samplecount = 0;
151         char *outbuf, *c;
152
153         ctx = o->internal;
154         max_linelen = 16 + ctx->num_enabled_probes * 2;
155         outsize = length_in / ctx->unitsize * max_linelen;
156         if (ctx->header)
157                 outsize += strlen(ctx->header);
158
159         if (!(outbuf = calloc(1, outsize)))
160                 return SR_ERR_MALLOC;
161
162         outbuf[0] = '\0';
163         if (ctx->header) {
164                 /* The header is still here, this must be the first packet. */
165                 strncpy(outbuf, ctx->header, outsize);
166                 free(ctx->header);
167                 ctx->header = NULL;
168         }
169
170         for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
171                 memcpy(&sample, data_in + i, ctx->unitsize);
172
173                 /* The first column is a counter (needed for gnuplot). */
174                 c = outbuf + strlen(outbuf);
175                 sprintf(c, "%" PRIu64 "\t", samplecount++);
176
177                 /* The next columns are the values of all channels. */
178                 for (p = 0; p < ctx->num_enabled_probes; p++) {
179                         curbit = (sample & ((uint64_t) (1 << p))) >> p;
180                         c = outbuf + strlen(outbuf);
181                         sprintf(c, "%d ", curbit);
182                 }
183
184                 c = outbuf + strlen(outbuf);
185                 sprintf(c, "\n");
186         }
187
188         *data_out = outbuf;
189         *length_out = strlen(outbuf);
190
191         return SR_OK;
192 }
193
194 static int analog_init(struct output *o)
195 {
196         struct context *ctx;
197         struct probe *probe;
198         GSList *l;
199         uint64_t samplerate;
200         unsigned int i;
201         int b, num_probes;
202         char *c, *frequency_s;
203         char wbuf[1000], comment[128];
204         time_t t;
205
206         if (!(ctx = calloc(1, sizeof(struct context))))
207                 return SR_ERR_MALLOC;
208
209         if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) {
210                 free(ctx);
211                 return SR_ERR_MALLOC;
212         }
213
214         o->internal = ctx;
215         ctx->num_enabled_probes = 0;
216         for (l = o->device->probes; l; l = l->next) {
217                 probe = l->data;
218                 if (!probe->enabled)
219                         continue;
220                 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
221         }
222         ctx->probelist[ctx->num_enabled_probes] = 0;
223 //      ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
224         ctx->unitsize = sizeof(struct analog_sample) +
225                         (ctx->num_enabled_probes * sizeof(struct analog_probe));
226
227         num_probes = g_slist_length(o->device->probes);
228         comment[0] = '\0';
229         if (o->device->plugin) {
230                 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
231                                 o->device->plugin_index, DI_CUR_SAMPLERATE));
232                 if (!(frequency_s = sigrok_samplerate_string(samplerate))) {
233                         free(ctx->header);
234                         free(ctx);
235                         return SR_ERR;
236                 }
237                 snprintf(comment, 127, gnuplot_header_comment,
238                         ctx->num_enabled_probes, num_probes, frequency_s);
239                 free(frequency_s);
240         }
241
242         /* Columns / channels */
243         wbuf[0] = '\0';
244         for (i = 0; i < ctx->num_enabled_probes; i++) {
245                 c = (char *)&wbuf + strlen((char *)&wbuf);
246                 sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]);
247         }
248
249         if (!(frequency_s = sigrok_period_string(samplerate))) {
250                 free(ctx->header);
251                 free(ctx);
252                 return SR_ERR;
253         }
254         t = time(NULL);
255         b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
256                      PACKAGE_STRING, ctime(&t), comment, frequency_s,
257                      (char *)&wbuf);
258         free(frequency_s);
259
260         if (b < 0) {
261                 free(ctx->header);
262                 free(ctx);
263                 return SR_ERR;
264         }
265
266         return 0;
267 }
268
269 static int analog_data(struct output *o, char *data_in, uint64_t length_in,
270                 char **data_out, uint64_t *length_out)
271 {
272         struct context *ctx;
273         unsigned int max_linelen, outsize, p, /* curbit, */ i;
274 //      uint64_t sample;
275         static uint64_t samplecount = 0;
276         char *outbuf, *c;
277         struct analog_sample *sample;
278
279         ctx = o->internal;
280 //      max_linelen = 16 + ctx->num_enabled_probes * 2;
281         max_linelen = 16 + ctx->num_enabled_probes * 30;
282         outsize = length_in / ctx->unitsize * max_linelen;
283         if (ctx->header)
284                 outsize += strlen(ctx->header);
285
286         if (!(outbuf = calloc(1, outsize)))
287                 return SR_ERR_MALLOC;
288
289         outbuf[0] = '\0';
290         if (ctx->header) {
291                 /* The header is still here, this must be the first packet. */
292                 strncpy(outbuf, ctx->header, outsize);
293                 free(ctx->header);
294                 ctx->header = NULL;
295         }
296
297         for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
298 //              memcpy(&sample, data_in + i, ctx->unitsize);
299                 sample = (struct analog_sample *) (data_in + i);
300
301                 /* The first column is a counter (needed for gnuplot). */
302                 c = outbuf + strlen(outbuf);
303                 sprintf(c, "%" PRIu64 "\t", samplecount++);
304
305                 /* The next columns are the values of all channels. */
306                 for (p = 0; p < ctx->num_enabled_probes; p++) {
307 //                      curbit = (sample & ((uint64_t) (1 << p))) >> p;
308                         c = outbuf + strlen(outbuf);
309 //                      sprintf(c, "%d ", curbit);
310                         /*
311                          * FIXME: Should be doing proper raw->voltage conversion
312                          * here, casting to int16_t isn't it. Remember that if
313                          * res = 1 conversion isn't necessary.
314                          */
315                         sprintf(c, "%f ", (double) ((int16_t) (sample->probes[p].val &
316                                         ((1 << sample->probes[p].res) - 1))));
317                 }
318
319                 c = outbuf + strlen(outbuf);
320                 sprintf(c, "\n");
321         }
322
323         *data_out = outbuf;
324         *length_out = strlen(outbuf);
325
326         return SR_OK;
327 }
328
329 struct output_format output_gnuplot = {
330         "gnuplot",
331         "Gnuplot",
332         DF_LOGIC,
333         init,
334         data,
335         event,
336 };
337
338 struct output_format output_analog_gnuplot = {
339         "analog_gnuplot",
340         "Gnuplot analog",
341         DF_ANALOG,
342         analog_init,
343         analog_data,
344         event,
345 };