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