]> sigrok.org Git - libsigrok.git/blame_incremental - output/csv.c
Replace 'probe' with 'channel' in most places.
[libsigrok.git] / output / csv.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
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
21#include <stdlib.h>
22#include <string.h>
23#include <glib.h>
24#include "config.h" /* Needed for PACKAGE_STRING and others. */
25#include "libsigrok.h"
26#include "libsigrok-internal.h"
27
28#define LOG_PREFIX "output/csv"
29
30struct context {
31 unsigned int num_enabled_channels;
32 unsigned int unitsize;
33 uint64_t samplerate;
34 GString *header;
35 char separator;
36};
37
38/*
39 * TODO:
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.
47 * - Trigger support.
48 */
49
50static int init(struct sr_output *o)
51{
52 struct context *ctx;
53 struct sr_channel *ch;
54 GSList *l;
55 GVariant *gvar;
56 int num_channels;
57 time_t t;
58
59 if (!o)
60 return SR_ERR_ARG;
61
62 if (!o->sdi)
63 return SR_ERR_ARG;
64
65 ctx = g_try_malloc0(sizeof(struct context));
66 o->internal = ctx;
67
68 /* Get the number of channels, and the unitsize. */
69 for (l = o->sdi->channels; l; l = l->next) {
70 ch = l->data;
71 if (ch->type != SR_PROBE_LOGIC)
72 continue;
73 if (!ch->enabled)
74 continue;
75 ctx->num_enabled_channels++;
76 }
77
78 ctx->unitsize = (ctx->num_enabled_channels + 7) / 8;
79
80 num_channels = g_slist_length(o->sdi->channels);
81
82 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
83 &gvar) == SR_OK) {
84 ctx->samplerate = g_variant_get_uint64(gvar);
85 g_variant_unref(gvar);
86 } else
87 ctx->samplerate = 0;
88
89 ctx->separator = ',';
90 ctx->header = g_string_sized_new(512);
91
92 t = time(NULL);
93
94 /* Some metadata */
95 g_string_append_printf(ctx->header, "; CSV, generated by %s on %s",
96 PACKAGE_STRING, ctime(&t));
97 g_string_append_printf(ctx->header, "; Samplerate: %"PRIu64"\n",
98 ctx->samplerate);
99
100 /* Columns / channels */
101 g_string_append_printf(ctx->header, "; Channels (%d/%d):",
102 ctx->num_enabled_channels, num_channels);
103 for (l = o->sdi->channels; l; l = l->next) {
104 ch = l->data;
105 if (ch->type != SR_PROBE_LOGIC)
106 continue;
107 if (!ch->enabled)
108 continue;
109 g_string_append_printf(ctx->header, " %s,", ch->name);
110 }
111 if (o->sdi->channels)
112 /* Drop last separator. */
113 g_string_truncate(ctx->header, ctx->header->len - 1);
114 g_string_append_printf(ctx->header, "\n");
115
116 return SR_OK;
117}
118
119static int receive(struct sr_output *o, const struct sr_dev_inst *sdi,
120 const struct sr_datafeed_packet *packet, GString **out)
121{
122 const struct sr_datafeed_logic *logic;
123 struct context *ctx;
124 uint64_t i, j;
125 gchar *p, c;
126
127 (void)sdi;
128
129 *out = NULL;
130 if (!o || !o->sdi)
131 return SR_ERR_ARG;
132 if (!(ctx = o->internal))
133 return SR_ERR_ARG;
134
135 switch (packet->type) {
136 case SR_DF_LOGIC:
137 logic = packet->payload;
138 if (ctx->header) {
139 /*
140 * First data packet: prime the output with the
141 * previously prepared header.
142 */
143 *out = ctx->header;
144 ctx->header = NULL;
145 } else {
146 *out = g_string_sized_new(512);
147 }
148
149 for (i = 0; i <= logic->length - ctx->unitsize; i += ctx->unitsize) {
150 for (j = 0; j < ctx->num_enabled_channels; j++) {
151 p = logic->data + i + j / 8;
152 c = *p & (1 << (j % 8));
153 g_string_append_c(*out, c ? '1' : '0');
154 g_string_append_c(*out, ctx->separator);
155 }
156 if (j) {
157 /* Drop last separator. */
158 g_string_truncate(*out, (*out)->len - 1);
159 }
160 g_string_append_printf(*out, "\n");
161 }
162 break;
163 case SR_DF_END:
164 g_free(o->internal);
165 o->internal = NULL;
166 break;
167 }
168
169 return SR_OK;
170}
171
172static int cleanup(struct sr_output *o)
173{
174 struct context *ctx;
175
176 if (!o || !o->sdi)
177 return SR_ERR_ARG;
178
179 if (o->internal) {
180 ctx = o->internal;
181 if (ctx->header)
182 g_string_free(ctx->header, TRUE);
183 g_free(o->internal);
184 o->internal = NULL;
185 }
186
187 return SR_OK;
188}
189
190SR_PRIV struct sr_output_format output_csv = {
191 .id = "csv",
192 .description = "Comma-separated values (CSV)",
193 .df_type = SR_DF_LOGIC,
194 .init = init,
195 .receive = receive,
196 .cleanup = cleanup,
197};