]>
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 | ||
9e24c8bc MM |
21 | /* |
22 | * Options and their values: | |
23 | * | |
24 | * gnuplot: Write out a gnuplot interpreter script (.gpi file) to plot | |
25 | * the datafile using the parameters given. It should be called | |
26 | * from a gnuplot session with the data file name as a parameter | |
27 | * after adjusting line styles, terminal, etc. | |
28 | * | |
29 | * scale: The gnuplot graphs are scaled so they all have the same | |
30 | * peak-to-peak distance. Defaults to TRUE. | |
31 | * | |
32 | * value: The string used to separate values in a record. Defaults to ','. | |
33 | * | |
34 | * record: The string to use to separate records. Default is newline. gnuplot | |
35 | * files must use newline. | |
36 | * | |
37 | * frame: The string to use when a frame ends. The default is a blank line. | |
38 | * This may confuse some CSV parsers, but it makes gnuplot happy. | |
39 | * | |
40 | * comment: The string that starts a comment line. Defaults to ';'. | |
41 | * | |
42 | * header: Print header comment with capture metadata. Defaults to TRUE. | |
43 | * | |
cad447d2 MM |
44 | * label: What to use for channel labels as the first line of output. |
45 | * Values are "channel", "units", "off". Defaults to "units". | |
9e24c8bc MM |
46 | * |
47 | * time: Whether or not the first column should include the time the sample | |
48 | * was taken. Defaults to TRUE. | |
49 | * | |
50 | * trigger: Whether or not to add a "trigger" column as the last column. | |
51 | * Defaults to FALSE. | |
52 | * | |
53 | * dedup: Don't output duplicate rows. Defaults to TRUE. If time is off, then | |
54 | * this is forced to be off. | |
55 | */ | |
56 | ||
57 | #include <math.h> | |
6ec6c43b | 58 | #include <config.h> |
02604ed6 UH |
59 | #include <stdlib.h> |
60 | #include <string.h> | |
61 | #include <glib.h> | |
c1aae900 | 62 | #include <libsigrok/libsigrok.h> |
45c59c8b | 63 | #include "libsigrok-internal.h" |
02604ed6 | 64 | |
3544f848 | 65 | #define LOG_PREFIX "output/csv" |
a944a84b | 66 | |
9e24c8bc MM |
67 | struct ctx_channel { |
68 | struct sr_channel *ch; | |
cad447d2 | 69 | char *label; |
9e24c8bc MM |
70 | float min, max; |
71 | }; | |
72 | ||
02604ed6 | 73 | struct context { |
9e24c8bc MM |
74 | /* Options */ |
75 | const char *gnuplot; | |
76 | gboolean scale; | |
77 | const char *value; | |
78 | const char *record; | |
79 | const char *frame; | |
80 | const char *comment; | |
81 | gboolean header, did_header; | |
cad447d2 | 82 | gboolean label_do, label_did, label_names; |
9e24c8bc MM |
83 | gboolean time; |
84 | gboolean do_trigger; | |
85 | gboolean dedup; | |
86 | ||
87 | /* Plot data */ | |
c04cf9aa | 88 | unsigned int num_analog_channels; |
9e24c8bc MM |
89 | unsigned int num_logic_channels; |
90 | struct ctx_channel *channels; | |
91 | ||
92 | /* Metadata */ | |
93 | gboolean trigger; | |
94 | uint32_t num_samples; | |
95 | uint32_t channel_count, logic_channel_count; | |
96 | uint32_t channels_seen; | |
97 | uint64_t period; | |
98 | uint64_t sample_time; | |
99 | uint8_t *previous_sample; | |
100 | float *analog_samples; | |
101 | uint8_t *logic_samples; | |
102 | const char *xlabel; /* Don't free: will point to a static string. */ | |
103 | const char *title; /* Don't free: will point into the driver struct. */ | |
02604ed6 UH |
104 | }; |
105 | ||
106 | /* | |
107 | * TODO: | |
02604ed6 | 108 | * - Option to print comma-separated bits, or whole bytes/words (for 8/16 |
ba7dd8bb | 109 | * channel LAs) as ASCII/hex etc. etc. |
02604ed6 UH |
110 | */ |
111 | ||
a755b0e1 | 112 | static int init(struct sr_output *o, GHashTable *options) |
02604ed6 | 113 | { |
9e24c8bc | 114 | unsigned int i, analog_channels, logic_channels; |
02604ed6 | 115 | struct context *ctx; |
ba7dd8bb | 116 | struct sr_channel *ch; |
cad447d2 | 117 | const char *label_string; |
02604ed6 | 118 | GSList *l; |
a755b0e1 | 119 | |
0f3d8c89 | 120 | if (!o || !o->sdi) |
02604ed6 | 121 | return SR_ERR_ARG; |
02604ed6 | 122 | |
2a035e53 | 123 | ctx = g_malloc0(sizeof(struct context)); |
d686c5ec | 124 | o->priv = ctx; |
02604ed6 | 125 | |
9e24c8bc MM |
126 | /* Options */ |
127 | ctx->gnuplot = g_strdup(g_variant_get_string( | |
128 | g_hash_table_lookup(options, "gnuplot"), NULL)); | |
129 | ctx->scale = g_variant_get_boolean(g_hash_table_lookup(options, "scale")); | |
130 | ctx->value = g_strdup(g_variant_get_string( | |
131 | g_hash_table_lookup(options, "value"), NULL)); | |
132 | ctx->record = g_strdup(g_variant_get_string( | |
133 | g_hash_table_lookup(options, "record"), NULL)); | |
134 | ctx->frame = g_strdup(g_variant_get_string( | |
135 | g_hash_table_lookup(options, "frame"), NULL)); | |
136 | ctx->comment = g_strdup(g_variant_get_string( | |
137 | g_hash_table_lookup(options, "comment"), NULL)); | |
138 | ctx->header = g_variant_get_boolean(g_hash_table_lookup(options, "header")); | |
139 | ctx->time = g_variant_get_boolean(g_hash_table_lookup(options, "time")); | |
140 | ctx->do_trigger = g_variant_get_boolean(g_hash_table_lookup(options, "trigger")); | |
cad447d2 MM |
141 | label_string = g_variant_get_string( |
142 | g_hash_table_lookup(options, "label"), NULL); | |
9e24c8bc MM |
143 | ctx->dedup = g_variant_get_boolean(g_hash_table_lookup(options, "dedup")); |
144 | ctx->dedup &= ctx->time; | |
145 | ||
146 | if (*ctx->gnuplot && g_strcmp0(ctx->record, "\n")) | |
147 | sr_warn("gnuplot record separator must be newline."); | |
148 | ||
149 | if (*ctx->gnuplot && strlen(ctx->value) > 1) | |
150 | sr_warn("gnuplot doesn't support multichar value separators."); | |
151 | ||
cad447d2 MM |
152 | if ((ctx->label_did = ctx->label_do = g_strcmp0(label_string, "off") != 0)) |
153 | ctx->label_names = g_strcmp0(label_string, "units") != 0; | |
154 | ||
9e24c8bc MM |
155 | sr_dbg("gnuplot = '%s', scale = %d", ctx->gnuplot, ctx->scale); |
156 | sr_dbg("value = '%s', record = '%s', frame = '%s', comment = '%s'", | |
157 | ctx->value, ctx->record, ctx->frame, ctx->comment); | |
cad447d2 MM |
158 | sr_dbg("header = %d, time = %d, do_trigger = %d, dedup = %d", |
159 | ctx->header, ctx->time, ctx->do_trigger, ctx->dedup); | |
160 | sr_dbg("label_do = %d, label_names = %d", ctx->label_do, ctx->label_names); | |
9e24c8bc MM |
161 | |
162 | analog_channels = logic_channels = 0; | |
ba7dd8bb UH |
163 | /* Get the number of channels, and the unitsize. */ |
164 | for (l = o->sdi->channels; l; l = l->next) { | |
165 | ch = l->data; | |
9e24c8bc MM |
166 | if (ch->type == SR_CHANNEL_LOGIC) { |
167 | ctx->logic_channel_count++; | |
168 | if (ch->enabled) | |
169 | logic_channels++; | |
c04cf9aa | 170 | } |
9e24c8bc MM |
171 | if (ch->type == SR_CHANNEL_ANALOG && ch->enabled) |
172 | analog_channels++; | |
173 | } | |
174 | if (analog_channels) { | |
175 | sr_info("Outputting %d analog values", analog_channels); | |
176 | ctx->num_analog_channels = analog_channels; | |
177 | } | |
178 | if (logic_channels) { | |
179 | sr_info("Outputting %d logic values", logic_channels); | |
180 | ctx->num_logic_channels = logic_channels; | |
02604ed6 | 181 | } |
9e24c8bc MM |
182 | ctx->channels = g_malloc(sizeof(struct ctx_channel) |
183 | * (ctx->num_analog_channels + ctx->num_logic_channels)); | |
02604ed6 | 184 | |
0f3d8c89 | 185 | /* Once more to map the enabled channels. */ |
9e24c8bc MM |
186 | ctx->channel_count = g_slist_length(o->sdi->channels); |
187 | for (i = 0, l = o->sdi->channels; l; l = l->next) { | |
0f3d8c89 | 188 | ch = l->data; |
c04cf9aa | 189 | if (ch->enabled) { |
9e24c8bc MM |
190 | if (ch->type == SR_CHANNEL_ANALOG) { |
191 | ctx->channels[i].min = FLT_MAX; | |
192 | ctx->channels[i].max = FLT_MIN; | |
193 | } else if (ch->type == SR_CHANNEL_LOGIC) { | |
194 | ctx->channels[i].min = 0; | |
195 | ctx->channels[i].max = 1; | |
196 | } else { | |
197 | sr_warn("Unknown channel type %d.", ch->type); | |
198 | } | |
cad447d2 MM |
199 | if (ctx->label_do && ctx->label_names) |
200 | ctx->channels[i].label = ch->name; | |
9e24c8bc | 201 | ctx->channels[i++].ch = ch; |
c04cf9aa | 202 | } |
0f3d8c89 | 203 | } |
02604ed6 | 204 | |
0f3d8c89 BV |
205 | return SR_OK; |
206 | } | |
02604ed6 | 207 | |
9e24c8bc MM |
208 | static const char *xlabels[] = { |
209 | "samples", "milliseconds", "microseconds", "nanoseconds", "picoseconds", | |
210 | "femtoseconds", "attoseconds", | |
211 | }; | |
212 | ||
213 | static GString *gen_header(const struct sr_output *o, | |
214 | const struct sr_datafeed_header *hdr) | |
0f3d8c89 BV |
215 | { |
216 | struct context *ctx; | |
217 | struct sr_channel *ch; | |
218 | GVariant *gvar; | |
219 | GString *header; | |
220 | GSList *l; | |
9e24c8bc MM |
221 | unsigned int num_channels, i; |
222 | uint64_t samplerate = 0, sr; | |
0f3d8c89 | 223 | char *samplerate_s; |
02604ed6 | 224 | |
d686c5ec | 225 | ctx = o->priv; |
0f3d8c89 | 226 | header = g_string_sized_new(512); |
02604ed6 | 227 | |
9e24c8bc MM |
228 | if (ctx->period == 0) { |
229 | if (sr_config_get(o->sdi->driver, o->sdi, NULL, | |
230 | SR_CONF_SAMPLERATE, &gvar) == SR_OK) { | |
231 | samplerate = g_variant_get_uint64(gvar); | |
0f3d8c89 BV |
232 | g_variant_unref(gvar); |
233 | } | |
9e24c8bc MM |
234 | |
235 | i = 0; | |
236 | sr = 1; | |
237 | while (sr < samplerate) { | |
238 | i++; | |
239 | sr *= 1000; | |
240 | } | |
241 | if (samplerate) | |
242 | ctx->period = sr / samplerate; | |
243 | if (i < ARRAY_SIZE(xlabels)) | |
244 | ctx->xlabel = xlabels[i]; | |
245 | sr_info("Set sample period to %" PRIu64 " %s", | |
246 | ctx->period, ctx->xlabel); | |
0f3d8c89 | 247 | } |
cf1d5f17 | 248 | ctx->title = (o->sdi && o->sdi->driver) ? o->sdi->driver->longname : "unknown"; |
9e24c8bc MM |
249 | |
250 | /* Some metadata */ | |
251 | if (ctx->header && !ctx->did_header) { | |
252 | /* save_gnuplot knows how many lines we print. */ | |
253 | g_string_append_printf(header, | |
254 | "%s CSV generated by %s %s\n%s from %s on %s", | |
255 | ctx->comment, PACKAGE_NAME, | |
256 | SR_PACKAGE_VERSION_STRING, ctx->comment, | |
257 | ctx->title, ctime(&hdr->starttime.tv_sec)); | |
258 | ||
259 | /* Columns / channels */ | |
260 | num_channels = g_slist_length(o->sdi->channels); | |
261 | g_string_append_printf(header, "%s Channels (%d/%d):", | |
262 | ctx->comment, ctx->num_analog_channels + | |
263 | ctx->num_logic_channels, num_channels); | |
264 | for (i = 0, l = o->sdi->channels; l; l = l->next, i++) { | |
265 | ch = l->data; | |
266 | if (ch->enabled) | |
267 | g_string_append_printf(header, " %s,", ch->name); | |
268 | } | |
269 | if (o->sdi->channels) | |
270 | /* Drop last separator. */ | |
271 | g_string_truncate(header, header->len - 1); | |
272 | g_string_append_printf(header, "\n"); | |
273 | if (samplerate != 0) { | |
274 | samplerate_s = sr_samplerate_string(samplerate); | |
275 | g_string_append_printf(header, "%s Samplerate: %s\n", | |
276 | ctx->comment, samplerate_s); | |
277 | g_free(samplerate_s); | |
278 | } | |
279 | ctx->did_header = TRUE; | |
0f3d8c89 | 280 | } |
02604ed6 | 281 | |
0f3d8c89 | 282 | return header; |
02604ed6 UH |
283 | } |
284 | ||
9e24c8bc MM |
285 | /* |
286 | * Analog devices can have samples of different types. Since each | |
287 | * packet has only one meaning, it is restricted to having at most one | |
288 | * type of data. So they can send multiple packets for a single sample. | |
289 | * To further complicate things, they can send multiple samples in a | |
290 | * single packet. | |
291 | * | |
292 | * So we need to pull any channels of interest out of a packet and save | |
293 | * them until we have complete samples to output. Some devices make this | |
294 | * simple by sending DF_FRAME_BEGIN/DF_FRAME_END packets, the latter of which | |
295 | * signals the end of a set of samples, so we can dump things there. | |
296 | * | |
297 | * At least one driver (the demo driver) sends packets that contain parts of | |
298 | * multiple samples without wrapping them in DF_FRAME. Possibly this driver | |
299 | * is buggy, but it's also the standard for testing, so it has to be supported | |
300 | * as is. | |
301 | * | |
302 | * Many assumptions about the "shape" of the data here: | |
303 | * | |
304 | * All of the data for a channel is assumed to be in one frame; | |
305 | * otherwise the data in the second packet will overwrite the data in | |
306 | * the first packet. | |
307 | */ | |
308 | static void process_analog(struct context *ctx, | |
309 | const struct sr_datafeed_analog *analog) | |
310 | { | |
311 | int ret; | |
312 | unsigned int i, j, c, num_channels; | |
313 | struct sr_analog_meaning *meaning; | |
314 | GSList *l; | |
315 | float *fdata = NULL; | |
316 | ||
317 | if (!ctx->analog_samples) { | |
318 | ctx->analog_samples = g_malloc(analog->num_samples | |
319 | * sizeof(float) * ctx->num_analog_channels); | |
320 | if (!ctx->num_samples) | |
321 | ctx->num_samples = analog->num_samples; | |
322 | } | |
323 | if (ctx->num_samples != analog->num_samples) | |
324 | sr_warn("Expecting %u analog samples, got %u.", | |
325 | ctx->num_samples, analog->num_samples); | |
326 | ||
327 | meaning = analog->meaning; | |
328 | num_channels = g_slist_length(meaning->channels); | |
329 | ctx->channels_seen += num_channels; | |
330 | sr_dbg("Processing packet of %u analog channels", num_channels); | |
e34dbc9d | 331 | fdata = g_malloc(analog->num_samples * num_channels * sizeof(float)); |
9e24c8bc MM |
332 | if ((ret = sr_analog_to_float(analog, fdata)) != SR_OK) |
333 | sr_warn("Problems converting data to floating point values."); | |
334 | ||
335 | for (i = 0; i < ctx->num_analog_channels + ctx->num_logic_channels; i++) { | |
336 | if (ctx->channels[i].ch->type == SR_CHANNEL_ANALOG) { | |
337 | sr_dbg("Looking for channel %s", | |
338 | ctx->channels[i].ch->name); | |
339 | for (l = meaning->channels, c = 0; l; l = l->next, c++) { | |
340 | struct sr_channel *ch = l->data; | |
341 | sr_dbg("Checking %s", ch->name); | |
342 | if (ctx->channels[i].ch == l->data) { | |
cad447d2 MM |
343 | if (ctx->label_do && !ctx->label_names) { |
344 | sr_analog_unit_to_string(analog, | |
345 | &ctx->channels[i].label); | |
346 | } | |
9e24c8bc MM |
347 | for (j = 0; j < analog->num_samples; j++) |
348 | ctx->analog_samples[j * ctx->num_analog_channels + i] = fdata[j * num_channels + c]; | |
349 | break; | |
350 | } | |
351 | } | |
352 | } | |
353 | } | |
354 | g_free(fdata); | |
355 | } | |
356 | ||
357 | /* | |
358 | * We treat logic packets the same as analog packets, though it's not | |
359 | * strictly required. This allows us to process mixed signals properly. | |
360 | */ | |
361 | static void process_logic(struct context *ctx, | |
362 | const struct sr_datafeed_logic *logic) | |
c04cf9aa | 363 | { |
9e24c8bc MM |
364 | unsigned int i, j, ch, num_samples; |
365 | int idx; | |
366 | uint8_t *sample; | |
367 | ||
368 | num_samples = logic->length / logic->unitsize; | |
369 | ctx->channels_seen += ctx->logic_channel_count; | |
370 | sr_dbg("Logic packet had %d channels", logic->unitsize * 8); | |
371 | if (!ctx->logic_samples) { | |
372 | ctx->logic_samples = g_malloc(num_samples * ctx->num_logic_channels); | |
373 | if (!ctx->num_samples) | |
374 | ctx->num_samples = num_samples; | |
375 | } | |
376 | if (ctx->num_samples != num_samples) | |
377 | sr_warn("Expecting %u samples, got %u", | |
378 | ctx->num_samples, num_samples); | |
379 | ||
380 | for (j = ch = 0; ch < ctx->num_logic_channels; j++) { | |
381 | if (ctx->channels[j].ch->type == SR_CHANNEL_LOGIC) { | |
382 | for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) { | |
383 | sample = logic->data + i; | |
384 | idx = ctx->channels[ch].ch->index; | |
cad447d2 MM |
385 | if (ctx->label_do && !ctx->label_names) |
386 | ctx->channels[i].label = "logic"; | |
9e24c8bc MM |
387 | ctx->logic_samples[i * ctx->num_logic_channels + ch] = sample[idx / 8] & (1 << (idx % 8)); |
388 | } | |
389 | ch++; | |
390 | } | |
391 | } | |
392 | } | |
393 | ||
394 | static void dump_saved_values(struct context *ctx, GString **out) | |
395 | { | |
396 | unsigned int i, j, analog_size, num_channels; | |
397 | float *analog_sample, value; | |
398 | uint8_t *logic_sample; | |
399 | ||
400 | /* If we haven't seen samples we're expecting, skip them. */ | |
401 | if ((ctx->num_analog_channels && !ctx->analog_samples) || | |
402 | (ctx->num_logic_channels && !ctx->logic_samples)) { | |
403 | sr_warn("Discarding partial packet"); | |
c04cf9aa | 404 | } else { |
9e24c8bc MM |
405 | sr_info("Dumping %u samples", ctx->num_samples); |
406 | ||
c04cf9aa | 407 | *out = g_string_sized_new(512); |
9e24c8bc MM |
408 | num_channels = |
409 | ctx->num_logic_channels + ctx->num_analog_channels; | |
410 | ||
cad447d2 | 411 | if (ctx->label_do) { |
9e24c8bc | 412 | if (ctx->time) |
cad447d2 MM |
413 | g_string_append_printf(*out, "%s%s", |
414 | ctx->label_names ? "Time" : | |
415 | ctx->xlabel, ctx->value); | |
9e24c8bc MM |
416 | for (i = 0; i < num_channels; i++) { |
417 | g_string_append_printf(*out, "%s%s", | |
cad447d2 MM |
418 | ctx->channels[i].label, ctx->value); |
419 | if (ctx->channels[i].ch->type == SR_CHANNEL_ANALOG | |
420 | && ctx->label_names) | |
421 | g_free(ctx->channels[i].label); | |
9e24c8bc MM |
422 | } |
423 | if (ctx->do_trigger) | |
424 | g_string_append_printf(*out, "Trigger%s", | |
425 | ctx->value); | |
426 | /* Drop last separator. */ | |
427 | g_string_truncate(*out, (*out)->len - 1); | |
428 | g_string_append(*out, ctx->record); | |
429 | ||
cad447d2 | 430 | ctx->label_do = FALSE; |
9e24c8bc MM |
431 | } |
432 | ||
433 | analog_size = ctx->num_analog_channels * sizeof(float); | |
434 | if (ctx->dedup && !ctx->previous_sample) | |
435 | ctx->previous_sample = g_malloc0(analog_size + ctx->num_logic_channels); | |
436 | ||
437 | for (i = 0; i < ctx->num_samples; i++) { | |
438 | ctx->sample_time += ctx->period; | |
439 | analog_sample = | |
440 | &ctx->analog_samples[i * ctx->num_analog_channels]; | |
441 | logic_sample = | |
442 | &ctx->logic_samples[i * ctx->num_logic_channels]; | |
443 | ||
444 | if (ctx->dedup) { | |
445 | if (i > 0 && i < ctx->num_samples - 1 && | |
446 | !memcmp(logic_sample, ctx->previous_sample, | |
447 | ctx->num_logic_channels) && | |
448 | !memcmp(analog_sample, | |
449 | ctx->previous_sample + | |
450 | ctx->num_logic_channels, | |
451 | analog_size)) | |
452 | continue; | |
453 | memcpy(ctx->previous_sample, logic_sample, | |
454 | ctx->num_logic_channels); | |
455 | memcpy(ctx->previous_sample | |
456 | + ctx->num_logic_channels, | |
457 | analog_sample, analog_size); | |
458 | } | |
459 | ||
460 | if (ctx->time) | |
461 | g_string_append_printf(*out, "%lu%s", | |
462 | ctx->sample_time, ctx->value); | |
463 | ||
464 | for (j = 0; j < num_channels; j++) { | |
465 | if (ctx->channels[j].ch->type == SR_CHANNEL_ANALOG) { | |
466 | value = ctx->analog_samples[i * ctx->num_analog_channels + j]; | |
467 | ctx->channels[j].max = | |
468 | fmax(value, ctx->channels[j].max); | |
469 | ctx->channels[j].min = | |
470 | fmin(value, ctx->channels[j].min); | |
471 | g_string_append_printf(*out, "%g%s", | |
472 | value, ctx->value); | |
473 | } else if (ctx->channels[j].ch->type == SR_CHANNEL_LOGIC) { | |
474 | g_string_append_printf(*out, "%c%s", | |
475 | ctx->logic_samples[i * ctx->num_logic_channels + j] ? '1' : '0', ctx->value); | |
476 | } else { | |
477 | sr_warn("Unexpected channel type: %d", | |
478 | ctx->channels[i].ch->type); | |
479 | } | |
480 | } | |
481 | ||
482 | if (ctx->do_trigger) { | |
483 | g_string_append_printf(*out, "%d%s", | |
484 | ctx->trigger, ctx->value); | |
485 | ctx->trigger = FALSE; | |
486 | } | |
487 | g_string_truncate(*out, (*out)->len - 1); | |
488 | g_string_append(*out, ctx->record); | |
489 | } | |
c04cf9aa | 490 | } |
9e24c8bc MM |
491 | |
492 | /* Discard all of the working space. */ | |
493 | g_free(ctx->previous_sample); | |
494 | g_free(ctx->analog_samples); | |
495 | g_free(ctx->logic_samples); | |
496 | ctx->channels_seen = 0; | |
497 | ctx->num_samples = 0; | |
498 | ctx->previous_sample = NULL; | |
499 | ctx->analog_samples = NULL; | |
500 | ctx->logic_samples = NULL; | |
c04cf9aa BG |
501 | } |
502 | ||
9e24c8bc | 503 | static void save_gnuplot(struct context *ctx) |
c04cf9aa | 504 | { |
9e24c8bc MM |
505 | float offset, max, sum; |
506 | unsigned int i, num_channels; | |
507 | GString *script; | |
508 | ||
509 | script = g_string_sized_new(512); | |
510 | g_string_append_printf(script, "set datafile separator '%s'\n", | |
511 | ctx->value); | |
cad447d2 | 512 | if (ctx->label_did) |
9e24c8bc MM |
513 | g_string_append(script, "set key autotitle columnhead\n"); |
514 | if (ctx->xlabel && ctx->time) | |
515 | g_string_append_printf(script, "set xlabel '%s'\n", | |
516 | ctx->xlabel); | |
517 | ||
518 | g_string_append(script, "plot "); | |
519 | ||
520 | num_channels = ctx->num_analog_channels + ctx->num_logic_channels; | |
521 | ||
522 | /* Graph position and scaling. */ | |
523 | max = FLT_MIN; | |
524 | sum = 0; | |
525 | for (i = 0; i < num_channels; i++) { | |
526 | ctx->channels[i].max = | |
527 | ctx->channels[i].max - ctx->channels[i].min; | |
528 | max = fmax(max, ctx->channels[i].max); | |
529 | sum += ctx->channels[i].max; | |
530 | } | |
531 | sum = (ctx->scale ? max : sum / num_channels) / 4; | |
532 | offset = sum; | |
533 | for (i = num_channels; i > 0;) { | |
534 | i--; | |
535 | ctx->channels[i].min = offset - ctx->channels[i].min; | |
536 | offset += sum + (ctx->scale ? max : ctx->channels[i].max); | |
537 | } | |
c04cf9aa | 538 | |
9e24c8bc MM |
539 | for (i = 0; i < num_channels; i++) { |
540 | sr_spew("Channel %d, min %g, max %g", i, ctx->channels[i].min, | |
541 | ctx->channels[i].max); | |
542 | g_string_append(script, "ARG1 "); | |
543 | if (ctx->did_header) | |
544 | g_string_append(script, "skip 4 "); | |
545 | g_string_append_printf(script, "using %u:($%u * %g + %g), ", | |
546 | ctx->time, i + 1 + ctx->time, ctx->scale ? | |
547 | max / ctx->channels[i].max : 1, ctx->channels[i].min); | |
548 | offset += 1.1 * (ctx->channels[i].max - ctx->channels[i].min); | |
c04cf9aa | 549 | } |
9e24c8bc MM |
550 | g_string_truncate(script, script->len - 2); |
551 | g_file_set_contents(ctx->gnuplot, script->str, script->len, NULL); | |
552 | g_string_free(script, TRUE); | |
c04cf9aa BG |
553 | } |
554 | ||
9e24c8bc MM |
555 | static int receive(const struct sr_output *o, |
556 | const struct sr_datafeed_packet *packet, GString **out) | |
02604ed6 UH |
557 | { |
558 | struct context *ctx; | |
02604ed6 | 559 | |
4829d37d BV |
560 | *out = NULL; |
561 | if (!o || !o->sdi) | |
02604ed6 | 562 | return SR_ERR_ARG; |
d686c5ec | 563 | if (!(ctx = o->priv)) |
02604ed6 | 564 | return SR_ERR_ARG; |
02604ed6 | 565 | |
9e24c8bc | 566 | sr_dbg("Got packet of type %d", packet->type); |
4829d37d | 567 | switch (packet->type) { |
9e24c8bc MM |
568 | case SR_DF_HEADER: |
569 | *out = gen_header(o, packet->payload); | |
0f3d8c89 | 570 | break; |
9e24c8bc MM |
571 | case SR_DF_TRIGGER: |
572 | ctx->trigger = TRUE; | |
c04cf9aa | 573 | break; |
4829d37d | 574 | case SR_DF_LOGIC: |
9e24c8bc | 575 | process_logic(ctx, packet->payload); |
02604ed6 | 576 | break; |
edb691fc | 577 | case SR_DF_ANALOG: |
9e24c8bc MM |
578 | process_analog(ctx, packet->payload); |
579 | break; | |
580 | case SR_DF_FRAME_BEGIN: | |
581 | *out = g_string_new(ctx->frame); | |
582 | /* And then fall through to... */ | |
583 | case SR_DF_END: | |
584 | /* Got to end of frame/session with part of the data. */ | |
585 | if (ctx->channels_seen) | |
586 | ctx->channels_seen = ctx->channel_count; | |
587 | if (*ctx->gnuplot) | |
588 | save_gnuplot(ctx); | |
c04cf9aa | 589 | break; |
02604ed6 UH |
590 | } |
591 | ||
9e24c8bc MM |
592 | /* If we've got them all, dump the values. */ |
593 | if (ctx->channels_seen >= ctx->channel_count) | |
594 | dump_saved_values(ctx, out); | |
595 | ||
596 | return SR_OK; | |
02604ed6 UH |
597 | } |
598 | ||
4829d37d | 599 | static int cleanup(struct sr_output *o) |
02604ed6 UH |
600 | { |
601 | struct context *ctx; | |
02604ed6 | 602 | |
4829d37d | 603 | if (!o || !o->sdi) |
02604ed6 | 604 | return SR_ERR_ARG; |
02604ed6 | 605 | |
d686c5ec BV |
606 | if (o->priv) { |
607 | ctx = o->priv; | |
9e24c8bc MM |
608 | g_free((gpointer)ctx->record); |
609 | g_free((gpointer)ctx->frame); | |
610 | g_free((gpointer)ctx->comment); | |
611 | g_free((gpointer)ctx->gnuplot); | |
612 | g_free(ctx->previous_sample); | |
c04cf9aa | 613 | g_free(ctx->channels); |
d686c5ec BV |
614 | g_free(o->priv); |
615 | o->priv = NULL; | |
02604ed6 UH |
616 | } |
617 | ||
02604ed6 UH |
618 | return SR_OK; |
619 | } | |
620 | ||
9e24c8bc MM |
621 | static struct sr_option options[] = { |
622 | {"gnuplot", "gnuplot", "gnuplot script file name", NULL, NULL}, | |
623 | {"scale", "scale", "Scale gnuplot graphs", NULL, NULL}, | |
624 | {"value", "Value separator", "Character to print between values", NULL, NULL}, | |
625 | {"record", "Record separator", "String to print between records", NULL, NULL}, | |
626 | {"frame", "Frame seperator", "String to print between frames", NULL, NULL}, | |
627 | {"comment", "Comment start string", "String used at start of comment lines", NULL, NULL}, | |
628 | {"header", "Output header", "Output header comment with capture metdata", NULL, NULL}, | |
cad447d2 | 629 | {"label", "Label values", "Type of column labels", NULL, NULL}, |
9e24c8bc MM |
630 | {"time", "Time column", "Output sample time as column 1", NULL, NULL}, |
631 | {"trigger", "Trigger column", "Output trigger indicator as last column ", NULL, NULL}, | |
632 | {"dedup", "Dedup rows", "Set to false to output duplicate rows", NULL, NULL}, | |
633 | ALL_ZERO | |
634 | }; | |
635 | ||
636 | static const struct sr_option *get_options(void) | |
637 | { | |
638 | if (!options[0].def) { | |
639 | options[0].def = g_variant_ref_sink(g_variant_new_string("")); | |
640 | options[1].def = g_variant_ref_sink(g_variant_new_boolean(TRUE)); | |
641 | options[2].def = g_variant_ref_sink(g_variant_new_string(",")); | |
642 | options[3].def = g_variant_ref_sink(g_variant_new_string("\n")); | |
643 | options[4].def = g_variant_ref_sink(g_variant_new_string("\n")); | |
644 | options[5].def = g_variant_ref_sink(g_variant_new_string(";")); | |
645 | options[6].def = g_variant_ref_sink(g_variant_new_boolean(TRUE)); | |
cad447d2 | 646 | options[7].def = g_variant_ref_sink(g_variant_new_string("units")); |
9e24c8bc MM |
647 | options[8].def = g_variant_ref_sink(g_variant_new_boolean(TRUE)); |
648 | options[9].def = g_variant_ref_sink(g_variant_new_boolean(FALSE)); | |
649 | options[10].def = g_variant_ref_sink(g_variant_new_boolean(TRUE)); | |
650 | } | |
651 | ||
652 | return options; | |
653 | } | |
654 | ||
a755b0e1 | 655 | SR_PRIV struct sr_output_module output_csv = { |
02604ed6 | 656 | .id = "csv", |
a755b0e1 BV |
657 | .name = "CSV", |
658 | .desc = "Comma-separated values", | |
9e24c8bc | 659 | .exts = (const char *[]){"csv", NULL}, |
3cd4b381 | 660 | .flags = 0, |
9e24c8bc | 661 | .options = get_options, |
02604ed6 | 662 | .init = init, |
4829d37d BV |
663 | .receive = receive, |
664 | .cleanup = cleanup, | |
02604ed6 | 665 | }; |