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
25 #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_warn("gnuplot out: %s: o was NULL", __func__);
70 sr_warn("gnuplot out: %s: o->device was NULL", __func__);
74 if (!o->device->plugin) {
75 sr_warn("gnuplot out: %s: o->device->plugin was NULL",
80 if (!(ctx = calloc(1, sizeof(struct context)))) {
81 sr_warn("gnuplot out: %s: ctx calloc failed", __func__);
85 if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) {
86 sr_warn("gnuplot out: %s: ctx->header calloc failed",
93 ctx->num_enabled_probes = 0;
94 for (l = o->device->probes; l; l = l->next) {
95 probe = l->data; /* TODO: Error checks. */
98 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
100 ctx->probelist[ctx->num_enabled_probes] = 0;
101 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
103 num_probes = g_slist_length(o->device->probes);
105 if (sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) {
106 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
107 o->device->plugin_index, SR_DI_CUR_SAMPLERATE));
108 if (!(frequency_s = sr_samplerate_string(samplerate))) {
109 sr_warn("gnuplot out: %s: sr_samplerate_string failed",
115 snprintf(comment, 127, gnuplot_header_comment,
116 ctx->num_enabled_probes, num_probes, frequency_s);
120 /* Columns / channels */
122 for (i = 0; i < ctx->num_enabled_probes; i++) {
123 c = (char *)&wbuf + strlen((char *)&wbuf);
124 sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]);
127 if (!(frequency_s = sr_period_string(samplerate))) {
128 sr_warn("gnuplot out: %s: sr_period_string failed", __func__);
135 b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
136 PACKAGE_STRING, ctime(&t), comment, frequency_s,
141 sr_warn("gnuplot out: %s: sprintf failed", __func__);
150 static int event(struct sr_output *o, int event_type, char **data_out,
151 uint64_t *length_out)
156 sr_warn("gnuplot out: %s: o was NULL", __func__);
161 sr_warn("gnuplot out: %s: data_out was NULL", __func__);
166 sr_warn("gnuplot out: %s: length_out was NULL", __func__);
172 switch (event_type) {
174 /* TODO: Can a trigger mark be in a gnuplot data file? */
181 sr_warn("gnuplot out: %s: unsupported event type: %d",
182 __func__, event_type);
192 static int data(struct sr_output *o, const char *data_in, uint64_t length_in,
193 char **data_out, uint64_t *length_out)
196 unsigned int max_linelen, outsize, p, curbit, i;
198 static uint64_t samplecount = 0, old_sample = 0;
202 sr_warn("gnuplot out: %s: o was NULL", __func__);
207 sr_warn("gnuplot out: %s: o->internal was NULL", __func__);
212 sr_warn("gnuplot out: %s: data_in was NULL", __func__);
217 sr_warn("gnuplot out: %s: data_out was NULL", __func__);
222 sr_warn("gnuplot out: %s: length_out was NULL", __func__);
227 max_linelen = 16 + ctx->num_enabled_probes * 2;
228 outsize = length_in / ctx->unitsize * max_linelen;
230 outsize += strlen(ctx->header);
232 if (!(outbuf = calloc(1, outsize))) {
233 sr_warn("gnuplot out: %s: outbuf calloc failed", __func__);
234 return SR_ERR_MALLOC;
239 /* The header is still here, this must be the first packet. */
240 strncpy(outbuf, ctx->header, outsize);
245 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
247 memcpy(&sample, data_in + i, ctx->unitsize);
250 * Don't output the same samples multiple times. However, make
251 * sure to output at least the first and last sample.
253 if (samplecount++ != 0 && sample == old_sample) {
254 if (i != (length_in - ctx->unitsize))
259 /* The first column is a counter (needed for gnuplot). */
260 c = outbuf + strlen(outbuf);
261 sprintf(c, "%" PRIu64 "\t", samplecount++);
263 /* The next columns are the values of all channels. */
264 for (p = 0; p < ctx->num_enabled_probes; p++) {
265 curbit = (sample & ((uint64_t) (1 << p))) >> p;
266 c = outbuf + strlen(outbuf);
267 sprintf(c, "%d ", curbit);
270 c = outbuf + strlen(outbuf);
275 *length_out = strlen(outbuf);
280 struct sr_output_format output_gnuplot = {
282 .description = "Gnuplot",
283 .df_type = SR_DF_LOGIC,
289 /* Temporarily disabled. */
291 static int analog_init(struct sr_output *o)
294 struct sr_probe *probe;
299 char *c, *frequency_s;
300 char wbuf[1000], comment[128];
303 if (!(ctx = calloc(1, sizeof(struct context))))
304 return SR_ERR_MALLOC;
306 if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) {
308 return SR_ERR_MALLOC;
312 ctx->num_enabled_probes = 0;
313 for (l = o->device->probes; l; l = l->next) {
317 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
319 ctx->probelist[ctx->num_enabled_probes] = 0;
320 // ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
321 ctx->unitsize = sizeof(struct sr_analog_sample) +
322 (ctx->num_enabled_probes * sizeof(struct sr_analog_probe));
324 num_probes = g_slist_length(o->device->probes);
326 if (o->device->plugin && sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) {
327 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
328 o->device->plugin_index, SR_DI_CUR_SAMPLERATE));
329 if (!(frequency_s = sr_samplerate_string(samplerate))) {
334 snprintf(comment, 127, gnuplot_header_comment,
335 ctx->num_enabled_probes, num_probes, frequency_s);
339 /* Columns / channels */
341 for (i = 0; i < ctx->num_enabled_probes; i++) {
342 c = (char *)&wbuf + strlen((char *)&wbuf);
343 sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]);
346 if (!(frequency_s = sr_period_string(samplerate))) {
352 b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
353 PACKAGE_STRING, ctime(&t), comment, frequency_s,
366 static int analog_data(struct sr_output *o, char *data_in, uint64_t length_in,
367 char **data_out, uint64_t *length_out)
370 unsigned int max_linelen, outsize, p, /* curbit, */ i;
372 static uint64_t samplecount = 0;
374 struct sr_analog_sample *sample;
377 // max_linelen = 16 + ctx->num_enabled_probes * 2;
378 max_linelen = 16 + ctx->num_enabled_probes * 30;
379 outsize = length_in / ctx->unitsize * max_linelen;
381 outsize += strlen(ctx->header);
383 if (!(outbuf = calloc(1, outsize)))
384 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,