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