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