]> sigrok.org Git - libsigrok.git/blame - output/gnuplot.c
output: Introduce output module API wrappers.
[libsigrok.git] / output / gnuplot.c
CommitLineData
e2ad47b5 1/*
50985c20 2 * This file is part of the libsigrok project.
e2ad47b5
UH
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>
545f9786 24#include "config.h" /* Needed for PACKAGE_STRING and others. */
45c59c8b
BV
25#include "libsigrok.h"
26#include "libsigrok-internal.h"
e2ad47b5 27
3544f848 28#define LOG_PREFIX "output/gnuplot"
a944a84b 29
e2ad47b5 30struct context {
ba7dd8bb 31 unsigned int num_enabled_channels;
b240ee08
BV
32 uint64_t samplecount;
33 GString *header;
34 uint8_t *prevsample;
35 int *channel_index;
e2ad47b5
UH
36};
37
d078d2e5 38static const char *gnuplot_header = "\
e2ad47b5
UH
39# Sample data in space-separated columns format usable by gnuplot\n\
40#\n\
5cca9adb 41# Generated by: %s on %s%s\
d4ae8eaa 42# Period: %s\n\
114fb93f 43#\n\
ba7dd8bb 44# Column\tChannel\n\
114fb93f
UH
45# -------------------------------------\
46----------------------------------------\n\
47# 0\t\tSample counter (for internal gnuplot purposes)\n%s\n";
e2ad47b5 48
d078d2e5 49static const char *gnuplot_header_comment = "\
ba7dd8bb 50# Comment: Acquisition with %d/%d channels at %s\n";
7aae7462 51
f50f3f40 52static int init(struct sr_output *o)
e2ad47b5 53{
e2ad47b5 54 struct context *ctx;
ba7dd8bb 55 struct sr_channel *ch;
e2ad47b5 56 GSList *l;
ec4063b8
BV
57 GVariant *gvar;
58 uint64_t samplerate;
b240ee08 59 unsigned int i, j;
ba7dd8bb 60 int num_channels;
d4ae8eaa 61 char *c, *frequency_s;
7aae7462 62 char wbuf[1000], comment[128];
5cca9adb 63 time_t t;
e2ad47b5 64
b240ee08 65 if (!o)
20ebd1fe 66 return SR_ERR_ARG;
20ebd1fe 67
b240ee08 68 if (!o->sdi)
20ebd1fe 69 return SR_ERR_ARG;
20ebd1fe 70
b240ee08 71 if (!(ctx = g_malloc0(sizeof(struct context))))
e46b8fb1 72 return SR_ERR_MALLOC;
a821069b 73
e2ad47b5 74 o->internal = ctx;
ba7dd8bb
UH
75 ctx->num_enabled_channels = 0;
76 for (l = o->sdi->channels; l; l = l->next) {
77 ch = l->data;
3f239f08 78 if (ch->type != SR_CHANNEL_LOGIC)
3699a8a1 79 continue;
ba7dd8bb 80 if (!ch->enabled)
3699a8a1 81 continue;
ba7dd8bb 82 ctx->num_enabled_channels++;
e2ad47b5 83 }
ba7dd8bb 84 if (ctx->num_enabled_channels <= 0) {
b240ee08 85 sr_err("No logic channel enabled.");
365cca8a
AJ
86 return SR_ERR;
87 }
b240ee08 88 ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
e2ad47b5 89
ba7dd8bb 90 num_channels = g_slist_length(o->sdi->channels);
a821069b 91 comment[0] = '\0';
ec4063b8 92 samplerate = 0;
d3c74a6f
BV
93 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
94 &gvar) == SR_OK) {
ec4063b8 95 samplerate = g_variant_get_uint64(gvar);
af51a771 96 g_variant_unref(gvar);
ec4063b8 97 if (!(frequency_s = sr_samplerate_string(samplerate))) {
a944a84b 98 sr_err("%s: sr_samplerate_string failed", __func__);
133a37bf 99 g_free(ctx);
e46b8fb1 100 return SR_ERR;
a821069b
UH
101 }
102 snprintf(comment, 127, gnuplot_header_comment,
ba7dd8bb 103 ctx->num_enabled_channels, num_channels, frequency_s);
133a37bf 104 g_free(frequency_s);
7aae7462 105 }
e2ad47b5
UH
106
107 /* Columns / channels */
108 wbuf[0] = '\0';
b240ee08 109 for (i = 0, j = 0, l = o->sdi->channels; l; l = l->next, i++) {
ba7dd8bb 110 ch = l->data;
3f239f08 111 if (ch->type != SR_CHANNEL_LOGIC)
3699a8a1 112 continue;
ba7dd8bb 113 if (!ch->enabled)
deb09083 114 continue;
054e6709 115 c = (char *)&wbuf + strlen((const char *)&wbuf);
ba7dd8bb 116 sprintf(c, "# %d\t\t%s\n", i + 1, ch->name);
b240ee08
BV
117 /* Remember the enabled channel's index while we're at it. */
118 ctx->channel_index[j++] = i;
e2ad47b5
UH
119 }
120
ec4063b8 121 if (!(frequency_s = sr_period_string(samplerate))) {
a944a84b 122 sr_err("%s: sr_period_string failed", __func__);
133a37bf 123 g_free(ctx);
e46b8fb1 124 return SR_ERR;
d4ae8eaa 125 }
20ebd1fe 126
5cca9adb 127 t = time(NULL);
b240ee08
BV
128 ctx->header = g_string_sized_new(512);
129 g_string_printf(ctx->header, gnuplot_header, PACKAGE_STRING,
90f680ff 130 ctime(&t), comment, frequency_s, (char *)&wbuf);
133a37bf 131 g_free(frequency_s);
e2ad47b5 132
e2ad47b5
UH
133 return 0;
134}
135
dba3e682
BV
136static int receive(struct sr_output *o, const struct sr_datafeed_packet *packet,
137 GString **out)
e2ad47b5 138{
b240ee08 139 const struct sr_datafeed_logic *logic;
e2ad47b5 140 struct context *ctx;
9c178ffa 141 const uint8_t *sample;
b240ee08 142 unsigned int curbit, p, idx, i;
e2ad47b5 143
b240ee08
BV
144 *out = NULL;
145 if (!o || !o->internal)
146 return SR_ERR_BUG;
e2ad47b5 147 ctx = o->internal;
a821069b 148
b240ee08
BV
149 if (packet->type != SR_DF_LOGIC)
150 return SR_OK;
151 logic = packet->payload;
152
153 if (!ctx->prevsample) {
154 /* Can't allocate this until we know the stream's unitsize. */
155 if (!(ctx->prevsample = g_malloc0(logic->unitsize))) {
156 g_free(ctx);
157 sr_err("%s: ctx->prevsample malloc failed", __func__);
158 return SR_ERR_MALLOC;
159 }
20ebd1fe 160 }
a821069b 161
e2ad47b5
UH
162 if (ctx->header) {
163 /* The header is still here, this must be the first packet. */
b240ee08 164 *out = ctx->header;
e2ad47b5 165 ctx->header = NULL;
b240ee08
BV
166 ctx->samplecount = 0;
167 } else {
168 *out = g_string_sized_new(512);
e2ad47b5
UH
169 }
170
b240ee08
BV
171 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
172 sample = logic->data + i;
173 ctx->samplecount++;
e2ad47b5 174
50959ddc 175 /*
b240ee08 176 * Don't output the same sample multiple times, but make
50959ddc
UH
177 * sure to output at least the first and last sample.
178 */
b240ee08
BV
179 if (i > 0 && i < logic->length - logic->unitsize) {
180 if (!memcmp(sample, ctx->prevsample, logic->unitsize))
50959ddc
UH
181 continue;
182 }
b240ee08 183 memcpy(ctx->prevsample, sample, logic->unitsize);
50959ddc 184
e2ad47b5 185 /* The first column is a counter (needed for gnuplot). */
b240ee08 186 g_string_append_printf(*out, "%" PRIu64 "\t", ctx->samplecount);
e2ad47b5
UH
187
188 /* The next columns are the values of all channels. */
ba7dd8bb 189 for (p = 0; p < ctx->num_enabled_channels; p++) {
b240ee08
BV
190 idx = ctx->channel_index[p];
191 curbit = (sample[idx / 8] & ((uint8_t) (1 << (idx % 8)))) >> (idx % 8);
192 g_string_append_printf(*out, "%d ", curbit);
e2ad47b5 193 }
b240ee08 194 g_string_append_printf(*out, "\n");
e2ad47b5
UH
195 }
196
b240ee08
BV
197 return SR_OK;
198}
199
200static int cleanup(struct sr_output *o)
201{
202 struct context *ctx;
203
204 if (!o || !o->internal)
205 return SR_ERR_BUG;
206 ctx = o->internal;
207 g_free(ctx->channel_index);
208 g_free(ctx->prevsample);
209 if (ctx->header)
210 g_string_free(ctx->header, TRUE);
211 g_free(ctx);
e2ad47b5 212
e46b8fb1 213 return SR_OK;
e2ad47b5
UH
214}
215
7c1d391c 216SR_PRIV struct sr_output_format output_gnuplot = {
cdb3573c 217 .id = "gnuplot",
d494a4aa 218 .description = "Gnuplot",
d494a4aa 219 .init = init,
b240ee08
BV
220 .receive = receive,
221 .cleanup = cleanup,
77b45442 222};