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;
38 static const char *gnuplot_header = "\
39 # Sample data in space-separated columns format usable by gnuplot\n\
41 # Generated by: %s on %s%s\
45 # -------------------------------------\
46 ----------------------------------------\n\
47 # 0\t\tSample counter (for internal gnuplot purposes)\n%s\n";
49 static const char *gnuplot_header_comment = "\
50 # Comment: Acquisition with %d/%d channels at %s\n";
52 static int init(struct sr_output *o)
55 struct sr_channel *ch;
61 char *c, *frequency_s;
62 char wbuf[1000], comment[128];
71 if (!(ctx = g_malloc0(sizeof(struct context))))
75 ctx->num_enabled_channels = 0;
76 for (l = o->sdi->channels; l; l = l->next) {
78 if (ch->type != SR_CHANNEL_LOGIC)
82 ctx->num_enabled_channels++;
84 if (ctx->num_enabled_channels <= 0) {
85 sr_err("No logic channel enabled.");
88 ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
90 num_channels = g_slist_length(o->sdi->channels);
93 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
95 samplerate = g_variant_get_uint64(gvar);
96 g_variant_unref(gvar);
97 if (!(frequency_s = sr_samplerate_string(samplerate))) {
98 sr_err("%s: sr_samplerate_string failed", __func__);
102 snprintf(comment, 127, gnuplot_header_comment,
103 ctx->num_enabled_channels, num_channels, frequency_s);
107 /* Columns / channels */
109 for (i = 0, j = 0, l = o->sdi->channels; l; l = l->next, i++) {
111 if (ch->type != SR_CHANNEL_LOGIC)
115 c = (char *)&wbuf + strlen((const char *)&wbuf);
116 sprintf(c, "# %d\t\t%s\n", i + 1, ch->name);
117 /* Remember the enabled channel's index while we're at it. */
118 ctx->channel_index[j++] = i;
121 if (!(frequency_s = sr_period_string(samplerate))) {
122 sr_err("%s: sr_period_string failed", __func__);
128 ctx->header = g_string_sized_new(512);
129 g_string_printf(ctx->header, gnuplot_header, PACKAGE_STRING,
130 ctime(&t), comment, frequency_s, (char *)&wbuf);
136 static int receive(struct sr_output *o, const struct sr_dev_inst *sdi,
137 const struct sr_datafeed_packet *packet, GString **out)
139 const struct sr_datafeed_logic *logic;
141 const uint8_t *sample;
142 unsigned int curbit, p, idx, i;
147 if (!o || !o->internal)
151 if (packet->type != SR_DF_LOGIC)
153 logic = packet->payload;
155 if (!ctx->prevsample) {
156 /* Can't allocate this until we know the stream's unitsize. */
157 if (!(ctx->prevsample = g_malloc0(logic->unitsize))) {
159 sr_err("%s: ctx->prevsample malloc failed", __func__);
160 return SR_ERR_MALLOC;
165 /* The header is still here, this must be the first packet. */
168 ctx->samplecount = 0;
170 *out = g_string_sized_new(512);
173 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
174 sample = logic->data + i;
178 * Don't output the same sample multiple times, but make
179 * sure to output at least the first and last sample.
181 if (i > 0 && i < logic->length - logic->unitsize) {
182 if (!memcmp(sample, ctx->prevsample, logic->unitsize))
185 memcpy(ctx->prevsample, sample, logic->unitsize);
187 /* The first column is a counter (needed for gnuplot). */
188 g_string_append_printf(*out, "%" PRIu64 "\t", ctx->samplecount);
190 /* The next columns are the values of all channels. */
191 for (p = 0; p < ctx->num_enabled_channels; p++) {
192 idx = ctx->channel_index[p];
193 curbit = (sample[idx / 8] & ((uint8_t) (1 << (idx % 8)))) >> (idx % 8);
194 g_string_append_printf(*out, "%d ", curbit);
196 g_string_append_printf(*out, "\n");
202 static int cleanup(struct sr_output *o)
206 if (!o || !o->internal)
209 g_free(ctx->channel_index);
210 g_free(ctx->prevsample);
212 g_string_free(ctx->header, TRUE);
218 SR_PRIV struct sr_output_format output_gnuplot = {
220 .description = "Gnuplot",