]>
Commit | Line | Data |
---|---|---|
e2ad47b5 UH |
1 | /* |
2 | * This file is part of the sigrok 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 <sigrok.h> | |
25 | #include "config.h" | |
26 | ||
27 | struct context { | |
afc8e4de UH |
28 | unsigned int num_enabled_probes; |
29 | unsigned int unitsize; | |
d4ae8eaa | 30 | char *probelist[MAX_NUM_PROBES+1]; |
e2ad47b5 UH |
31 | char *header; |
32 | }; | |
33 | ||
d4ae8eaa | 34 | #define MAX_HEADER_LEN 1024 + (MAX_NUM_PROBES * (MAX_PROBENAME_LEN + 10)) |
e2ad47b5 UH |
35 | const char *gnuplot_header = "\ |
36 | # Sample data in space-separated columns format usable by gnuplot\n\ | |
37 | #\n\ | |
5cca9adb | 38 | # Generated by: %s on %s%s\ |
d4ae8eaa | 39 | # Period: %s\n\ |
114fb93f UH |
40 | #\n\ |
41 | # Column\tProbe\n\ | |
42 | # -------------------------------------\ | |
43 | ----------------------------------------\n\ | |
44 | # 0\t\tSample counter (for internal gnuplot purposes)\n%s\n"; | |
e2ad47b5 | 45 | |
7aae7462 BV |
46 | const char *gnuplot_header_comment = "\ |
47 | # Comment: Acquisition with %d/%d probes at %s\n"; | |
48 | ||
e2ad47b5 UH |
49 | static int init(struct output *o) |
50 | { | |
e2ad47b5 UH |
51 | struct context *ctx; |
52 | struct probe *probe; | |
53 | GSList *l; | |
54 | uint64_t samplerate; | |
afc8e4de UH |
55 | unsigned int i; |
56 | int b, num_probes; | |
d4ae8eaa | 57 | char *c, *frequency_s; |
7aae7462 | 58 | char wbuf[1000], comment[128]; |
5cca9adb | 59 | time_t t; |
e2ad47b5 | 60 | |
a821069b | 61 | if (!(ctx = calloc(1, sizeof(struct context)))) |
e2ad47b5 | 62 | return SIGROK_ERR_MALLOC; |
a821069b | 63 | |
d4ae8eaa BV |
64 | if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) { |
65 | free(ctx); | |
66 | return SIGROK_ERR_MALLOC; | |
67 | } | |
68 | ||
e2ad47b5 UH |
69 | o->internal = ctx; |
70 | ctx->num_enabled_probes = 0; | |
71 | for (l = o->device->probes; l; l = l->next) { | |
72 | probe = l->data; | |
757b8c62 UH |
73 | if (!probe->enabled) |
74 | continue; | |
75 | ctx->probelist[ctx->num_enabled_probes++] = probe->name; | |
e2ad47b5 | 76 | } |
e2ad47b5 UH |
77 | ctx->probelist[ctx->num_enabled_probes] = 0; |
78 | ctx->unitsize = (ctx->num_enabled_probes + 7) / 8; | |
79 | ||
e2ad47b5 | 80 | num_probes = g_slist_length(o->device->probes); |
a821069b | 81 | comment[0] = '\0'; |
7aae7462 BV |
82 | if (o->device->plugin) { |
83 | samplerate = *((uint64_t *) o->device->plugin->get_device_info( | |
84 | o->device->plugin_index, DI_CUR_SAMPLERATE)); | |
d4ae8eaa | 85 | if (!(frequency_s = sigrok_samplerate_string(samplerate))) { |
a821069b UH |
86 | free(ctx->header); |
87 | free(ctx); | |
7aae7462 | 88 | return SIGROK_ERR; |
a821069b UH |
89 | } |
90 | snprintf(comment, 127, gnuplot_header_comment, | |
d4ae8eaa BV |
91 | ctx->num_enabled_probes, num_probes, frequency_s); |
92 | free(frequency_s); | |
7aae7462 | 93 | } |
e2ad47b5 UH |
94 | |
95 | /* Columns / channels */ | |
96 | wbuf[0] = '\0'; | |
97 | for (i = 0; i < ctx->num_enabled_probes; i++) { | |
98 | c = (char *)&wbuf + strlen((char *)&wbuf); | |
114fb93f | 99 | sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]); |
e2ad47b5 UH |
100 | } |
101 | ||
d4ae8eaa BV |
102 | if (!(frequency_s = sigrok_period_string(samplerate))) { |
103 | free(ctx->header); | |
104 | free(ctx); | |
105 | return SIGROK_ERR; | |
106 | } | |
5cca9adb | 107 | t = time(NULL); |
e2ad47b5 | 108 | b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header, |
d4ae8eaa | 109 | PACKAGE_STRING, ctime(&t), comment, frequency_s, |
5cca9adb | 110 | (char *)&wbuf); |
d4ae8eaa | 111 | free(frequency_s); |
e2ad47b5 | 112 | |
d4ae8eaa BV |
113 | if (b < 0) { |
114 | free(ctx->header); | |
115 | free(ctx); | |
116 | return SIGROK_ERR; | |
117 | } | |
e2ad47b5 UH |
118 | |
119 | return 0; | |
120 | } | |
121 | ||
122 | static int event(struct output *o, int event_type, char **data_out, | |
123 | uint64_t *length_out) | |
124 | { | |
125 | struct context *ctx; | |
e2ad47b5 UH |
126 | |
127 | ctx = o->internal; | |
99c1fc59 | 128 | switch (event_type) { |
e2ad47b5 | 129 | case DF_TRIGGER: |
a821069b | 130 | /* TODO */ |
e2ad47b5 UH |
131 | break; |
132 | case DF_END: | |
caf62e22 UH |
133 | *data_out = NULL; |
134 | *length_out = 0; | |
e2ad47b5 UH |
135 | free(o->internal); |
136 | o->internal = NULL; | |
137 | break; | |
138 | } | |
139 | ||
140 | return SIGROK_OK; | |
141 | } | |
142 | ||
143 | static int data(struct output *o, char *data_in, uint64_t length_in, | |
144 | char **data_out, uint64_t *length_out) | |
145 | { | |
146 | struct context *ctx; | |
d4ae8eaa | 147 | unsigned int max_linelen, outsize, p, curbit, i; |
086eac7c UH |
148 | uint64_t sample; |
149 | static uint64_t samplecount = 0; | |
e2ad47b5 UH |
150 | char *outbuf, *c; |
151 | ||
152 | ctx = o->internal; | |
d4ae8eaa BV |
153 | max_linelen = 16 + ctx->num_enabled_probes * 2; |
154 | outsize = length_in / ctx->unitsize * max_linelen; | |
e273a904 | 155 | if (ctx->header) |
d4ae8eaa | 156 | outsize += strlen(ctx->header); |
a821069b | 157 | |
d4ae8eaa BV |
158 | if (!(outbuf = calloc(1, outsize))) |
159 | return SIGROK_ERR_MALLOC; | |
a821069b UH |
160 | |
161 | outbuf[0] = '\0'; | |
e2ad47b5 UH |
162 | if (ctx->header) { |
163 | /* The header is still here, this must be the first packet. */ | |
164 | strncpy(outbuf, ctx->header, outsize); | |
165 | free(ctx->header); | |
166 | ctx->header = NULL; | |
e2ad47b5 UH |
167 | } |
168 | ||
607b58de UH |
169 | for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) { |
170 | memcpy(&sample, data_in + i, ctx->unitsize); | |
e2ad47b5 UH |
171 | |
172 | /* The first column is a counter (needed for gnuplot). */ | |
173 | c = outbuf + strlen(outbuf); | |
d4ae8eaa | 174 | sprintf(c, "%" PRIu64 "\t", samplecount++); |
e2ad47b5 UH |
175 | |
176 | /* The next columns are the values of all channels. */ | |
177 | for (p = 0; p < ctx->num_enabled_probes; p++) { | |
fbe2f794 | 178 | curbit = (sample & ((uint64_t) (1 << p))) >> p; |
e2ad47b5 UH |
179 | c = outbuf + strlen(outbuf); |
180 | sprintf(c, "%d ", curbit); | |
181 | } | |
182 | ||
183 | c = outbuf + strlen(outbuf); | |
184 | sprintf(c, "\n"); | |
e2ad47b5 UH |
185 | } |
186 | ||
187 | *data_out = outbuf; | |
188 | *length_out = strlen(outbuf); | |
189 | ||
190 | return SIGROK_OK; | |
191 | } | |
192 | ||
193 | struct output_format output_gnuplot = { | |
194 | "gnuplot", | |
195 | "Gnuplot", | |
196 | init, | |
197 | data, | |
198 | event, | |
199 | }; |