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