]>
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 | |
a821069b | 63 | if (!(ctx = calloc(1, sizeof(struct context)))) |
e46b8fb1 | 64 | return SR_ERR_MALLOC; |
a821069b | 65 | |
d4ae8eaa BV |
66 | if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) { |
67 | free(ctx); | |
e46b8fb1 | 68 | return SR_ERR_MALLOC; |
d4ae8eaa BV |
69 | } |
70 | ||
e2ad47b5 UH |
71 | o->internal = ctx; |
72 | ctx->num_enabled_probes = 0; | |
73 | for (l = o->device->probes; l; l = l->next) { | |
74 | probe = l->data; | |
757b8c62 UH |
75 | if (!probe->enabled) |
76 | continue; | |
77 | ctx->probelist[ctx->num_enabled_probes++] = probe->name; | |
e2ad47b5 | 78 | } |
e2ad47b5 UH |
79 | ctx->probelist[ctx->num_enabled_probes] = 0; |
80 | ctx->unitsize = (ctx->num_enabled_probes + 7) / 8; | |
81 | ||
e2ad47b5 | 82 | num_probes = g_slist_length(o->device->probes); |
a821069b | 83 | comment[0] = '\0'; |
2bf4aca6 | 84 | if (o->device->plugin && sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) { |
7aae7462 | 85 | samplerate = *((uint64_t *) o->device->plugin->get_device_info( |
5a2326a7 | 86 | o->device->plugin_index, SR_DI_CUR_SAMPLERATE)); |
a00ba012 | 87 | if (!(frequency_s = sr_samplerate_string(samplerate))) { |
a821069b UH |
88 | free(ctx->header); |
89 | free(ctx); | |
e46b8fb1 | 90 | return SR_ERR; |
a821069b UH |
91 | } |
92 | snprintf(comment, 127, gnuplot_header_comment, | |
d4ae8eaa BV |
93 | ctx->num_enabled_probes, num_probes, frequency_s); |
94 | free(frequency_s); | |
7aae7462 | 95 | } |
e2ad47b5 UH |
96 | |
97 | /* Columns / channels */ | |
98 | wbuf[0] = '\0'; | |
99 | for (i = 0; i < ctx->num_enabled_probes; i++) { | |
100 | c = (char *)&wbuf + strlen((char *)&wbuf); | |
114fb93f | 101 | sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]); |
e2ad47b5 UH |
102 | } |
103 | ||
a00ba012 | 104 | if (!(frequency_s = sr_period_string(samplerate))) { |
d4ae8eaa BV |
105 | free(ctx->header); |
106 | free(ctx); | |
e46b8fb1 | 107 | return SR_ERR; |
d4ae8eaa | 108 | } |
5cca9adb | 109 | t = time(NULL); |
e2ad47b5 | 110 | b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header, |
d4ae8eaa | 111 | PACKAGE_STRING, ctime(&t), comment, frequency_s, |
5cca9adb | 112 | (char *)&wbuf); |
d4ae8eaa | 113 | free(frequency_s); |
e2ad47b5 | 114 | |
d4ae8eaa BV |
115 | if (b < 0) { |
116 | free(ctx->header); | |
117 | free(ctx); | |
e46b8fb1 | 118 | return SR_ERR; |
d4ae8eaa | 119 | } |
e2ad47b5 UH |
120 | |
121 | return 0; | |
122 | } | |
123 | ||
f50f3f40 | 124 | static int event(struct sr_output *o, int event_type, char **data_out, |
e2ad47b5 UH |
125 | uint64_t *length_out) |
126 | { | |
127 | struct context *ctx; | |
e2ad47b5 UH |
128 | |
129 | ctx = o->internal; | |
99c1fc59 | 130 | switch (event_type) { |
5a2326a7 | 131 | case SR_DF_TRIGGER: |
9ab95e54 | 132 | /* TODO: can a trigger mark be in a gnuplot data file? */ |
e2ad47b5 | 133 | break; |
5a2326a7 | 134 | case SR_DF_END: |
e2ad47b5 UH |
135 | free(o->internal); |
136 | o->internal = NULL; | |
137 | break; | |
138 | } | |
139 | ||
9ab95e54 BV |
140 | *data_out = NULL; |
141 | *length_out = 0; | |
142 | ||
e46b8fb1 | 143 | return SR_OK; |
e2ad47b5 UH |
144 | } |
145 | ||
54ac5277 | 146 | static int data(struct sr_output *o, const char *data_in, uint64_t length_in, |
e2ad47b5 UH |
147 | char **data_out, uint64_t *length_out) |
148 | { | |
149 | struct context *ctx; | |
d4ae8eaa | 150 | unsigned int max_linelen, outsize, p, curbit, i; |
086eac7c UH |
151 | uint64_t sample; |
152 | static uint64_t samplecount = 0; | |
e2ad47b5 UH |
153 | char *outbuf, *c; |
154 | ||
155 | ctx = o->internal; | |
d4ae8eaa BV |
156 | max_linelen = 16 + ctx->num_enabled_probes * 2; |
157 | outsize = length_in / ctx->unitsize * max_linelen; | |
e273a904 | 158 | if (ctx->header) |
d4ae8eaa | 159 | outsize += strlen(ctx->header); |
a821069b | 160 | |
d4ae8eaa | 161 | if (!(outbuf = calloc(1, outsize))) |
e46b8fb1 | 162 | return SR_ERR_MALLOC; |
a821069b UH |
163 | |
164 | outbuf[0] = '\0'; | |
e2ad47b5 UH |
165 | if (ctx->header) { |
166 | /* The header is still here, this must be the first packet. */ | |
167 | strncpy(outbuf, ctx->header, outsize); | |
168 | free(ctx->header); | |
169 | ctx->header = NULL; | |
e2ad47b5 UH |
170 | } |
171 | ||
607b58de UH |
172 | for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) { |
173 | memcpy(&sample, data_in + i, ctx->unitsize); | |
e2ad47b5 UH |
174 | |
175 | /* The first column is a counter (needed for gnuplot). */ | |
176 | c = outbuf + strlen(outbuf); | |
d4ae8eaa | 177 | sprintf(c, "%" PRIu64 "\t", samplecount++); |
e2ad47b5 UH |
178 | |
179 | /* The next columns are the values of all channels. */ | |
180 | for (p = 0; p < ctx->num_enabled_probes; p++) { | |
fbe2f794 | 181 | curbit = (sample & ((uint64_t) (1 << p))) >> p; |
e2ad47b5 UH |
182 | c = outbuf + strlen(outbuf); |
183 | sprintf(c, "%d ", curbit); | |
184 | } | |
185 | ||
186 | c = outbuf + strlen(outbuf); | |
187 | sprintf(c, "\n"); | |
e2ad47b5 UH |
188 | } |
189 | ||
190 | *data_out = outbuf; | |
191 | *length_out = strlen(outbuf); | |
192 | ||
e46b8fb1 | 193 | return SR_OK; |
e2ad47b5 UH |
194 | } |
195 | ||
77b45442 | 196 | struct sr_output_format output_gnuplot = { |
d494a4aa UH |
197 | .extension = "gnuplot", |
198 | .description = "Gnuplot", | |
199 | .df_type = SR_DF_LOGIC, | |
200 | .init = init, | |
201 | .data = data, | |
202 | .event = event, | |
77b45442 UH |
203 | }; |
204 | ||
205 | /* Temporarily disabled. */ | |
206 | #if 0 | |
f50f3f40 | 207 | static int analog_init(struct sr_output *o) |
81bbdf6a DR |
208 | { |
209 | struct context *ctx; | |
1afe8989 | 210 | struct sr_probe *probe; |
81bbdf6a DR |
211 | GSList *l; |
212 | uint64_t samplerate; | |
213 | unsigned int i; | |
214 | int b, num_probes; | |
215 | char *c, *frequency_s; | |
216 | char wbuf[1000], comment[128]; | |
217 | time_t t; | |
218 | ||
219 | if (!(ctx = calloc(1, sizeof(struct context)))) | |
e46b8fb1 | 220 | return SR_ERR_MALLOC; |
81bbdf6a DR |
221 | |
222 | if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) { | |
223 | free(ctx); | |
e46b8fb1 | 224 | return SR_ERR_MALLOC; |
81bbdf6a DR |
225 | } |
226 | ||
227 | o->internal = ctx; | |
228 | ctx->num_enabled_probes = 0; | |
229 | for (l = o->device->probes; l; l = l->next) { | |
230 | probe = l->data; | |
231 | if (!probe->enabled) | |
232 | continue; | |
233 | ctx->probelist[ctx->num_enabled_probes++] = probe->name; | |
234 | } | |
235 | ctx->probelist[ctx->num_enabled_probes] = 0; | |
236 | // ctx->unitsize = (ctx->num_enabled_probes + 7) / 8; | |
809c5f20 UH |
237 | ctx->unitsize = sizeof(struct sr_analog_sample) + |
238 | (ctx->num_enabled_probes * sizeof(struct sr_analog_probe)); | |
81bbdf6a DR |
239 | |
240 | num_probes = g_slist_length(o->device->probes); | |
241 | comment[0] = '\0'; | |
2bf4aca6 | 242 | if (o->device->plugin && sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) { |
81bbdf6a | 243 | samplerate = *((uint64_t *) o->device->plugin->get_device_info( |
5a2326a7 | 244 | o->device->plugin_index, SR_DI_CUR_SAMPLERATE)); |
a00ba012 | 245 | if (!(frequency_s = sr_samplerate_string(samplerate))) { |
81bbdf6a DR |
246 | free(ctx->header); |
247 | free(ctx); | |
e46b8fb1 | 248 | return SR_ERR; |
81bbdf6a DR |
249 | } |
250 | snprintf(comment, 127, gnuplot_header_comment, | |
251 | ctx->num_enabled_probes, num_probes, frequency_s); | |
252 | free(frequency_s); | |
253 | } | |
254 | ||
255 | /* Columns / channels */ | |
256 | wbuf[0] = '\0'; | |
257 | for (i = 0; i < ctx->num_enabled_probes; i++) { | |
258 | c = (char *)&wbuf + strlen((char *)&wbuf); | |
259 | sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]); | |
260 | } | |
261 | ||
a00ba012 | 262 | if (!(frequency_s = sr_period_string(samplerate))) { |
81bbdf6a DR |
263 | free(ctx->header); |
264 | free(ctx); | |
e46b8fb1 | 265 | return SR_ERR; |
81bbdf6a DR |
266 | } |
267 | t = time(NULL); | |
268 | b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header, | |
269 | PACKAGE_STRING, ctime(&t), comment, frequency_s, | |
270 | (char *)&wbuf); | |
271 | free(frequency_s); | |
272 | ||
273 | if (b < 0) { | |
274 | free(ctx->header); | |
275 | free(ctx); | |
e46b8fb1 | 276 | return SR_ERR; |
81bbdf6a DR |
277 | } |
278 | ||
279 | return 0; | |
280 | } | |
281 | ||
f50f3f40 | 282 | static int analog_data(struct sr_output *o, char *data_in, uint64_t length_in, |
81bbdf6a DR |
283 | char **data_out, uint64_t *length_out) |
284 | { | |
285 | struct context *ctx; | |
ff35879b | 286 | unsigned int max_linelen, outsize, p, /* curbit, */ i; |
81bbdf6a DR |
287 | // uint64_t sample; |
288 | static uint64_t samplecount = 0; | |
289 | char *outbuf, *c; | |
809c5f20 | 290 | struct sr_analog_sample *sample; |
81bbdf6a DR |
291 | |
292 | ctx = o->internal; | |
293 | // max_linelen = 16 + ctx->num_enabled_probes * 2; | |
294 | max_linelen = 16 + ctx->num_enabled_probes * 30; | |
295 | outsize = length_in / ctx->unitsize * max_linelen; | |
296 | if (ctx->header) | |
297 | outsize += strlen(ctx->header); | |
298 | ||
299 | if (!(outbuf = calloc(1, outsize))) | |
e46b8fb1 | 300 | return SR_ERR_MALLOC; |
81bbdf6a DR |
301 | |
302 | outbuf[0] = '\0'; | |
303 | if (ctx->header) { | |
304 | /* The header is still here, this must be the first packet. */ | |
305 | strncpy(outbuf, ctx->header, outsize); | |
306 | free(ctx->header); | |
307 | ctx->header = NULL; | |
308 | } | |
309 | ||
310 | for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) { | |
311 | // memcpy(&sample, data_in + i, ctx->unitsize); | |
809c5f20 | 312 | sample = (struct sr_analog_sample *) (data_in + i); |
81bbdf6a DR |
313 | |
314 | /* The first column is a counter (needed for gnuplot). */ | |
315 | c = outbuf + strlen(outbuf); | |
316 | sprintf(c, "%" PRIu64 "\t", samplecount++); | |
317 | ||
318 | /* The next columns are the values of all channels. */ | |
319 | for (p = 0; p < ctx->num_enabled_probes; p++) { | |
320 | // curbit = (sample & ((uint64_t) (1 << p))) >> p; | |
321 | c = outbuf + strlen(outbuf); | |
322 | // sprintf(c, "%d ", curbit); | |
323 | /* | |
324 | * FIXME: Should be doing proper raw->voltage conversion | |
325 | * here, casting to int16_t isn't it. Remember that if | |
326 | * res = 1 conversion isn't necessary. | |
327 | */ | |
328 | sprintf(c, "%f ", (double) ((int16_t) (sample->probes[p].val & | |
329 | ((1 << sample->probes[p].res) - 1)))); | |
330 | } | |
331 | ||
332 | c = outbuf + strlen(outbuf); | |
333 | sprintf(c, "\n"); | |
334 | } | |
335 | ||
336 | *data_out = outbuf; | |
337 | *length_out = strlen(outbuf); | |
338 | ||
e46b8fb1 | 339 | return SR_OK; |
81bbdf6a DR |
340 | } |
341 | ||
f50f3f40 | 342 | struct sr_output_format output_analog_gnuplot = { |
d494a4aa UH |
343 | .extension = "analog_gnuplot", |
344 | .description = "Gnuplot analog", | |
345 | .df_type = SR_DF_ANALOG, | |
346 | .init = analog_init, | |
347 | .data = analog_data, | |
348 | .event = event, | |
81bbdf6a | 349 | }; |
77b45442 | 350 | #endif |