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