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;
30 char *probelist[MAX_NUM_PROBES+1];
34 #define MAX_HEADER_LEN 1024 + (MAX_NUM_PROBES * (MAX_PROBENAME_LEN + 10))
35 const char *gnuplot_header = "\
36 # Sample data in space-separated columns format usable by gnuplot\n\
38 # Generated by: %s on %s%s\
42 # -------------------------------------\
43 ----------------------------------------\n\
44 # 0\t\tSample counter (for internal gnuplot purposes)\n%s\n";
46 const char *gnuplot_header_comment = "\
47 # Comment: Acquisition with %d/%d probes at %s\n";
49 static int init(struct output *o)
57 char *c, *frequency_s;
58 char wbuf[1000], comment[128];
61 if (!(ctx = calloc(1, sizeof(struct context))))
62 return SIGROK_ERR_MALLOC;
64 if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) {
66 return SIGROK_ERR_MALLOC;
70 ctx->num_enabled_probes = 0;
71 for (l = o->device->probes; l; l = l->next) {
75 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
77 ctx->probelist[ctx->num_enabled_probes] = 0;
78 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
80 num_probes = g_slist_length(o->device->probes);
82 if (o->device->plugin) {
83 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
84 o->device->plugin_index, DI_CUR_SAMPLERATE));
85 if (!(frequency_s = sigrok_samplerate_string(samplerate))) {
90 snprintf(comment, 127, gnuplot_header_comment,
91 ctx->num_enabled_probes, num_probes, frequency_s);
95 /* Columns / channels */
97 for (i = 0; i < ctx->num_enabled_probes; i++) {
98 c = (char *)&wbuf + strlen((char *)&wbuf);
99 sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]);
102 if (!(frequency_s = sigrok_period_string(samplerate))) {
108 b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
109 PACKAGE_STRING, ctime(&t), comment, frequency_s,
122 static int event(struct output *o, int event_type, char **data_out,
123 uint64_t *length_out)
128 switch (event_type) {
130 /* TODO: can a trigger mark be in a gnuplot data file? */
144 static int data(struct output *o, char *data_in, uint64_t length_in,
145 char **data_out, uint64_t *length_out)
148 unsigned int max_linelen, outsize, p, curbit, i;
150 static uint64_t samplecount = 0;
154 max_linelen = 16 + ctx->num_enabled_probes * 2;
155 outsize = length_in / ctx->unitsize * max_linelen;
157 outsize += strlen(ctx->header);
159 if (!(outbuf = calloc(1, outsize)))
160 return SIGROK_ERR_MALLOC;
164 /* The header is still here, this must be the first packet. */
165 strncpy(outbuf, ctx->header, outsize);
170 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
171 memcpy(&sample, data_in + i, ctx->unitsize);
173 /* The first column is a counter (needed for gnuplot). */
174 c = outbuf + strlen(outbuf);
175 sprintf(c, "%" PRIu64 "\t", samplecount++);
177 /* The next columns are the values of all channels. */
178 for (p = 0; p < ctx->num_enabled_probes; p++) {
179 curbit = (sample & ((uint64_t) (1 << p))) >> p;
180 c = outbuf + strlen(outbuf);
181 sprintf(c, "%d ", curbit);
184 c = outbuf + strlen(outbuf);
189 *length_out = strlen(outbuf);
194 struct output_format output_gnuplot = {