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