]> sigrok.org Git - libsigrok.git/blame_incremental - output/output_gnuplot.c
Constify some more 'char *' parameters.
[libsigrok.git] / output / output_gnuplot.c
... / ...
CommitLineData
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 unsigned int num_enabled_probes;
29 unsigned int unitsize;
30 char *probelist[MAX_NUM_PROBES+1];
31 char *header;
32};
33
34#define MAX_HEADER_LEN 1024 + (MAX_NUM_PROBES * (MAX_PROBENAME_LEN + 10))
35const char *gnuplot_header = "\
36# Sample data in space-separated columns format usable by gnuplot\n\
37#\n\
38# Generated by: %s on %s%s\
39# Period: %s\n\
40#\n\
41# Column\tProbe\n\
42# -------------------------------------\
43----------------------------------------\n\
44# 0\t\tSample counter (for internal gnuplot purposes)\n%s\n";
45
46const char *gnuplot_header_comment = "\
47# Comment: Acquisition with %d/%d probes at %s\n";
48
49static int init(struct sr_output *o)
50{
51 struct context *ctx;
52 struct sr_probe *probe;
53 GSList *l;
54 uint64_t samplerate;
55 unsigned int i;
56 int b, num_probes;
57 char *c, *frequency_s;
58 char wbuf[1000], comment[128];
59 time_t t;
60
61 if (!(ctx = calloc(1, sizeof(struct context))))
62 return SR_ERR_MALLOC;
63
64 if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) {
65 free(ctx);
66 return SR_ERR_MALLOC;
67 }
68
69 o->internal = ctx;
70 ctx->num_enabled_probes = 0;
71 for (l = o->device->probes; l; l = l->next) {
72 probe = l->data;
73 if (!probe->enabled)
74 continue;
75 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
76 }
77 ctx->probelist[ctx->num_enabled_probes] = 0;
78 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
79
80 num_probes = g_slist_length(o->device->probes);
81 comment[0] = '\0';
82 if (o->device->plugin && sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) {
83 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
84 o->device->plugin_index, SR_DI_CUR_SAMPLERATE));
85 if (!(frequency_s = sr_samplerate_string(samplerate))) {
86 free(ctx->header);
87 free(ctx);
88 return SR_ERR;
89 }
90 snprintf(comment, 127, gnuplot_header_comment,
91 ctx->num_enabled_probes, num_probes, frequency_s);
92 free(frequency_s);
93 }
94
95 /* Columns / channels */
96 wbuf[0] = '\0';
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]);
100 }
101
102 if (!(frequency_s = sr_period_string(samplerate))) {
103 free(ctx->header);
104 free(ctx);
105 return SR_ERR;
106 }
107 t = time(NULL);
108 b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
109 PACKAGE_STRING, ctime(&t), comment, frequency_s,
110 (char *)&wbuf);
111 free(frequency_s);
112
113 if (b < 0) {
114 free(ctx->header);
115 free(ctx);
116 return SR_ERR;
117 }
118
119 return 0;
120}
121
122static int event(struct sr_output *o, int event_type, char **data_out,
123 uint64_t *length_out)
124{
125 struct context *ctx;
126
127 ctx = o->internal;
128 switch (event_type) {
129 case SR_DF_TRIGGER:
130 /* TODO: can a trigger mark be in a gnuplot data file? */
131 break;
132 case SR_DF_END:
133 free(o->internal);
134 o->internal = NULL;
135 break;
136 }
137
138 *data_out = NULL;
139 *length_out = 0;
140
141 return SR_OK;
142}
143
144static int data(struct sr_output *o, const char *data_in, uint64_t length_in,
145 char **data_out, uint64_t *length_out)
146{
147 struct context *ctx;
148 unsigned int max_linelen, outsize, p, curbit, i;
149 uint64_t sample;
150 static uint64_t samplecount = 0;
151 char *outbuf, *c;
152
153 ctx = o->internal;
154 max_linelen = 16 + ctx->num_enabled_probes * 2;
155 outsize = length_in / ctx->unitsize * max_linelen;
156 if (ctx->header)
157 outsize += strlen(ctx->header);
158
159 if (!(outbuf = calloc(1, outsize)))
160 return SR_ERR_MALLOC;
161
162 outbuf[0] = '\0';
163 if (ctx->header) {
164 /* The header is still here, this must be the first packet. */
165 strncpy(outbuf, ctx->header, outsize);
166 free(ctx->header);
167 ctx->header = NULL;
168 }
169
170 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
171 memcpy(&sample, data_in + i, ctx->unitsize);
172
173 /* The first column is a counter (needed for gnuplot). */
174 c = outbuf + strlen(outbuf);
175 sprintf(c, "%" PRIu64 "\t", samplecount++);
176
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);
182 }
183
184 c = outbuf + strlen(outbuf);
185 sprintf(c, "\n");
186 }
187
188 *data_out = outbuf;
189 *length_out = strlen(outbuf);
190
191 return SR_OK;
192}
193
194struct sr_output_format output_gnuplot = {
195 "gnuplot",
196 "Gnuplot",
197 SR_DF_LOGIC,
198 init,
199 data,
200 event,
201};
202
203/* Temporarily disabled. */
204#if 0
205static int analog_init(struct sr_output *o)
206{
207 struct context *ctx;
208 struct sr_probe *probe;
209 GSList *l;
210 uint64_t samplerate;
211 unsigned int i;
212 int b, num_probes;
213 char *c, *frequency_s;
214 char wbuf[1000], comment[128];
215 time_t t;
216
217 if (!(ctx = calloc(1, sizeof(struct context))))
218 return SR_ERR_MALLOC;
219
220 if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) {
221 free(ctx);
222 return SR_ERR_MALLOC;
223 }
224
225 o->internal = ctx;
226 ctx->num_enabled_probes = 0;
227 for (l = o->device->probes; l; l = l->next) {
228 probe = l->data;
229 if (!probe->enabled)
230 continue;
231 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
232 }
233 ctx->probelist[ctx->num_enabled_probes] = 0;
234// ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
235 ctx->unitsize = sizeof(struct sr_analog_sample) +
236 (ctx->num_enabled_probes * sizeof(struct sr_analog_probe));
237
238 num_probes = g_slist_length(o->device->probes);
239 comment[0] = '\0';
240 if (o->device->plugin && sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) {
241 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
242 o->device->plugin_index, SR_DI_CUR_SAMPLERATE));
243 if (!(frequency_s = sr_samplerate_string(samplerate))) {
244 free(ctx->header);
245 free(ctx);
246 return SR_ERR;
247 }
248 snprintf(comment, 127, gnuplot_header_comment,
249 ctx->num_enabled_probes, num_probes, frequency_s);
250 free(frequency_s);
251 }
252
253 /* Columns / channels */
254 wbuf[0] = '\0';
255 for (i = 0; i < ctx->num_enabled_probes; i++) {
256 c = (char *)&wbuf + strlen((char *)&wbuf);
257 sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]);
258 }
259
260 if (!(frequency_s = sr_period_string(samplerate))) {
261 free(ctx->header);
262 free(ctx);
263 return SR_ERR;
264 }
265 t = time(NULL);
266 b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
267 PACKAGE_STRING, ctime(&t), comment, frequency_s,
268 (char *)&wbuf);
269 free(frequency_s);
270
271 if (b < 0) {
272 free(ctx->header);
273 free(ctx);
274 return SR_ERR;
275 }
276
277 return 0;
278}
279
280static int analog_data(struct sr_output *o, char *data_in, uint64_t length_in,
281 char **data_out, uint64_t *length_out)
282{
283 struct context *ctx;
284 unsigned int max_linelen, outsize, p, /* curbit, */ i;
285// uint64_t sample;
286 static uint64_t samplecount = 0;
287 char *outbuf, *c;
288 struct sr_analog_sample *sample;
289
290 ctx = o->internal;
291// max_linelen = 16 + ctx->num_enabled_probes * 2;
292 max_linelen = 16 + ctx->num_enabled_probes * 30;
293 outsize = length_in / ctx->unitsize * max_linelen;
294 if (ctx->header)
295 outsize += strlen(ctx->header);
296
297 if (!(outbuf = calloc(1, outsize)))
298 return SR_ERR_MALLOC;
299
300 outbuf[0] = '\0';
301 if (ctx->header) {
302 /* The header is still here, this must be the first packet. */
303 strncpy(outbuf, ctx->header, outsize);
304 free(ctx->header);
305 ctx->header = NULL;
306 }
307
308 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
309// memcpy(&sample, data_in + i, ctx->unitsize);
310 sample = (struct sr_analog_sample *) (data_in + i);
311
312 /* The first column is a counter (needed for gnuplot). */
313 c = outbuf + strlen(outbuf);
314 sprintf(c, "%" PRIu64 "\t", samplecount++);
315
316 /* The next columns are the values of all channels. */
317 for (p = 0; p < ctx->num_enabled_probes; p++) {
318// curbit = (sample & ((uint64_t) (1 << p))) >> p;
319 c = outbuf + strlen(outbuf);
320// sprintf(c, "%d ", curbit);
321 /*
322 * FIXME: Should be doing proper raw->voltage conversion
323 * here, casting to int16_t isn't it. Remember that if
324 * res = 1 conversion isn't necessary.
325 */
326 sprintf(c, "%f ", (double) ((int16_t) (sample->probes[p].val &
327 ((1 << sample->probes[p].res) - 1))));
328 }
329
330 c = outbuf + strlen(outbuf);
331 sprintf(c, "\n");
332 }
333
334 *data_out = outbuf;
335 *length_out = strlen(outbuf);
336
337 return SR_OK;
338}
339
340struct sr_output_format output_analog_gnuplot = {
341 "analog_gnuplot",
342 "Gnuplot analog",
343 SR_DF_ANALOG,
344 analog_init,
345 analog_data,
346 event,
347};
348#endif