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