]> sigrok.org Git - libsigrok.git/blame - output/gnuplot.c
Revise SCPI read API to allow backend-independent data handling.
[libsigrok.git] / output / gnuplot.c
CommitLineData
e2ad47b5 1/*
50985c20 2 * This file is part of the libsigrok project.
e2ad47b5
UH
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>
545f9786 24#include "config.h" /* Needed for PACKAGE_STRING and others. */
45c59c8b
BV
25#include "libsigrok.h"
26#include "libsigrok-internal.h"
e2ad47b5 27
3544f848 28#define LOG_PREFIX "output/gnuplot"
a944a84b 29
e2ad47b5 30struct context {
afc8e4de
UH
31 unsigned int num_enabled_probes;
32 unsigned int unitsize;
e2ad47b5 33 char *header;
9c178ffa 34 uint8_t *old_sample;
e2ad47b5
UH
35};
36
d078d2e5 37static const char *gnuplot_header = "\
e2ad47b5
UH
38# Sample data in space-separated columns format usable by gnuplot\n\
39#\n\
5cca9adb 40# Generated by: %s on %s%s\
d4ae8eaa 41# Period: %s\n\
114fb93f
UH
42#\n\
43# Column\tProbe\n\
44# -------------------------------------\
45----------------------------------------\n\
46# 0\t\tSample counter (for internal gnuplot purposes)\n%s\n";
e2ad47b5 47
d078d2e5 48static const char *gnuplot_header_comment = "\
7aae7462
BV
49# Comment: Acquisition with %d/%d probes at %s\n";
50
f50f3f40 51static int init(struct sr_output *o)
e2ad47b5 52{
e2ad47b5 53 struct context *ctx;
1afe8989 54 struct sr_probe *probe;
e2ad47b5 55 GSList *l;
ec4063b8
BV
56 GVariant *gvar;
57 uint64_t samplerate;
afc8e4de 58 unsigned int i;
90f680ff 59 int num_probes;
d4ae8eaa 60 char *c, *frequency_s;
7aae7462 61 char wbuf[1000], comment[128];
5cca9adb 62 time_t t;
e2ad47b5 63
20ebd1fe 64 if (!o) {
a944a84b 65 sr_err("%s: o was NULL", __func__);
20ebd1fe
UH
66 return SR_ERR_ARG;
67 }
68
5c3c1241 69 if (!o->sdi) {
a944a84b 70 sr_err("%s: o->sdi was NULL", __func__);
20ebd1fe
UH
71 return SR_ERR_ARG;
72 }
73
133a37bf 74 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
a944a84b 75 sr_err("%s: ctx malloc failed", __func__);
e46b8fb1 76 return SR_ERR_MALLOC;
20ebd1fe 77 }
a821069b 78
e2ad47b5
UH
79 o->internal = ctx;
80 ctx->num_enabled_probes = 0;
5c3c1241 81 for (l = o->sdi->probes; l; l = l->next) {
ec4063b8 82 probe = l->data;
deb09083
ML
83 if (probe->enabled)
84 ctx->num_enabled_probes++;
e2ad47b5 85 }
e2ad47b5
UH
86 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
87
5c3c1241 88 num_probes = g_slist_length(o->sdi->probes);
a821069b 89 comment[0] = '\0';
ec4063b8 90 samplerate = 0;
d3c74a6f
BV
91 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
92 &gvar) == SR_OK) {
ec4063b8 93 samplerate = g_variant_get_uint64(gvar);
af51a771 94 g_variant_unref(gvar);
ec4063b8 95 if (!(frequency_s = sr_samplerate_string(samplerate))) {
a944a84b 96 sr_err("%s: sr_samplerate_string failed", __func__);
133a37bf 97 g_free(ctx);
e46b8fb1 98 return SR_ERR;
a821069b
UH
99 }
100 snprintf(comment, 127, gnuplot_header_comment,
d4ae8eaa 101 ctx->num_enabled_probes, num_probes, frequency_s);
133a37bf 102 g_free(frequency_s);
7aae7462 103 }
e2ad47b5
UH
104
105 /* Columns / channels */
106 wbuf[0] = '\0';
deb09083
ML
107 for (i = 0, l = o->sdi->probes; l; l = l->next, i++) {
108 probe = l->data;
109 if (!probe->enabled)
110 continue;
054e6709 111 c = (char *)&wbuf + strlen((const char *)&wbuf);
deb09083 112 sprintf(c, "# %d\t\t%s\n", i + 1, probe->name);
e2ad47b5
UH
113 }
114
ec4063b8 115 if (!(frequency_s = sr_period_string(samplerate))) {
a944a84b 116 sr_err("%s: sr_period_string failed", __func__);
133a37bf 117 g_free(ctx);
e46b8fb1 118 return SR_ERR;
d4ae8eaa 119 }
20ebd1fe 120
5cca9adb 121 t = time(NULL);
90f680ff
ML
122 ctx->header = g_strdup_printf(gnuplot_header, PACKAGE_STRING,
123 ctime(&t), comment, frequency_s, (char *)&wbuf);
133a37bf 124 g_free(frequency_s);
e2ad47b5 125
9c178ffa
ML
126 if (!(ctx->old_sample = g_try_malloc0(ctx->unitsize))) {
127 sr_err("%s: ctx->old_sample malloc failed", __func__);
128 g_free(ctx->header);
129 g_free(ctx);
130 return SR_ERR_MALLOC;
131 }
132
e2ad47b5
UH
133 return 0;
134}
135
054e6709 136static int event(struct sr_output *o, int event_type, uint8_t **data_out,
e2ad47b5
UH
137 uint64_t *length_out)
138{
20ebd1fe 139 if (!o) {
a944a84b 140 sr_err("%s: o was NULL", __func__);
20ebd1fe
UH
141 return SR_ERR_ARG;
142 }
143
144 if (!data_out) {
a944a84b 145 sr_err("%s: data_out was NULL", __func__);
20ebd1fe
UH
146 return SR_ERR_ARG;
147 }
148
149 if (!length_out) {
a944a84b 150 sr_err("%s: length_out was NULL", __func__);
20ebd1fe
UH
151 return SR_ERR_ARG;
152 }
153
99c1fc59 154 switch (event_type) {
5a2326a7 155 case SR_DF_TRIGGER:
20ebd1fe 156 /* TODO: Can a trigger mark be in a gnuplot data file? */
e2ad47b5 157 break;
5a2326a7 158 case SR_DF_END:
133a37bf 159 g_free(o->internal);
e2ad47b5
UH
160 o->internal = NULL;
161 break;
20ebd1fe 162 default:
a944a84b 163 sr_err("%s: unsupported event type: %d", __func__, event_type);
20ebd1fe 164 break;
e2ad47b5
UH
165 }
166
9ab95e54
BV
167 *data_out = NULL;
168 *length_out = 0;
169
e46b8fb1 170 return SR_OK;
e2ad47b5
UH
171}
172
054e6709
UH
173static int data(struct sr_output *o, const uint8_t *data_in,
174 uint64_t length_in, uint8_t **data_out, uint64_t *length_out)
e2ad47b5
UH
175{
176 struct context *ctx;
d4ae8eaa 177 unsigned int max_linelen, outsize, p, curbit, i;
9c178ffa
ML
178 const uint8_t *sample;
179 static uint64_t samplecount = 0;
054e6709 180 uint8_t *outbuf, *c;
e2ad47b5 181
20ebd1fe 182 if (!o) {
a944a84b 183 sr_err("%s: o was NULL", __func__);
20ebd1fe
UH
184 return SR_ERR_ARG;
185 }
186
187 if (!o->internal) {
a944a84b 188 sr_err("%s: o->internal was NULL", __func__);
20ebd1fe
UH
189 return SR_ERR_ARG;
190 }
191
192 if (!data_in) {
a944a84b 193 sr_err("%s: data_in was NULL", __func__);
20ebd1fe
UH
194 return SR_ERR_ARG;
195 }
196
197 if (!data_out) {
a944a84b 198 sr_err("%s: data_out was NULL", __func__);
20ebd1fe
UH
199 return SR_ERR_ARG;
200 }
201
202 if (!length_out) {
a944a84b 203 sr_err("%s: length_out was NULL", __func__);
20ebd1fe
UH
204 return SR_ERR_ARG;
205 }
206
e2ad47b5 207 ctx = o->internal;
d4ae8eaa
BV
208 max_linelen = 16 + ctx->num_enabled_probes * 2;
209 outsize = length_in / ctx->unitsize * max_linelen;
e273a904 210 if (ctx->header)
d4ae8eaa 211 outsize += strlen(ctx->header);
a821069b 212
133a37bf 213 if (!(outbuf = g_try_malloc0(outsize))) {
a944a84b 214 sr_err("%s: outbuf malloc failed", __func__);
e46b8fb1 215 return SR_ERR_MALLOC;
20ebd1fe 216 }
a821069b
UH
217
218 outbuf[0] = '\0';
e2ad47b5
UH
219 if (ctx->header) {
220 /* The header is still here, this must be the first packet. */
054e6709 221 strncpy((char *)outbuf, ctx->header, outsize);
133a37bf 222 g_free(ctx->header);
e2ad47b5 223 ctx->header = NULL;
e2ad47b5
UH
224 }
225
607b58de 226 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
50959ddc 227
9c178ffa 228 sample = data_in + i;
e2ad47b5 229
50959ddc
UH
230 /*
231 * Don't output the same samples multiple times. However, make
232 * sure to output at least the first and last sample.
233 */
9c178ffa
ML
234 if (samplecount++ != 0 &&
235 !memcmp(sample, ctx->old_sample, ctx->unitsize)) {
50959ddc
UH
236 if (i != (length_in - ctx->unitsize))
237 continue;
238 }
9c178ffa 239 memcpy(ctx->old_sample, sample, ctx->unitsize);
50959ddc 240
e2ad47b5 241 /* The first column is a counter (needed for gnuplot). */
054e6709
UH
242 c = outbuf + strlen((const char *)outbuf);
243 sprintf((char *)c, "%" PRIu64 "\t", samplecount++);
e2ad47b5
UH
244
245 /* The next columns are the values of all channels. */
246 for (p = 0; p < ctx->num_enabled_probes; p++) {
9c178ffa 247 curbit = (sample[p / 8] & ((uint8_t) (1 << (p % 8)))) >> (p % 8);
054e6709
UH
248 c = outbuf + strlen((const char *)outbuf);
249 sprintf((char *)c, "%d ", curbit);
e2ad47b5
UH
250 }
251
054e6709
UH
252 c = outbuf + strlen((const char *)outbuf);
253 sprintf((char *)c, "\n");
e2ad47b5
UH
254 }
255
256 *data_out = outbuf;
054e6709 257 *length_out = strlen((const char *)outbuf);
e2ad47b5 258
e46b8fb1 259 return SR_OK;
e2ad47b5
UH
260}
261
7c1d391c 262SR_PRIV struct sr_output_format output_gnuplot = {
cdb3573c 263 .id = "gnuplot",
d494a4aa
UH
264 .description = "Gnuplot",
265 .df_type = SR_DF_LOGIC,
266 .init = init,
267 .data = data,
268 .event = event,
77b45442 269};