]> sigrok.org Git - libsigrok.git/blame_incremental - output/gnuplot.c
output/ascii: Rewrite for new output API.
[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 samplecount;
33 GString *header;
34 uint8_t *prevsample;
35 int *channel_index;
36};
37
38static const char *gnuplot_header = "\
39# Sample data in space-separated columns format usable by gnuplot\n\
40#\n\
41# Generated by: %s on %s%s\
42# Period: %s\n\
43#\n\
44# Column\tChannel\n\
45# -------------------------------------\
46----------------------------------------\n\
47# 0\t\tSample counter (for internal gnuplot purposes)\n%s\n";
48
49static const char *gnuplot_header_comment = "\
50# Comment: Acquisition with %d/%d channels at %s\n";
51
52static int init(struct sr_output *o)
53{
54 struct context *ctx;
55 struct sr_channel *ch;
56 GSList *l;
57 GVariant *gvar;
58 uint64_t samplerate;
59 unsigned int i, j;
60 int num_channels;
61 char *c, *frequency_s;
62 char wbuf[1000], comment[128];
63 time_t t;
64
65 if (!o)
66 return SR_ERR_ARG;
67
68 if (!o->sdi)
69 return SR_ERR_ARG;
70
71 if (!(ctx = g_malloc0(sizeof(struct context))))
72 return SR_ERR_MALLOC;
73
74 o->internal = ctx;
75 ctx->num_enabled_channels = 0;
76 for (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->num_enabled_channels++;
83 }
84 if (ctx->num_enabled_channels <= 0) {
85 sr_err("No logic channel enabled.");
86 return SR_ERR;
87 }
88 ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
89
90 num_channels = g_slist_length(o->sdi->channels);
91 comment[0] = '\0';
92 samplerate = 0;
93 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
94 &gvar) == SR_OK) {
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__);
99 g_free(ctx);
100 return SR_ERR;
101 }
102 snprintf(comment, 127, gnuplot_header_comment,
103 ctx->num_enabled_channels, num_channels, frequency_s);
104 g_free(frequency_s);
105 }
106
107 /* Columns / channels */
108 wbuf[0] = '\0';
109 for (i = 0, j = 0, l = o->sdi->channels; l; l = l->next, i++) {
110 ch = l->data;
111 if (ch->type != SR_CHANNEL_LOGIC)
112 continue;
113 if (!ch->enabled)
114 continue;
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;
119 }
120
121 if (!(frequency_s = sr_period_string(samplerate))) {
122 sr_err("%s: sr_period_string failed", __func__);
123 g_free(ctx);
124 return SR_ERR;
125 }
126
127 t = time(NULL);
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);
131 g_free(frequency_s);
132
133 return 0;
134}
135
136static int receive(struct sr_output *o, const struct sr_dev_inst *sdi,
137 const struct sr_datafeed_packet *packet, GString **out)
138{
139 const struct sr_datafeed_logic *logic;
140 struct context *ctx;
141 const uint8_t *sample;
142 unsigned int curbit, p, idx, i;
143
144 (void)sdi;
145
146 *out = NULL;
147 if (!o || !o->internal)
148 return SR_ERR_BUG;
149 ctx = o->internal;
150
151 if (packet->type != SR_DF_LOGIC)
152 return SR_OK;
153 logic = packet->payload;
154
155 if (!ctx->prevsample) {
156 /* Can't allocate this until we know the stream's unitsize. */
157 if (!(ctx->prevsample = g_malloc0(logic->unitsize))) {
158 g_free(ctx);
159 sr_err("%s: ctx->prevsample malloc failed", __func__);
160 return SR_ERR_MALLOC;
161 }
162 }
163
164 if (ctx->header) {
165 /* The header is still here, this must be the first packet. */
166 *out = ctx->header;
167 ctx->header = NULL;
168 ctx->samplecount = 0;
169 } else {
170 *out = g_string_sized_new(512);
171 }
172
173 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
174 sample = logic->data + i;
175 ctx->samplecount++;
176
177 /*
178 * Don't output the same sample multiple times, but make
179 * sure to output at least the first and last sample.
180 */
181 if (i > 0 && i < logic->length - logic->unitsize) {
182 if (!memcmp(sample, ctx->prevsample, logic->unitsize))
183 continue;
184 }
185 memcpy(ctx->prevsample, sample, logic->unitsize);
186
187 /* The first column is a counter (needed for gnuplot). */
188 g_string_append_printf(*out, "%" PRIu64 "\t", ctx->samplecount);
189
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);
195 }
196 g_string_append_printf(*out, "\n");
197 }
198
199 return SR_OK;
200}
201
202static int cleanup(struct sr_output *o)
203{
204 struct context *ctx;
205
206 if (!o || !o->internal)
207 return SR_ERR_BUG;
208 ctx = o->internal;
209 g_free(ctx->channel_index);
210 g_free(ctx->prevsample);
211 if (ctx->header)
212 g_string_free(ctx->header, TRUE);
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 .df_type = SR_DF_LOGIC,
222 .init = init,
223 .receive = receive,
224 .cleanup = cleanup,
225};