2 * This file is part of the libsigrok project.
4 * Copyright (C) 2011 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
24 #include "config.h" /* Needed for PACKAGE_STRING and others. */
25 #include "libsigrok.h"
26 #include "libsigrok-internal.h"
28 #define LOG_PREFIX "output/csv"
31 unsigned int num_enabled_channels;
40 * - Option to specify delimiter character and/or string.
41 * - Option to (not) print metadata as comments.
42 * - Option to specify the comment character(s), e.g. # or ; or C/C++-style.
43 * - Option to (not) print samplenumber / time as extra column.
44 * - Option to "compress" output (only print changed samples, VCD-like).
45 * - Option to print comma-separated bits, or whole bytes/words (for 8/16
46 * channel LAs) as ASCII/hex etc. etc.
50 static int init(struct sr_output *o)
53 struct sr_channel *ch;
60 ctx = g_malloc0(sizeof(struct context));
64 /* Get the number of channels, and the unitsize. */
65 for (l = o->sdi->channels; l; l = l->next) {
67 if (ch->type != SR_CHANNEL_LOGIC)
71 ctx->num_enabled_channels++;
73 ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
75 /* Once more to map the enabled channels. */
76 for (i = 0, l = o->sdi->channels; l; l = l->next) {
78 if (ch->type != SR_CHANNEL_LOGIC)
82 ctx->channel_index[i++] = ch->index;
88 static GString *gen_header(struct sr_output *o)
91 struct sr_channel *ch;
100 header = g_string_sized_new(512);
104 g_string_append_printf(header, "; CSV, generated by %s on %s",
105 PACKAGE_STRING, ctime(&t));
107 /* Columns / channels */
108 num_channels = g_slist_length(o->sdi->channels);
109 g_string_append_printf(header, "; Channels (%d/%d):",
110 ctx->num_enabled_channels, num_channels);
111 for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
113 if (ch->type != SR_CHANNEL_LOGIC)
117 g_string_append_printf(header, " %s,", ch->name);
119 if (o->sdi->channels)
120 /* Drop last separator. */
121 g_string_truncate(header, header->len - 1);
122 g_string_append_printf(header, "\n");
124 if (ctx->samplerate == 0) {
125 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
127 ctx->samplerate = g_variant_get_uint64(gvar);
128 g_variant_unref(gvar);
131 if (ctx->samplerate != 0) {
132 samplerate_s = sr_samplerate_string(ctx->samplerate);
133 g_string_append_printf(header, "; Samplerate: %s\n", samplerate_s);
134 g_free(samplerate_s);
140 static int receive(struct sr_output *o, const struct sr_datafeed_packet *packet,
143 const struct sr_datafeed_meta *meta;
144 const struct sr_datafeed_logic *logic;
145 const struct sr_config *src;
155 if (!(ctx = o->internal))
158 switch (packet->type) {
160 meta = packet->payload;
161 for (l = meta->config; l; l = l->next) {
163 if (src->key != SR_CONF_SAMPLERATE)
165 ctx->samplerate = g_variant_get_uint64(src->data);
169 logic = packet->payload;
170 if (!ctx->header_done) {
171 *out = gen_header(o);
172 ctx->header_done = TRUE;
174 *out = g_string_sized_new(512);
177 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
178 for (j = 0; j < ctx->num_enabled_channels; j++) {
179 idx = ctx->channel_index[j];
180 p = logic->data + i + idx / 8;
181 c = *p & (1 << (idx % 8));
182 g_string_append_c(*out, c ? '1' : '0');
183 g_string_append_c(*out, ctx->separator);
186 /* Drop last separator. */
187 g_string_truncate(*out, (*out)->len - 1);
189 g_string_append_printf(*out, "\n");
197 static int cleanup(struct sr_output *o)
206 g_free(ctx->channel_index);
214 SR_PRIV struct sr_output_format output_csv = {
216 .description = "Comma-separated values (CSV)",