]> sigrok.org Git - libsigrok.git/blame_incremental - output/gnuplot.c
sr/cli/gtk/qt: s/device/dev/ in many places.
[libsigrok.git] / 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 "config.h"
25#include "sigrok.h"
26#include "sigrok-internal.h"
27
28struct context {
29 unsigned int num_enabled_probes;
30 unsigned int unitsize;
31 char *probelist[SR_MAX_NUM_PROBES + 1];
32 char *header;
33};
34
35#define MAX_HEADER_LEN \
36 (1024 + (SR_MAX_NUM_PROBES * (SR_MAX_PROBENAME_LEN + 10)))
37
38static const char *gnuplot_header = "\
39# Sample data in space-separated columns format usable by gnuplot\n\
40#\n\
41# Generated by: %s on %s%s\
42# Period: %s\n\
43#\n\
44# Column\tProbe\n\
45# -------------------------------------\
46----------------------------------------\n\
47# 0\t\tSample counter (for internal gnuplot purposes)\n%s\n";
48
49static const char *gnuplot_header_comment = "\
50# Comment: Acquisition with %d/%d probes at %s\n";
51
52static int init(struct sr_output *o)
53{
54 struct context *ctx;
55 struct sr_probe *probe;
56 GSList *l;
57 uint64_t samplerate;
58 unsigned int i;
59 int b, num_probes;
60 char *c, *frequency_s;
61 char wbuf[1000], comment[128];
62 time_t t;
63
64 if (!o) {
65 sr_err("gnuplot out: %s: o was NULL", __func__);
66 return SR_ERR_ARG;
67 }
68
69 if (!o->dev) {
70 sr_err("gnuplot out: %s: o->dev was NULL", __func__);
71 return SR_ERR_ARG;
72 }
73
74 if (!o->dev->plugin) {
75 sr_err("gnuplot out: %s: o->dev->plugin was NULL", __func__);
76 return SR_ERR_ARG;
77 }
78
79 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
80 sr_err("gnuplot out: %s: ctx malloc failed", __func__);
81 return SR_ERR_MALLOC;
82 }
83
84 if (!(ctx->header = g_try_malloc0(MAX_HEADER_LEN + 1))) {
85 sr_err("gnuplot out: %s: ctx->header malloc failed", __func__);
86 g_free(ctx);
87 return SR_ERR_MALLOC;
88 }
89
90 o->internal = ctx;
91 ctx->num_enabled_probes = 0;
92 for (l = o->dev->probes; l; l = l->next) {
93 probe = l->data; /* TODO: Error checks. */
94 if (!probe->enabled)
95 continue;
96 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
97 }
98 ctx->probelist[ctx->num_enabled_probes] = 0;
99 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
100
101 num_probes = g_slist_length(o->dev->probes);
102 comment[0] = '\0';
103 if (sr_dev_has_hwcap(o->dev, SR_HWCAP_SAMPLERATE)) {
104 samplerate = *((uint64_t *) o->dev->plugin->get_dev_info(
105 o->dev->plugin_index, SR_DI_CUR_SAMPLERATE));
106 if (!(frequency_s = sr_samplerate_string(samplerate))) {
107 sr_err("gnuplot out: %s: sr_samplerate_string failed",
108 __func__);
109 g_free(ctx->header);
110 g_free(ctx);
111 return SR_ERR;
112 }
113 snprintf(comment, 127, gnuplot_header_comment,
114 ctx->num_enabled_probes, num_probes, frequency_s);
115 g_free(frequency_s);
116 }
117
118 /* Columns / channels */
119 wbuf[0] = '\0';
120 for (i = 0; i < ctx->num_enabled_probes; i++) {
121 c = (char *)&wbuf + strlen((char *)&wbuf);
122 sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]);
123 }
124
125 if (!(frequency_s = sr_period_string(samplerate))) {
126 sr_err("gnuplot out: %s: sr_period_string failed", __func__);
127 g_free(ctx->header);
128 g_free(ctx);
129 return SR_ERR;
130 }
131
132 t = time(NULL);
133 b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
134 PACKAGE_STRING, ctime(&t), comment, frequency_s,
135 (char *)&wbuf);
136 g_free(frequency_s);
137
138 if (b < 0) {
139 sr_err("gnuplot out: %s: sprintf failed", __func__);
140 g_free(ctx->header);
141 g_free(ctx);
142 return SR_ERR;
143 }
144
145 return 0;
146}
147
148static int event(struct sr_output *o, int event_type, char **data_out,
149 uint64_t *length_out)
150{
151 struct context *ctx;
152
153 if (!o) {
154 sr_err("gnuplot out: %s: o was NULL", __func__);
155 return SR_ERR_ARG;
156 }
157
158 if (!data_out) {
159 sr_err("gnuplot out: %s: data_out was NULL", __func__);
160 return SR_ERR_ARG;
161 }
162
163 if (!length_out) {
164 sr_err("gnuplot out: %s: length_out was NULL", __func__);
165 return SR_ERR_ARG;
166 }
167
168 ctx = o->internal;
169
170 switch (event_type) {
171 case SR_DF_TRIGGER:
172 /* TODO: Can a trigger mark be in a gnuplot data file? */
173 break;
174 case SR_DF_END:
175 g_free(o->internal);
176 o->internal = NULL;
177 break;
178 default:
179 sr_err("gnuplot out: %s: unsupported event type: %d",
180 __func__, event_type);
181 break;
182 }
183
184 *data_out = NULL;
185 *length_out = 0;
186
187 return SR_OK;
188}
189
190static int data(struct sr_output *o, const char *data_in, uint64_t length_in,
191 char **data_out, uint64_t *length_out)
192{
193 struct context *ctx;
194 unsigned int max_linelen, outsize, p, curbit, i;
195 uint64_t sample;
196 static uint64_t samplecount = 0, old_sample = 0;
197 char *outbuf, *c;
198
199 if (!o) {
200 sr_err("gnuplot out: %s: o was NULL", __func__);
201 return SR_ERR_ARG;
202 }
203
204 if (!o->internal) {
205 sr_err("gnuplot out: %s: o->internal was NULL", __func__);
206 return SR_ERR_ARG;
207 }
208
209 if (!data_in) {
210 sr_err("gnuplot out: %s: data_in was NULL", __func__);
211 return SR_ERR_ARG;
212 }
213
214 if (!data_out) {
215 sr_err("gnuplot out: %s: data_out was NULL", __func__);
216 return SR_ERR_ARG;
217 }
218
219 if (!length_out) {
220 sr_err("gnuplot out: %s: length_out was NULL", __func__);
221 return SR_ERR_ARG;
222 }
223
224 ctx = o->internal;
225 max_linelen = 16 + ctx->num_enabled_probes * 2;
226 outsize = length_in / ctx->unitsize * max_linelen;
227 if (ctx->header)
228 outsize += strlen(ctx->header);
229
230 if (!(outbuf = g_try_malloc0(outsize))) {
231 sr_err("gnuplot out: %s: outbuf malloc failed", __func__);
232 return SR_ERR_MALLOC;
233 }
234
235 outbuf[0] = '\0';
236 if (ctx->header) {
237 /* The header is still here, this must be the first packet. */
238 strncpy(outbuf, ctx->header, outsize);
239 g_free(ctx->header);
240 ctx->header = NULL;
241 }
242
243 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
244
245 memcpy(&sample, data_in + i, ctx->unitsize);
246
247 /*
248 * Don't output the same samples multiple times. However, make
249 * sure to output at least the first and last sample.
250 */
251 if (samplecount++ != 0 && sample == old_sample) {
252 if (i != (length_in - ctx->unitsize))
253 continue;
254 }
255 old_sample = sample;
256
257 /* The first column is a counter (needed for gnuplot). */
258 c = outbuf + strlen(outbuf);
259 sprintf(c, "%" PRIu64 "\t", samplecount++);
260
261 /* The next columns are the values of all channels. */
262 for (p = 0; p < ctx->num_enabled_probes; p++) {
263 curbit = (sample & ((uint64_t) (1 << p))) >> p;
264 c = outbuf + strlen(outbuf);
265 sprintf(c, "%d ", curbit);
266 }
267
268 c = outbuf + strlen(outbuf);
269 sprintf(c, "\n");
270 }
271
272 *data_out = outbuf;
273 *length_out = strlen(outbuf);
274
275 return SR_OK;
276}
277
278SR_PRIV struct sr_output_format output_gnuplot = {
279 .id = "gnuplot",
280 .description = "Gnuplot",
281 .df_type = SR_DF_LOGIC,
282 .init = init,
283 .data = data,
284 .event = event,
285};
286
287/* Temporarily disabled. */
288#if 0
289static int analog_init(struct sr_output *o)
290{
291 struct context *ctx;
292 struct sr_probe *probe;
293 GSList *l;
294 uint64_t samplerate;
295 unsigned int i;
296 int b, num_probes;
297 char *c, *frequency_s;
298 char wbuf[1000], comment[128];
299 time_t t;
300
301 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
302 sr_err("gnuplot out: %s: ctx malloc failed", __func__);
303 return SR_ERR_MALLOC;
304 }
305
306 if (!(ctx->header = g_try_malloc0(MAX_HEADER_LEN + 1))) {
307 g_free(ctx);
308 sr_err("gnuplot out: %s: ctx->header malloc failed", __func__);
309 return SR_ERR_MALLOC;
310 }
311
312 o->internal = ctx;
313 ctx->num_enabled_probes = 0;
314 for (l = o->dev->probes; l; l = l->next) {
315 probe = l->data;
316 if (!probe->enabled)
317 continue;
318 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
319 }
320 ctx->probelist[ctx->num_enabled_probes] = 0;
321// ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
322 ctx->unitsize = sizeof(struct sr_analog_sample) +
323 (ctx->num_enabled_probes * sizeof(struct sr_analog_probe));
324
325 num_probes = g_slist_length(o->dev->probes);
326 comment[0] = '\0';
327 if (o->dev->plugin && sr_dev_has_hwcap(o->dev, SR_HWCAP_SAMPLERATE)) {
328 samplerate = *((uint64_t *) o->dev->plugin->get_dev_info(
329 o->dev->plugin_index, SR_DI_CUR_SAMPLERATE));
330 if (!(frequency_s = sr_samplerate_string(samplerate))) {
331 g_free(ctx->header);
332 g_free(ctx);
333 return SR_ERR;
334 }
335 snprintf(comment, 127, gnuplot_header_comment,
336 ctx->num_enabled_probes, num_probes, frequency_s);
337 g_free(frequency_s);
338 }
339
340 /* Columns / channels */
341 wbuf[0] = '\0';
342 for (i = 0; i < ctx->num_enabled_probes; i++) {
343 c = (char *)&wbuf + strlen((char *)&wbuf);
344 sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]);
345 }
346
347 if (!(frequency_s = sr_period_string(samplerate))) {
348 g_free(ctx->header);
349 g_free(ctx);
350 return SR_ERR;
351 }
352 t = time(NULL);
353 b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
354 PACKAGE_STRING, ctime(&t), comment, frequency_s,
355 (char *)&wbuf);
356 g_free(frequency_s);
357
358 if (b < 0) {
359 g_free(ctx->header);
360 g_free(ctx);
361 return SR_ERR;
362 }
363
364 return 0;
365}
366
367static int analog_data(struct sr_output *o, char *data_in, uint64_t length_in,
368 char **data_out, uint64_t *length_out)
369{
370 struct context *ctx;
371 unsigned int max_linelen, outsize, p, /* curbit, */ i;
372// uint64_t sample;
373 static uint64_t samplecount = 0;
374 char *outbuf, *c;
375 struct sr_analog_sample *sample;
376
377 ctx = o->internal;
378// max_linelen = 16 + ctx->num_enabled_probes * 2;
379 max_linelen = 16 + ctx->num_enabled_probes * 30;
380 outsize = length_in / ctx->unitsize * max_linelen;
381 if (ctx->header)
382 outsize += strlen(ctx->header);
383
384 if (!(outbuf = g_try_malloc0(outsize))) {
385 sr_err("gnuplot out: %s: outbuf malloc failed", __func__);
386 return SR_ERR_MALLOC;
387 }
388
389 outbuf[0] = '\0';
390 if (ctx->header) {
391 /* The header is still here, this must be the first packet. */
392 strncpy(outbuf, ctx->header, outsize);
393 g_free(ctx->header);
394 ctx->header = NULL;
395 }
396
397 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
398// memcpy(&sample, data_in + i, ctx->unitsize);
399 sample = (struct sr_analog_sample *) (data_in + i);
400
401 /* The first column is a counter (needed for gnuplot). */
402 c = outbuf + strlen(outbuf);
403 sprintf(c, "%" PRIu64 "\t", samplecount++);
404
405 /* The next columns are the values of all channels. */
406 for (p = 0; p < ctx->num_enabled_probes; p++) {
407// curbit = (sample & ((uint64_t) (1 << p))) >> p;
408 c = outbuf + strlen(outbuf);
409// sprintf(c, "%d ", curbit);
410 /*
411 * FIXME: Should be doing proper raw->voltage conversion
412 * here, casting to int16_t isn't it. Remember that if
413 * res = 1 conversion isn't necessary.
414 */
415 sprintf(c, "%f ", (double) ((int16_t) (sample->probes[p].val &
416 ((1 << sample->probes[p].res) - 1))));
417 }
418
419 c = outbuf + strlen(outbuf);
420 sprintf(c, "\n");
421 }
422
423 *data_out = outbuf;
424 *length_out = strlen(outbuf);
425
426 return SR_OK;
427}
428
429struct sr_output_format output_analog_gnuplot = {
430 .id = "analog_gnuplot",
431 .description = "Gnuplot analog",
432 .df_type = SR_DF_ANALOG,
433 .init = analog_init,
434 .data = analog_data,
435 .event = event,
436};
437#endif