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