]> sigrok.org Git - libsigrok.git/blame - output/output_gnuplot.c
zeroplus: Fix compiler warnings.
[libsigrok.git] / output / output_gnuplot.c
CommitLineData
e2ad47b5
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include <stdlib.h>
22#include <string.h>
23#include <glib.h>
24#include <sigrok.h>
25#include "config.h"
26
27struct context {
28 int num_enabled_probes;
29 int unitsize;
30 char *probelist[65];
31 char *header;
32};
33
34const char *gnuplot_header = "\
35# Sample data in space-separated columns format usable by gnuplot\n\
36#\n\
37# Generated by: %s on %s\n\
38# Comment: Acquisition with %d/%d probes at %s\n\
39# Timescale: %d %s\n\
40# Column assignment:\n%s\n";
41
42static int init(struct output *o)
43{
44/* Maximum header length */
45#define MAX_HEADER_LEN 2048
46
47 struct context *ctx;
48 struct probe *probe;
49 GSList *l;
50 uint64_t samplerate;
51 int i, b, num_probes;
25e7d9b1
UH
52 char *c, *samplerate_s;
53 char wbuf[1000];
e2ad47b5
UH
54
55 ctx = malloc(sizeof(struct context));
56 if (ctx == NULL)
57 return SIGROK_ERR_MALLOC;
58 o->internal = ctx;
59 ctx->num_enabled_probes = 0;
60 for (l = o->device->probes; l; l = l->next) {
61 probe = l->data;
62 if (probe->enabled)
63 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
64 }
65
66 ctx->probelist[ctx->num_enabled_probes] = 0;
67 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
68
69 /* TODO: Allow for configuration via o->param. */
70
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));
25e7d9b1
UH
78
79 if ((samplerate_s = sigrok_samplerate_string(samplerate)) == NULL)
80 return -1; // FIXME
e2ad47b5
UH
81
82 /* Columns / channels */
83 wbuf[0] = '\0';
84 for (i = 0; i < ctx->num_enabled_probes; i++) {
85 c = (char *)&wbuf + strlen((char *)&wbuf);
86 sprintf(c, "# Column %d: channel %s\n", i, ctx->probelist[i]);
87 }
88
89 /* TODO: date: File or signals? Make y/n configurable. */
90 /* TODO: Timescale */
91 b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
92 PACKAGE_STRING, "TODO", ctx->num_enabled_probes,
25e7d9b1
UH
93 num_probes, samplerate_s, 1, "ns", (char *)&wbuf);
94
95 free(samplerate_s);
e2ad47b5
UH
96
97 /* TODO: Handle snprintf errors. */
98
99 return 0;
100}
101
102static int event(struct output *o, int event_type, char **data_out,
103 uint64_t *length_out)
104{
105 struct context *ctx;
106 char *outbuf;
107 int outlen = 1; /* FIXME */
108
109 ctx = o->internal;
110 switch(event_type) {
111 case DF_TRIGGER:
112 break;
113 case DF_END:
114 outbuf = calloc(1, 1); // FIXME
115 if (outbuf == NULL)
116 return SIGROK_ERR_MALLOC;
117 *data_out = outbuf;
118 *length_out = outlen;
119 free(o->internal);
120 o->internal = NULL;
121 break;
122 }
123
124 return SIGROK_OK;
125}
126
127static int data(struct output *o, char *data_in, uint64_t length_in,
128 char **data_out, uint64_t *length_out)
129{
130 struct context *ctx;
131 int offset, outsize, p, curbit;
132 uint64_t sample, count = 0;
133 char *outbuf, *c;
134
135 ctx = o->internal;
136 outsize = strlen(ctx->header);
137 outbuf = calloc(1, outsize + 1 + 10000); // FIXME: Use realloc().
138 if (outbuf == NULL)
139 return SIGROK_ERR_MALLOC;
140 if (ctx->header) {
141 /* The header is still here, this must be the first packet. */
142 strncpy(outbuf, ctx->header, outsize);
143 free(ctx->header);
144 ctx->header = NULL;
145 } else {
146 outbuf[0] = 0;
147 }
148
149 /* TODO: Are disabled probes handled correctly? */
150
151 for (offset = 0; offset <= length_in - ctx->unitsize;
152 offset += ctx->unitsize) {
153 memcpy(&sample, data_in + offset, ctx->unitsize);
154
155 /* The first column is a counter (needed for gnuplot). */
156 c = outbuf + strlen(outbuf);
157 sprintf(c, "%"PRIu64"\t\t", count++);
158
159 /* The next columns are the values of all channels. */
160 for (p = 0; p < ctx->num_enabled_probes; p++) {
161 curbit = (sample & ((uint64_t) (1 << p))) != 0;
162 c = outbuf + strlen(outbuf);
163 sprintf(c, "%d ", curbit);
164 }
165
166 c = outbuf + strlen(outbuf);
167 sprintf(c, "\n");
168
169 /* TODO: realloc() if strlen(outbuf) is almost "full"... */
170 }
171
172 *data_out = outbuf;
173 *length_out = strlen(outbuf);
174
175 return SIGROK_OK;
176}
177
178struct output_format output_gnuplot = {
179 "gnuplot",
180 "Gnuplot",
181 init,
182 data,
183 event,
184};