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