]>
Commit | Line | Data |
---|---|---|
e2ad47b5 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
e2ad47b5 UH |
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> | |
545f9786 | 24 | #include "config.h" /* Needed for PACKAGE_STRING and others. */ |
45c59c8b BV |
25 | #include "libsigrok.h" |
26 | #include "libsigrok-internal.h" | |
e2ad47b5 | 27 | |
3544f848 | 28 | #define LOG_PREFIX "output/gnuplot" |
a944a84b | 29 | |
e2ad47b5 | 30 | struct context { |
ba7dd8bb | 31 | unsigned int num_enabled_channels; |
83023573 | 32 | uint64_t samplerate; |
b240ee08 | 33 | uint64_t samplecount; |
83023573 | 34 | gboolean header_done; |
b240ee08 BV |
35 | uint8_t *prevsample; |
36 | int *channel_index; | |
e2ad47b5 UH |
37 | }; |
38 | ||
d078d2e5 | 39 | static const char *gnuplot_header = "\ |
83023573 BV |
40 | # Sample data in space-separated columns format usable by gnuplot.\n"; |
41 | static const char *gnuplot_header2 = "\ | |
42 | #\n# Column\tChannel\n\ | |
43 | # -----------------------------------------------------------------------------\n\ | |
44 | # 0\t\tSample counter (for internal gnuplot purposes)\n"; | |
45 | ||
7aae7462 | 46 | |
a755b0e1 | 47 | static int init(struct sr_output *o, GHashTable *options) |
e2ad47b5 | 48 | { |
e2ad47b5 | 49 | struct context *ctx; |
ba7dd8bb | 50 | struct sr_channel *ch; |
e2ad47b5 | 51 | GSList *l; |
83023573 | 52 | unsigned int i; |
20ebd1fe | 53 | |
a755b0e1 BV |
54 | (void)options; |
55 | ||
83023573 | 56 | if (!o || !o->sdi) |
20ebd1fe | 57 | return SR_ERR_ARG; |
20ebd1fe | 58 | |
83023573 | 59 | ctx = g_malloc0(sizeof(struct context)); |
d686c5ec | 60 | o->priv = ctx; |
ba7dd8bb UH |
61 | ctx->num_enabled_channels = 0; |
62 | for (l = o->sdi->channels; l; l = l->next) { | |
63 | ch = l->data; | |
3f239f08 | 64 | if (ch->type != SR_CHANNEL_LOGIC) |
3699a8a1 | 65 | continue; |
ba7dd8bb | 66 | if (!ch->enabled) |
3699a8a1 | 67 | continue; |
ba7dd8bb | 68 | ctx->num_enabled_channels++; |
e2ad47b5 | 69 | } |
ba7dd8bb | 70 | if (ctx->num_enabled_channels <= 0) { |
b240ee08 | 71 | sr_err("No logic channel enabled."); |
365cca8a AJ |
72 | return SR_ERR; |
73 | } | |
b240ee08 | 74 | ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels); |
e2ad47b5 | 75 | |
83023573 BV |
76 | /* Once more to map the enabled channels. */ |
77 | for (i = 0, l = o->sdi->channels; l; l = l->next) { | |
ba7dd8bb | 78 | ch = l->data; |
3f239f08 | 79 | if (ch->type != SR_CHANNEL_LOGIC) |
3699a8a1 | 80 | continue; |
ba7dd8bb | 81 | if (!ch->enabled) |
deb09083 | 82 | continue; |
83023573 | 83 | ctx->channel_index[i++] = ch->index; |
e2ad47b5 UH |
84 | } |
85 | ||
83023573 BV |
86 | return SR_OK; |
87 | } | |
88 | ||
a755b0e1 | 89 | static GString *gen_header(const struct sr_output *o) |
83023573 BV |
90 | { |
91 | struct context *ctx; | |
92 | struct sr_channel *ch; | |
93 | GVariant *gvar; | |
94 | GString *header; | |
95 | time_t t; | |
96 | unsigned int num_channels, i; | |
97 | char *samplerate_s; | |
98 | ||
d686c5ec | 99 | ctx = o->priv; |
83023573 BV |
100 | if (ctx->samplerate == 0) { |
101 | if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE, | |
102 | &gvar) == SR_OK) { | |
103 | ctx->samplerate = g_variant_get_uint64(gvar); | |
104 | g_variant_unref(gvar); | |
105 | } | |
d4ae8eaa | 106 | } |
20ebd1fe | 107 | |
5cca9adb | 108 | t = time(NULL); |
83023573 BV |
109 | header = g_string_sized_new(512); |
110 | g_string_printf(header, "%s", gnuplot_header); | |
111 | g_string_append_printf(header, "# Generated by %s on %s", | |
112 | PACKAGE_STRING, ctime(&t)); | |
113 | ||
114 | num_channels = g_slist_length(o->sdi->channels); | |
115 | g_string_append_printf(header, "# Acquisition with %d/%d channels", | |
116 | ctx->num_enabled_channels, num_channels); | |
117 | if (ctx->samplerate != 0) { | |
118 | samplerate_s = sr_samplerate_string(ctx->samplerate); | |
119 | g_string_append_printf(header, " at %s", samplerate_s); | |
120 | g_free(samplerate_s); | |
121 | } | |
122 | g_string_append_printf(header, "\n"); | |
e2ad47b5 | 123 | |
83023573 BV |
124 | g_string_append_printf(header, "%s", gnuplot_header2); |
125 | ||
126 | /* Columns / channels */ | |
127 | for (i = 0; i < ctx->num_enabled_channels; i++) { | |
128 | ch = g_slist_nth_data(o->sdi->channels, ctx->channel_index[i]); | |
129 | g_string_append_printf(header, "# %d\t\t%s\n", i + 1, ch->name); | |
130 | } | |
131 | ||
132 | return header; | |
e2ad47b5 UH |
133 | } |
134 | ||
a755b0e1 | 135 | static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet, |
dba3e682 | 136 | GString **out) |
e2ad47b5 | 137 | { |
83023573 | 138 | const struct sr_datafeed_meta *meta; |
b240ee08 | 139 | const struct sr_datafeed_logic *logic; |
83023573 BV |
140 | const struct sr_config *src; |
141 | GSList *l; | |
e2ad47b5 | 142 | struct context *ctx; |
9c178ffa | 143 | const uint8_t *sample; |
b240ee08 | 144 | unsigned int curbit, p, idx, i; |
e2ad47b5 | 145 | |
b240ee08 | 146 | *out = NULL; |
d686c5ec | 147 | if (!o || !o->priv) |
b240ee08 | 148 | return SR_ERR_BUG; |
d686c5ec | 149 | ctx = o->priv; |
a821069b | 150 | |
83023573 BV |
151 | if (packet->type == SR_DF_META) { |
152 | meta = packet->payload; | |
153 | for (l = meta->config; l; l = l->next) { | |
154 | src = l->data; | |
155 | if (src->key != SR_CONF_SAMPLERATE) | |
156 | continue; | |
157 | ctx->samplerate = g_variant_get_uint64(src->data); | |
158 | } | |
159 | } | |
160 | ||
b240ee08 BV |
161 | if (packet->type != SR_DF_LOGIC) |
162 | return SR_OK; | |
163 | logic = packet->payload; | |
164 | ||
165 | if (!ctx->prevsample) { | |
166 | /* Can't allocate this until we know the stream's unitsize. */ | |
83023573 | 167 | ctx->prevsample = g_malloc0(logic->unitsize); |
20ebd1fe | 168 | } |
a821069b | 169 | |
83023573 BV |
170 | if (!ctx->header_done) { |
171 | *out = gen_header(o); | |
172 | ctx->header_done = TRUE; | |
b240ee08 BV |
173 | } else { |
174 | *out = g_string_sized_new(512); | |
e2ad47b5 UH |
175 | } |
176 | ||
b240ee08 BV |
177 | for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) { |
178 | sample = logic->data + i; | |
179 | ctx->samplecount++; | |
e2ad47b5 | 180 | |
50959ddc | 181 | /* |
b240ee08 | 182 | * Don't output the same sample multiple times, but make |
50959ddc UH |
183 | * sure to output at least the first and last sample. |
184 | */ | |
b240ee08 BV |
185 | if (i > 0 && i < logic->length - logic->unitsize) { |
186 | if (!memcmp(sample, ctx->prevsample, logic->unitsize)) | |
50959ddc UH |
187 | continue; |
188 | } | |
b240ee08 | 189 | memcpy(ctx->prevsample, sample, logic->unitsize); |
50959ddc | 190 | |
e2ad47b5 | 191 | /* The first column is a counter (needed for gnuplot). */ |
b240ee08 | 192 | g_string_append_printf(*out, "%" PRIu64 "\t", ctx->samplecount); |
e2ad47b5 UH |
193 | |
194 | /* The next columns are the values of all channels. */ | |
ba7dd8bb | 195 | for (p = 0; p < ctx->num_enabled_channels; p++) { |
b240ee08 BV |
196 | idx = ctx->channel_index[p]; |
197 | curbit = (sample[idx / 8] & ((uint8_t) (1 << (idx % 8)))) >> (idx % 8); | |
198 | g_string_append_printf(*out, "%d ", curbit); | |
e2ad47b5 | 199 | } |
b240ee08 | 200 | g_string_append_printf(*out, "\n"); |
e2ad47b5 UH |
201 | } |
202 | ||
b240ee08 BV |
203 | return SR_OK; |
204 | } | |
205 | ||
206 | static int cleanup(struct sr_output *o) | |
207 | { | |
208 | struct context *ctx; | |
209 | ||
d686c5ec | 210 | if (!o || !o->priv) |
b240ee08 | 211 | return SR_ERR_BUG; |
d686c5ec | 212 | ctx = o->priv; |
b240ee08 BV |
213 | g_free(ctx->channel_index); |
214 | g_free(ctx->prevsample); | |
b240ee08 | 215 | g_free(ctx); |
e2ad47b5 | 216 | |
e46b8fb1 | 217 | return SR_OK; |
e2ad47b5 UH |
218 | } |
219 | ||
a755b0e1 | 220 | SR_PRIV struct sr_output_module output_gnuplot = { |
cdb3573c | 221 | .id = "gnuplot", |
a755b0e1 | 222 | .name = "Gnuplot", |
8656a717 UH |
223 | .desc = "Gnuplot data file format", |
224 | .exts = (const char*[]){"dat", NULL}, | |
a755b0e1 | 225 | .options = NULL, |
d494a4aa | 226 | .init = init, |
b240ee08 BV |
227 | .receive = receive, |
228 | .cleanup = cleanup, | |
77b45442 | 229 | }; |