2 * This file is part of the libsigrok project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
24 #include "config.h" /* Needed for PACKAGE_STRING and others. */
25 #include "libsigrok.h"
26 #include "libsigrok-internal.h"
28 #define LOG_PREFIX "output/gnuplot"
31 unsigned int num_enabled_channels;
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";
46 static int init(struct sr_output *o, GHashTable *options)
49 struct sr_channel *ch;
58 ctx = g_malloc0(sizeof(struct context));
60 ctx->num_enabled_channels = 0;
61 for (l = o->sdi->channels; l; l = l->next) {
63 if (ch->type != SR_CHANNEL_LOGIC)
67 ctx->num_enabled_channels++;
69 if (ctx->num_enabled_channels <= 0) {
70 sr_err("No logic channel enabled.");
73 ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
75 /* Once more to map the enabled channels. */
76 for (i = 0, l = o->sdi->channels; l; l = l->next) {
78 if (ch->type != SR_CHANNEL_LOGIC)
82 ctx->channel_index[i++] = ch->index;
88 static GString *gen_header(const struct sr_output *o)
91 struct sr_channel *ch;
95 unsigned int num_channels, i;
99 if (ctx->samplerate == 0) {
100 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
102 ctx->samplerate = g_variant_get_uint64(gvar);
103 g_variant_unref(gvar);
108 header = g_string_sized_new(512);
109 g_string_printf(header, "%s", gnuplot_header);
110 g_string_append_printf(header, "# Generated by %s on %s",
111 PACKAGE_STRING, ctime(&t));
113 num_channels = g_slist_length(o->sdi->channels);
114 g_string_append_printf(header, "# Acquisition with %d/%d channels",
115 ctx->num_enabled_channels, num_channels);
116 if (ctx->samplerate != 0) {
117 samplerate_s = sr_samplerate_string(ctx->samplerate);
118 g_string_append_printf(header, " at %s", samplerate_s);
119 g_free(samplerate_s);
121 g_string_append_printf(header, "\n");
123 g_string_append_printf(header, "%s", gnuplot_header2);
125 /* Columns / channels */
126 for (i = 0; i < ctx->num_enabled_channels; i++) {
127 ch = g_slist_nth_data(o->sdi->channels, ctx->channel_index[i]);
128 g_string_append_printf(header, "# %d\t\t%s\n", i + 1, ch->name);
134 static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
137 const struct sr_datafeed_meta *meta;
138 const struct sr_datafeed_logic *logic;
139 const struct sr_config *src;
142 const uint8_t *sample;
143 unsigned int curbit, p, idx, i;
150 if (packet->type == SR_DF_META) {
151 meta = packet->payload;
152 for (l = meta->config; l; l = l->next) {
154 if (src->key != SR_CONF_SAMPLERATE)
156 ctx->samplerate = g_variant_get_uint64(src->data);
160 if (packet->type != SR_DF_LOGIC)
162 logic = packet->payload;
164 if (!ctx->prevsample) {
165 /* Can't allocate this until we know the stream's unitsize. */
166 ctx->prevsample = g_malloc0(logic->unitsize);
169 if (!ctx->header_done) {
170 *out = gen_header(o);
171 ctx->header_done = TRUE;
173 *out = g_string_sized_new(512);
176 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
177 sample = logic->data + i;
181 * Don't output the same sample multiple times, but make
182 * sure to output at least the first and last sample.
184 if (i > 0 && i < logic->length - logic->unitsize) {
185 if (!memcmp(sample, ctx->prevsample, logic->unitsize))
188 memcpy(ctx->prevsample, sample, logic->unitsize);
190 /* The first column is a counter (needed for gnuplot). */
191 g_string_append_printf(*out, "%" PRIu64 "\t", ctx->samplecount);
193 /* The next columns are the values of all channels. */
194 for (p = 0; p < ctx->num_enabled_channels; p++) {
195 idx = ctx->channel_index[p];
196 curbit = (sample[idx / 8] & ((uint8_t) (1 << (idx % 8)))) >> (idx % 8);
197 g_string_append_printf(*out, "%d ", curbit);
199 g_string_append_printf(*out, "\n");
205 static int cleanup(struct sr_output *o)
212 g_free(ctx->channel_index);
213 g_free(ctx->prevsample);
219 SR_PRIV struct sr_output_module output_gnuplot = {
222 .desc = "Gnuplot data file format",
223 .exts = (const char*[]){"dat", NULL},