]> sigrok.org Git - libsigrok.git/blob - src/output/gnuplot.c
c92907394f1f1dd94f61c1ba081d97bc99a125c3
[libsigrok.git] / src / output / gnuplot.c
1 /*
2  * This file is part of the libsigrok 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 <libsigrok/libsigrok.h>
25 #include "libsigrok-internal.h"
26
27 #define LOG_PREFIX "output/gnuplot"
28
29 struct context {
30         unsigned int num_enabled_channels;
31         uint64_t samplerate;
32         uint64_t samplecount;
33         gboolean header_done;
34         uint8_t *prevsample;
35         int *channel_index;
36 };
37
38 static const char *gnuplot_header = "\
39 # Sample data in space-separated columns format usable by gnuplot.\n";
40 static const char *gnuplot_header2 = "\
41 #\n# Column\tChannel\n\
42 # -----------------------------------------------------------------------------\n\
43 # 0\t\tSample counter (for internal gnuplot purposes)\n";
44
45 static int init(struct sr_output *o, GHashTable *options)
46 {
47         struct context *ctx;
48         struct sr_channel *ch;
49         GSList *l;
50         unsigned int i;
51
52         (void)options;
53
54         if (!o || !o->sdi)
55                 return SR_ERR_ARG;
56
57         ctx = g_malloc0(sizeof(struct context));
58         o->priv = ctx;
59         ctx->num_enabled_channels = 0;
60         for (l = o->sdi->channels; l; l = l->next) {
61                 ch = l->data;
62                 if (ch->type != SR_CHANNEL_LOGIC)
63                         continue;
64                 if (!ch->enabled)
65                         continue;
66                 ctx->num_enabled_channels++;
67         }
68         if (ctx->num_enabled_channels <= 0) {
69                 sr_err("No logic channel enabled.");
70                 return SR_ERR;
71         }
72         ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
73
74         /* Once more to map the enabled channels. */
75         for (i = 0, l = o->sdi->channels; l; l = l->next) {
76                 ch = l->data;
77                 if (ch->type != SR_CHANNEL_LOGIC)
78                         continue;
79                 if (!ch->enabled)
80                         continue;
81                 ctx->channel_index[i++] = ch->index;
82         }
83
84         return SR_OK;
85 }
86
87 static GString *gen_header(const struct sr_output *o)
88 {
89         struct context *ctx;
90         struct sr_channel *ch;
91         GVariant *gvar;
92         GString *header;
93         time_t t;
94         unsigned int num_channels, i;
95         char *samplerate_s;
96
97         ctx = o->priv;
98         if (ctx->samplerate == 0) {
99                 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
100                                 &gvar) == SR_OK) {
101                         ctx->samplerate = g_variant_get_uint64(gvar);
102                         g_variant_unref(gvar);
103                 }
104         }
105
106         t = time(NULL);
107         header = g_string_sized_new(512);
108         g_string_printf(header, "%s", gnuplot_header);
109         g_string_append_printf(header, "# Generated by %s %s on %s",
110                         PACKAGE_NAME, SR_PACKAGE_VERSION_STRING, ctime(&t));
111
112         num_channels = g_slist_length(o->sdi->channels);
113         g_string_append_printf(header, "# Acquisition with %d/%d channels",
114                         ctx->num_enabled_channels, num_channels);
115         if (ctx->samplerate != 0) {
116                 samplerate_s = sr_samplerate_string(ctx->samplerate);
117                 g_string_append_printf(header, " at %s", samplerate_s);
118                 g_free(samplerate_s);
119         }
120         g_string_append_printf(header, "\n");
121
122         g_string_append_printf(header, "%s", gnuplot_header2);
123
124         /* Columns / channels */
125         for (i = 0; i < ctx->num_enabled_channels; i++) {
126                 ch = g_slist_nth_data(o->sdi->channels, ctx->channel_index[i]);
127                 g_string_append_printf(header, "# %d\t\t%s\n", i + 1, ch->name);
128         }
129
130         return header;
131 }
132
133 static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
134                 GString **out)
135 {
136         const struct sr_datafeed_meta *meta;
137         const struct sr_datafeed_logic *logic;
138         const struct sr_config *src;
139         GSList *l;
140         struct context *ctx;
141         const uint8_t *sample;
142         unsigned int curbit, p, idx, i;
143
144         *out = NULL;
145         if (!o || !o->priv)
146                 return SR_ERR_BUG;
147         ctx = o->priv;
148
149         if (packet->type == SR_DF_META) {
150                 meta = packet->payload;
151                 for (l = meta->config; l; l = l->next) {
152                         src = l->data;
153                         if (src->key != SR_CONF_SAMPLERATE)
154                                 continue;
155                         ctx->samplerate = g_variant_get_uint64(src->data);
156                 }
157         }
158
159         if (packet->type != SR_DF_LOGIC)
160                 return SR_OK;
161         logic = packet->payload;
162
163         if (!ctx->prevsample) {
164                 /* Can't allocate this until we know the stream's unitsize. */
165                 ctx->prevsample = g_malloc0(logic->unitsize);
166         }
167
168         if (!ctx->header_done) {
169                 *out = gen_header(o);
170                 ctx->header_done = TRUE;
171         } else {
172                 *out = g_string_sized_new(512);
173         }
174
175         for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
176                 sample = logic->data + i;
177                 ctx->samplecount++;
178
179                 /*
180                  * Don't output the same sample multiple times, but make
181                  * sure to output at least the first and last sample.
182                  */
183                 if (i > 0 && i < logic->length - logic->unitsize) {
184                         if (!memcmp(sample, ctx->prevsample, logic->unitsize))
185                                 continue;
186                 }
187                 memcpy(ctx->prevsample, sample, logic->unitsize);
188
189                 /* The first column is a counter (needed for gnuplot). */
190                 g_string_append_printf(*out, "%" PRIu64 "\t", ctx->samplecount);
191
192                 /* The next columns are the values of all channels. */
193                 for (p = 0; p < ctx->num_enabled_channels; p++) {
194                         idx = ctx->channel_index[p];
195                         curbit = (sample[idx / 8] & ((uint8_t) (1 << (idx % 8)))) >> (idx % 8);
196                         g_string_append_printf(*out, "%d ", curbit);
197                 }
198                 g_string_append_printf(*out, "\n");
199         }
200
201         return SR_OK;
202 }
203
204 static int cleanup(struct sr_output *o)
205 {
206         struct context *ctx;
207
208         if (!o || !o->priv)
209                 return SR_ERR_BUG;
210         ctx = o->priv;
211         g_free(ctx->channel_index);
212         g_free(ctx->prevsample);
213         g_free(ctx);
214
215         return SR_OK;
216 }
217
218 SR_PRIV struct sr_output_module output_gnuplot = {
219         .id = "gnuplot",
220         .name = "Gnuplot",
221         .desc = "Gnuplot data file format",
222         .exts = (const char*[]){"dat", NULL},
223         .flags = 0,
224         .options = NULL,
225         .init = init,
226         .receive = receive,
227         .cleanup = cleanup,
228 };