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