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->driver) {
75 sr_err("gnuplot out: %s: o->dev->driver 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->driver->dev_info_get(
105 o->dev->driver_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((const 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, uint8_t **data_out,
149 uint64_t *length_out)
152 sr_err("gnuplot out: %s: o was NULL", __func__);
157 sr_err("gnuplot out: %s: data_out was NULL", __func__);
162 sr_err("gnuplot out: %s: length_out was NULL", __func__);
166 switch (event_type) {
168 /* TODO: Can a trigger mark be in a gnuplot data file? */
175 sr_err("gnuplot out: %s: unsupported event type: %d",
176 __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;
192 static uint64_t samplecount = 0, old_sample = 0;
196 sr_err("gnuplot out: %s: o was NULL", __func__);
201 sr_err("gnuplot out: %s: o->internal was NULL", __func__);
206 sr_err("gnuplot out: %s: data_in was NULL", __func__);
211 sr_err("gnuplot out: %s: data_out was NULL", __func__);
216 sr_err("gnuplot out: %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("gnuplot out: %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 memcpy(&sample, data_in + i, ctx->unitsize);
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 && sample == old_sample) {
248 if (i != (length_in - ctx->unitsize))
253 /* The first column is a counter (needed for gnuplot). */
254 c = outbuf + strlen((const char *)outbuf);
255 sprintf((char *)c, "%" PRIu64 "\t", samplecount++);
257 /* The next columns are the values of all channels. */
258 for (p = 0; p < ctx->num_enabled_probes; p++) {
259 curbit = (sample & ((uint64_t) (1 << p))) >> p;
260 c = outbuf + strlen((const char *)outbuf);
261 sprintf((char *)c, "%d ", curbit);
264 c = outbuf + strlen((const char *)outbuf);
265 sprintf((char *)c, "\n");
269 *length_out = strlen((const char *)outbuf);
274 SR_PRIV struct sr_output_format output_gnuplot = {
276 .description = "Gnuplot",
277 .df_type = SR_DF_LOGIC,
283 /* Temporarily disabled. */
285 static int analog_init(struct sr_output *o)
288 struct sr_probe *probe;
293 char *c, *frequency_s;
294 char wbuf[1000], comment[128];
297 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
298 sr_err("gnuplot out: %s: ctx malloc failed", __func__);
299 return SR_ERR_MALLOC;
302 if (!(ctx->header = g_try_malloc0(MAX_HEADER_LEN + 1))) {
304 sr_err("gnuplot out: %s: ctx->header malloc failed", __func__);
305 return SR_ERR_MALLOC;
309 ctx->num_enabled_probes = 0;
310 for (l = o->dev->probes; l; l = l->next) {
314 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
316 ctx->probelist[ctx->num_enabled_probes] = 0;
317 // ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
318 ctx->unitsize = sizeof(struct sr_analog_sample) +
319 (ctx->num_enabled_probes * sizeof(struct sr_analog_probe));
321 num_probes = g_slist_length(o->dev->probes);
323 if (o->dev->driver && sr_dev_has_hwcap(o->dev, SR_HWCAP_SAMPLERATE)) {
324 samplerate = *((uint64_t *) o->dev->driver->dev_info_get(
325 o->dev->driver_index, SR_DI_CUR_SAMPLERATE));
326 if (!(frequency_s = sr_samplerate_string(samplerate))) {
331 snprintf(comment, 127, gnuplot_header_comment,
332 ctx->num_enabled_probes, num_probes, frequency_s);
336 /* Columns / channels */
338 for (i = 0; i < ctx->num_enabled_probes; i++) {
339 c = (char *)&wbuf + strlen((char *)&wbuf);
340 sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]);
343 if (!(frequency_s = sr_period_string(samplerate))) {
349 b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
350 PACKAGE_STRING, ctime(&t), comment, frequency_s,
363 static int analog_data(struct sr_output *o, uint8_t *data_in,
364 uint64_t length_in, uint8_t **data_out,
365 uint64_t *length_out)
368 unsigned int max_linelen, outsize, p, /* curbit, */ i;
370 static uint64_t samplecount = 0;
372 struct sr_analog_sample *sample;
375 // max_linelen = 16 + ctx->num_enabled_probes * 2;
376 max_linelen = 16 + ctx->num_enabled_probes * 30;
377 outsize = length_in / ctx->unitsize * max_linelen;
379 outsize += strlen(ctx->header);
381 if (!(outbuf = g_try_malloc0(outsize))) {
382 sr_err("gnuplot out: %s: outbuf malloc failed", __func__);
383 return SR_ERR_MALLOC;
388 /* The header is still here, this must be the first packet. */
389 strncpy(outbuf, ctx->header, outsize);
394 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
395 // memcpy(&sample, data_in + i, ctx->unitsize);
396 sample = (struct sr_analog_sample *) (data_in + i);
398 /* The first column is a counter (needed for gnuplot). */
399 c = outbuf + strlen(outbuf);
400 sprintf(c, "%" PRIu64 "\t", samplecount++);
402 /* The next columns are the values of all channels. */
403 for (p = 0; p < ctx->num_enabled_probes; p++) {
404 // curbit = (sample & ((uint64_t) (1 << p))) >> p;
405 c = outbuf + strlen(outbuf);
406 // sprintf(c, "%d ", curbit);
408 * FIXME: Should be doing proper raw->voltage conversion
409 * here, casting to int16_t isn't it. Remember that if
410 * res = 1 conversion isn't necessary.
412 sprintf(c, "%f ", (double) ((int16_t) (sample->probes[p].val &
413 ((1 << sample->probes[p].res) - 1))));
416 c = outbuf + strlen(outbuf);
421 *length_out = strlen(outbuf);
426 struct sr_output_format output_analog_gnuplot = {
427 .id = "analog_gnuplot",
428 .description = "Gnuplot analog",
429 .df_type = SR_DF_ANALOG,