]>
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; |
afc8e4de | 32 | unsigned int unitsize; |
e2ad47b5 | 33 | char *header; |
9c178ffa | 34 | uint8_t *old_sample; |
e2ad47b5 UH |
35 | }; |
36 | ||
d078d2e5 | 37 | static const char *gnuplot_header = "\ |
e2ad47b5 UH |
38 | # Sample data in space-separated columns format usable by gnuplot\n\ |
39 | #\n\ | |
5cca9adb | 40 | # Generated by: %s on %s%s\ |
d4ae8eaa | 41 | # Period: %s\n\ |
114fb93f | 42 | #\n\ |
ba7dd8bb | 43 | # Column\tChannel\n\ |
114fb93f UH |
44 | # -------------------------------------\ |
45 | ----------------------------------------\n\ | |
46 | # 0\t\tSample counter (for internal gnuplot purposes)\n%s\n"; | |
e2ad47b5 | 47 | |
d078d2e5 | 48 | static const char *gnuplot_header_comment = "\ |
ba7dd8bb | 49 | # Comment: Acquisition with %d/%d channels at %s\n"; |
7aae7462 | 50 | |
f50f3f40 | 51 | static int init(struct sr_output *o) |
e2ad47b5 | 52 | { |
e2ad47b5 | 53 | struct context *ctx; |
ba7dd8bb | 54 | struct sr_channel *ch; |
e2ad47b5 | 55 | GSList *l; |
ec4063b8 BV |
56 | GVariant *gvar; |
57 | uint64_t samplerate; | |
afc8e4de | 58 | unsigned int i; |
ba7dd8bb | 59 | int num_channels; |
d4ae8eaa | 60 | char *c, *frequency_s; |
7aae7462 | 61 | char wbuf[1000], comment[128]; |
5cca9adb | 62 | time_t t; |
e2ad47b5 | 63 | |
20ebd1fe | 64 | if (!o) { |
a944a84b | 65 | sr_err("%s: o was NULL", __func__); |
20ebd1fe UH |
66 | return SR_ERR_ARG; |
67 | } | |
68 | ||
5c3c1241 | 69 | if (!o->sdi) { |
a944a84b | 70 | sr_err("%s: o->sdi was NULL", __func__); |
20ebd1fe UH |
71 | return SR_ERR_ARG; |
72 | } | |
73 | ||
133a37bf | 74 | if (!(ctx = g_try_malloc0(sizeof(struct context)))) { |
a944a84b | 75 | sr_err("%s: ctx malloc failed", __func__); |
e46b8fb1 | 76 | return SR_ERR_MALLOC; |
20ebd1fe | 77 | } |
a821069b | 78 | |
e2ad47b5 | 79 | o->internal = ctx; |
ba7dd8bb UH |
80 | ctx->num_enabled_channels = 0; |
81 | for (l = o->sdi->channels; l; l = l->next) { | |
82 | ch = l->data; | |
3f239f08 | 83 | if (ch->type != SR_CHANNEL_LOGIC) |
3699a8a1 | 84 | continue; |
ba7dd8bb | 85 | if (!ch->enabled) |
3699a8a1 | 86 | continue; |
ba7dd8bb | 87 | ctx->num_enabled_channels++; |
e2ad47b5 | 88 | } |
ba7dd8bb UH |
89 | if (ctx->num_enabled_channels <= 0) { |
90 | sr_err("%s: no logic channel enabled", __func__); | |
365cca8a AJ |
91 | return SR_ERR; |
92 | } | |
93 | ||
ba7dd8bb | 94 | ctx->unitsize = (ctx->num_enabled_channels + 7) / 8; |
e2ad47b5 | 95 | |
ba7dd8bb | 96 | num_channels = g_slist_length(o->sdi->channels); |
a821069b | 97 | comment[0] = '\0'; |
ec4063b8 | 98 | samplerate = 0; |
d3c74a6f BV |
99 | if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE, |
100 | &gvar) == SR_OK) { | |
ec4063b8 | 101 | samplerate = g_variant_get_uint64(gvar); |
af51a771 | 102 | g_variant_unref(gvar); |
ec4063b8 | 103 | if (!(frequency_s = sr_samplerate_string(samplerate))) { |
a944a84b | 104 | sr_err("%s: sr_samplerate_string failed", __func__); |
133a37bf | 105 | g_free(ctx); |
e46b8fb1 | 106 | return SR_ERR; |
a821069b UH |
107 | } |
108 | snprintf(comment, 127, gnuplot_header_comment, | |
ba7dd8bb | 109 | ctx->num_enabled_channels, num_channels, frequency_s); |
133a37bf | 110 | g_free(frequency_s); |
7aae7462 | 111 | } |
e2ad47b5 UH |
112 | |
113 | /* Columns / channels */ | |
114 | wbuf[0] = '\0'; | |
ba7dd8bb UH |
115 | for (i = 0, l = o->sdi->channels; l; l = l->next, i++) { |
116 | ch = l->data; | |
3f239f08 | 117 | if (ch->type != SR_CHANNEL_LOGIC) |
3699a8a1 | 118 | continue; |
ba7dd8bb | 119 | if (!ch->enabled) |
deb09083 | 120 | continue; |
054e6709 | 121 | c = (char *)&wbuf + strlen((const char *)&wbuf); |
ba7dd8bb | 122 | sprintf(c, "# %d\t\t%s\n", i + 1, ch->name); |
e2ad47b5 UH |
123 | } |
124 | ||
ec4063b8 | 125 | if (!(frequency_s = sr_period_string(samplerate))) { |
a944a84b | 126 | sr_err("%s: sr_period_string failed", __func__); |
133a37bf | 127 | g_free(ctx); |
e46b8fb1 | 128 | return SR_ERR; |
d4ae8eaa | 129 | } |
20ebd1fe | 130 | |
5cca9adb | 131 | t = time(NULL); |
90f680ff ML |
132 | ctx->header = g_strdup_printf(gnuplot_header, PACKAGE_STRING, |
133 | ctime(&t), comment, frequency_s, (char *)&wbuf); | |
133a37bf | 134 | g_free(frequency_s); |
e2ad47b5 | 135 | |
9c178ffa ML |
136 | if (!(ctx->old_sample = g_try_malloc0(ctx->unitsize))) { |
137 | sr_err("%s: ctx->old_sample malloc failed", __func__); | |
138 | g_free(ctx->header); | |
139 | g_free(ctx); | |
140 | return SR_ERR_MALLOC; | |
141 | } | |
142 | ||
e2ad47b5 UH |
143 | return 0; |
144 | } | |
145 | ||
054e6709 | 146 | static int event(struct sr_output *o, int event_type, uint8_t **data_out, |
e2ad47b5 UH |
147 | uint64_t *length_out) |
148 | { | |
20ebd1fe | 149 | if (!o) { |
a944a84b | 150 | sr_err("%s: o was NULL", __func__); |
20ebd1fe UH |
151 | return SR_ERR_ARG; |
152 | } | |
153 | ||
154 | if (!data_out) { | |
a944a84b | 155 | sr_err("%s: data_out was NULL", __func__); |
20ebd1fe UH |
156 | return SR_ERR_ARG; |
157 | } | |
158 | ||
159 | if (!length_out) { | |
a944a84b | 160 | sr_err("%s: length_out was NULL", __func__); |
20ebd1fe UH |
161 | return SR_ERR_ARG; |
162 | } | |
163 | ||
99c1fc59 | 164 | switch (event_type) { |
5a2326a7 | 165 | case SR_DF_TRIGGER: |
20ebd1fe | 166 | /* TODO: Can a trigger mark be in a gnuplot data file? */ |
e2ad47b5 | 167 | break; |
5a2326a7 | 168 | case SR_DF_END: |
133a37bf | 169 | g_free(o->internal); |
e2ad47b5 UH |
170 | o->internal = NULL; |
171 | break; | |
20ebd1fe | 172 | default: |
a944a84b | 173 | sr_err("%s: unsupported event type: %d", __func__, event_type); |
20ebd1fe | 174 | break; |
e2ad47b5 UH |
175 | } |
176 | ||
9ab95e54 BV |
177 | *data_out = NULL; |
178 | *length_out = 0; | |
179 | ||
e46b8fb1 | 180 | return SR_OK; |
e2ad47b5 UH |
181 | } |
182 | ||
054e6709 UH |
183 | static int data(struct sr_output *o, const uint8_t *data_in, |
184 | uint64_t length_in, uint8_t **data_out, uint64_t *length_out) | |
e2ad47b5 UH |
185 | { |
186 | struct context *ctx; | |
d4ae8eaa | 187 | unsigned int max_linelen, outsize, p, curbit, i; |
9c178ffa ML |
188 | const uint8_t *sample; |
189 | static uint64_t samplecount = 0; | |
054e6709 | 190 | uint8_t *outbuf, *c; |
e2ad47b5 | 191 | |
20ebd1fe | 192 | if (!o) { |
a944a84b | 193 | sr_err("%s: o was NULL", __func__); |
20ebd1fe UH |
194 | return SR_ERR_ARG; |
195 | } | |
196 | ||
197 | if (!o->internal) { | |
a944a84b | 198 | sr_err("%s: o->internal was NULL", __func__); |
20ebd1fe UH |
199 | return SR_ERR_ARG; |
200 | } | |
201 | ||
202 | if (!data_in) { | |
a944a84b | 203 | sr_err("%s: data_in was NULL", __func__); |
20ebd1fe UH |
204 | return SR_ERR_ARG; |
205 | } | |
206 | ||
207 | if (!data_out) { | |
a944a84b | 208 | sr_err("%s: data_out was NULL", __func__); |
20ebd1fe UH |
209 | return SR_ERR_ARG; |
210 | } | |
211 | ||
212 | if (!length_out) { | |
a944a84b | 213 | sr_err("%s: length_out was NULL", __func__); |
20ebd1fe UH |
214 | return SR_ERR_ARG; |
215 | } | |
216 | ||
e2ad47b5 | 217 | ctx = o->internal; |
ba7dd8bb | 218 | max_linelen = 16 + ctx->num_enabled_channels * 2; |
d4ae8eaa | 219 | outsize = length_in / ctx->unitsize * max_linelen; |
e273a904 | 220 | if (ctx->header) |
d4ae8eaa | 221 | outsize += strlen(ctx->header); |
a821069b | 222 | |
133a37bf | 223 | if (!(outbuf = g_try_malloc0(outsize))) { |
a944a84b | 224 | sr_err("%s: outbuf malloc failed", __func__); |
e46b8fb1 | 225 | return SR_ERR_MALLOC; |
20ebd1fe | 226 | } |
a821069b UH |
227 | |
228 | outbuf[0] = '\0'; | |
e2ad47b5 UH |
229 | if (ctx->header) { |
230 | /* The header is still here, this must be the first packet. */ | |
054e6709 | 231 | strncpy((char *)outbuf, ctx->header, outsize); |
133a37bf | 232 | g_free(ctx->header); |
e2ad47b5 | 233 | ctx->header = NULL; |
e2ad47b5 UH |
234 | } |
235 | ||
607b58de | 236 | for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) { |
50959ddc | 237 | |
9c178ffa | 238 | sample = data_in + i; |
e2ad47b5 | 239 | |
50959ddc UH |
240 | /* |
241 | * Don't output the same samples multiple times. However, make | |
242 | * sure to output at least the first and last sample. | |
243 | */ | |
9c178ffa ML |
244 | if (samplecount++ != 0 && |
245 | !memcmp(sample, ctx->old_sample, ctx->unitsize)) { | |
50959ddc UH |
246 | if (i != (length_in - ctx->unitsize)) |
247 | continue; | |
248 | } | |
9c178ffa | 249 | memcpy(ctx->old_sample, sample, ctx->unitsize); |
50959ddc | 250 | |
e2ad47b5 | 251 | /* The first column is a counter (needed for gnuplot). */ |
054e6709 UH |
252 | c = outbuf + strlen((const char *)outbuf); |
253 | sprintf((char *)c, "%" PRIu64 "\t", samplecount++); | |
e2ad47b5 UH |
254 | |
255 | /* The next columns are the values of all channels. */ | |
ba7dd8bb | 256 | for (p = 0; p < ctx->num_enabled_channels; p++) { |
9c178ffa | 257 | curbit = (sample[p / 8] & ((uint8_t) (1 << (p % 8)))) >> (p % 8); |
054e6709 UH |
258 | c = outbuf + strlen((const char *)outbuf); |
259 | sprintf((char *)c, "%d ", curbit); | |
e2ad47b5 UH |
260 | } |
261 | ||
054e6709 UH |
262 | c = outbuf + strlen((const char *)outbuf); |
263 | sprintf((char *)c, "\n"); | |
e2ad47b5 UH |
264 | } |
265 | ||
266 | *data_out = outbuf; | |
054e6709 | 267 | *length_out = strlen((const char *)outbuf); |
e2ad47b5 | 268 | |
e46b8fb1 | 269 | return SR_OK; |
e2ad47b5 UH |
270 | } |
271 | ||
7c1d391c | 272 | SR_PRIV struct sr_output_format output_gnuplot = { |
cdb3573c | 273 | .id = "gnuplot", |
d494a4aa UH |
274 | .description = "Gnuplot", |
275 | .df_type = SR_DF_LOGIC, | |
276 | .init = init, | |
277 | .data = data, | |
278 | .event = event, | |
77b45442 | 279 | }; |