]> sigrok.org Git - libsigrok.git/blob - output/output_gnuplot.c
VCD/Gnuplot: Cosmetics, code simplifications.
[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[65];
31         char *header;
32 };
33
34 const char *gnuplot_header = "\
35 # Sample data in space-separated columns format usable by gnuplot\n\
36 #\n\
37 # Generated by: %s on %s\n%s\
38 # Timescale: %d %s\n\
39 # Column assignment:\n%s\n";
40
41 const char *gnuplot_header_comment = "\
42 # Comment: Acquisition with %d/%d probes at %s\n";
43
44 static int init(struct output *o)
45 {
46 /* Maximum header length */
47 #define MAX_HEADER_LEN 2048
48
49         struct context *ctx;
50         struct probe *probe;
51         GSList *l;
52         uint64_t samplerate;
53         unsigned int i;
54         int b, num_probes;
55         char *c, *samplerate_s;
56         char wbuf[1000], comment[128];
57
58         if (!(ctx = calloc(1, sizeof(struct context))))
59                 return SIGROK_ERR_MALLOC;
60
61         o->internal = ctx;
62         ctx->num_enabled_probes = 0;
63         for (l = o->device->probes; l; l = l->next) {
64                 probe = l->data;
65                 if (probe->enabled)
66                         ctx->probelist[ctx->num_enabled_probes++] = probe->name;
67         }
68
69         ctx->probelist[ctx->num_enabled_probes] = 0;
70         ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
71
72         /* TODO: Allow for configuration via o->param. */
73
74         if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) {
75                 free(ctx);
76                 return SIGROK_ERR_MALLOC;
77         }
78
79         num_probes = g_slist_length(o->device->probes);
80         /* TODO: Handle num_probes == 0, too many probes, etc. */
81
82         comment[0] = '\0';
83         if (o->device->plugin) {
84                 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
85                                 o->device->plugin_index, DI_CUR_SAMPLERATE));
86                 if (!(samplerate_s = sigrok_samplerate_string(samplerate))) {
87                         free(ctx->header);
88                         free(ctx);
89                         return SIGROK_ERR;
90                 }
91                 snprintf(comment, 127, gnuplot_header_comment,
92                          ctx->num_enabled_probes, num_probes, samplerate_s);
93                 free(samplerate_s);
94         }
95
96         /* Columns / channels */
97         wbuf[0] = '\0';
98         for (i = 0; i < ctx->num_enabled_probes; i++) {
99                 c = (char *)&wbuf + strlen((char *)&wbuf);
100                 sprintf(c, "# Column %d: channel %s\n", i, ctx->probelist[i]);
101         }
102
103         /* TODO: date: File or signals? Make y/n configurable. */
104         /* TODO: Timescale */
105         b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
106                      PACKAGE_STRING, "TODO", comment, 1, "ns", (char *)&wbuf);
107
108         /* TODO: Handle snprintf errors. */
109
110         return 0;
111 }
112
113 static int event(struct output *o, int event_type, char **data_out,
114                  uint64_t *length_out)
115 {
116         struct context *ctx;
117         char *outbuf;
118         int outlen = 1; /* FIXME */
119
120         ctx = o->internal;
121         switch (event_type) {
122         case DF_TRIGGER:
123                 /* TODO */
124                 break;
125         case DF_END:
126                 outbuf = calloc(1, 1); /* FIXME */
127                 if (outbuf == NULL)
128                         return SIGROK_ERR_MALLOC;
129                 *data_out = outbuf;
130                 *length_out = outlen;
131                 free(o->internal);
132                 o->internal = NULL;
133                 break;
134         }
135
136         return SIGROK_OK;
137 }
138
139 static int data(struct output *o, char *data_in, uint64_t length_in,
140                 char **data_out, uint64_t *length_out)
141 {
142         struct context *ctx;
143         unsigned int i, outsize, p, curbit;
144         uint64_t sample, count = 0;
145         char *outbuf, *c;
146
147         ctx = o->internal;
148         outsize = 0;
149         if (ctx->header)
150                 outsize = strlen(ctx->header);
151
152         /* FIXME: Use realloc(). */
153         if (!(outbuf = calloc(1, outsize + 1 + 10000)))
154                 return SIGROK_ERR_MALLOC; /* TODO: free()? What to free? */
155
156         outbuf[0] = '\0';
157         if (ctx->header) {
158                 /* The header is still here, this must be the first packet. */
159                 strncpy(outbuf, ctx->header, outsize);
160                 free(ctx->header);
161                 ctx->header = NULL;
162         }
163
164         /* TODO: Are disabled probes handled correctly? */
165
166         for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
167                 memcpy(&sample, data_in + i, ctx->unitsize);
168
169                 /* The first column is a counter (needed for gnuplot). */
170                 c = outbuf + strlen(outbuf);
171                 sprintf(c, "%" PRIu64 "\t\t", count++);
172
173                 /* The next columns are the values of all channels. */
174                 for (p = 0; p < ctx->num_enabled_probes; p++) {
175                         curbit = (sample & ((uint64_t) (1 << p))) != 0;
176                         c = outbuf + strlen(outbuf);
177                         sprintf(c, "%d ", curbit);
178                 }
179
180                 c = outbuf + strlen(outbuf);
181                 sprintf(c, "\n");
182
183                 /* TODO: realloc() if strlen(outbuf) is almost "full"... */
184         }
185
186         *data_out = outbuf;
187         *length_out = strlen(outbuf);
188
189         return SIGROK_OK;
190 }
191
192 struct output_format output_gnuplot = {
193         "gnuplot",
194         "Gnuplot",
195         init,
196         data,
197         event,
198 };