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
26 #include "sigrok-internal.h"
29 unsigned int num_enabled_probes;
30 unsigned int unitsize;
31 char *probelist[SR_MAX_NUM_PROBES + 1];
35 #define MAX_HEADER_LEN \
36 (1024 + (SR_MAX_NUM_PROBES * (SR_MAX_PROBENAME_LEN + 10)))
38 static const char *gnuplot_header = "\
39 # Sample data in space-separated columns format usable by gnuplot\n\
41 # Generated by: %s on %s%s\
45 # -------------------------------------\
46 ----------------------------------------\n\
47 # 0\t\tSample counter (for internal gnuplot purposes)\n%s\n";
49 static const char *gnuplot_header_comment = "\
50 # Comment: Acquisition with %d/%d probes at %s\n";
52 static int init(struct sr_output *o)
55 struct sr_probe *probe;
60 char *c, *frequency_s;
61 char wbuf[1000], comment[128];
65 sr_err("gnuplot out: %s: o was NULL", __func__);
70 sr_err("gnuplot out: %s: o->dev was NULL", __func__);
74 if (!o->dev->plugin) {
75 sr_err("gnuplot out: %s: o->dev->plugin was NULL", __func__);
79 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
80 sr_err("gnuplot out: %s: ctx malloc failed", __func__);
84 if (!(ctx->header = g_try_malloc0(MAX_HEADER_LEN + 1))) {
85 sr_err("gnuplot out: %s: ctx->header malloc failed", __func__);
91 ctx->num_enabled_probes = 0;
92 for (l = o->dev->probes; l; l = l->next) {
93 probe = l->data; /* TODO: Error checks. */
96 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
98 ctx->probelist[ctx->num_enabled_probes] = 0;
99 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
101 num_probes = g_slist_length(o->dev->probes);
103 if (sr_dev_has_hwcap(o->dev, SR_HWCAP_SAMPLERATE)) {
104 samplerate = *((uint64_t *) o->dev->plugin->dev_info_get(
105 o->dev->plugin_index, SR_DI_CUR_SAMPLERATE));
106 if (!(frequency_s = sr_samplerate_string(samplerate))) {
107 sr_err("gnuplot out: %s: sr_samplerate_string failed",
113 snprintf(comment, 127, gnuplot_header_comment,
114 ctx->num_enabled_probes, num_probes, frequency_s);
118 /* Columns / channels */
120 for (i = 0; i < ctx->num_enabled_probes; i++) {
121 c = (char *)&wbuf + strlen((char *)&wbuf);
122 sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]);
125 if (!(frequency_s = sr_period_string(samplerate))) {
126 sr_err("gnuplot out: %s: sr_period_string failed", __func__);
133 b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
134 PACKAGE_STRING, ctime(&t), comment, frequency_s,
139 sr_err("gnuplot out: %s: sprintf failed", __func__);
148 static int event(struct sr_output *o, int event_type, char **data_out,
149 uint64_t *length_out)
154 sr_err("gnuplot out: %s: o was NULL", __func__);
159 sr_err("gnuplot out: %s: data_out was NULL", __func__);
164 sr_err("gnuplot out: %s: length_out was NULL", __func__);
170 switch (event_type) {
172 /* TODO: Can a trigger mark be in a gnuplot data file? */
179 sr_err("gnuplot out: %s: unsupported event type: %d",
180 __func__, event_type);
190 static int data(struct sr_output *o, const char *data_in, uint64_t length_in,
191 char **data_out, uint64_t *length_out)
194 unsigned int max_linelen, outsize, p, curbit, i;
196 static uint64_t samplecount = 0, old_sample = 0;
200 sr_err("gnuplot out: %s: o was NULL", __func__);
205 sr_err("gnuplot out: %s: o->internal was NULL", __func__);
210 sr_err("gnuplot out: %s: data_in was NULL", __func__);
215 sr_err("gnuplot out: %s: data_out was NULL", __func__);
220 sr_err("gnuplot out: %s: length_out was NULL", __func__);
225 max_linelen = 16 + ctx->num_enabled_probes * 2;
226 outsize = length_in / ctx->unitsize * max_linelen;
228 outsize += strlen(ctx->header);
230 if (!(outbuf = g_try_malloc0(outsize))) {
231 sr_err("gnuplot out: %s: outbuf malloc failed", __func__);
232 return SR_ERR_MALLOC;
237 /* The header is still here, this must be the first packet. */
238 strncpy(outbuf, ctx->header, outsize);
243 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
245 memcpy(&sample, data_in + i, ctx->unitsize);
248 * Don't output the same samples multiple times. However, make
249 * sure to output at least the first and last sample.
251 if (samplecount++ != 0 && sample == old_sample) {
252 if (i != (length_in - ctx->unitsize))
257 /* The first column is a counter (needed for gnuplot). */
258 c = outbuf + strlen(outbuf);
259 sprintf(c, "%" PRIu64 "\t", samplecount++);
261 /* The next columns are the values of all channels. */
262 for (p = 0; p < ctx->num_enabled_probes; p++) {
263 curbit = (sample & ((uint64_t) (1 << p))) >> p;
264 c = outbuf + strlen(outbuf);
265 sprintf(c, "%d ", curbit);
268 c = outbuf + strlen(outbuf);
273 *length_out = strlen(outbuf);
278 SR_PRIV struct sr_output_format output_gnuplot = {
280 .description = "Gnuplot",
281 .df_type = SR_DF_LOGIC,
287 /* Temporarily disabled. */
289 static int analog_init(struct sr_output *o)
292 struct sr_probe *probe;
297 char *c, *frequency_s;
298 char wbuf[1000], comment[128];
301 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
302 sr_err("gnuplot out: %s: ctx malloc failed", __func__);
303 return SR_ERR_MALLOC;
306 if (!(ctx->header = g_try_malloc0(MAX_HEADER_LEN + 1))) {
308 sr_err("gnuplot out: %s: ctx->header malloc failed", __func__);
309 return SR_ERR_MALLOC;
313 ctx->num_enabled_probes = 0;
314 for (l = o->dev->probes; l; l = l->next) {
318 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
320 ctx->probelist[ctx->num_enabled_probes] = 0;
321 // ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
322 ctx->unitsize = sizeof(struct sr_analog_sample) +
323 (ctx->num_enabled_probes * sizeof(struct sr_analog_probe));
325 num_probes = g_slist_length(o->dev->probes);
327 if (o->dev->plugin && sr_dev_has_hwcap(o->dev, SR_HWCAP_SAMPLERATE)) {
328 samplerate = *((uint64_t *) o->dev->plugin->dev_info_get(
329 o->dev->plugin_index, SR_DI_CUR_SAMPLERATE));
330 if (!(frequency_s = sr_samplerate_string(samplerate))) {
335 snprintf(comment, 127, gnuplot_header_comment,
336 ctx->num_enabled_probes, num_probes, frequency_s);
340 /* Columns / channels */
342 for (i = 0; i < ctx->num_enabled_probes; i++) {
343 c = (char *)&wbuf + strlen((char *)&wbuf);
344 sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]);
347 if (!(frequency_s = sr_period_string(samplerate))) {
353 b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
354 PACKAGE_STRING, ctime(&t), comment, frequency_s,
367 static int analog_data(struct sr_output *o, char *data_in, uint64_t length_in,
368 char **data_out, uint64_t *length_out)
371 unsigned int max_linelen, outsize, p, /* curbit, */ i;
373 static uint64_t samplecount = 0;
375 struct sr_analog_sample *sample;
378 // max_linelen = 16 + ctx->num_enabled_probes * 2;
379 max_linelen = 16 + ctx->num_enabled_probes * 30;
380 outsize = length_in / ctx->unitsize * max_linelen;
382 outsize += strlen(ctx->header);
384 if (!(outbuf = g_try_malloc0(outsize))) {
385 sr_err("gnuplot out: %s: outbuf malloc failed", __func__);
386 return SR_ERR_MALLOC;
391 /* The header is still here, this must be the first packet. */
392 strncpy(outbuf, ctx->header, outsize);
397 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
398 // memcpy(&sample, data_in + i, ctx->unitsize);
399 sample = (struct sr_analog_sample *) (data_in + i);
401 /* The first column is a counter (needed for gnuplot). */
402 c = outbuf + strlen(outbuf);
403 sprintf(c, "%" PRIu64 "\t", samplecount++);
405 /* The next columns are the values of all channels. */
406 for (p = 0; p < ctx->num_enabled_probes; p++) {
407 // curbit = (sample & ((uint64_t) (1 << p))) >> p;
408 c = outbuf + strlen(outbuf);
409 // sprintf(c, "%d ", curbit);
411 * FIXME: Should be doing proper raw->voltage conversion
412 * here, casting to int16_t isn't it. Remember that if
413 * res = 1 conversion isn't necessary.
415 sprintf(c, "%f ", (double) ((int16_t) (sample->probes[p].val &
416 ((1 << sample->probes[p].res) - 1))));
419 c = outbuf + strlen(outbuf);
424 *length_out = strlen(outbuf);
429 struct sr_output_format output_analog_gnuplot = {
430 .id = "analog_gnuplot",
431 .description = "Gnuplot analog",
432 .df_type = SR_DF_ANALOG,