2 * This file is part of the sigrok 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;
40 char *probelist[SR_MAX_NUM_PROBES + 1];
44 #define MAX_HEADER_LEN \
45 (1024 + (SR_MAX_NUM_PROBES * (SR_MAX_PROBENAME_LEN + 10)))
47 static const char *gnuplot_header = "\
48 # Sample data in space-separated columns format usable by gnuplot\n\
50 # Generated by: %s on %s%s\
54 # -------------------------------------\
55 ----------------------------------------\n\
56 # 0\t\tSample counter (for internal gnuplot purposes)\n%s\n";
58 static const char *gnuplot_header_comment = "\
59 # Comment: Acquisition with %d/%d probes at %s\n";
61 static int init(struct sr_output *o)
64 struct sr_probe *probe;
70 char *c, *frequency_s;
71 char wbuf[1000], comment[128];
75 sr_err("%s: o was NULL", __func__);
80 sr_err("%s: o->sdi was NULL", __func__);
84 if (!o->sdi->driver) {
85 sr_err("%s: o->sdi->driver was NULL", __func__);
89 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
90 sr_err("%s: ctx malloc failed", __func__);
94 if (!(ctx->header = g_try_malloc0(MAX_HEADER_LEN + 1))) {
95 sr_err("%s: ctx->header malloc failed", __func__);
101 ctx->num_enabled_probes = 0;
102 for (l = o->sdi->probes; l; l = l->next) {
106 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
108 ctx->probelist[ctx->num_enabled_probes] = 0;
109 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
111 num_probes = g_slist_length(o->sdi->probes);
114 if (sr_dev_has_option(o->sdi, SR_CONF_SAMPLERATE)) {
115 o->sdi->driver->config_get(SR_CONF_SAMPLERATE, &gvar, o->sdi);
116 samplerate = g_variant_get_uint64(gvar);
117 if (!(frequency_s = sr_samplerate_string(samplerate))) {
118 sr_err("%s: sr_samplerate_string failed", __func__);
121 g_variant_unref(gvar);
124 snprintf(comment, 127, gnuplot_header_comment,
125 ctx->num_enabled_probes, num_probes, frequency_s);
127 g_variant_unref(gvar);
130 /* Columns / channels */
132 for (i = 0; i < ctx->num_enabled_probes; i++) {
133 c = (char *)&wbuf + strlen((const char *)&wbuf);
134 sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]);
137 if (!(frequency_s = sr_period_string(samplerate))) {
138 sr_err("%s: sr_period_string failed", __func__);
145 b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
146 PACKAGE_STRING, ctime(&t), comment, frequency_s,
151 sr_err("%s: sprintf failed", __func__);
160 static int event(struct sr_output *o, int event_type, uint8_t **data_out,
161 uint64_t *length_out)
164 sr_err("%s: o was NULL", __func__);
169 sr_err("%s: data_out was NULL", __func__);
174 sr_err("%s: length_out was NULL", __func__);
178 switch (event_type) {
180 /* TODO: Can a trigger mark be in a gnuplot data file? */
187 sr_err("%s: unsupported event type: %d", __func__, event_type);
197 static int data(struct sr_output *o, const uint8_t *data_in,
198 uint64_t length_in, uint8_t **data_out, uint64_t *length_out)
201 unsigned int max_linelen, outsize, p, curbit, i;
203 static uint64_t samplecount = 0, old_sample = 0;
207 sr_err("%s: o was NULL", __func__);
212 sr_err("%s: o->internal was NULL", __func__);
217 sr_err("%s: data_in was NULL", __func__);
222 sr_err("%s: data_out was NULL", __func__);
227 sr_err("%s: length_out was NULL", __func__);
232 max_linelen = 16 + ctx->num_enabled_probes * 2;
233 outsize = length_in / ctx->unitsize * max_linelen;
235 outsize += strlen(ctx->header);
237 if (!(outbuf = g_try_malloc0(outsize))) {
238 sr_err("%s: outbuf malloc failed", __func__);
239 return SR_ERR_MALLOC;
244 /* The header is still here, this must be the first packet. */
245 strncpy((char *)outbuf, ctx->header, outsize);
250 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
252 memcpy(&sample, data_in + i, ctx->unitsize);
255 * Don't output the same samples multiple times. However, make
256 * sure to output at least the first and last sample.
258 if (samplecount++ != 0 && sample == old_sample) {
259 if (i != (length_in - ctx->unitsize))
264 /* The first column is a counter (needed for gnuplot). */
265 c = outbuf + strlen((const char *)outbuf);
266 sprintf((char *)c, "%" PRIu64 "\t", samplecount++);
268 /* The next columns are the values of all channels. */
269 for (p = 0; p < ctx->num_enabled_probes; p++) {
270 curbit = (sample & ((uint64_t) (1 << p))) >> p;
271 c = outbuf + strlen((const char *)outbuf);
272 sprintf((char *)c, "%d ", curbit);
275 c = outbuf + strlen((const char *)outbuf);
276 sprintf((char *)c, "\n");
280 *length_out = strlen((const char *)outbuf);
285 SR_PRIV struct sr_output_format output_gnuplot = {
287 .description = "Gnuplot",
288 .df_type = SR_DF_LOGIC,