]> sigrok.org Git - libsigrok.git/blob - output/gnuplot.c
f70625a9fd42cdf47b6bd57f1013df0eb53ffce0
[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_CONF_SAMPLERATE)) {
113                 o->sdi->driver->config_get(SR_CONF_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 };