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