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
28 unsigned int num_enabled_probes;
29 unsigned int unitsize;
34 const char *gnuplot_header = "\
35 # Sample data in space-separated columns format usable by gnuplot\n\
37 # Generated by: %s on %s\n\
38 # Comment: Acquisition with %d/%d probes at %s\n\
40 # Column assignment:\n%s\n";
42 static int init(struct output *o)
44 /* Maximum header length */
45 #define MAX_HEADER_LEN 2048
53 char *c, *samplerate_s;
56 ctx = malloc(sizeof(struct context));
58 return SIGROK_ERR_MALLOC;
60 ctx->num_enabled_probes = 0;
61 for (l = o->device->probes; l; l = l->next) {
64 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
67 ctx->probelist[ctx->num_enabled_probes] = 0;
68 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
70 /* TODO: Allow for configuration via o->param. */
72 ctx->header = calloc(1, MAX_HEADER_LEN + 1);
73 if (ctx->header == NULL)
74 return SIGROK_ERR_MALLOC;
75 num_probes = g_slist_length(o->device->probes);
76 /* TODO: Handle num_probes == 0, too many probes, etc. */
77 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
78 o->device->plugin_index, DI_CUR_SAMPLERATE));
80 if ((samplerate_s = sigrok_samplerate_string(samplerate)) == NULL)
81 return -1; /* FIXME */
83 /* Columns / channels */
85 for (i = 0; i < ctx->num_enabled_probes; i++) {
86 c = (char *)&wbuf + strlen((char *)&wbuf);
87 sprintf(c, "# Column %d: channel %s\n", i, ctx->probelist[i]);
90 /* TODO: date: File or signals? Make y/n configurable. */
92 b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
93 PACKAGE_STRING, "TODO", ctx->num_enabled_probes,
94 num_probes, samplerate_s, 1, "ns", (char *)&wbuf);
98 /* TODO: Handle snprintf errors. */
103 static int event(struct output *o, int event_type, char **data_out,
104 uint64_t *length_out)
108 int outlen = 1; /* FIXME */
111 switch (event_type) {
115 outbuf = calloc(1, 1); /* FIXME */
117 return SIGROK_ERR_MALLOC;
119 *length_out = outlen;
128 static int data(struct output *o, char *data_in, uint64_t length_in,
129 char **data_out, uint64_t *length_out)
132 unsigned int offset, outsize, p, curbit;
133 uint64_t sample, count = 0;
137 outsize = strlen(ctx->header);
138 outbuf = calloc(1, outsize + 1 + 10000); /* FIXME: Use realloc(). */
140 return SIGROK_ERR_MALLOC;
142 /* The header is still here, this must be the first packet. */
143 strncpy(outbuf, ctx->header, outsize);
150 /* TODO: Are disabled probes handled correctly? */
152 for (offset = 0; offset <= length_in - ctx->unitsize;
153 offset += ctx->unitsize) {
154 memcpy(&sample, data_in + offset, ctx->unitsize);
156 /* The first column is a counter (needed for gnuplot). */
157 c = outbuf + strlen(outbuf);
158 sprintf(c, "%" PRIu64 "\t\t", count++);
160 /* The next columns are the values of all channels. */
161 for (p = 0; p < ctx->num_enabled_probes; p++) {
162 curbit = (sample & ((uint64_t) (1 << p))) != 0;
163 c = outbuf + strlen(outbuf);
164 sprintf(c, "%d ", curbit);
167 c = outbuf + strlen(outbuf);
170 /* TODO: realloc() if strlen(outbuf) is almost "full"... */
174 *length_out = strlen(outbuf);
179 struct output_format output_gnuplot = {