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 /* Message logging helpers with driver-specific prefix string. */
29 #define DRIVER_LOG_DOMAIN "output/gnuplot: "
30 #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
31 #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
32 #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
33 #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
34 #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
35 #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
38 unsigned int num_enabled_probes;
39 unsigned int unitsize;
44 static const char *gnuplot_header = "\
45 # Sample data in space-separated columns format usable by gnuplot\n\
47 # Generated by: %s on %s%s\
51 # -------------------------------------\
52 ----------------------------------------\n\
53 # 0\t\tSample counter (for internal gnuplot purposes)\n%s\n";
55 static const char *gnuplot_header_comment = "\
56 # Comment: Acquisition with %d/%d probes at %s\n";
58 static int init(struct sr_output *o)
61 struct sr_probe *probe;
67 char *c, *frequency_s;
68 char wbuf[1000], comment[128];
72 sr_err("%s: o was NULL", __func__);
77 sr_err("%s: o->sdi was NULL", __func__);
81 if (!o->sdi->driver) {
82 sr_err("%s: o->sdi->driver was NULL", __func__);
86 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
87 sr_err("%s: ctx malloc failed", __func__);
92 ctx->num_enabled_probes = 0;
93 for (l = o->sdi->probes; l; l = l->next) {
96 ctx->num_enabled_probes++;
98 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
100 num_probes = g_slist_length(o->sdi->probes);
103 if (sr_dev_has_option(o->sdi, SR_CONF_SAMPLERATE)) {
104 o->sdi->driver->config_get(SR_CONF_SAMPLERATE, &gvar, o->sdi);
105 samplerate = g_variant_get_uint64(gvar);
106 if (!(frequency_s = sr_samplerate_string(samplerate))) {
107 sr_err("%s: sr_samplerate_string failed", __func__);
109 g_variant_unref(gvar);
112 snprintf(comment, 127, gnuplot_header_comment,
113 ctx->num_enabled_probes, num_probes, frequency_s);
115 g_variant_unref(gvar);
118 /* Columns / channels */
120 for (i = 0, l = o->sdi->probes; l; l = l->next, i++) {
124 c = (char *)&wbuf + strlen((const char *)&wbuf);
125 sprintf(c, "# %d\t\t%s\n", i + 1, probe->name);
128 if (!(frequency_s = sr_period_string(samplerate))) {
129 sr_err("%s: sr_period_string failed", __func__);
135 ctx->header = g_strdup_printf(gnuplot_header, PACKAGE_STRING,
136 ctime(&t), comment, frequency_s, (char *)&wbuf);
139 if (!(ctx->old_sample = g_try_malloc0(ctx->unitsize))) {
140 sr_err("%s: ctx->old_sample malloc failed", __func__);
143 return SR_ERR_MALLOC;
149 static int event(struct sr_output *o, int event_type, uint8_t **data_out,
150 uint64_t *length_out)
153 sr_err("%s: o was NULL", __func__);
158 sr_err("%s: data_out was NULL", __func__);
163 sr_err("%s: length_out was NULL", __func__);
167 switch (event_type) {
169 /* TODO: Can a trigger mark be in a gnuplot data file? */
176 sr_err("%s: unsupported event type: %d", __func__, event_type);
186 static int data(struct sr_output *o, const uint8_t *data_in,
187 uint64_t length_in, uint8_t **data_out, uint64_t *length_out)
190 unsigned int max_linelen, outsize, p, curbit, i;
191 const uint8_t *sample;
192 static uint64_t samplecount = 0;
196 sr_err("%s: o was NULL", __func__);
201 sr_err("%s: o->internal was NULL", __func__);
206 sr_err("%s: data_in was NULL", __func__);
211 sr_err("%s: data_out was NULL", __func__);
216 sr_err("%s: length_out was NULL", __func__);
221 max_linelen = 16 + ctx->num_enabled_probes * 2;
222 outsize = length_in / ctx->unitsize * max_linelen;
224 outsize += strlen(ctx->header);
226 if (!(outbuf = g_try_malloc0(outsize))) {
227 sr_err("%s: outbuf malloc failed", __func__);
228 return SR_ERR_MALLOC;
233 /* The header is still here, this must be the first packet. */
234 strncpy((char *)outbuf, ctx->header, outsize);
239 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
241 sample = data_in + i;
244 * Don't output the same samples multiple times. However, make
245 * sure to output at least the first and last sample.
247 if (samplecount++ != 0 &&
248 !memcmp(sample, ctx->old_sample, ctx->unitsize)) {
249 if (i != (length_in - ctx->unitsize))
252 memcpy(ctx->old_sample, sample, ctx->unitsize);
254 /* The first column is a counter (needed for gnuplot). */
255 c = outbuf + strlen((const char *)outbuf);
256 sprintf((char *)c, "%" PRIu64 "\t", samplecount++);
258 /* The next columns are the values of all channels. */
259 for (p = 0; p < ctx->num_enabled_probes; p++) {
260 curbit = (sample[p / 8] & ((uint8_t) (1 << (p % 8)))) >> (p % 8);
261 c = outbuf + strlen((const char *)outbuf);
262 sprintf((char *)c, "%d ", curbit);
265 c = outbuf + strlen((const char *)outbuf);
266 sprintf((char *)c, "\n");
270 *length_out = strlen((const char *)outbuf);
275 SR_PRIV struct sr_output_format output_gnuplot = {
277 .description = "Gnuplot",
278 .df_type = SR_DF_LOGIC,