]> sigrok.org Git - libsigrok.git/blame - output/output_gnuplot.c
input: use dummy device when loading from file
[libsigrok.git] / output / 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>
24#include <sigrok.h>
25#include "config.h"
26
27struct context {
afc8e4de
UH
28 unsigned int num_enabled_probes;
29 unsigned int unitsize;
e2ad47b5
UH
30 char *probelist[65];
31 char *header;
32};
33
34const 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
42static 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;
afc8e4de
UH
51 unsigned int i;
52 int b, num_probes;
25e7d9b1
UH
53 char *c, *samplerate_s;
54 char wbuf[1000];
e2ad47b5
UH
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));
99c1fc59 79
25e7d9b1 80 if ((samplerate_s = sigrok_samplerate_string(samplerate)) == NULL)
99c1fc59 81 return -1; /* FIXME */
e2ad47b5
UH
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,
25e7d9b1
UH
94 num_probes, samplerate_s, 1, "ns", (char *)&wbuf);
95
96 free(samplerate_s);
e2ad47b5
UH
97
98 /* TODO: Handle snprintf errors. */
99
100 return 0;
101}
102
103static 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;
99c1fc59 111 switch (event_type) {
e2ad47b5
UH
112 case DF_TRIGGER:
113 break;
114 case DF_END:
99c1fc59 115 outbuf = calloc(1, 1); /* FIXME */
e2ad47b5
UH
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
128static 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;
afc8e4de 132 unsigned int offset, outsize, p, curbit;
e2ad47b5
UH
133 uint64_t sample, count = 0;
134 char *outbuf, *c;
135
136 ctx = o->internal;
e273a904
HE
137
138 outsize = 0;
139 if (ctx->header)
140 outsize = strlen(ctx->header);
99c1fc59 141 outbuf = calloc(1, outsize + 1 + 10000); /* FIXME: Use realloc(). */
e2ad47b5
UH
142 if (outbuf == NULL)
143 return SIGROK_ERR_MALLOC;
144 if (ctx->header) {
145 /* The header is still here, this must be the first packet. */
146 strncpy(outbuf, ctx->header, outsize);
147 free(ctx->header);
148 ctx->header = NULL;
149 } else {
150 outbuf[0] = 0;
151 }
152
153 /* TODO: Are disabled probes handled correctly? */
154
155 for (offset = 0; offset <= length_in - ctx->unitsize;
99c1fc59 156 offset += ctx->unitsize) {
e2ad47b5
UH
157 memcpy(&sample, data_in + offset, ctx->unitsize);
158
159 /* The first column is a counter (needed for gnuplot). */
160 c = outbuf + strlen(outbuf);
99c1fc59 161 sprintf(c, "%" PRIu64 "\t\t", count++);
e2ad47b5
UH
162
163 /* The next columns are the values of all channels. */
164 for (p = 0; p < ctx->num_enabled_probes; p++) {
165 curbit = (sample & ((uint64_t) (1 << p))) != 0;
166 c = outbuf + strlen(outbuf);
167 sprintf(c, "%d ", curbit);
168 }
169
170 c = outbuf + strlen(outbuf);
171 sprintf(c, "\n");
172
173 /* TODO: realloc() if strlen(outbuf) is almost "full"... */
174 }
175
176 *data_out = outbuf;
177 *length_out = strlen(outbuf);
178
179 return SIGROK_OK;
180}
181
182struct output_format output_gnuplot = {
183 "gnuplot",
184 "Gnuplot",
185 init,
186 data,
187 event,
188};