]> sigrok.org Git - libsigrok.git/blame_incremental - src/output/gnuplot.c
output/wav: use the right buffer for SR_DF_ANALOG
[libsigrok.git] / src / output / gnuplot.c
... / ...
CommitLineData
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 <config.h>
22#include <stdlib.h>
23#include <string.h>
24#include <glib.h>
25#include <libsigrok/libsigrok.h>
26#include "libsigrok-internal.h"
27
28#define LOG_PREFIX "output/gnuplot"
29
30struct 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
39static const char *gnuplot_header = "\
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
46static int init(struct sr_output *o, GHashTable *options)
47{
48 struct context *ctx;
49 struct sr_channel *ch;
50 GSList *l;
51 unsigned int i;
52
53 (void)options;
54
55 if (!o || !o->sdi)
56 return SR_ERR_ARG;
57
58 ctx = g_malloc0(sizeof(struct context));
59 o->priv = ctx;
60 ctx->num_enabled_channels = 0;
61 for (l = o->sdi->channels; l; l = l->next) {
62 ch = l->data;
63 if (ch->type != SR_CHANNEL_LOGIC)
64 continue;
65 if (!ch->enabled)
66 continue;
67 ctx->num_enabled_channels++;
68 }
69 if (ctx->num_enabled_channels <= 0) {
70 sr_err("No logic channel enabled.");
71 return SR_ERR;
72 }
73 ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
74
75 /* Once more to map the enabled channels. */
76 for (i = 0, l = o->sdi->channels; l; l = l->next) {
77 ch = l->data;
78 if (ch->type != SR_CHANNEL_LOGIC)
79 continue;
80 if (!ch->enabled)
81 continue;
82 ctx->channel_index[i++] = ch->index;
83 }
84
85 return SR_OK;
86}
87
88static GString *gen_header(const struct sr_output *o)
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
98 ctx = o->priv;
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 }
105 }
106
107 t = time(NULL);
108 header = g_string_sized_new(512);
109 g_string_printf(header, "%s", gnuplot_header);
110 g_string_append_printf(header, "# Generated by %s %s on %s",
111 PACKAGE_NAME, SR_PACKAGE_VERSION_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");
122
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;
132}
133
134static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
135 GString **out)
136{
137 const struct sr_datafeed_meta *meta;
138 const struct sr_datafeed_logic *logic;
139 const struct sr_config *src;
140 GSList *l;
141 struct context *ctx;
142 const uint8_t *sample;
143 unsigned int curbit, p, idx, i;
144
145 *out = NULL;
146 if (!o || !o->priv)
147 return SR_ERR_BUG;
148 ctx = o->priv;
149
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
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. */
166 ctx->prevsample = g_malloc0(logic->unitsize);
167 }
168
169 if (!ctx->header_done) {
170 *out = gen_header(o);
171 ctx->header_done = TRUE;
172 } else {
173 *out = g_string_sized_new(512);
174 }
175
176 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
177 sample = logic->data + i;
178 ctx->samplecount++;
179
180 /*
181 * Don't output the same sample multiple times, but make
182 * sure to output at least the first and last sample.
183 */
184 if (i > 0 && i < logic->length - logic->unitsize) {
185 if (!memcmp(sample, ctx->prevsample, logic->unitsize))
186 continue;
187 }
188 memcpy(ctx->prevsample, sample, logic->unitsize);
189
190 /* The first column is a counter (needed for gnuplot). */
191 g_string_append_printf(*out, "%" PRIu64 "\t", ctx->samplecount);
192
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);
198 }
199 g_string_append_printf(*out, "\n");
200 }
201
202 return SR_OK;
203}
204
205static int cleanup(struct sr_output *o)
206{
207 struct context *ctx;
208
209 if (!o || !o->priv)
210 return SR_ERR_BUG;
211 ctx = o->priv;
212 g_free(ctx->channel_index);
213 g_free(ctx->prevsample);
214 g_free(ctx);
215
216 return SR_OK;
217}
218
219SR_PRIV struct sr_output_module output_gnuplot = {
220 .id = "gnuplot",
221 .name = "Gnuplot",
222 .desc = "Gnuplot data file format",
223 .exts = (const char*[]){"dat", NULL},
224 .flags = 0,
225 .options = NULL,
226 .init = init,
227 .receive = receive,
228 .cleanup = cleanup,
229};