2 * This file is part of the libsigrok project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
24 #include "config.h" /* Needed for PACKAGE_STRING and others. */
25 #include "libsigrok.h"
26 #include "libsigrok-internal.h"
28 #define LOG_PREFIX "output/gnuplot"
31 unsigned int num_enabled_channels;
32 unsigned int unitsize;
37 static const char *gnuplot_header = "\
38 # Sample data in space-separated columns format usable by gnuplot\n\
40 # Generated by: %s on %s%s\
44 # -------------------------------------\
45 ----------------------------------------\n\
46 # 0\t\tSample counter (for internal gnuplot purposes)\n%s\n";
48 static const char *gnuplot_header_comment = "\
49 # Comment: Acquisition with %d/%d channels at %s\n";
51 static int init(struct sr_output *o)
54 struct sr_channel *ch;
60 char *c, *frequency_s;
61 char wbuf[1000], comment[128];
65 sr_err("%s: o was NULL", __func__);
70 sr_err("%s: o->sdi was NULL", __func__);
74 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
75 sr_err("%s: ctx malloc failed", __func__);
80 ctx->num_enabled_channels = 0;
81 for (l = o->sdi->channels; l; l = l->next) {
83 if (ch->type != SR_PROBE_LOGIC)
87 ctx->num_enabled_channels++;
89 if (ctx->num_enabled_channels <= 0) {
90 sr_err("%s: no logic channel enabled", __func__);
94 ctx->unitsize = (ctx->num_enabled_channels + 7) / 8;
96 num_channels = g_slist_length(o->sdi->channels);
99 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
101 samplerate = g_variant_get_uint64(gvar);
102 g_variant_unref(gvar);
103 if (!(frequency_s = sr_samplerate_string(samplerate))) {
104 sr_err("%s: sr_samplerate_string failed", __func__);
108 snprintf(comment, 127, gnuplot_header_comment,
109 ctx->num_enabled_channels, num_channels, frequency_s);
113 /* Columns / channels */
115 for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
117 if (ch->type != SR_PROBE_LOGIC)
121 c = (char *)&wbuf + strlen((const char *)&wbuf);
122 sprintf(c, "# %d\t\t%s\n", i + 1, ch->name);
125 if (!(frequency_s = sr_period_string(samplerate))) {
126 sr_err("%s: sr_period_string failed", __func__);
132 ctx->header = g_strdup_printf(gnuplot_header, PACKAGE_STRING,
133 ctime(&t), comment, frequency_s, (char *)&wbuf);
136 if (!(ctx->old_sample = g_try_malloc0(ctx->unitsize))) {
137 sr_err("%s: ctx->old_sample malloc failed", __func__);
140 return SR_ERR_MALLOC;
146 static int event(struct sr_output *o, int event_type, uint8_t **data_out,
147 uint64_t *length_out)
150 sr_err("%s: o was NULL", __func__);
155 sr_err("%s: data_out was NULL", __func__);
160 sr_err("%s: length_out was NULL", __func__);
164 switch (event_type) {
166 /* TODO: Can a trigger mark be in a gnuplot data file? */
173 sr_err("%s: unsupported event type: %d", __func__, event_type);
183 static int data(struct sr_output *o, const uint8_t *data_in,
184 uint64_t length_in, uint8_t **data_out, uint64_t *length_out)
187 unsigned int max_linelen, outsize, p, curbit, i;
188 const uint8_t *sample;
189 static uint64_t samplecount = 0;
193 sr_err("%s: o was NULL", __func__);
198 sr_err("%s: o->internal was NULL", __func__);
203 sr_err("%s: data_in was NULL", __func__);
208 sr_err("%s: data_out was NULL", __func__);
213 sr_err("%s: length_out was NULL", __func__);
218 max_linelen = 16 + ctx->num_enabled_channels * 2;
219 outsize = length_in / ctx->unitsize * max_linelen;
221 outsize += strlen(ctx->header);
223 if (!(outbuf = g_try_malloc0(outsize))) {
224 sr_err("%s: outbuf malloc failed", __func__);
225 return SR_ERR_MALLOC;
230 /* The header is still here, this must be the first packet. */
231 strncpy((char *)outbuf, ctx->header, outsize);
236 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
238 sample = data_in + i;
241 * Don't output the same samples multiple times. However, make
242 * sure to output at least the first and last sample.
244 if (samplecount++ != 0 &&
245 !memcmp(sample, ctx->old_sample, ctx->unitsize)) {
246 if (i != (length_in - ctx->unitsize))
249 memcpy(ctx->old_sample, sample, ctx->unitsize);
251 /* The first column is a counter (needed for gnuplot). */
252 c = outbuf + strlen((const char *)outbuf);
253 sprintf((char *)c, "%" PRIu64 "\t", samplecount++);
255 /* The next columns are the values of all channels. */
256 for (p = 0; p < ctx->num_enabled_channels; p++) {
257 curbit = (sample[p / 8] & ((uint8_t) (1 << (p % 8)))) >> (p % 8);
258 c = outbuf + strlen((const char *)outbuf);
259 sprintf((char *)c, "%d ", curbit);
262 c = outbuf + strlen((const char *)outbuf);
263 sprintf((char *)c, "\n");
267 *length_out = strlen((const char *)outbuf);
272 SR_PRIV struct sr_output_format output_gnuplot = {
274 .description = "Gnuplot",
275 .df_type = SR_DF_LOGIC,