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