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[SR_MAX_NUM_PROBES + 1];
34 #define MAX_HEADER_LEN \
35 (1024 + (SR_MAX_NUM_PROBES * (SR_MAX_PROBENAME_LEN + 10)))
37 static const char *gnuplot_header = "\
38 # Sample data in space-separated columns format usable by gnuplot\n\
40 # Generated by: %s on %s%s\
44 # -------------------------------------\
45 ----------------------------------------\n\
46 # 0\t\tSample counter (for internal gnuplot purposes)\n%s\n";
48 static const char *gnuplot_header_comment = "\
49 # Comment: Acquisition with %d/%d probes at %s\n";
51 static int init(struct sr_output *o)
54 struct sr_probe *probe;
59 char *c, *frequency_s;
60 char wbuf[1000], comment[128];
64 g_warning("gnuplot out: %s: o was NULL", __func__);
69 g_warning("gnuplot out: %s: o->device was NULL", __func__);
73 if (!o->device->plugin) {
74 g_warning("gnuplot out: %s: o->device->plugin was NULL",
79 if (!(ctx = calloc(1, sizeof(struct context)))) {
80 g_warning("gnuplot out: %s: ctx calloc failed", __func__);
84 if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) {
85 g_warning("gnuplot out: %s: ctx->header calloc failed",
92 ctx->num_enabled_probes = 0;
93 for (l = o->device->probes; l; l = l->next) {
94 probe = l->data; /* TODO: Error checks. */
97 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
99 ctx->probelist[ctx->num_enabled_probes] = 0;
100 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
102 num_probes = g_slist_length(o->device->probes);
104 if (sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) {
105 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
106 o->device->plugin_index, SR_DI_CUR_SAMPLERATE));
107 if (!(frequency_s = sr_samplerate_string(samplerate))) {
108 g_warning("gnuplot out: %s: sr_samplerate_string "
114 snprintf(comment, 127, gnuplot_header_comment,
115 ctx->num_enabled_probes, num_probes, frequency_s);
119 /* Columns / channels */
121 for (i = 0; i < ctx->num_enabled_probes; i++) {
122 c = (char *)&wbuf + strlen((char *)&wbuf);
123 sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]);
126 if (!(frequency_s = sr_period_string(samplerate))) {
127 g_warning("gnuplot out: %s: sr_period_string failed", __func__);
134 b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
135 PACKAGE_STRING, ctime(&t), comment, frequency_s,
140 g_warning("gnuplot out: %s: sprintf failed", __func__);
149 static int event(struct sr_output *o, int event_type, char **data_out,
150 uint64_t *length_out)
155 g_warning("gnuplot out: %s: o was NULL", __func__);
160 g_warning("gnuplot out: %s: data_out was NULL", __func__);
165 g_warning("gnuplot out: %s: length_out was NULL", __func__);
171 switch (event_type) {
173 /* TODO: Can a trigger mark be in a gnuplot data file? */
180 g_warning("gnuplot out: %s: unsupported event type: %d",
181 __func__, event_type);
191 static int data(struct sr_output *o, const char *data_in, uint64_t length_in,
192 char **data_out, uint64_t *length_out)
195 unsigned int max_linelen, outsize, p, curbit, i;
197 static uint64_t samplecount = 0;
201 g_warning("gnuplot out: %s: o was NULL", __func__);
206 g_warning("gnuplot out: %s: o->internal was NULL", __func__);
211 g_warning("gnuplot out: %s: data_in was NULL", __func__);
216 g_warning("gnuplot out: %s: data_out was NULL", __func__);
221 g_warning("gnuplot out: %s: length_out was NULL", __func__);
226 max_linelen = 16 + ctx->num_enabled_probes * 2;
227 outsize = length_in / ctx->unitsize * max_linelen;
229 outsize += strlen(ctx->header);
231 if (!(outbuf = calloc(1, outsize))) {
232 g_warning("gnuplot out: %s: outbuf calloc failed", __func__);
233 return SR_ERR_MALLOC;
238 /* The header is still here, this must be the first packet. */
239 strncpy(outbuf, ctx->header, outsize);
244 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
245 memcpy(&sample, data_in + i, ctx->unitsize);
247 /* The first column is a counter (needed for gnuplot). */
248 c = outbuf + strlen(outbuf);
249 sprintf(c, "%" PRIu64 "\t", samplecount++);
251 /* The next columns are the values of all channels. */
252 for (p = 0; p < ctx->num_enabled_probes; p++) {
253 curbit = (sample & ((uint64_t) (1 << p))) >> p;
254 c = outbuf + strlen(outbuf);
255 sprintf(c, "%d ", curbit);
258 c = outbuf + strlen(outbuf);
263 *length_out = strlen(outbuf);
268 struct sr_output_format output_gnuplot = {
270 .description = "Gnuplot",
271 .df_type = SR_DF_LOGIC,
277 /* Temporarily disabled. */
279 static int analog_init(struct sr_output *o)
282 struct sr_probe *probe;
287 char *c, *frequency_s;
288 char wbuf[1000], comment[128];
291 if (!(ctx = calloc(1, sizeof(struct context))))
292 return SR_ERR_MALLOC;
294 if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) {
296 return SR_ERR_MALLOC;
300 ctx->num_enabled_probes = 0;
301 for (l = o->device->probes; l; l = l->next) {
305 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
307 ctx->probelist[ctx->num_enabled_probes] = 0;
308 // ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
309 ctx->unitsize = sizeof(struct sr_analog_sample) +
310 (ctx->num_enabled_probes * sizeof(struct sr_analog_probe));
312 num_probes = g_slist_length(o->device->probes);
314 if (o->device->plugin && sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) {
315 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
316 o->device->plugin_index, SR_DI_CUR_SAMPLERATE));
317 if (!(frequency_s = sr_samplerate_string(samplerate))) {
322 snprintf(comment, 127, gnuplot_header_comment,
323 ctx->num_enabled_probes, num_probes, frequency_s);
327 /* Columns / channels */
329 for (i = 0; i < ctx->num_enabled_probes; i++) {
330 c = (char *)&wbuf + strlen((char *)&wbuf);
331 sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]);
334 if (!(frequency_s = sr_period_string(samplerate))) {
340 b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
341 PACKAGE_STRING, ctime(&t), comment, frequency_s,
354 static int analog_data(struct sr_output *o, char *data_in, uint64_t length_in,
355 char **data_out, uint64_t *length_out)
358 unsigned int max_linelen, outsize, p, /* curbit, */ i;
360 static uint64_t samplecount = 0;
362 struct sr_analog_sample *sample;
365 // max_linelen = 16 + ctx->num_enabled_probes * 2;
366 max_linelen = 16 + ctx->num_enabled_probes * 30;
367 outsize = length_in / ctx->unitsize * max_linelen;
369 outsize += strlen(ctx->header);
371 if (!(outbuf = calloc(1, outsize)))
372 return SR_ERR_MALLOC;
376 /* The header is still here, this must be the first packet. */
377 strncpy(outbuf, ctx->header, outsize);
382 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
383 // memcpy(&sample, data_in + i, ctx->unitsize);
384 sample = (struct sr_analog_sample *) (data_in + i);
386 /* The first column is a counter (needed for gnuplot). */
387 c = outbuf + strlen(outbuf);
388 sprintf(c, "%" PRIu64 "\t", samplecount++);
390 /* The next columns are the values of all channels. */
391 for (p = 0; p < ctx->num_enabled_probes; p++) {
392 // curbit = (sample & ((uint64_t) (1 << p))) >> p;
393 c = outbuf + strlen(outbuf);
394 // sprintf(c, "%d ", curbit);
396 * FIXME: Should be doing proper raw->voltage conversion
397 * here, casting to int16_t isn't it. Remember that if
398 * res = 1 conversion isn't necessary.
400 sprintf(c, "%f ", (double) ((int16_t) (sample->probes[p].val &
401 ((1 << sample->probes[p].res) - 1))));
404 c = outbuf + strlen(outbuf);
409 *length_out = strlen(outbuf);
414 struct sr_output_format output_analog_gnuplot = {
415 .id = "analog_gnuplot",
416 .description = "Gnuplot analog",
417 .df_type = SR_DF_ANALOG,