]>
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; |
5c3c1241 | 66 | uint64_t *samplerate; |
afc8e4de UH |
67 | unsigned int i; |
68 | int b, num_probes; | |
d4ae8eaa | 69 | char *c, *frequency_s; |
7aae7462 | 70 | char wbuf[1000], comment[128]; |
5cca9adb | 71 | time_t t; |
e2ad47b5 | 72 | |
20ebd1fe | 73 | if (!o) { |
a944a84b | 74 | sr_err("%s: o was NULL", __func__); |
20ebd1fe UH |
75 | return SR_ERR_ARG; |
76 | } | |
77 | ||
5c3c1241 | 78 | if (!o->sdi) { |
a944a84b | 79 | sr_err("%s: o->sdi was NULL", __func__); |
20ebd1fe UH |
80 | return SR_ERR_ARG; |
81 | } | |
82 | ||
5c3c1241 | 83 | if (!o->sdi->driver) { |
a944a84b | 84 | sr_err("%s: o->sdi->driver was NULL", __func__); |
20ebd1fe UH |
85 | return SR_ERR_ARG; |
86 | } | |
87 | ||
133a37bf | 88 | if (!(ctx = g_try_malloc0(sizeof(struct context)))) { |
a944a84b | 89 | sr_err("%s: ctx malloc failed", __func__); |
e46b8fb1 | 90 | return SR_ERR_MALLOC; |
20ebd1fe | 91 | } |
a821069b | 92 | |
133a37bf | 93 | if (!(ctx->header = g_try_malloc0(MAX_HEADER_LEN + 1))) { |
a944a84b | 94 | sr_err("%s: ctx->header malloc failed", __func__); |
133a37bf | 95 | g_free(ctx); |
e46b8fb1 | 96 | return SR_ERR_MALLOC; |
d4ae8eaa BV |
97 | } |
98 | ||
e2ad47b5 UH |
99 | o->internal = ctx; |
100 | ctx->num_enabled_probes = 0; | |
5c3c1241 | 101 | for (l = o->sdi->probes; l; l = l->next) { |
20ebd1fe | 102 | probe = l->data; /* TODO: Error checks. */ |
757b8c62 UH |
103 | if (!probe->enabled) |
104 | continue; | |
105 | ctx->probelist[ctx->num_enabled_probes++] = probe->name; | |
e2ad47b5 | 106 | } |
e2ad47b5 UH |
107 | ctx->probelist[ctx->num_enabled_probes] = 0; |
108 | ctx->unitsize = (ctx->num_enabled_probes + 7) / 8; | |
109 | ||
5c3c1241 | 110 | num_probes = g_slist_length(o->sdi->probes); |
a821069b | 111 | comment[0] = '\0'; |
5c3c1241 BV |
112 | if (sr_dev_has_hwcap(o->sdi, SR_HWCAP_SAMPLERATE)) { |
113 | o->sdi->driver->info_get(SR_DI_CUR_SAMPLERATE, | |
114 | (const void **)&samplerate, o->sdi); | |
115 | if (!(frequency_s = sr_samplerate_string(*samplerate))) { | |
a944a84b | 116 | sr_err("%s: sr_samplerate_string failed", __func__); |
133a37bf UH |
117 | g_free(ctx->header); |
118 | g_free(ctx); | |
e46b8fb1 | 119 | return SR_ERR; |
a821069b UH |
120 | } |
121 | snprintf(comment, 127, gnuplot_header_comment, | |
d4ae8eaa | 122 | ctx->num_enabled_probes, num_probes, frequency_s); |
133a37bf | 123 | g_free(frequency_s); |
7aae7462 | 124 | } |
e2ad47b5 UH |
125 | |
126 | /* Columns / channels */ | |
127 | wbuf[0] = '\0'; | |
128 | for (i = 0; i < ctx->num_enabled_probes; i++) { | |
054e6709 | 129 | c = (char *)&wbuf + strlen((const char *)&wbuf); |
114fb93f | 130 | sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]); |
e2ad47b5 UH |
131 | } |
132 | ||
5c3c1241 | 133 | if (!(frequency_s = sr_period_string(*samplerate))) { |
a944a84b | 134 | sr_err("%s: sr_period_string failed", __func__); |
133a37bf UH |
135 | g_free(ctx->header); |
136 | g_free(ctx); | |
e46b8fb1 | 137 | return SR_ERR; |
d4ae8eaa | 138 | } |
20ebd1fe | 139 | |
5cca9adb | 140 | t = time(NULL); |
e2ad47b5 | 141 | b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header, |
d4ae8eaa | 142 | PACKAGE_STRING, ctime(&t), comment, frequency_s, |
5cca9adb | 143 | (char *)&wbuf); |
133a37bf | 144 | g_free(frequency_s); |
e2ad47b5 | 145 | |
d4ae8eaa | 146 | if (b < 0) { |
a944a84b | 147 | sr_err("%s: sprintf failed", __func__); |
133a37bf UH |
148 | g_free(ctx->header); |
149 | g_free(ctx); | |
e46b8fb1 | 150 | return SR_ERR; |
d4ae8eaa | 151 | } |
e2ad47b5 UH |
152 | |
153 | return 0; | |
154 | } | |
155 | ||
054e6709 | 156 | static int event(struct sr_output *o, int event_type, uint8_t **data_out, |
e2ad47b5 UH |
157 | uint64_t *length_out) |
158 | { | |
20ebd1fe | 159 | if (!o) { |
a944a84b | 160 | sr_err("%s: o was NULL", __func__); |
20ebd1fe UH |
161 | return SR_ERR_ARG; |
162 | } | |
163 | ||
164 | if (!data_out) { | |
a944a84b | 165 | sr_err("%s: data_out was NULL", __func__); |
20ebd1fe UH |
166 | return SR_ERR_ARG; |
167 | } | |
168 | ||
169 | if (!length_out) { | |
a944a84b | 170 | sr_err("%s: length_out was NULL", __func__); |
20ebd1fe UH |
171 | return SR_ERR_ARG; |
172 | } | |
173 | ||
99c1fc59 | 174 | switch (event_type) { |
5a2326a7 | 175 | case SR_DF_TRIGGER: |
20ebd1fe | 176 | /* TODO: Can a trigger mark be in a gnuplot data file? */ |
e2ad47b5 | 177 | break; |
5a2326a7 | 178 | case SR_DF_END: |
133a37bf | 179 | g_free(o->internal); |
e2ad47b5 UH |
180 | o->internal = NULL; |
181 | break; | |
20ebd1fe | 182 | default: |
a944a84b | 183 | sr_err("%s: unsupported event type: %d", __func__, event_type); |
20ebd1fe | 184 | break; |
e2ad47b5 UH |
185 | } |
186 | ||
9ab95e54 BV |
187 | *data_out = NULL; |
188 | *length_out = 0; | |
189 | ||
e46b8fb1 | 190 | return SR_OK; |
e2ad47b5 UH |
191 | } |
192 | ||
054e6709 UH |
193 | static int data(struct sr_output *o, const uint8_t *data_in, |
194 | uint64_t length_in, uint8_t **data_out, uint64_t *length_out) | |
e2ad47b5 UH |
195 | { |
196 | struct context *ctx; | |
d4ae8eaa | 197 | unsigned int max_linelen, outsize, p, curbit, i; |
086eac7c | 198 | uint64_t sample; |
50959ddc | 199 | static uint64_t samplecount = 0, old_sample = 0; |
054e6709 | 200 | uint8_t *outbuf, *c; |
e2ad47b5 | 201 | |
20ebd1fe | 202 | if (!o) { |
a944a84b | 203 | sr_err("%s: o was NULL", __func__); |
20ebd1fe UH |
204 | return SR_ERR_ARG; |
205 | } | |
206 | ||
207 | if (!o->internal) { | |
a944a84b | 208 | sr_err("%s: o->internal was NULL", __func__); |
20ebd1fe UH |
209 | return SR_ERR_ARG; |
210 | } | |
211 | ||
212 | if (!data_in) { | |
a944a84b | 213 | sr_err("%s: data_in was NULL", __func__); |
20ebd1fe UH |
214 | return SR_ERR_ARG; |
215 | } | |
216 | ||
217 | if (!data_out) { | |
a944a84b | 218 | sr_err("%s: data_out was NULL", __func__); |
20ebd1fe UH |
219 | return SR_ERR_ARG; |
220 | } | |
221 | ||
222 | if (!length_out) { | |
a944a84b | 223 | sr_err("%s: length_out was NULL", __func__); |
20ebd1fe UH |
224 | return SR_ERR_ARG; |
225 | } | |
226 | ||
e2ad47b5 | 227 | ctx = o->internal; |
d4ae8eaa BV |
228 | max_linelen = 16 + ctx->num_enabled_probes * 2; |
229 | outsize = length_in / ctx->unitsize * max_linelen; | |
e273a904 | 230 | if (ctx->header) |
d4ae8eaa | 231 | outsize += strlen(ctx->header); |
a821069b | 232 | |
133a37bf | 233 | if (!(outbuf = g_try_malloc0(outsize))) { |
a944a84b | 234 | sr_err("%s: outbuf malloc failed", __func__); |
e46b8fb1 | 235 | return SR_ERR_MALLOC; |
20ebd1fe | 236 | } |
a821069b UH |
237 | |
238 | outbuf[0] = '\0'; | |
e2ad47b5 UH |
239 | if (ctx->header) { |
240 | /* The header is still here, this must be the first packet. */ | |
054e6709 | 241 | strncpy((char *)outbuf, ctx->header, outsize); |
133a37bf | 242 | g_free(ctx->header); |
e2ad47b5 | 243 | ctx->header = NULL; |
e2ad47b5 UH |
244 | } |
245 | ||
607b58de | 246 | for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) { |
50959ddc | 247 | |
607b58de | 248 | memcpy(&sample, data_in + i, ctx->unitsize); |
e2ad47b5 | 249 | |
50959ddc UH |
250 | /* |
251 | * Don't output the same samples multiple times. However, make | |
252 | * sure to output at least the first and last sample. | |
253 | */ | |
254 | if (samplecount++ != 0 && sample == old_sample) { | |
255 | if (i != (length_in - ctx->unitsize)) | |
256 | continue; | |
257 | } | |
258 | old_sample = sample; | |
259 | ||
e2ad47b5 | 260 | /* The first column is a counter (needed for gnuplot). */ |
054e6709 UH |
261 | c = outbuf + strlen((const char *)outbuf); |
262 | sprintf((char *)c, "%" PRIu64 "\t", samplecount++); | |
e2ad47b5 UH |
263 | |
264 | /* The next columns are the values of all channels. */ | |
265 | for (p = 0; p < ctx->num_enabled_probes; p++) { | |
fbe2f794 | 266 | curbit = (sample & ((uint64_t) (1 << p))) >> p; |
054e6709 UH |
267 | c = outbuf + strlen((const char *)outbuf); |
268 | sprintf((char *)c, "%d ", curbit); | |
e2ad47b5 UH |
269 | } |
270 | ||
054e6709 UH |
271 | c = outbuf + strlen((const char *)outbuf); |
272 | sprintf((char *)c, "\n"); | |
e2ad47b5 UH |
273 | } |
274 | ||
275 | *data_out = outbuf; | |
054e6709 | 276 | *length_out = strlen((const char *)outbuf); |
e2ad47b5 | 277 | |
e46b8fb1 | 278 | return SR_OK; |
e2ad47b5 UH |
279 | } |
280 | ||
7c1d391c | 281 | SR_PRIV struct sr_output_format output_gnuplot = { |
cdb3573c | 282 | .id = "gnuplot", |
d494a4aa UH |
283 | .description = "Gnuplot", |
284 | .df_type = SR_DF_LOGIC, | |
285 | .init = init, | |
286 | .data = data, | |
287 | .event = event, | |
77b45442 UH |
288 | }; |
289 | ||
290 | /* Temporarily disabled. */ | |
291 | #if 0 | |
f50f3f40 | 292 | static int analog_init(struct sr_output *o) |
81bbdf6a DR |
293 | { |
294 | struct context *ctx; | |
1afe8989 | 295 | struct sr_probe *probe; |
81bbdf6a DR |
296 | GSList *l; |
297 | uint64_t samplerate; | |
298 | unsigned int i; | |
299 | int b, num_probes; | |
300 | char *c, *frequency_s; | |
301 | char wbuf[1000], comment[128]; | |
302 | time_t t; | |
303 | ||
133a37bf UH |
304 | if (!(ctx = g_try_malloc0(sizeof(struct context)))) { |
305 | sr_err("gnuplot out: %s: ctx malloc failed", __func__); | |
e46b8fb1 | 306 | return SR_ERR_MALLOC; |
133a37bf | 307 | } |
81bbdf6a | 308 | |
133a37bf UH |
309 | if (!(ctx->header = g_try_malloc0(MAX_HEADER_LEN + 1))) { |
310 | g_free(ctx); | |
311 | sr_err("gnuplot out: %s: ctx->header malloc failed", __func__); | |
e46b8fb1 | 312 | return SR_ERR_MALLOC; |
81bbdf6a DR |
313 | } |
314 | ||
315 | o->internal = ctx; | |
316 | ctx->num_enabled_probes = 0; | |
bb7ef793 | 317 | for (l = o->dev->probes; l; l = l->next) { |
81bbdf6a DR |
318 | probe = l->data; |
319 | if (!probe->enabled) | |
320 | continue; | |
321 | ctx->probelist[ctx->num_enabled_probes++] = probe->name; | |
322 | } | |
323 | ctx->probelist[ctx->num_enabled_probes] = 0; | |
324 | // ctx->unitsize = (ctx->num_enabled_probes + 7) / 8; | |
809c5f20 UH |
325 | ctx->unitsize = sizeof(struct sr_analog_sample) + |
326 | (ctx->num_enabled_probes * sizeof(struct sr_analog_probe)); | |
81bbdf6a | 327 | |
bb7ef793 | 328 | num_probes = g_slist_length(o->dev->probes); |
81bbdf6a | 329 | comment[0] = '\0'; |
c09f0b57 UH |
330 | if (o->dev->driver && sr_dev_has_hwcap(o->dev, SR_HWCAP_SAMPLERATE)) { |
331 | samplerate = *((uint64_t *) o->dev->driver->dev_info_get( | |
332 | o->dev->driver_index, SR_DI_CUR_SAMPLERATE)); | |
a00ba012 | 333 | if (!(frequency_s = sr_samplerate_string(samplerate))) { |
133a37bf UH |
334 | g_free(ctx->header); |
335 | g_free(ctx); | |
e46b8fb1 | 336 | return SR_ERR; |
81bbdf6a DR |
337 | } |
338 | snprintf(comment, 127, gnuplot_header_comment, | |
339 | ctx->num_enabled_probes, num_probes, frequency_s); | |
133a37bf | 340 | g_free(frequency_s); |
81bbdf6a DR |
341 | } |
342 | ||
343 | /* Columns / channels */ | |
344 | wbuf[0] = '\0'; | |
345 | for (i = 0; i < ctx->num_enabled_probes; i++) { | |
346 | c = (char *)&wbuf + strlen((char *)&wbuf); | |
347 | sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]); | |
348 | } | |
349 | ||
a00ba012 | 350 | if (!(frequency_s = sr_period_string(samplerate))) { |
133a37bf UH |
351 | g_free(ctx->header); |
352 | g_free(ctx); | |
e46b8fb1 | 353 | return SR_ERR; |
81bbdf6a DR |
354 | } |
355 | t = time(NULL); | |
356 | b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header, | |
357 | PACKAGE_STRING, ctime(&t), comment, frequency_s, | |
358 | (char *)&wbuf); | |
133a37bf | 359 | g_free(frequency_s); |
81bbdf6a DR |
360 | |
361 | if (b < 0) { | |
133a37bf UH |
362 | g_free(ctx->header); |
363 | g_free(ctx); | |
e46b8fb1 | 364 | return SR_ERR; |
81bbdf6a DR |
365 | } |
366 | ||
367 | return 0; | |
368 | } | |
369 | ||
054e6709 UH |
370 | static int analog_data(struct sr_output *o, uint8_t *data_in, |
371 | uint64_t length_in, uint8_t **data_out, | |
372 | uint64_t *length_out) | |
81bbdf6a DR |
373 | { |
374 | struct context *ctx; | |
ff35879b | 375 | unsigned int max_linelen, outsize, p, /* curbit, */ i; |
81bbdf6a DR |
376 | // uint64_t sample; |
377 | static uint64_t samplecount = 0; | |
054e6709 | 378 | uint8_t *outbuf, *c; |
809c5f20 | 379 | struct sr_analog_sample *sample; |
81bbdf6a DR |
380 | |
381 | ctx = o->internal; | |
382 | // max_linelen = 16 + ctx->num_enabled_probes * 2; | |
383 | max_linelen = 16 + ctx->num_enabled_probes * 30; | |
384 | outsize = length_in / ctx->unitsize * max_linelen; | |
385 | if (ctx->header) | |
386 | outsize += strlen(ctx->header); | |
387 | ||
133a37bf UH |
388 | if (!(outbuf = g_try_malloc0(outsize))) { |
389 | sr_err("gnuplot out: %s: outbuf malloc failed", __func__); | |
e46b8fb1 | 390 | return SR_ERR_MALLOC; |
133a37bf | 391 | } |
81bbdf6a DR |
392 | |
393 | outbuf[0] = '\0'; | |
394 | if (ctx->header) { | |
395 | /* The header is still here, this must be the first packet. */ | |
396 | strncpy(outbuf, ctx->header, outsize); | |
133a37bf | 397 | g_free(ctx->header); |
81bbdf6a DR |
398 | ctx->header = NULL; |
399 | } | |
400 | ||
401 | for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) { | |
402 | // memcpy(&sample, data_in + i, ctx->unitsize); | |
809c5f20 | 403 | sample = (struct sr_analog_sample *) (data_in + i); |
81bbdf6a DR |
404 | |
405 | /* The first column is a counter (needed for gnuplot). */ | |
406 | c = outbuf + strlen(outbuf); | |
407 | sprintf(c, "%" PRIu64 "\t", samplecount++); | |
408 | ||
409 | /* The next columns are the values of all channels. */ | |
410 | for (p = 0; p < ctx->num_enabled_probes; p++) { | |
411 | // curbit = (sample & ((uint64_t) (1 << p))) >> p; | |
412 | c = outbuf + strlen(outbuf); | |
413 | // sprintf(c, "%d ", curbit); | |
414 | /* | |
415 | * FIXME: Should be doing proper raw->voltage conversion | |
416 | * here, casting to int16_t isn't it. Remember that if | |
417 | * res = 1 conversion isn't necessary. | |
418 | */ | |
419 | sprintf(c, "%f ", (double) ((int16_t) (sample->probes[p].val & | |
420 | ((1 << sample->probes[p].res) - 1)))); | |
421 | } | |
422 | ||
423 | c = outbuf + strlen(outbuf); | |
424 | sprintf(c, "\n"); | |
425 | } | |
426 | ||
427 | *data_out = outbuf; | |
428 | *length_out = strlen(outbuf); | |
429 | ||
e46b8fb1 | 430 | return SR_OK; |
81bbdf6a DR |
431 | } |
432 | ||
f50f3f40 | 433 | struct sr_output_format output_analog_gnuplot = { |
cdb3573c | 434 | .id = "analog_gnuplot", |
d494a4aa UH |
435 | .description = "Gnuplot analog", |
436 | .df_type = SR_DF_ANALOG, | |
437 | .init = analog_init, | |
438 | .data = analog_data, | |
439 | .event = event, | |
81bbdf6a | 440 | }; |
77b45442 | 441 | #endif |