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