]> sigrok.org Git - libsigrok.git/blob - output/output_gnuplot.c
Sigma: Small cleanups.
[libsigrok.git] / output / output_gnuplot.c
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
27 struct context {
28         unsigned int num_enabled_probes;
29         unsigned int unitsize;
30         char *probelist[65];
31         char *header;
32 };
33
34 const char *gnuplot_header = "\
35 # Sample data in space-separated columns format usable by gnuplot\n\
36 #\n\
37 # Generated by: %s on %s\n\
38 # Comment: Acquisition with %d/%d probes at %s\n\
39 # Timescale: %d %s\n\
40 # Column assignment:\n%s\n";
41
42 static int init(struct output *o)
43 {
44 /* Maximum header length */
45 #define MAX_HEADER_LEN 2048
46
47         struct context *ctx;
48         struct probe *probe;
49         GSList *l;
50         uint64_t samplerate;
51         unsigned int i;
52         int b, num_probes;
53         char *c, *samplerate_s;
54         char wbuf[1000];
55
56         ctx = malloc(sizeof(struct context));
57         if (ctx == NULL)
58                 return SIGROK_ERR_MALLOC;
59         o->internal = ctx;
60         ctx->num_enabled_probes = 0;
61         for (l = o->device->probes; l; l = l->next) {
62                 probe = l->data;
63                 if (probe->enabled)
64                         ctx->probelist[ctx->num_enabled_probes++] = probe->name;
65         }
66
67         ctx->probelist[ctx->num_enabled_probes] = 0;
68         ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
69
70         /* TODO: Allow for configuration via o->param. */
71
72         ctx->header = calloc(1, MAX_HEADER_LEN + 1);
73         if (ctx->header == NULL)
74                 return SIGROK_ERR_MALLOC;
75         num_probes = g_slist_length(o->device->probes);
76         /* TODO: Handle num_probes == 0, too many probes, etc. */
77         samplerate = *((uint64_t *) o->device->plugin->get_device_info(
78                         o->device->plugin_index, DI_CUR_SAMPLERATE));
79
80         if ((samplerate_s = sigrok_samplerate_string(samplerate)) == NULL)
81                 return -1; /* FIXME */
82
83         /* Columns / channels */
84         wbuf[0] = '\0';
85         for (i = 0; i < ctx->num_enabled_probes; i++) {
86                 c = (char *)&wbuf + strlen((char *)&wbuf);
87                 sprintf(c, "# Column %d: channel %s\n", i, ctx->probelist[i]);
88         }
89
90         /* TODO: date: File or signals? Make y/n configurable. */
91         /* TODO: Timescale */
92         b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
93                      PACKAGE_STRING, "TODO", ctx->num_enabled_probes,
94                      num_probes, samplerate_s, 1, "ns", (char *)&wbuf);
95
96         free(samplerate_s);
97
98         /* TODO: Handle snprintf errors. */
99
100         return 0;
101 }
102
103 static int event(struct output *o, int event_type, char **data_out,
104                  uint64_t *length_out)
105 {
106         struct context *ctx;
107         char *outbuf;
108         int outlen = 1; /* FIXME */
109
110         ctx = o->internal;
111         switch (event_type) {
112         case DF_TRIGGER:
113                 break;
114         case DF_END:
115                 outbuf = calloc(1, 1); /* FIXME */
116                 if (outbuf == NULL)
117                         return SIGROK_ERR_MALLOC;
118                 *data_out = outbuf;
119                 *length_out = outlen;
120                 free(o->internal);
121                 o->internal = NULL;
122                 break;
123         }
124
125         return SIGROK_OK;
126 }
127
128 static int data(struct output *o, char *data_in, uint64_t length_in,
129                 char **data_out, uint64_t *length_out)
130 {
131         struct context *ctx;
132         unsigned int offset, outsize, p, curbit;
133         uint64_t sample, count = 0;
134         char *outbuf, *c;
135
136         ctx = o->internal;
137         outsize = strlen(ctx->header);
138         outbuf = calloc(1, outsize + 1 + 10000); /* FIXME: Use realloc(). */
139         if (outbuf == NULL)
140                 return SIGROK_ERR_MALLOC;
141         if (ctx->header) {
142                 /* The header is still here, this must be the first packet. */
143                 strncpy(outbuf, ctx->header, outsize);
144                 free(ctx->header);
145                 ctx->header = NULL;
146         } else {
147                 outbuf[0] = 0;
148         }
149
150         /* TODO: Are disabled probes handled correctly? */
151
152         for (offset = 0; offset <= length_in - ctx->unitsize;
153              offset += ctx->unitsize) {
154                 memcpy(&sample, data_in + offset, ctx->unitsize);
155
156                 /* The first column is a counter (needed for gnuplot). */
157                 c = outbuf + strlen(outbuf);
158                 sprintf(c, "%" PRIu64 "\t\t", count++);
159
160                 /* The next columns are the values of all channels. */
161                 for (p = 0; p < ctx->num_enabled_probes; p++) {
162                         curbit = (sample & ((uint64_t) (1 << p))) != 0;
163                         c = outbuf + strlen(outbuf);
164                         sprintf(c, "%d ", curbit);
165                 }
166
167                 c = outbuf + strlen(outbuf);
168                 sprintf(c, "\n");
169
170                 /* TODO: realloc() if strlen(outbuf) is almost "full"... */
171         }
172
173         *data_out = outbuf;
174         *length_out = strlen(outbuf);
175
176         return SIGROK_OK;
177 }
178
179 struct output_format output_gnuplot = {
180         "gnuplot",
181         "Gnuplot",
182         init,
183         data,
184         event,
185 };