]> sigrok.org Git - libsigrok.git/blame_incremental - output/gnuplot.c
build: Portability fixes.
[libsigrok.git] / 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 <stdlib.h>
22#include <string.h>
23#include <glib.h>
24#include "config.h" /* Needed for PACKAGE_STRING and others. */
25#include "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
46
47static int init(struct sr_output *o)
48{
49 struct context *ctx;
50 struct sr_channel *ch;
51 GSList *l;
52 unsigned int i;
53
54 if (!o || !o->sdi)
55 return SR_ERR_ARG;
56
57 ctx = g_malloc0(sizeof(struct context));
58 o->internal = ctx;
59 ctx->num_enabled_channels = 0;
60 for (l = o->sdi->channels; l; l = l->next) {
61 ch = l->data;
62 if (ch->type != SR_CHANNEL_LOGIC)
63 continue;
64 if (!ch->enabled)
65 continue;
66 ctx->num_enabled_channels++;
67 }
68 if (ctx->num_enabled_channels <= 0) {
69 sr_err("No logic channel enabled.");
70 return SR_ERR;
71 }
72 ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
73
74 /* Once more to map the enabled channels. */
75 for (i = 0, l = o->sdi->channels; l; l = l->next) {
76 ch = l->data;
77 if (ch->type != SR_CHANNEL_LOGIC)
78 continue;
79 if (!ch->enabled)
80 continue;
81 ctx->channel_index[i++] = ch->index;
82 }
83
84 return SR_OK;
85}
86
87static GString *gen_header(struct sr_output *o)
88{
89 struct context *ctx;
90 struct sr_channel *ch;
91 GVariant *gvar;
92 GString *header;
93 time_t t;
94 unsigned int num_channels, i;
95 char *samplerate_s;
96
97 ctx = o->internal;
98 if (ctx->samplerate == 0) {
99 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
100 &gvar) == SR_OK) {
101 ctx->samplerate = g_variant_get_uint64(gvar);
102 g_variant_unref(gvar);
103 }
104 }
105
106 t = time(NULL);
107 header = g_string_sized_new(512);
108 g_string_printf(header, "%s", gnuplot_header);
109 g_string_append_printf(header, "# Generated by %s on %s",
110 PACKAGE_STRING, ctime(&t));
111
112 num_channels = g_slist_length(o->sdi->channels);
113 g_string_append_printf(header, "# Acquisition with %d/%d channels",
114 ctx->num_enabled_channels, num_channels);
115 if (ctx->samplerate != 0) {
116 samplerate_s = sr_samplerate_string(ctx->samplerate);
117 g_string_append_printf(header, " at %s", samplerate_s);
118 g_free(samplerate_s);
119 }
120 g_string_append_printf(header, "\n");
121
122 g_string_append_printf(header, "%s", gnuplot_header2);
123
124 /* Columns / channels */
125 for (i = 0; i < ctx->num_enabled_channels; i++) {
126 ch = g_slist_nth_data(o->sdi->channels, ctx->channel_index[i]);
127 g_string_append_printf(header, "# %d\t\t%s\n", i + 1, ch->name);
128 }
129
130 return header;
131}
132
133static int receive(struct sr_output *o, const struct sr_datafeed_packet *packet,
134 GString **out)
135{
136 const struct sr_datafeed_meta *meta;
137 const struct sr_datafeed_logic *logic;
138 const struct sr_config *src;
139 GSList *l;
140 struct context *ctx;
141 const uint8_t *sample;
142 unsigned int curbit, p, idx, i;
143
144 *out = NULL;
145 if (!o || !o->internal)
146 return SR_ERR_BUG;
147 ctx = o->internal;
148
149 if (packet->type == SR_DF_META) {
150 meta = packet->payload;
151 for (l = meta->config; l; l = l->next) {
152 src = l->data;
153 if (src->key != SR_CONF_SAMPLERATE)
154 continue;
155 ctx->samplerate = g_variant_get_uint64(src->data);
156 }
157 }
158
159 if (packet->type != SR_DF_LOGIC)
160 return SR_OK;
161 logic = packet->payload;
162
163 if (!ctx->prevsample) {
164 /* Can't allocate this until we know the stream's unitsize. */
165 ctx->prevsample = g_malloc0(logic->unitsize);
166 }
167
168 if (!ctx->header_done) {
169 *out = gen_header(o);
170 ctx->header_done = TRUE;
171 } else {
172 *out = g_string_sized_new(512);
173 }
174
175 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
176 sample = logic->data + i;
177 ctx->samplecount++;
178
179 /*
180 * Don't output the same sample multiple times, but make
181 * sure to output at least the first and last sample.
182 */
183 if (i > 0 && i < logic->length - logic->unitsize) {
184 if (!memcmp(sample, ctx->prevsample, logic->unitsize))
185 continue;
186 }
187 memcpy(ctx->prevsample, sample, logic->unitsize);
188
189 /* The first column is a counter (needed for gnuplot). */
190 g_string_append_printf(*out, "%" PRIu64 "\t", ctx->samplecount);
191
192 /* The next columns are the values of all channels. */
193 for (p = 0; p < ctx->num_enabled_channels; p++) {
194 idx = ctx->channel_index[p];
195 curbit = (sample[idx / 8] & ((uint8_t) (1 << (idx % 8)))) >> (idx % 8);
196 g_string_append_printf(*out, "%d ", curbit);
197 }
198 g_string_append_printf(*out, "\n");
199 }
200
201 return SR_OK;
202}
203
204static int cleanup(struct sr_output *o)
205{
206 struct context *ctx;
207
208 if (!o || !o->internal)
209 return SR_ERR_BUG;
210 ctx = o->internal;
211 g_free(ctx->channel_index);
212 g_free(ctx->prevsample);
213 g_free(ctx);
214
215 return SR_OK;
216}
217
218SR_PRIV struct sr_output_format output_gnuplot = {
219 .id = "gnuplot",
220 .description = "Gnuplot",
221 .init = init,
222 .receive = receive,
223 .cleanup = cleanup,
224};