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 int num_enabled_probes;
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 sbuf[10], wbuf[1000];
55 ctx = malloc(sizeof(struct context));
57 return SIGROK_ERR_MALLOC;
59 ctx->num_enabled_probes = 0;
60 for (l = o->device->probes; l; l = l->next) {
63 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
66 ctx->probelist[ctx->num_enabled_probes] = 0;
67 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
69 /* TODO: Allow for configuration via o->param. */
71 ctx->header = calloc(1, MAX_HEADER_LEN + 1);
72 if (ctx->header == NULL)
73 return SIGROK_ERR_MALLOC;
74 num_probes = g_slist_length(o->device->probes);
75 /* TODO: Handle num_probes == 0, too many probes, etc. */
76 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
77 o->device->plugin_index, DI_CUR_SAMPLERATE));
79 /* Samplerate string */
80 if (samplerate >= GHZ(1))
81 snprintf(sbuf, 10, "%"PRIu64" GHz", samplerate / 1000000000);
82 else if (samplerate >= MHZ(1))
83 snprintf(sbuf, 10, "%"PRIu64" MHz", samplerate / 1000000);
84 else if (samplerate >= KHZ(1))
85 snprintf(sbuf, 10, "%"PRIu64" KHz", samplerate / 1000);
87 snprintf(sbuf, 10, "%"PRIu64" Hz", samplerate);
89 /* Columns / channels */
91 for (i = 0; i < ctx->num_enabled_probes; i++) {
92 c = (char *)&wbuf + strlen((char *)&wbuf);
93 sprintf(c, "# Column %d: channel %s\n", i, ctx->probelist[i]);
96 /* TODO: date: File or signals? Make y/n configurable. */
98 b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
99 PACKAGE_STRING, "TODO", ctx->num_enabled_probes,
100 num_probes, (char *)&sbuf, 1, "ns", (char *)&wbuf);
102 /* TODO: Handle snprintf errors. */
107 static int event(struct output *o, int event_type, char **data_out,
108 uint64_t *length_out)
112 int outlen = 1; /* FIXME */
119 outbuf = calloc(1, 1); // FIXME
121 return SIGROK_ERR_MALLOC;
123 *length_out = outlen;
132 static int data(struct output *o, char *data_in, uint64_t length_in,
133 char **data_out, uint64_t *length_out)
136 int offset, outsize, p, curbit;
137 uint64_t sample, count = 0;
141 outsize = strlen(ctx->header);
142 outbuf = calloc(1, outsize + 1 + 10000); // FIXME: Use realloc().
144 return SIGROK_ERR_MALLOC;
146 /* The header is still here, this must be the first packet. */
147 strncpy(outbuf, ctx->header, outsize);
154 /* TODO: Are disabled probes handled correctly? */
156 for (offset = 0; offset <= length_in - ctx->unitsize;
157 offset += ctx->unitsize) {
158 memcpy(&sample, data_in + offset, ctx->unitsize);
160 /* The first column is a counter (needed for gnuplot). */
161 c = outbuf + strlen(outbuf);
162 sprintf(c, "%"PRIu64"\t\t", count++);
164 /* The next columns are the values of all channels. */
165 for (p = 0; p < ctx->num_enabled_probes; p++) {
166 curbit = (sample & ((uint64_t) (1 << p))) != 0;
167 c = outbuf + strlen(outbuf);
168 sprintf(c, "%d ", curbit);
171 c = outbuf + strlen(outbuf);
174 /* TODO: realloc() if strlen(outbuf) is almost "full"... */
178 *length_out = strlen(outbuf);
183 struct output_format output_gnuplot = {