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];
63 if (!(ctx = calloc(1, sizeof(struct context))))
66 if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) {
72 ctx->num_enabled_probes = 0;
73 for (l = o->device->probes; l; l = l->next) {
77 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
79 ctx->probelist[ctx->num_enabled_probes] = 0;
80 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
82 num_probes = g_slist_length(o->device->probes);
84 if (o->device->plugin && sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) {
85 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
86 o->device->plugin_index, SR_DI_CUR_SAMPLERATE));
87 if (!(frequency_s = sr_samplerate_string(samplerate))) {
92 snprintf(comment, 127, gnuplot_header_comment,
93 ctx->num_enabled_probes, num_probes, frequency_s);
97 /* Columns / channels */
99 for (i = 0; i < ctx->num_enabled_probes; i++) {
100 c = (char *)&wbuf + strlen((char *)&wbuf);
101 sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]);
104 if (!(frequency_s = sr_period_string(samplerate))) {
110 b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
111 PACKAGE_STRING, ctime(&t), comment, frequency_s,
124 static int event(struct sr_output *o, int event_type, char **data_out,
125 uint64_t *length_out)
130 switch (event_type) {
132 /* TODO: can a trigger mark be in a gnuplot data file? */
146 static int data(struct sr_output *o, const char *data_in, uint64_t length_in,
147 char **data_out, uint64_t *length_out)
150 unsigned int max_linelen, outsize, p, curbit, i;
152 static uint64_t samplecount = 0;
156 max_linelen = 16 + ctx->num_enabled_probes * 2;
157 outsize = length_in / ctx->unitsize * max_linelen;
159 outsize += strlen(ctx->header);
161 if (!(outbuf = calloc(1, outsize)))
162 return SR_ERR_MALLOC;
166 /* The header is still here, this must be the first packet. */
167 strncpy(outbuf, ctx->header, outsize);
172 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
173 memcpy(&sample, data_in + i, ctx->unitsize);
175 /* The first column is a counter (needed for gnuplot). */
176 c = outbuf + strlen(outbuf);
177 sprintf(c, "%" PRIu64 "\t", samplecount++);
179 /* The next columns are the values of all channels. */
180 for (p = 0; p < ctx->num_enabled_probes; p++) {
181 curbit = (sample & ((uint64_t) (1 << p))) >> p;
182 c = outbuf + strlen(outbuf);
183 sprintf(c, "%d ", curbit);
186 c = outbuf + strlen(outbuf);
191 *length_out = strlen(outbuf);
196 struct sr_output_format output_gnuplot = {
205 /* Temporarily disabled. */
207 static int analog_init(struct sr_output *o)
210 struct sr_probe *probe;
215 char *c, *frequency_s;
216 char wbuf[1000], comment[128];
219 if (!(ctx = calloc(1, sizeof(struct context))))
220 return SR_ERR_MALLOC;
222 if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) {
224 return SR_ERR_MALLOC;
228 ctx->num_enabled_probes = 0;
229 for (l = o->device->probes; l; l = l->next) {
233 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
235 ctx->probelist[ctx->num_enabled_probes] = 0;
236 // ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
237 ctx->unitsize = sizeof(struct sr_analog_sample) +
238 (ctx->num_enabled_probes * sizeof(struct sr_analog_probe));
240 num_probes = g_slist_length(o->device->probes);
242 if (o->device->plugin && sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) {
243 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
244 o->device->plugin_index, SR_DI_CUR_SAMPLERATE));
245 if (!(frequency_s = sr_samplerate_string(samplerate))) {
250 snprintf(comment, 127, gnuplot_header_comment,
251 ctx->num_enabled_probes, num_probes, frequency_s);
255 /* Columns / channels */
257 for (i = 0; i < ctx->num_enabled_probes; i++) {
258 c = (char *)&wbuf + strlen((char *)&wbuf);
259 sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]);
262 if (!(frequency_s = sr_period_string(samplerate))) {
268 b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
269 PACKAGE_STRING, ctime(&t), comment, frequency_s,
282 static int analog_data(struct sr_output *o, char *data_in, uint64_t length_in,
283 char **data_out, uint64_t *length_out)
286 unsigned int max_linelen, outsize, p, /* curbit, */ i;
288 static uint64_t samplecount = 0;
290 struct sr_analog_sample *sample;
293 // max_linelen = 16 + ctx->num_enabled_probes * 2;
294 max_linelen = 16 + ctx->num_enabled_probes * 30;
295 outsize = length_in / ctx->unitsize * max_linelen;
297 outsize += strlen(ctx->header);
299 if (!(outbuf = calloc(1, outsize)))
300 return SR_ERR_MALLOC;
304 /* The header is still here, this must be the first packet. */
305 strncpy(outbuf, ctx->header, outsize);
310 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
311 // memcpy(&sample, data_in + i, ctx->unitsize);
312 sample = (struct sr_analog_sample *) (data_in + i);
314 /* The first column is a counter (needed for gnuplot). */
315 c = outbuf + strlen(outbuf);
316 sprintf(c, "%" PRIu64 "\t", samplecount++);
318 /* The next columns are the values of all channels. */
319 for (p = 0; p < ctx->num_enabled_probes; p++) {
320 // curbit = (sample & ((uint64_t) (1 << p))) >> p;
321 c = outbuf + strlen(outbuf);
322 // sprintf(c, "%d ", curbit);
324 * FIXME: Should be doing proper raw->voltage conversion
325 * here, casting to int16_t isn't it. Remember that if
326 * res = 1 conversion isn't necessary.
328 sprintf(c, "%f ", (double) ((int16_t) (sample->probes[p].val &
329 ((1 << sample->probes[p].res) - 1))));
332 c = outbuf + strlen(outbuf);
337 *length_out = strlen(outbuf);
342 struct sr_output_format output_analog_gnuplot = {