libsigrok
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
gnuplot.c
Go to the documentation of this file.
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 
28 struct 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 
38 static 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 
49 static const char *gnuplot_header_comment = "\
50 # Comment: Acquisition with %d/%d probes at %s\n";
51 
52 static 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->driver) {
75  sr_err("gnuplot out: %s: o->dev->driver 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';
104  samplerate = *((uint64_t *) o->dev->driver->dev_info_get(
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((const 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 
148 static int event(struct sr_output *o, int event_type, uint8_t **data_out,
149  uint64_t *length_out)
150 {
151  if (!o) {
152  sr_err("gnuplot out: %s: o was NULL", __func__);
153  return SR_ERR_ARG;
154  }
155 
156  if (!data_out) {
157  sr_err("gnuplot out: %s: data_out was NULL", __func__);
158  return SR_ERR_ARG;
159  }
160 
161  if (!length_out) {
162  sr_err("gnuplot out: %s: length_out was NULL", __func__);
163  return SR_ERR_ARG;
164  }
165 
166  switch (event_type) {
167  case SR_DF_TRIGGER:
168  /* TODO: Can a trigger mark be in a gnuplot data file? */
169  break;
170  case SR_DF_END:
171  g_free(o->internal);
172  o->internal = NULL;
173  break;
174  default:
175  sr_err("gnuplot out: %s: unsupported event type: %d",
176  __func__, event_type);
177  break;
178  }
179 
180  *data_out = NULL;
181  *length_out = 0;
182 
183  return SR_OK;
184 }
185 
186 static int data(struct sr_output *o, const uint8_t *data_in,
187  uint64_t length_in, uint8_t **data_out, uint64_t *length_out)
188 {
189  struct context *ctx;
190  unsigned int max_linelen, outsize, p, curbit, i;
191  uint64_t sample;
192  static uint64_t samplecount = 0, old_sample = 0;
193  uint8_t *outbuf, *c;
194 
195  if (!o) {
196  sr_err("gnuplot out: %s: o was NULL", __func__);
197  return SR_ERR_ARG;
198  }
199 
200  if (!o->internal) {
201  sr_err("gnuplot out: %s: o->internal was NULL", __func__);
202  return SR_ERR_ARG;
203  }
204 
205  if (!data_in) {
206  sr_err("gnuplot out: %s: data_in was NULL", __func__);
207  return SR_ERR_ARG;
208  }
209 
210  if (!data_out) {
211  sr_err("gnuplot out: %s: data_out was NULL", __func__);
212  return SR_ERR_ARG;
213  }
214 
215  if (!length_out) {
216  sr_err("gnuplot out: %s: length_out was NULL", __func__);
217  return SR_ERR_ARG;
218  }
219 
220  ctx = o->internal;
221  max_linelen = 16 + ctx->num_enabled_probes * 2;
222  outsize = length_in / ctx->unitsize * max_linelen;
223  if (ctx->header)
224  outsize += strlen(ctx->header);
225 
226  if (!(outbuf = g_try_malloc0(outsize))) {
227  sr_err("gnuplot out: %s: outbuf malloc failed", __func__);
228  return SR_ERR_MALLOC;
229  }
230 
231  outbuf[0] = '\0';
232  if (ctx->header) {
233  /* The header is still here, this must be the first packet. */
234  strncpy((char *)outbuf, ctx->header, outsize);
235  g_free(ctx->header);
236  ctx->header = NULL;
237  }
238 
239  for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
240 
241  memcpy(&sample, data_in + i, ctx->unitsize);
242 
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 
253  /* The first column is a counter (needed for gnuplot). */
254  c = outbuf + strlen((const char *)outbuf);
255  sprintf((char *)c, "%" PRIu64 "\t", samplecount++);
256 
257  /* The next columns are the values of all channels. */
258  for (p = 0; p < ctx->num_enabled_probes; p++) {
259  curbit = (sample & ((uint64_t) (1 << p))) >> p;
260  c = outbuf + strlen((const char *)outbuf);
261  sprintf((char *)c, "%d ", curbit);
262  }
263 
264  c = outbuf + strlen((const char *)outbuf);
265  sprintf((char *)c, "\n");
266  }
267 
268  *data_out = outbuf;
269  *length_out = strlen((const char *)outbuf);
270 
271  return SR_OK;
272 }
273 
275  .id = "gnuplot",
276  .description = "Gnuplot",
277  .df_type = SR_DF_LOGIC,
278  .init = init,
279  .data = data,
280  .event = event,
281 };
282 
283 /* Temporarily disabled. */
284 #if 0
285 static int analog_init(struct sr_output *o)
286 {
287  struct context *ctx;
288  struct sr_probe *probe;
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 
297  if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
298  sr_err("gnuplot out: %s: ctx malloc failed", __func__);
299  return SR_ERR_MALLOC;
300  }
301 
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__);
305  return SR_ERR_MALLOC;
306  }
307 
308  o->internal = ctx;
309  ctx->num_enabled_probes = 0;
310  for (l = o->dev->probes; l; l = l->next) {
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;
318  ctx->unitsize = sizeof(struct sr_analog_sample) +
319  (ctx->num_enabled_probes * sizeof(struct sr_analog_probe));
320 
321  num_probes = g_slist_length(o->dev->probes);
322  comment[0] = '\0';
324  samplerate = *((uint64_t *) o->dev->driver->dev_info_get(
326  if (!(frequency_s = sr_samplerate_string(samplerate))) {
327  g_free(ctx->header);
328  g_free(ctx);
329  return SR_ERR;
330  }
331  snprintf(comment, 127, gnuplot_header_comment,
332  ctx->num_enabled_probes, num_probes, frequency_s);
333  g_free(frequency_s);
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 
343  if (!(frequency_s = sr_period_string(samplerate))) {
344  g_free(ctx->header);
345  g_free(ctx);
346  return SR_ERR;
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);
352  g_free(frequency_s);
353 
354  if (b < 0) {
355  g_free(ctx->header);
356  g_free(ctx);
357  return SR_ERR;
358  }
359 
360  return 0;
361 }
362 
363 static 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)
366 {
367  struct context *ctx;
368  unsigned int max_linelen, outsize, p, /* curbit, */ i;
369 // uint64_t sample;
370  static uint64_t samplecount = 0;
371  uint8_t *outbuf, *c;
372  struct sr_analog_sample *sample;
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 
381  if (!(outbuf = g_try_malloc0(outsize))) {
382  sr_err("gnuplot out: %s: outbuf malloc failed", __func__);
383  return SR_ERR_MALLOC;
384  }
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);
390  g_free(ctx->header);
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);
396  sample = (struct sr_analog_sample *) (data_in + i);
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 
423  return SR_OK;
424 }
425 
426 struct sr_output_format output_analog_gnuplot = {
427  .id = "analog_gnuplot",
428  .description = "Gnuplot analog",
429  .df_type = SR_DF_ANALOG,
430  .init = analog_init,
431  .data = analog_data,
432  .event = event,
433 };
434 #endif