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