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