]>
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; |
b240ee08 BV |
32 | uint64_t samplecount; |
33 | GString *header; | |
34 | uint8_t *prevsample; | |
35 | int *channel_index; | |
e2ad47b5 UH |
36 | }; |
37 | ||
d078d2e5 | 38 | static const char *gnuplot_header = "\ |
e2ad47b5 UH |
39 | # Sample data in space-separated columns format usable by gnuplot\n\ |
40 | #\n\ | |
5cca9adb | 41 | # Generated by: %s on %s%s\ |
d4ae8eaa | 42 | # Period: %s\n\ |
114fb93f | 43 | #\n\ |
ba7dd8bb | 44 | # Column\tChannel\n\ |
114fb93f UH |
45 | # -------------------------------------\ |
46 | ----------------------------------------\n\ | |
47 | # 0\t\tSample counter (for internal gnuplot purposes)\n%s\n"; | |
e2ad47b5 | 48 | |
d078d2e5 | 49 | static const char *gnuplot_header_comment = "\ |
ba7dd8bb | 50 | # Comment: Acquisition with %d/%d channels at %s\n"; |
7aae7462 | 51 | |
f50f3f40 | 52 | static int init(struct sr_output *o) |
e2ad47b5 | 53 | { |
e2ad47b5 | 54 | struct context *ctx; |
ba7dd8bb | 55 | struct sr_channel *ch; |
e2ad47b5 | 56 | GSList *l; |
ec4063b8 BV |
57 | GVariant *gvar; |
58 | uint64_t samplerate; | |
b240ee08 | 59 | unsigned int i, j; |
ba7dd8bb | 60 | int num_channels; |
d4ae8eaa | 61 | char *c, *frequency_s; |
7aae7462 | 62 | char wbuf[1000], comment[128]; |
5cca9adb | 63 | time_t t; |
e2ad47b5 | 64 | |
b240ee08 | 65 | if (!o) |
20ebd1fe | 66 | return SR_ERR_ARG; |
20ebd1fe | 67 | |
b240ee08 | 68 | if (!o->sdi) |
20ebd1fe | 69 | return SR_ERR_ARG; |
20ebd1fe | 70 | |
b240ee08 | 71 | if (!(ctx = g_malloc0(sizeof(struct context)))) |
e46b8fb1 | 72 | return SR_ERR_MALLOC; |
a821069b | 73 | |
e2ad47b5 | 74 | o->internal = ctx; |
ba7dd8bb UH |
75 | ctx->num_enabled_channels = 0; |
76 | for (l = o->sdi->channels; l; l = l->next) { | |
77 | ch = l->data; | |
3f239f08 | 78 | if (ch->type != SR_CHANNEL_LOGIC) |
3699a8a1 | 79 | continue; |
ba7dd8bb | 80 | if (!ch->enabled) |
3699a8a1 | 81 | continue; |
ba7dd8bb | 82 | ctx->num_enabled_channels++; |
e2ad47b5 | 83 | } |
ba7dd8bb | 84 | if (ctx->num_enabled_channels <= 0) { |
b240ee08 | 85 | sr_err("No logic channel enabled."); |
365cca8a AJ |
86 | return SR_ERR; |
87 | } | |
b240ee08 | 88 | ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels); |
e2ad47b5 | 89 | |
ba7dd8bb | 90 | num_channels = g_slist_length(o->sdi->channels); |
a821069b | 91 | comment[0] = '\0'; |
ec4063b8 | 92 | samplerate = 0; |
d3c74a6f BV |
93 | if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE, |
94 | &gvar) == SR_OK) { | |
ec4063b8 | 95 | samplerate = g_variant_get_uint64(gvar); |
af51a771 | 96 | g_variant_unref(gvar); |
ec4063b8 | 97 | if (!(frequency_s = sr_samplerate_string(samplerate))) { |
a944a84b | 98 | sr_err("%s: sr_samplerate_string failed", __func__); |
133a37bf | 99 | g_free(ctx); |
e46b8fb1 | 100 | return SR_ERR; |
a821069b UH |
101 | } |
102 | snprintf(comment, 127, gnuplot_header_comment, | |
ba7dd8bb | 103 | ctx->num_enabled_channels, num_channels, frequency_s); |
133a37bf | 104 | g_free(frequency_s); |
7aae7462 | 105 | } |
e2ad47b5 UH |
106 | |
107 | /* Columns / channels */ | |
108 | wbuf[0] = '\0'; | |
b240ee08 | 109 | for (i = 0, j = 0, l = o->sdi->channels; l; l = l->next, i++) { |
ba7dd8bb | 110 | ch = l->data; |
3f239f08 | 111 | if (ch->type != SR_CHANNEL_LOGIC) |
3699a8a1 | 112 | continue; |
ba7dd8bb | 113 | if (!ch->enabled) |
deb09083 | 114 | continue; |
054e6709 | 115 | c = (char *)&wbuf + strlen((const char *)&wbuf); |
ba7dd8bb | 116 | sprintf(c, "# %d\t\t%s\n", i + 1, ch->name); |
b240ee08 BV |
117 | /* Remember the enabled channel's index while we're at it. */ |
118 | ctx->channel_index[j++] = i; | |
e2ad47b5 UH |
119 | } |
120 | ||
ec4063b8 | 121 | if (!(frequency_s = sr_period_string(samplerate))) { |
a944a84b | 122 | sr_err("%s: sr_period_string failed", __func__); |
133a37bf | 123 | g_free(ctx); |
e46b8fb1 | 124 | return SR_ERR; |
d4ae8eaa | 125 | } |
20ebd1fe | 126 | |
5cca9adb | 127 | t = time(NULL); |
b240ee08 BV |
128 | ctx->header = g_string_sized_new(512); |
129 | g_string_printf(ctx->header, gnuplot_header, PACKAGE_STRING, | |
90f680ff | 130 | ctime(&t), comment, frequency_s, (char *)&wbuf); |
133a37bf | 131 | g_free(frequency_s); |
e2ad47b5 | 132 | |
e2ad47b5 UH |
133 | return 0; |
134 | } | |
135 | ||
b240ee08 BV |
136 | static int receive(struct sr_output *o, const struct sr_dev_inst *sdi, |
137 | const struct sr_datafeed_packet *packet, GString **out) | |
e2ad47b5 | 138 | { |
b240ee08 | 139 | const struct sr_datafeed_logic *logic; |
e2ad47b5 | 140 | struct context *ctx; |
9c178ffa | 141 | const uint8_t *sample; |
b240ee08 | 142 | unsigned int curbit, p, idx, i; |
e2ad47b5 | 143 | |
b240ee08 | 144 | (void)sdi; |
20ebd1fe | 145 | |
b240ee08 BV |
146 | *out = NULL; |
147 | if (!o || !o->internal) | |
148 | return SR_ERR_BUG; | |
e2ad47b5 | 149 | ctx = o->internal; |
a821069b | 150 | |
b240ee08 BV |
151 | if (packet->type != SR_DF_LOGIC) |
152 | return SR_OK; | |
153 | logic = packet->payload; | |
154 | ||
155 | if (!ctx->prevsample) { | |
156 | /* Can't allocate this until we know the stream's unitsize. */ | |
157 | if (!(ctx->prevsample = g_malloc0(logic->unitsize))) { | |
158 | g_free(ctx); | |
159 | sr_err("%s: ctx->prevsample malloc failed", __func__); | |
160 | return SR_ERR_MALLOC; | |
161 | } | |
20ebd1fe | 162 | } |
a821069b | 163 | |
e2ad47b5 UH |
164 | if (ctx->header) { |
165 | /* The header is still here, this must be the first packet. */ | |
b240ee08 | 166 | *out = ctx->header; |
e2ad47b5 | 167 | ctx->header = NULL; |
b240ee08 BV |
168 | ctx->samplecount = 0; |
169 | } else { | |
170 | *out = g_string_sized_new(512); | |
e2ad47b5 UH |
171 | } |
172 | ||
b240ee08 BV |
173 | for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) { |
174 | sample = logic->data + i; | |
175 | ctx->samplecount++; | |
e2ad47b5 | 176 | |
50959ddc | 177 | /* |
b240ee08 | 178 | * Don't output the same sample multiple times, but make |
50959ddc UH |
179 | * sure to output at least the first and last sample. |
180 | */ | |
b240ee08 BV |
181 | if (i > 0 && i < logic->length - logic->unitsize) { |
182 | if (!memcmp(sample, ctx->prevsample, logic->unitsize)) | |
50959ddc UH |
183 | continue; |
184 | } | |
b240ee08 | 185 | memcpy(ctx->prevsample, sample, logic->unitsize); |
50959ddc | 186 | |
e2ad47b5 | 187 | /* The first column is a counter (needed for gnuplot). */ |
b240ee08 | 188 | g_string_append_printf(*out, "%" PRIu64 "\t", ctx->samplecount); |
e2ad47b5 UH |
189 | |
190 | /* The next columns are the values of all channels. */ | |
ba7dd8bb | 191 | for (p = 0; p < ctx->num_enabled_channels; p++) { |
b240ee08 BV |
192 | idx = ctx->channel_index[p]; |
193 | curbit = (sample[idx / 8] & ((uint8_t) (1 << (idx % 8)))) >> (idx % 8); | |
194 | g_string_append_printf(*out, "%d ", curbit); | |
e2ad47b5 | 195 | } |
b240ee08 | 196 | g_string_append_printf(*out, "\n"); |
e2ad47b5 UH |
197 | } |
198 | ||
b240ee08 BV |
199 | return SR_OK; |
200 | } | |
201 | ||
202 | static int cleanup(struct sr_output *o) | |
203 | { | |
204 | struct context *ctx; | |
205 | ||
206 | if (!o || !o->internal) | |
207 | return SR_ERR_BUG; | |
208 | ctx = o->internal; | |
209 | g_free(ctx->channel_index); | |
210 | g_free(ctx->prevsample); | |
211 | if (ctx->header) | |
212 | g_string_free(ctx->header, TRUE); | |
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 UH |
220 | .description = "Gnuplot", |
221 | .df_type = SR_DF_LOGIC, | |
222 | .init = init, | |
b240ee08 BV |
223 | .receive = receive, |
224 | .cleanup = cleanup, | |
77b45442 | 225 | }; |