]>
Commit | Line | Data |
---|---|---|
02604ed6 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
02604ed6 UH |
3 | * |
4 | * Copyright (C) 2011 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 | ||
6ec6c43b | 21 | #include <config.h> |
02604ed6 UH |
22 | #include <stdlib.h> |
23 | #include <string.h> | |
24 | #include <glib.h> | |
c1aae900 | 25 | #include <libsigrok/libsigrok.h> |
45c59c8b | 26 | #include "libsigrok-internal.h" |
02604ed6 | 27 | |
3544f848 | 28 | #define LOG_PREFIX "output/csv" |
a944a84b | 29 | |
02604ed6 | 30 | struct context { |
ba7dd8bb | 31 | unsigned int num_enabled_channels; |
02604ed6 | 32 | uint64_t samplerate; |
02604ed6 | 33 | char separator; |
0f3d8c89 | 34 | gboolean header_done; |
c04cf9aa BG |
35 | struct sr_channel **channels; |
36 | ||
37 | /* For analog measurements split into frames, not packets. */ | |
38 | struct sr_channel **analog_channels; | |
39 | float *analog_vals; /* Analog values stored until the end of the frame. */ | |
40 | unsigned int num_analog_channels; | |
41 | gboolean inframe; | |
02604ed6 UH |
42 | }; |
43 | ||
44 | /* | |
45 | * TODO: | |
46 | * - Option to specify delimiter character and/or string. | |
47 | * - Option to (not) print metadata as comments. | |
48 | * - Option to specify the comment character(s), e.g. # or ; or C/C++-style. | |
49 | * - Option to (not) print samplenumber / time as extra column. | |
50 | * - Option to "compress" output (only print changed samples, VCD-like). | |
51 | * - Option to print comma-separated bits, or whole bytes/words (for 8/16 | |
ba7dd8bb | 52 | * channel LAs) as ASCII/hex etc. etc. |
02604ed6 UH |
53 | * - Trigger support. |
54 | */ | |
55 | ||
a755b0e1 | 56 | static int init(struct sr_output *o, GHashTable *options) |
02604ed6 UH |
57 | { |
58 | struct context *ctx; | |
ba7dd8bb | 59 | struct sr_channel *ch; |
02604ed6 | 60 | GSList *l; |
c04cf9aa | 61 | int i, j; |
02604ed6 | 62 | |
a755b0e1 BV |
63 | (void)options; |
64 | ||
0f3d8c89 | 65 | if (!o || !o->sdi) |
02604ed6 | 66 | return SR_ERR_ARG; |
02604ed6 | 67 | |
2a035e53 | 68 | ctx = g_malloc0(sizeof(struct context)); |
d686c5ec | 69 | o->priv = ctx; |
0f3d8c89 | 70 | ctx->separator = ','; |
02604ed6 | 71 | |
ba7dd8bb UH |
72 | /* Get the number of channels, and the unitsize. */ |
73 | for (l = o->sdi->channels; l; l = l->next) { | |
74 | ch = l->data; | |
c04cf9aa BG |
75 | if (ch->enabled) { |
76 | if (ch->type == SR_CHANNEL_LOGIC || | |
77 | ch->type == SR_CHANNEL_ANALOG) | |
78 | ctx->num_enabled_channels++; | |
79 | if (ch->type == SR_CHANNEL_ANALOG) | |
80 | ctx->num_analog_channels++; | |
81 | } | |
02604ed6 | 82 | } |
c04cf9aa BG |
83 | ctx->channels = g_malloc(sizeof(struct sr_channel *) |
84 | * ctx->num_enabled_channels); | |
85 | ctx->analog_channels = g_malloc(sizeof(struct sr_channel *) | |
86 | * ctx->num_analog_channels); | |
87 | ctx->analog_vals = g_malloc(sizeof(float) * ctx->num_analog_channels); | |
02604ed6 | 88 | |
0f3d8c89 | 89 | /* Once more to map the enabled channels. */ |
c04cf9aa | 90 | for (i = 0, l = o->sdi->channels, j = 0; l; l = l->next) { |
0f3d8c89 | 91 | ch = l->data; |
c04cf9aa BG |
92 | if (ch->enabled) { |
93 | if (ch->type == SR_CHANNEL_LOGIC || | |
94 | ch->type == SR_CHANNEL_ANALOG) | |
95 | ctx->channels[i++] = ch; | |
96 | if (ch->type == SR_CHANNEL_ANALOG) | |
97 | ctx->analog_channels[j++] = ch; | |
98 | } | |
99 | ||
0f3d8c89 | 100 | } |
02604ed6 | 101 | |
0f3d8c89 BV |
102 | return SR_OK; |
103 | } | |
02604ed6 | 104 | |
a755b0e1 | 105 | static GString *gen_header(const struct sr_output *o) |
0f3d8c89 BV |
106 | { |
107 | struct context *ctx; | |
108 | struct sr_channel *ch; | |
109 | GVariant *gvar; | |
110 | GString *header; | |
111 | GSList *l; | |
112 | time_t t; | |
113 | int num_channels, i; | |
114 | char *samplerate_s; | |
02604ed6 | 115 | |
d686c5ec | 116 | ctx = o->priv; |
0f3d8c89 | 117 | header = g_string_sized_new(512); |
02604ed6 UH |
118 | |
119 | /* Some metadata */ | |
0f3d8c89 | 120 | t = time(NULL); |
b9eb8e1a DE |
121 | g_string_append_printf(header, "; CSV, generated by %s %s on %s", |
122 | PACKAGE_NAME, SR_PACKAGE_VERSION_STRING, ctime(&t)); | |
02604ed6 UH |
123 | |
124 | /* Columns / channels */ | |
0f3d8c89 BV |
125 | num_channels = g_slist_length(o->sdi->channels); |
126 | g_string_append_printf(header, "; Channels (%d/%d):", | |
127 | ctx->num_enabled_channels, num_channels); | |
128 | for (i = 0, l = o->sdi->channels; l; l = l->next, i++) { | |
ba7dd8bb | 129 | ch = l->data; |
c04cf9aa BG |
130 | if (ch->enabled && |
131 | (ch->type == SR_CHANNEL_LOGIC || | |
132 | ch->type == SR_CHANNEL_ANALOG)) | |
133 | g_string_append_printf(header, " %s,", ch->name); | |
1c5b099a | 134 | } |
ba7dd8bb | 135 | if (o->sdi->channels) |
e36aae98 | 136 | /* Drop last separator. */ |
0f3d8c89 BV |
137 | g_string_truncate(header, header->len - 1); |
138 | g_string_append_printf(header, "\n"); | |
139 | ||
140 | if (ctx->samplerate == 0) { | |
141 | if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE, | |
142 | &gvar) == SR_OK) { | |
143 | ctx->samplerate = g_variant_get_uint64(gvar); | |
144 | g_variant_unref(gvar); | |
145 | } | |
146 | } | |
147 | if (ctx->samplerate != 0) { | |
148 | samplerate_s = sr_samplerate_string(ctx->samplerate); | |
149 | g_string_append_printf(header, "; Samplerate: %s\n", samplerate_s); | |
150 | g_free(samplerate_s); | |
151 | } | |
02604ed6 | 152 | |
0f3d8c89 | 153 | return header; |
02604ed6 UH |
154 | } |
155 | ||
c04cf9aa BG |
156 | static void init_output(GString **out, struct context *ctx, |
157 | const struct sr_output *o) | |
158 | { | |
159 | if (!ctx->header_done) { | |
160 | *out = gen_header(o); | |
161 | ctx->header_done = TRUE; | |
162 | } else { | |
163 | *out = g_string_sized_new(512); | |
164 | } | |
165 | } | |
166 | ||
453629c1 ML |
167 | static void handle_analog_frame(struct context *ctx, GSList *channels, |
168 | unsigned int num_samples, float *data) | |
c04cf9aa BG |
169 | { |
170 | unsigned int numch, nums, i, j, s; | |
171 | GSList *l; | |
172 | ||
453629c1 ML |
173 | numch = g_slist_length(channels); |
174 | if (num_samples > numch) | |
175 | nums = num_samples / numch; | |
c04cf9aa BG |
176 | else |
177 | nums = 1; | |
178 | ||
179 | s = 0; | |
453629c1 | 180 | l = channels; |
c04cf9aa BG |
181 | for (i = 0; i < nums; i++) { |
182 | for (j = 0; j < ctx->num_analog_channels; j++) { | |
183 | if (ctx->analog_channels[j] == l->data) | |
453629c1 | 184 | ctx->analog_vals[j] = data[s++]; |
c04cf9aa BG |
185 | } |
186 | l = l->next; | |
187 | } | |
188 | } | |
189 | ||
a755b0e1 | 190 | static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet, |
dba3e682 | 191 | GString **out) |
02604ed6 | 192 | { |
0f3d8c89 | 193 | const struct sr_datafeed_meta *meta; |
4829d37d | 194 | const struct sr_datafeed_logic *logic; |
5faebab2 | 195 | const struct sr_datafeed_analog_old *analog_old; |
edb691fc | 196 | const struct sr_datafeed_analog *analog; |
0f3d8c89 | 197 | const struct sr_config *src; |
453629c1 ML |
198 | unsigned int num_samples; |
199 | float *data; | |
200 | GSList *l, *channels; | |
02604ed6 | 201 | struct context *ctx; |
2a035e53 | 202 | int idx; |
c04cf9aa | 203 | uint64_t i, j, k, nums, numch; |
e96cf218 | 204 | gchar *p, c; |
c04cf9aa | 205 | int ret = SR_OK; |
02604ed6 | 206 | |
4829d37d BV |
207 | *out = NULL; |
208 | if (!o || !o->sdi) | |
02604ed6 | 209 | return SR_ERR_ARG; |
d686c5ec | 210 | if (!(ctx = o->priv)) |
02604ed6 | 211 | return SR_ERR_ARG; |
02604ed6 | 212 | |
4829d37d | 213 | switch (packet->type) { |
0f3d8c89 BV |
214 | case SR_DF_META: |
215 | meta = packet->payload; | |
216 | for (l = meta->config; l; l = l->next) { | |
217 | src = l->data; | |
218 | if (src->key != SR_CONF_SAMPLERATE) | |
219 | continue; | |
220 | ctx->samplerate = g_variant_get_uint64(src->data); | |
221 | } | |
222 | break; | |
c04cf9aa BG |
223 | case SR_DF_FRAME_BEGIN: |
224 | /* | |
225 | * Special case - we start gathering data from analog channels | |
226 | * and wait for SR_DF_FRAME_END to dump it. | |
227 | */ | |
228 | memset(ctx->analog_vals, 0, sizeof(float) * ctx->num_analog_channels); | |
229 | ctx->inframe = TRUE; | |
b9300678 | 230 | ret = SR_OK; |
c04cf9aa BG |
231 | break; |
232 | case SR_DF_FRAME_END: | |
233 | /* | |
234 | * Dump gathered data. | |
235 | */ | |
236 | init_output(out, ctx, o); | |
237 | ||
238 | for (i = 0, j = 0; i < ctx->num_enabled_channels; i++) { | |
239 | if (ctx->channels[i]->type == SR_CHANNEL_ANALOG) { | |
240 | g_string_append_printf(*out, "%f", | |
241 | ctx->analog_vals[j++]); | |
242 | } | |
243 | g_string_append_c(*out, ctx->separator); | |
244 | } | |
245 | g_string_truncate(*out, (*out)->len - 1); | |
246 | g_string_append_printf(*out, "\n"); | |
247 | ||
248 | ctx->inframe = FALSE; | |
249 | break; | |
4829d37d BV |
250 | case SR_DF_LOGIC: |
251 | logic = packet->payload; | |
c04cf9aa | 252 | init_output(out, ctx, o); |
4829d37d | 253 | |
2a035e53 | 254 | for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) { |
ba7dd8bb | 255 | for (j = 0; j < ctx->num_enabled_channels; j++) { |
c04cf9aa BG |
256 | if (ctx->channels[j]->type == SR_CHANNEL_LOGIC) { |
257 | idx = ctx->channels[j]->index; | |
258 | p = logic->data + i + idx / 8; | |
259 | c = *p & (1 << (idx % 8)); | |
260 | g_string_append_c(*out, c ? '1' : '0'); | |
261 | } | |
e96cf218 | 262 | g_string_append_c(*out, ctx->separator); |
4829d37d | 263 | } |
e36aae98 | 264 | if (j) { |
54da58ca BV |
265 | /* Drop last separator. */ |
266 | g_string_truncate(*out, (*out)->len - 1); | |
267 | } | |
4829d37d BV |
268 | g_string_append_printf(*out, "\n"); |
269 | } | |
02604ed6 | 270 | break; |
5faebab2 | 271 | case SR_DF_ANALOG_OLD: |
edb691fc | 272 | case SR_DF_ANALOG: |
5faebab2 | 273 | analog_old = packet->payload; |
edb691fc | 274 | analog = packet->payload; |
453629c1 | 275 | |
5faebab2 UH |
276 | if (packet->type == SR_DF_ANALOG_OLD) { |
277 | channels = analog_old->channels; | |
453629c1 | 278 | numch = g_slist_length(channels); |
5faebab2 UH |
279 | num_samples = analog_old->num_samples; |
280 | data = analog_old->data; | |
453629c1 | 281 | } else { |
edb691fc | 282 | channels = analog->meaning->channels; |
453629c1 | 283 | numch = g_slist_length(channels); |
edb691fc | 284 | num_samples = analog->num_samples; |
453629c1 | 285 | data = g_malloc(sizeof(float) * num_samples * numch); |
edb691fc | 286 | ret = sr_analog_to_float(analog, data); |
453629c1 ML |
287 | if (ret != SR_OK) |
288 | return ret; | |
289 | } | |
c04cf9aa BG |
290 | |
291 | if (ctx->inframe) { | |
453629c1 | 292 | handle_analog_frame(ctx, channels, num_samples, data); |
c04cf9aa BG |
293 | break; |
294 | } | |
295 | ||
296 | init_output(out, ctx, o); | |
297 | k = 0; | |
298 | l = NULL; | |
299 | ||
453629c1 ML |
300 | if (num_samples > numch) |
301 | nums = num_samples / numch; | |
c04cf9aa BG |
302 | else |
303 | nums = 1; | |
304 | ||
305 | for (i = 0; i < nums; i++) { | |
306 | for (j = 0; j < ctx->num_enabled_channels; j++) { | |
307 | if (ctx->channels[j]->type == SR_CHANNEL_ANALOG) { | |
308 | if (!l) | |
453629c1 | 309 | l = channels; |
c04cf9aa BG |
310 | |
311 | if (ctx->channels[j] == l->data) { | |
312 | g_string_append_printf(*out, | |
453629c1 | 313 | "%f", data[k++]); |
c04cf9aa BG |
314 | } |
315 | ||
316 | l = l->next; | |
317 | } | |
318 | g_string_append_c(*out, ctx->separator); | |
319 | } | |
320 | g_string_truncate(*out, (*out)->len - 1); | |
321 | g_string_append_printf(*out, "\n"); | |
322 | } | |
323 | break; | |
02604ed6 UH |
324 | } |
325 | ||
c04cf9aa | 326 | return ret; |
02604ed6 UH |
327 | } |
328 | ||
4829d37d | 329 | static int cleanup(struct sr_output *o) |
02604ed6 UH |
330 | { |
331 | struct context *ctx; | |
02604ed6 | 332 | |
4829d37d | 333 | if (!o || !o->sdi) |
02604ed6 | 334 | return SR_ERR_ARG; |
02604ed6 | 335 | |
d686c5ec BV |
336 | if (o->priv) { |
337 | ctx = o->priv; | |
c04cf9aa BG |
338 | g_free(ctx->channels); |
339 | g_free(ctx->analog_channels); | |
340 | g_free(ctx->analog_vals); | |
d686c5ec BV |
341 | g_free(o->priv); |
342 | o->priv = NULL; | |
02604ed6 UH |
343 | } |
344 | ||
02604ed6 UH |
345 | return SR_OK; |
346 | } | |
347 | ||
a755b0e1 | 348 | SR_PRIV struct sr_output_module output_csv = { |
02604ed6 | 349 | .id = "csv", |
a755b0e1 BV |
350 | .name = "CSV", |
351 | .desc = "Comma-separated values", | |
8a174d23 | 352 | .exts = (const char*[]){"csv", NULL}, |
3cd4b381 | 353 | .flags = 0, |
a755b0e1 | 354 | .options = NULL, |
02604ed6 | 355 | .init = init, |
4829d37d BV |
356 | .receive = receive, |
357 | .cleanup = cleanup, | |
02604ed6 | 358 | }; |